From cac5c2e8eee680421349554e0709549b3eac8640 Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Thu, 21 May 2015 17:25:01 +0300 Subject: [PATCH] IDEA-121346 Settings->Colors&Fonts: remember last selected entry from attributes list --- .../options/colors/ColorOptionsTree.java | 26 +++++++++++++++---- .../options/colors/OptionsPanelImpl.java | 22 +++++++++++++++- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/application/options/colors/ColorOptionsTree.java b/platform/lang-impl/src/com/intellij/application/options/colors/ColorOptionsTree.java index abdce373e5ba..c4a14ace8ab2 100644 --- a/platform/lang-impl/src/com/intellij/application/options/colors/ColorOptionsTree.java +++ b/platform/lang-impl/src/com/intellij/application/options/colors/ColorOptionsTree.java @@ -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); } })); } diff --git a/platform/lang-impl/src/com/intellij/application/options/colors/OptionsPanelImpl.java b/platform/lang-impl/src/com/intellij/application/options/colors/OptionsPanelImpl.java index 3d78329c00fa..538e8000d74f 100644 --- a/platform/lang-impl/src/com/intellij/application/options/colors/OptionsPanelImpl.java +++ b/platform/lang-impl/src/com/intellij/application/options/colors/OptionsPanelImpl.java @@ -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 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); }