IDEA-121346 Settings->Colors&Fonts: remember last selected entry from attributes list

This commit is contained in:
Rustam Vishnyakov
2015-05-21 17:43:09 +03:00
parent 6d757de7f1
commit cac5c2e8ee
2 changed files with 42 additions and 6 deletions
@@ -83,12 +83,15 @@ public class ColorOptionsTree extends Tree {
@Nullable
public ColorAndFontDescription getSelectedDescriptor() {
Object selectedValue = getSelectedValue();
return selectedValue instanceof ColorAndFontDescription ? (ColorAndFontDescription)selectedValue : null;
}
@Nullable
public Object getSelectedValue() {
Object selectedNode = getLastSelectedPathComponent();
if (selectedNode instanceof DefaultMutableTreeNode) {
Object selectedValue = ((DefaultMutableTreeNode)selectedNode).getUserObject();
if (selectedValue instanceof ColorAndFontDescription) {
return (ColorAndFontDescription)selectedValue;
}
return ((DefaultMutableTreeNode)selectedNode).getUserObject();
}
return null;
}
@@ -105,11 +108,24 @@ public class ColorOptionsTree extends Tree {
}));
}
@Nullable
public Object selectFirstItem() {
Object root = myTreeModel.getRoot();
if (root != null && myTreeModel.getChildCount(root) > 0) {
Object first = myTreeModel.getChild(root, 0);
if (first instanceof MyTreeNode) {
selectPath(new TreePath(((MyTreeNode)first).getPath()));
return ((MyTreeNode)first).getUserObject();
}
}
return null;
}
public void selectOptionByName(@NotNull final String optionName) {
selectPath(findOption(myTreeModel.getRoot(), new DescriptorMatcher() {
@Override
public boolean matches(@NotNull Object data) {
return StringUtil.containsIgnoreCase(data.toString(), optionName);
return !optionName.isEmpty() && StringUtil.containsIgnoreCase(data.toString(), optionName);
}
}));
}
@@ -17,6 +17,7 @@
package com.intellij.application.options.colors;
import com.intellij.ide.DataManager;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.options.ex.Settings;
import com.intellij.openapi.util.ActionCallback;
@@ -36,6 +37,7 @@ import java.util.HashSet;
import java.util.Set;
public class OptionsPanelImpl extends JPanel implements OptionsPanel {
public static final String SELECTED_COLOR_OPTION_PROPERTY = "selected.color.option.type";
private final ColorOptionsTree myOptionsTree;
private final ColorAndFontDescriptionPanel myOptionsPanel;
@@ -44,6 +46,8 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel {
private final SchemesPanel mySchemesProvider;
private final String myCategoryName;
private final PropertiesComponent myProperties;
private final EventDispatcher<ColorAndFontSettingsListener> myDispatcher = EventDispatcher.create(ColorAndFontSettingsListener.class);
public OptionsPanelImpl(ColorAndFontOptions options,
@@ -53,6 +57,7 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel {
myOptions = options;
mySchemesProvider = schemesProvider;
myCategoryName = categoryName;
myProperties = PropertiesComponent.getInstance();
myOptionsPanel = new ColorAndFontDescriptionPanel() {
@Override
@@ -109,8 +114,23 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel {
}
private void processListValueChanged() {
ColorAndFontDescription description = myOptionsTree.getSelectedDescriptor();
Object selectedValue = myOptionsTree.getSelectedValue();
ColorAndFontDescription description = selectedValue instanceof ColorAndFontDescription ? (ColorAndFontDescription)selectedValue : null;
if (description == null) {
if (selectedValue == null) {
String preselectedType = myProperties.getValue(SELECTED_COLOR_OPTION_PROPERTY);
if (preselectedType != null) {
myOptionsTree.selectOptionByType(preselectedType);
description = myOptionsTree.getSelectedDescriptor();
}
}
}
if (description == null) {
Object firstItem = myOptionsTree.selectFirstItem();
if (firstItem instanceof ColorAndFontDescription) description = (ColorAndFontDescription)firstItem;
}
if (description != null) {
myProperties.setValue(SELECTED_COLOR_OPTION_PROPERTY, description.getType());
myOptionsPanel.reset(description);
myDispatcher.getMulticaster().selectedOptionChanged(description);
}