Skip to content

Commit

Permalink
Define global file exclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses committed Dec 29, 2017
1 parent 32f9894 commit 96f9cfa
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package org.sonarlint.intellij.config.component;

import com.intellij.ui.AnActionButton;
import com.intellij.ui.AnActionButtonRunnable;
import com.intellij.ui.CollectionListModel;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.components.JBList;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import javax.swing.JComponent;
import javax.swing.JPanel;

public class EditableList<T> {
private final Supplier<T> onAdd;
private final Function<T, T> onEdit;
private final JBList list;
private final JPanel listPanel;
private final CollectionListModel<T> model;

public EditableList(String emptyLabel, Supplier<T> onAdd, Function<T, T> onEdit) {
this.onAdd = onAdd;
this.onEdit = onEdit;
list = new JBList();
list.getEmptyText().setText(emptyLabel);
list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 2) {
editEntry();
}
}
});
list.addListSelectionListener(e -> {
if (!e.getValueIsAdjusting()) {
// nothing to do?
}
});
model = new CollectionListModel<>(new ArrayList<T>());
list.setModel(model);

ToolbarDecorator toolbarDecorator = ToolbarDecorator.createDecorator(list)
.setEditActionName("Edit")
.setEditAction(e -> editEntry());

toolbarDecorator.setAddAction(new AddEntryAction());
toolbarDecorator.setRemoveAction(new RemoveEntryAction());

listPanel = new JPanel(new BorderLayout());
listPanel.add(toolbarDecorator.createPanel(), BorderLayout.CENTER);
}

public JComponent getComponent() {
return listPanel;
}

public void set(List<T> data) {
model.removeAll();
for (T s : data) {
model.add(s);
}
}

public List<T> get() {
return new ArrayList<>(model.getItems());
}

private void editEntry() {
int selectedIndex = list.getSelectedIndex();
if (selectedIndex >= 0) {
T value = model.getElementAt(selectedIndex);
T newValue = onEdit.apply(value);
if (newValue != null) {
model.setElementAt(newValue, selectedIndex);
}
}
}

private class AddEntryAction implements AnActionButtonRunnable {
@Override
public void run(AnActionButton anActionButton) {
T input = onAdd.get();
if (input != null) {
model.add(input);
list.setSelectedIndex(list.getModel().getSize() - 1);
}
}
}

private class RemoveEntryAction implements AnActionButtonRunnable {
@Override
public void run(AnActionButton anActionButton) {
T entry = getSelectedEntry();
if (entry == null) {
return;
}
int selectedIndex = list.getSelectedIndex();
model.remove(entry);

if (model.getSize() > 0) {
int newIndex = Math.min(model.getSize() - 1, Math.max(selectedIndex - 1, 0));
list.setSelectedValue(model.getElementAt(newIndex), true);
}
}

@Nullable
private T getSelectedEntry() {
return (T) list.getSelectedValue();
}
}

public void add(T value) {
model.add(value);
list.setSelectedIndex(model.getSize() - 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.sonarlint.intellij.config.global;

import javax.swing.JComponent;

public interface ConfigurationPanel<T> {
JComponent getComponent();

boolean isModified(T settings);

void save(T settings);

void load(T settings);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.sonarlint.intellij.config.global;

import com.intellij.openapi.ui.Messages;
import com.intellij.ui.IdeBorderFactory;
import java.awt.BorderLayout;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.Border;
import org.apache.commons.lang.StringUtils;
import org.sonarlint.intellij.config.component.EditableList;

public class ExclusionsPanel implements ConfigurationPanel<SonarLintGlobalSettings> {
private static final String EMPTY_LABEL = "No exclusions configured";
private static final String BORDER_TITLE = "Exclusions";

private JComponent panel;
private EditableList<String> list;

@Override
public JComponent getComponent() {
return panel;
}

@Override public boolean isModified(SonarLintGlobalSettings settings) {
return !Objects.equals(settings.getFileExclusions(), list.get());
}

@Override
public void load(SonarLintGlobalSettings settings) {
list.set(settings.getFileExclusions());
}

@Override
public void save(SonarLintGlobalSettings settings) {
settings.setFileExclusions(list.get());
}

public ExclusionsPanel() {
create();
}

public void create() {
Border b = IdeBorderFactory.createTitledBorder(BORDER_TITLE);
panel = new JPanel(new BorderLayout());
panel.setBorder(b);
Supplier<String> onAdd = () -> {
String s = Messages.showInputDialog(panel, "Enter new exclusion pattern", "Add File Exclusion", null, null, null);
return StringUtils.stripToNull(s);
};

Function<String, String> onEdit = (value) -> {
String s = Messages.showInputDialog(panel, "Modify exclusion pattern", "Edit File Exclusion", null, value, null);
return StringUtils.stripToNull(s);
};

list = new EditableList<>(EMPTY_LABEL, onAdd, onEdit);
panel.add(list.getComponent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.sonarlint.intellij.SonarApplication;
import org.sonarlint.intellij.telemetry.SonarLintTelemetry;

public class SonarLintAboutPanel {
public class SonarLintAboutPanel implements ConfigurationPanel<SonarLintTelemetry> {
private final SonarApplication application;
private JPanel panel;
private JCheckBox enableCheckBox;
Expand Down Expand Up @@ -149,14 +149,17 @@ public JComponent getComponent() {
return panel;
}

public void load(boolean telemetryEnabled) {
enableCheckBox.setSelected(telemetryEnabled);
@Override
public void load(SonarLintTelemetry telemetry) {
enableCheckBox.setSelected(telemetry.enabled());
}

@Override
public void save(SonarLintTelemetry telemetry) {
telemetry.optOut(!enableCheckBox.isSelected());
}

@Override
public boolean isModified(SonarLintTelemetry telemetry) {
return telemetry.enabled() != enableCheckBox.isSelected();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class SonarLintGlobalConfigurable implements Configurable, Configurable.N
private SonarQubeServerMgmtPanel serversPanel;
private SonarLintGlobalOptionsPanel globalPanel;
private SonarLintAboutPanel about;
private ExclusionsPanel exclusions;

public SonarLintGlobalConfigurable() {
this.app = ApplicationManager.getApplication();
Expand All @@ -68,13 +69,15 @@ public SonarLintGlobalConfigurable() {
}

@Override public boolean isModified() {
return serversPanel.isModified(globalSettings) || globalPanel.isModified(globalSettings) || about.isModified(telemetry);
return serversPanel.isModified(globalSettings) || globalPanel.isModified(globalSettings)
|| about.isModified(telemetry) || exclusions.isModified(globalSettings);
}

@Override public void apply() throws ConfigurationException {
serversPanel.save(globalSettings);
globalPanel.save(globalSettings);
about.save(telemetry);
exclusions.save(globalSettings);

GlobalConfigurationListener globalConfigurationListener = app.getMessageBus().syncPublisher(GlobalConfigurationListener.TOPIC);
globalConfigurationListener.applied(globalSettings.getSonarQubeServers(), globalSettings.isAutoTrigger());
Expand All @@ -100,7 +103,10 @@ public List<SonarQubeServer> getCurrentSettings() {
globalPanel.load(globalSettings);
}
if (about != null) {
about.load(telemetry.enabled());
about.load(telemetry);
}
if (exclusions != null) {
exclusions.load(globalSettings);
}
}

Expand All @@ -120,8 +126,8 @@ public List<SonarQubeServer> getCurrentSettings() {
private JPanel getPanel() {
if (rootPanel == null) {
about = new SonarLintAboutPanel(sonarApplication);

globalPanel = new SonarLintGlobalOptionsPanel(globalSettings);
exclusions = new ExclusionsPanel();
globalPanel = new SonarLintGlobalOptionsPanel();
serversPanel = new SonarQubeServerMgmtPanel();

JPanel settingsPanel = new JPanel(new BorderLayout());
Expand All @@ -131,7 +137,8 @@ private JPanel getPanel() {
rootPanel = new JPanel(new BorderLayout());
JBTabbedPane tabs = new JBTabbedPane();
tabs.insertTab("Settings", null, settingsPanel, "Configure SonarLint for all projects", 0);
tabs.insertTab("About", null, about.getComponent(), "About SonarLint", 1);
tabs.insertTab("File Exclusions", null, exclusions.getComponent(), "Configure which files should be excluded from analysis", 1);
tabs.insertTab("About", null, about.getComponent(), "About SonarLint", 2);
rootPanel.add(tabs, BorderLayout.CENTER);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@
import javax.swing.JComponent;
import javax.swing.JPanel;

public class SonarLintGlobalOptionsPanel {
public class SonarLintGlobalOptionsPanel implements ConfigurationPanel<SonarLintGlobalSettings> {
private JPanel rootPane;
private JCheckBox autoTrigger;

public SonarLintGlobalOptionsPanel(SonarLintGlobalSettings model) {
load(model);
}

@Override
public JComponent getComponent() {
if (rootPane == null) {

Expand All @@ -54,16 +51,19 @@ private JPanel createTopPanel() {
return tickOptions;
}

@Override
public boolean isModified(SonarLintGlobalSettings model) {
getComponent();
return model.isAutoTrigger() != autoTrigger.isSelected();
}

@Override
public void load(SonarLintGlobalSettings model) {
getComponent();
autoTrigger.setSelected(model.isAutoTrigger());
}

@Override
public void save(SonarLintGlobalSettings model) {
getComponent();
model.setAutoTrigger(autoTrigger.isSelected());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.util.xmlb.XmlSerializerUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -43,6 +44,7 @@ public final class SonarLintGlobalSettings extends ApplicationComponent.Adapter

private boolean autoTrigger = true;
private List<SonarQubeServer> servers = new LinkedList<>();
private List<String> fileExclusions = new ArrayList<>();

public static SonarLintGlobalSettings getInstance() {
return ApplicationManager.getApplication().getComponent(SonarLintGlobalSettings.class);
Expand Down Expand Up @@ -93,4 +95,12 @@ public void setSonarQubeServers(List<SonarQubeServer> servers) {
public List<SonarQubeServer> getSonarQubeServers() {
return this.servers;
}

public List<String> getFileExclusions() {
return fileExclusions;
}

public void setFileExclusions(List<String> fileExclusions) {
this.fileExclusions = fileExclusions;
}
}
Loading

0 comments on commit 96f9cfa

Please sign in to comment.