sort attributes & install speed-search

This commit is contained in:
Gregory.Shrago
2015-02-10 19:23:15 +03:00
parent 9f1275112c
commit 0c9df8ff4b
@@ -16,10 +16,15 @@
package com.intellij.application.options.colors;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.CollectionListModel;
import com.intellij.ui.ListScrollingUtil;
import com.intellij.ui.ListSpeedSearch;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.components.JBList;
import com.intellij.util.EventDispatcher;
import com.intellij.util.containers.ContainerUtil;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
@@ -27,11 +32,16 @@ import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
public class OptionsPanelImpl extends JPanel implements OptionsPanel {
private final JList myOptionsList;
private static final Comparator<EditorSchemeAttributeDescriptor> ATTR_COMPARATOR = new Comparator<EditorSchemeAttributeDescriptor>() {
@Override
public int compare(EditorSchemeAttributeDescriptor o1, EditorSchemeAttributeDescriptor o2) {
return StringUtil.naturalCompare(o1.getType(), o2.getType());
}
};
private final JBList myOptionsList;
private final ColorAndFontDescriptionPanel myOptionsPanel;
private final ColorAndFontOptions myOptions;
@@ -39,9 +49,12 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel {
private final String myCategoryName;
private final EventDispatcher<ColorAndFontSettingsListener> myDispatcher = EventDispatcher.create(ColorAndFontSettingsListener.class);
private final CollectionListModel<EditorSchemeAttributeDescriptor> myListModel;
public OptionsPanelImpl(ColorAndFontDescriptionPanel optionsPanel, ColorAndFontOptions options, SchemesPanel schemesProvider,
String categoryName) {
public OptionsPanelImpl(ColorAndFontDescriptionPanel optionsPanel,
ColorAndFontOptions options,
SchemesPanel schemesProvider,
String categoryName) {
super(new BorderLayout());
myOptions = options;
mySchemesProvider = schemesProvider;
@@ -54,7 +67,9 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel {
}
});
myOptionsList = new JBList();
myListModel = new CollectionListModel<EditorSchemeAttributeDescriptor>();
myOptionsList = new JBList(myListModel);
new ListSpeedSearch(myOptionsList);
myOptionsList.addListSelectionListener(new ListSelectionListener() {
@Override
@@ -75,7 +90,6 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel {
}
});
myOptionsList.setModel(new DefaultListModel());
myOptionsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myOptionsList);
@@ -109,16 +123,15 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel {
private void fillOptionsList() {
int selIndex = myOptionsList.getSelectedIndex();
DefaultListModel listModel = (DefaultListModel)myOptionsList.getModel();
listModel.removeAllElements();
myListModel.removeAll();
EditorSchemeAttributeDescriptor[] descriptions = myOptions.getCurrentDescriptions();
for (EditorSchemeAttributeDescriptor description : descriptions) {
if (description.getGroup().equals(myCategoryName)) {
listModel.addElement(description);
}
ArrayList<EditorSchemeAttributeDescriptor> list = ContainerUtil.newArrayList();
for (EditorSchemeAttributeDescriptor description : myOptions.getCurrentDescriptions()) {
if (!description.getGroup().equals(myCategoryName)) continue;
list.add(description);
}
Collections.sort(list, ATTR_COMPARATOR);
myListModel.add(list);
if (selIndex >= 0) {
myOptionsList.setSelectedIndex(selIndex);
}
@@ -142,54 +155,38 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel {
}
@Override
public Runnable showOption(final String option) {
final String lowerCaseOption = option.toLowerCase();
DefaultListModel model = (DefaultListModel)myOptionsList.getModel();
for (int i = 0; i < model.size(); i++) {
Object o = model.get(i);
if (o instanceof EditorSchemeAttributeDescriptor) {
String type = ((EditorSchemeAttributeDescriptor)o).getType();
if (type.toLowerCase().contains(lowerCaseOption) ||
o.toString().toLowerCase().contains(lowerCaseOption)) {
final int i1 = i;
return new Runnable() {
@Override
public void run() {
ListScrollingUtil.selectItem(myOptionsList, i1);
}
};
}
public Runnable showOption(String attribute) {
final int index = getAttributeIndex(attribute);
return index < 0 ? null : new Runnable() {
@Override
public void run() {
ListScrollingUtil.selectItem(myOptionsList, index);
}
}
};
}
return null;
private int getAttributeIndex(final String option) {
return ContainerUtil.indexOf(myListModel.getItems(), new Condition<EditorSchemeAttributeDescriptor>() {
@Override
public boolean value(EditorSchemeAttributeDescriptor o) {
return StringUtil.naturalCompare(o.getType(), option) == 0;
}
});
}
@Override
public void applyChangesToScheme() {
Object selectedValue = myOptionsList.getSelectedValue();
if (selectedValue instanceof ColorAndFontDescription) {
myOptionsPanel.apply((ColorAndFontDescription)selectedValue,myOptions.getSelectedScheme());
myOptionsPanel.apply((ColorAndFontDescription)selectedValue, myOptions.getSelectedScheme());
}
}
@Override
public void selectOption(final String typeToSelect) {
DefaultListModel model = (DefaultListModel)myOptionsList.getModel();
for (int i = 0; i < model.size(); i++) {
Object o = model.get(i);
if (o instanceof EditorSchemeAttributeDescriptor) {
if (typeToSelect.equals(((EditorSchemeAttributeDescriptor)o).getType())) {
ListScrollingUtil.selectItem(myOptionsList, i);
return;
}
}
}
public void selectOption(String attribute) {
int index = getAttributeIndex(attribute);
if (index < 0) return;
ListScrollingUtil.selectItem(myOptionsList, index);
}
@Override