forked from SonarSource/sonarlint-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
248 additions
and
19 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
src/main/java/org/sonarlint/intellij/config/component/EditableList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/sonarlint/intellij/config/global/ConfigurationPanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/org/sonarlint/intellij/config/global/ExclusionsPanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.