From ae8b0f329a38d730fc37cb9920c8fa73736e51b4 Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Mon, 26 Dec 2016 12:56:29 +0300 Subject: [PATCH] Code style & color scheme panels refactoring (IDEA-165087, IDEA-165088) --- .../colors/impl/AbstractColorsScheme.java | 9 - .../options/InitialConfigurationDialog.java | 4 +- .../options/codeStyle/CodeStyleMainPanel.java | 2 +- .../codeStyle/CodeStyleSchemesActions.java | 21 ++- .../codeStyle/CodeStyleSchemesPanel.java | 81 +++----- .../options/colors/ColorAndFontOptions.java | 16 +- .../options/colors/ColorSchemeActions.java | 16 +- .../options/colors/NewColorAndFontPanel.java | 6 +- .../options/colors/SchemesPanel.java | 105 +++-------- .../options/schemes/AbstractSchemesPanel.form | 58 ------ .../options/schemes/AbstractSchemesPanel.java | 64 ++++--- .../options/schemes/DefaultSchemeActions.java | 21 ++- .../options/schemes/SchemesCombo.java | 177 ++++++++++++++++++ .../actions/QuickChangeColorSchemeAction.java | 4 +- .../openapi/options/SchemeManager.java | 8 + 15 files changed, 332 insertions(+), 260 deletions(-) delete mode 100644 platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemesPanel.form create mode 100644 platform/lang-impl/src/com/intellij/application/options/schemes/SchemesCombo.java diff --git a/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java b/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java index ea6bdc8da192..247d89fa6210 100644 --- a/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java +++ b/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java @@ -27,7 +27,6 @@ import com.intellij.openapi.editor.colors.ex.DefaultColorSchemesManager; import com.intellij.openapi.editor.markup.EffectType; import com.intellij.openapi.editor.markup.TextAttributes; import com.intellij.openapi.options.FontSize; -import com.intellij.openapi.options.SchemeManager; import com.intellij.openapi.options.SchemeState; import com.intellij.openapi.util.*; import com.intellij.util.JdomKt; @@ -871,14 +870,6 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial public static boolean isVisible(@NotNull EditorColorsScheme scheme) { return !(scheme instanceof AbstractColorsScheme) || ((AbstractColorsScheme)scheme).isVisible(); } - - public static String getDisplayName(@NotNull EditorColorsScheme scheme) { - String schemeName = scheme.getName(); - return - schemeName.startsWith(SchemeManager.EDITABLE_COPY_PREFIX) ? - schemeName.substring(SchemeManager.EDITABLE_COPY_PREFIX.length()) : - schemeName; - } @Nullable public AbstractColorsScheme getOriginal() { diff --git a/platform/lang-impl/src/com/intellij/application/options/InitialConfigurationDialog.java b/platform/lang-impl/src/com/intellij/application/options/InitialConfigurationDialog.java index a224210fd285..99ac8305ba60 100644 --- a/platform/lang-impl/src/com/intellij/application/options/InitialConfigurationDialog.java +++ b/platform/lang-impl/src/com/intellij/application/options/InitialConfigurationDialog.java @@ -37,11 +37,11 @@ import com.intellij.openapi.application.ApplicationNamesInfo; import com.intellij.openapi.application.impl.ApplicationImpl; import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.colors.EditorColorsScheme; -import com.intellij.openapi.editor.colors.impl.AbstractColorsScheme; import com.intellij.openapi.keymap.Keymap; import com.intellij.openapi.keymap.KeymapManager; import com.intellij.openapi.keymap.impl.DefaultKeymap; import com.intellij.openapi.keymap.impl.KeymapManagerImpl; +import com.intellij.openapi.options.SchemeManager; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.Task; @@ -151,7 +151,7 @@ public class InitialConfigurationDialog extends DialogWrapper { @Override public void customize(JList list, Object value, int index, boolean selected, boolean cellHasFocus) { if (value != null) { - setText(AbstractColorsScheme.getDisplayName((EditorColorsScheme)value)); + setText(SchemeManager.getDisplayName((EditorColorsScheme)value)); } } }); diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleMainPanel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleMainPanel.java index 9359a80a9a86..d745db7e7551 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleMainPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleMainPanel.java @@ -116,7 +116,7 @@ public class CodeStyleMainPanel extends JPanel implements TabbedLanguageCodeStyl link.setVerticalAlignment(SwingConstants.BOTTOM); JPanel top = new JPanel(new BorderLayout()); - top.add(BorderLayout.WEST, mySchemesPanel.getRootPanel()); + top.add(BorderLayout.WEST, mySchemesPanel); top.add(BorderLayout.EAST, link); top.setBorder(JBUI.Borders.empty(10)); add(top, BorderLayout.NORTH); diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesActions.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesActions.java index 9a6c46f5c37b..e2a5e91c795e 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesActions.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesActions.java @@ -17,6 +17,7 @@ package com.intellij.application.options.codeStyle; import com.intellij.application.options.SaveSchemeDialog; import com.intellij.application.options.SchemesToImportPopup; +import com.intellij.application.options.schemes.AbstractSchemesPanel; import com.intellij.application.options.schemes.DefaultSchemeActions; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; @@ -47,6 +48,10 @@ abstract class CodeStyleSchemesActions extends DefaultSchemeActions schemesPanel) { + super(schemesPanel); + } + @Override protected void addAdditionalActions(@NotNull List defaultActions) { @@ -117,7 +122,7 @@ abstract class CodeStyleSchemesActions extends DefaultSchemeActions names = CodeStyleSchemesImpl.getSchemeManager().getAllSchemeNames(); SaveSchemeDialog saveDialog = - new SaveSchemeDialog(getParentComponent(), ApplicationBundle.message("title.save.code.style.scheme.as"), names, selectedName); + new SaveSchemeDialog(getSchemesPanel(), ApplicationBundle.message("title.save.code.style.scheme.as"), names, selectedName); if (saveDialog.showAndGet()) { CodeStyleScheme newScheme = getSchemesModel().createNewScheme(saveDialog.getSchemeName(), getCurrentScheme()); getSchemesModel().addScheme(newScheme, true); @@ -167,7 +172,7 @@ abstract class CodeStyleSchemesActions extends DefaultSchemeActions(getParentComponent()) { + new SchemesToImportPopup(getSchemesPanel()) { @Override protected void onSchemeSelected(CodeStyleScheme scheme) { if (scheme != null) { @@ -184,7 +189,7 @@ abstract class CodeStyleSchemesActions extends DefaultSchemeActions importer, @NotNull CodeStyleScheme currentScheme) throws SchemeImportException { final VirtualFile selectedFile = SchemeImportUtil - .selectImportSource(importer.getSourceExtensions(), getParentComponent(), CodeStyleSchemesUIConfiguration.Util.getRecentImportFile()); + .selectImportSource(importer.getSourceExtensions(), getSchemesPanel(), CodeStyleSchemesUIConfiguration.Util.getRecentImportFile()); if (selectedFile != null) { CodeStyleSchemesUIConfiguration.Util.setRecentImportFile(selectedFile); final SchemeCreator schemeCreator = new SchemeCreator(); @@ -268,7 +273,7 @@ abstract class CodeStyleSchemesActions extends DefaultSchemeActions private final CodeStyleSchemesModel myModel; private boolean myIsReset = false; - private Font myDefaultComboFont; - private Font myBoldComboFont; public CodeStyleSchemesPanel(CodeStyleSchemesModel model) { myModel = model; @@ -58,27 +50,17 @@ public class CodeStyleSchemesPanel extends AbstractSchemesPanel } } - @Nullable - private CodeStyleScheme getSelectedScheme() { - Object selected = getSchemesCombo().getSelectedItem(); - if (selected instanceof CodeStyleScheme) { - return (CodeStyleScheme)selected; - } - return null; - } - public void resetSchemesCombo() { myIsReset = true; try { List schemes = new ArrayList<>(); schemes.addAll(myModel.getAllSortedSchemes()); - DefaultComboBoxModel model = new DefaultComboBoxModel(schemes.toArray()); - getSchemesCombo().setModel(model); + resetSchemes(schemes); if (myModel.isUsePerProjectSettings()) { - getSchemesCombo().setSelectedItem(myModel.getProjectScheme()); + selectScheme(myModel.getProjectScheme()); } else { - getSchemesCombo().setSelectedItem(myModel.getSelectedGlobalScheme()); + selectScheme(myModel.getSelectedGlobalScheme()); } } finally { @@ -90,10 +72,10 @@ public class CodeStyleSchemesPanel extends AbstractSchemesPanel myIsReset = true; try { if (myModel.isUsePerProjectSettings()) { - getSchemesCombo().setSelectedItem(myModel.getProjectScheme()); + selectScheme(myModel.getProjectScheme()); } else { - getSchemesCombo().setSelectedItem(myModel.getSelectedGlobalScheme()); + selectScheme(myModel.getSelectedGlobalScheme()); } } finally { @@ -103,51 +85,18 @@ public class CodeStyleSchemesPanel extends AbstractSchemesPanel public void usePerProjectSettingsOptionChanged() { if (myModel.isProjectScheme(myModel.getSelectedScheme())) { - getSchemesCombo().setSelectedItem(myModel.getProjectScheme()); + selectScheme(myModel.getProjectScheme()); } else { - getSchemesCombo().setSelectedItem(myModel.getSelectedScheme()); + selectScheme(myModel.getSelectedScheme()); } } - - @Override - protected ComboBox createSchemesCombo() { - ComboBox schemesCombo = new ComboBox(); - myDefaultComboFont = schemesCombo.getFont(); - myBoldComboFont = myDefaultComboFont.deriveFont(Font.BOLD); - schemesCombo.addActionListener(new ActionListener() { - @Override - public void actionPerformed(@NotNull ActionEvent e) { - if (!myIsReset) { - ApplicationManager.getApplication().invokeLater(() -> onCombo()); - } - } - }); - schemesCombo.setRenderer(new ListCellRendererWrapper() { - @Override - public void customize(final JList list, final Object value, final int index, final boolean selected, final boolean hasFocus) { - Font font = myDefaultComboFont; - if (value instanceof CodeStyleScheme) { - CodeStyleScheme scheme = (CodeStyleScheme)value; - if (scheme.isDefault() || myModel.isProjectScheme(scheme)) { - font = myBoldComboFont; - } - } - setFont(font); - } - }); - return schemesCombo; - } + @Override protected DefaultSchemeActions createSchemeActions() { return - new CodeStyleSchemesActions() { - @NotNull - @Override - protected JComponent getParentComponent() { - return getToolbarPanel(); - } + new CodeStyleSchemesActions(this) { @Override protected CodeStyleSchemesModel getSchemesModel() { @@ -159,6 +108,18 @@ public class CodeStyleSchemesPanel extends AbstractSchemesPanel protected CodeStyleScheme getCurrentScheme() { return getSelectedScheme(); } + + @Override + public SchemeLevel getSchemeLevel(@NotNull CodeStyleScheme scheme) { + return myModel.isProjectScheme(scheme) ? SchemeLevel.Project : SchemeLevel.IDE; + } + + @Override + protected void onSchemeChanged(@Nullable CodeStyleScheme scheme) { + if (!myIsReset) { + ApplicationManager.getApplication().invokeLater(() -> onCombo()); + } + } }; } } diff --git a/platform/lang-impl/src/com/intellij/application/options/colors/ColorAndFontOptions.java b/platform/lang-impl/src/com/intellij/application/options/colors/ColorAndFontOptions.java index c67f5fa3907e..538a4348db07 100644 --- a/platform/lang-impl/src/com/intellij/application/options/colors/ColorAndFontOptions.java +++ b/platform/lang-impl/src/com/intellij/application/options/colors/ColorAndFontOptions.java @@ -178,16 +178,20 @@ public class ColorAndFontOptions extends SearchableConfigurable.Parent.Abstract @NotNull public String[] getSchemeNames() { - List schemes = new ArrayList<>(mySchemes.values()); - Collections.sort(schemes, EditorColorSchemesComparator.INSTANCE); - - List names = new ArrayList<>(schemes.size()); - for (MyColorScheme scheme : schemes) { + List names = new ArrayList<>(); + for (EditorColorsScheme scheme : getOrderedSchemes()) { names.add(scheme.getName()); } return ArrayUtil.toStringArray(names); } + + @NotNull + public Collection getOrderedSchemes() { + List schemes = new ArrayList<>(mySchemes.values()); + Collections.sort(schemes, EditorColorSchemesComparator.INSTANCE); + return schemes; + } @NotNull public Collection getSchemes() { @@ -376,7 +380,7 @@ public class ColorAndFontOptions extends SearchableConfigurable.Parent.Abstract if (myRootSchemesPanel == null) { ensureSchemesPanel(); } - return myRootSchemesPanel.getRootPanel(); + return myRootSchemesPanel; } @Override diff --git a/platform/lang-impl/src/com/intellij/application/options/colors/ColorSchemeActions.java b/platform/lang-impl/src/com/intellij/application/options/colors/ColorSchemeActions.java index 0ee717daf7fe..35ae024a02d6 100644 --- a/platform/lang-impl/src/com/intellij/application/options/colors/ColorSchemeActions.java +++ b/platform/lang-impl/src/com/intellij/application/options/colors/ColorSchemeActions.java @@ -16,6 +16,7 @@ package com.intellij.application.options.colors; import com.intellij.application.options.SaveSchemeDialog; +import com.intellij.application.options.schemes.AbstractSchemesPanel; import com.intellij.application.options.schemes.DefaultSchemeActions; import com.intellij.openapi.application.ApplicationBundle; import com.intellij.openapi.editor.colors.EditorColorsScheme; @@ -32,13 +33,16 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; -import javax.swing.*; import java.util.ArrayList; import java.util.Collection; import java.util.List; public abstract class ColorSchemeActions extends DefaultSchemeActions { + protected ColorSchemeActions(@NotNull AbstractSchemesPanel schemesPanel) { + super(schemesPanel); + } + @Override protected Collection getSchemeImportersNames() { List importersNames = new ArrayList<>(); @@ -56,7 +60,7 @@ public abstract class ColorSchemeActions extends DefaultSchemeActions importer = SchemeImporterEP.getImporter(importerName, EditorColorsScheme.class); if (importer != null) { - VirtualFile importSource = SchemeImportUtil.selectImportSource(importer.getSourceExtensions(), getParentComponent(), null); + VirtualFile importSource = SchemeImportUtil.selectImportSource(importer.getSourceExtensions(), getSchemesPanel(), null); if (importSource != null) { try { EditorColorsScheme imported = @@ -73,7 +77,7 @@ public abstract class ColorSchemeActions extends DefaultSchemeActions { + importHandler.performImport(getSchemesPanel(), scheme -> { if (scheme != null) getOptions().addImportedScheme(scheme); }); return true; @@ -103,9 +107,9 @@ public abstract class ColorSchemeActions extends DefaultSchemeActions names = ContainerUtil.newArrayList(getOptions().getSchemeNames()); - String selectedName = AbstractColorsScheme.getDisplayName(scheme); + String selectedName = SchemeManager.getDisplayName(scheme); SaveSchemeDialog dialog = - new SaveSchemeDialog(getParentComponent(), ApplicationBundle.message("title.save.color.scheme.as"), names, selectedName); + new SaveSchemeDialog(getSchemesPanel(), ApplicationBundle.message("title.save.color.scheme.as"), names, selectedName); if (dialog.showAndGet()) { getOptions().saveSchemeAs(dialog.getSchemeName()); } diff --git a/platform/lang-impl/src/com/intellij/application/options/colors/NewColorAndFontPanel.java b/platform/lang-impl/src/com/intellij/application/options/colors/NewColorAndFontPanel.java index 61b68729f65d..6196aac537f4 100644 --- a/platform/lang-impl/src/com/intellij/application/options/colors/NewColorAndFontPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/colors/NewColorAndFontPanel.java @@ -61,7 +61,7 @@ public class NewColorAndFontPanel extends JPanel { JPanel top = new JPanel(new BorderLayout()); - top.add(mySchemesPanel.getRootPanel(), BorderLayout.NORTH); + top.add(mySchemesPanel, BorderLayout.NORTH); top.add(myOptionsPanel.getPanel(), BorderLayout.CENTER); if (optionsPanel instanceof ConsoleFontOptions) { JPanel wrapper = new JPanel(new FlowLayout(FlowLayout.TRAILING)); @@ -220,7 +220,7 @@ public class NewColorAndFontPanel extends JPanel { .createBalloon(); Disposer.register(ApplicationManager.getApplication(), balloon); balloon.show(new RelativePoint( - mySchemesPanel.getRootPanel(), - new Point(mySchemesPanel.getRootPanel().getWidth() / 10, mySchemesPanel.getRootPanel().getHeight())), Balloon.Position.below); + mySchemesPanel, + new Point(mySchemesPanel.getWidth() / 10, mySchemesPanel.getHeight())), Balloon.Position.below); } } diff --git a/platform/lang-impl/src/com/intellij/application/options/colors/SchemesPanel.java b/platform/lang-impl/src/com/intellij/application/options/colors/SchemesPanel.java index b4fc82e32033..369ae1921bc8 100644 --- a/platform/lang-impl/src/com/intellij/application/options/colors/SchemesPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/colors/SchemesPanel.java @@ -20,16 +20,10 @@ import com.intellij.application.options.SkipSelfSearchComponent; import com.intellij.application.options.schemes.AbstractSchemesPanel; import com.intellij.application.options.schemes.DefaultSchemeActions; import com.intellij.openapi.editor.colors.EditorColorsScheme; -import com.intellij.openapi.editor.colors.impl.AbstractColorsScheme; -import com.intellij.openapi.ui.ComboBox; import com.intellij.util.EventDispatcher; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import javax.swing.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - public class SchemesPanel extends AbstractSchemesPanel implements SkipSelfSearchComponent { private final ColorAndFontOptions myOptions; @@ -60,30 +54,14 @@ public class SchemesPanel extends AbstractSchemesPanel imple public void resetSchemesCombo(final Object source) { if (this != source) { setListLoaded(false); - EditorColorsScheme selectedSchemeBackup = myOptions.getSelectedScheme(); - getSchemesCombo().removeAllItems(); - - String[] schemeNames = myOptions.getSchemeNames(); - MySchemeItem itemToSelect = null; - for (String schemeName : schemeNames) { - EditorColorsScheme scheme = myOptions.getScheme(schemeName); - MySchemeItem item = new MySchemeItem(scheme); - if (scheme == selectedSchemeBackup) itemToSelect = item; - getSchemesCombo().addItem(item); - } - - getSchemesCombo().setSelectedItem(itemToSelect); + resetSchemes(myOptions.getOrderedSchemes()); + selectScheme(selectedSchemeBackup); setListLoaded(true); - myDispatcher.getMulticaster().schemeChanged(this); } } - @Nullable - private String getSelectedSchemeName() { - return getSchemesCombo().getSelectedIndex() != -1 ? ((MySchemeItem)getSchemesCombo().getSelectedItem()).getSchemeName() : null; - } private void setListLoaded(final boolean b) { myListLoaded = b; @@ -93,63 +71,40 @@ public class SchemesPanel extends AbstractSchemesPanel imple myDispatcher.addListener(listener); } - @Override - protected ComboBox createSchemesCombo() { - ComboBox schemesCombo = new ComboBox(); - schemesCombo.addActionListener(new ActionListener() { - @Override - public void actionPerformed(@NotNull ActionEvent e) { - String selectedName = getSelectedSchemeName(); - if (selectedName != null) { - myOptions.selectScheme(selectedName); - if (areSchemesLoaded()) { - myDispatcher.getMulticaster().schemeChanged(SchemesPanel.this); - } - } - } - }); - return schemesCombo; - } - @Override protected DefaultSchemeActions createSchemeActions() { return - new ColorSchemeActions() { - @NotNull - @Override - protected JComponent getParentComponent() { - return getToolbarPanel(); - } + new ColorSchemeActions(this) { - @NotNull - @Override - protected ColorAndFontOptions getOptions() { - return myOptions; - } + @NotNull + @Override + protected ColorAndFontOptions getOptions() { + return myOptions; + } - @Nullable - @Override - protected EditorColorsScheme getCurrentScheme() { - return myOptions != null ? myOptions.getScheme(getSelectedSchemeName()) : null; - } - }; + @Override + protected void onSchemeChanged(@Nullable EditorColorsScheme scheme) { + if (scheme != null) { + myOptions.selectScheme(scheme.getName()); + if (areSchemesLoaded()) { + myDispatcher.getMulticaster().schemeChanged(SchemesPanel.this); + } + } + } + + @Nullable + @Override + protected EditorColorsScheme getCurrentScheme() { + EditorColorsScheme selectedScheme = getSelectedScheme(); + return selectedScheme != null ? myOptions.getScheme(selectedScheme.getName()) : null; + } + + @Override + public SchemeLevel getSchemeLevel(@NotNull EditorColorsScheme scheme) { + return SchemeLevel.IDE_Only; + } + }; } - private final static class MySchemeItem { - private EditorColorsScheme myScheme; - - public MySchemeItem(EditorColorsScheme scheme) { - myScheme = scheme; - } - - public String getSchemeName() { - return myScheme.getName(); - } - - @Override - public String toString() { - return AbstractColorsScheme.getDisplayName(myScheme); - } - } } diff --git a/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemesPanel.form b/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemesPanel.form deleted file mode 100644 index 54484d6be3df..000000000000 --- a/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemesPanel.form +++ /dev/null @@ -1,58 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemesPanel.java b/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemesPanel.java index ff5e73e4d12c..79ff67e07031 100644 --- a/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemesPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemesPanel.java @@ -17,35 +17,41 @@ package com.intellij.application.options.schemes; import com.intellij.icons.AllIcons; import com.intellij.openapi.actionSystem.*; +import com.intellij.openapi.application.ApplicationBundle; import com.intellij.openapi.options.Scheme; import com.intellij.openapi.project.DumbAware; -import com.intellij.openapi.ui.ComboBox; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.awt.*; import java.util.Collection; -public abstract class AbstractSchemesPanel { +public abstract class AbstractSchemesPanel extends JPanel { - private ComboBox mySchemesCombo; - private JPanel myPanel; - private JPanel myToolbarPanel; + private SchemesCombo mySchemesCombo; + private DefaultSchemeActions myActions; + private JComponent myToolbar; public AbstractSchemesPanel() { - myToolbarPanel.add(createToolbar()); + setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); + createUIComponents(); } - - public JPanel getRootPanel() { - return myPanel; - } - - public JPanel getToolbarPanel() { - return myToolbarPanel; - } - + private void createUIComponents() { - mySchemesCombo = createSchemesCombo(); + JPanel controlsPanel = new JPanel(); + controlsPanel.setLayout(new BoxLayout(controlsPanel, BoxLayout.LINE_AXIS)); + controlsPanel.add(new JLabel(ApplicationBundle.message("editbox.scheme.name"))); + controlsPanel.add(Box.createRigidArea(new Dimension(10, 0))); + myActions = createSchemeActions(); + mySchemesCombo = new SchemesCombo<>(myActions); + controlsPanel.add(mySchemesCombo.getComboBox()); + myToolbar = createToolbar(); + controlsPanel.add(myToolbar); + controlsPanel.setMaximumSize(new Dimension(controlsPanel.getMaximumSize().width, mySchemesCombo.getComboBox().getPreferredSize().height)); + add(controlsPanel); + add(Box.createVerticalGlue()); + add(Box.createRigidArea(new Dimension(0, 10))); } private JComponent createToolbar() { @@ -64,7 +70,7 @@ public abstract class AbstractSchemesPanel { @NotNull @Override public AnAction[] getChildren(@Nullable AnActionEvent e) { - Collection actions = createSchemeActions().getActions(); + Collection actions = myActions.getActions(); return actions.toArray(new AnAction[actions.size()]); } @@ -74,16 +80,26 @@ public abstract class AbstractSchemesPanel { p.setIcon(AllIcons.General.GearPlain); } } - - protected abstract ComboBox createSchemesCombo(); - - protected abstract DefaultSchemeActions createSchemeActions(); - public ComboBox getSchemesCombo() { - return mySchemesCombo; + public JComponent getToolbar() { + return myToolbar; + } + + protected abstract DefaultSchemeActions createSchemeActions(); + + public T getSelectedScheme() { + return mySchemesCombo.getSelectedScheme(); + } + + public void selectScheme(@Nullable T scheme) { + mySchemesCombo.selectScheme(scheme); + } + + public void resetSchemes(@NotNull Collection schemes) { + mySchemesCombo.resetSchemes(schemes); } public void disposeUIResources() { - myPanel.removeAll(); + removeAll(); } } diff --git a/platform/lang-impl/src/com/intellij/application/options/schemes/DefaultSchemeActions.java b/platform/lang-impl/src/com/intellij/application/options/schemes/DefaultSchemeActions.java index c5489e986858..b3addab2ce9e 100644 --- a/platform/lang-impl/src/com/intellij/application/options/schemes/DefaultSchemeActions.java +++ b/platform/lang-impl/src/com/intellij/application/options/schemes/DefaultSchemeActions.java @@ -26,7 +26,6 @@ import com.intellij.openapi.ui.popup.ListPopup; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import javax.swing.*; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -34,8 +33,14 @@ import java.util.List; public abstract class DefaultSchemeActions { private final Collection mySchemeImportersNames; private final Collection mySchemeExporterNames; + private AbstractSchemesPanel mySchemesPanel; + + public enum SchemeLevel { + IDE_Only, IDE, Project + } - protected DefaultSchemeActions() { + protected DefaultSchemeActions(@NotNull AbstractSchemesPanel schemesPanel) { + mySchemesPanel = schemesPanel; mySchemeImportersNames = getSchemeImportersNames(); mySchemeExporterNames = getSchemeExporterNames(); } @@ -173,7 +178,7 @@ public abstract class DefaultSchemeActions { return namedActions.toArray(new AnAction[namedActions.size()]); } }, e.getDataContext(), null, true); - listPopup.showUnderneathOf(getParentComponent()); + listPopup.showUnderneathOf(mySchemesPanel.getToolbar()); } @NotNull @@ -232,13 +237,17 @@ public abstract class DefaultSchemeActions { protected abstract void doExport(@NotNull T scheme, @NotNull String exporterName); + protected abstract void onSchemeChanged(@Nullable T scheme); + @Nullable protected abstract T getCurrentScheme(); protected abstract Class getSchemeType(); - @NotNull - protected abstract JComponent getParentComponent(); - + public AbstractSchemesPanel getSchemesPanel() { + return mySchemesPanel; + } + + public abstract SchemeLevel getSchemeLevel(@NotNull T scheme); } diff --git a/platform/lang-impl/src/com/intellij/application/options/schemes/SchemesCombo.java b/platform/lang-impl/src/com/intellij/application/options/schemes/SchemesCombo.java new file mode 100644 index 000000000000..ccd90c94a55f --- /dev/null +++ b/platform/lang-impl/src/com/intellij/application/options/schemes/SchemesCombo.java @@ -0,0 +1,177 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.application.options.schemes; + +import com.intellij.openapi.options.Scheme; +import com.intellij.openapi.options.SchemeManager; +import com.intellij.openapi.ui.ComboBox; +import com.intellij.ui.*; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Collection; + +public class SchemesCombo { + private ComboBox myComboBox; + private DefaultSchemeActions myActions; + + public SchemesCombo(@NotNull DefaultSchemeActions actions) { + myActions = actions; + myComboBox = new ComboBox<>(); + myComboBox.setRenderer(new MyListCellRenderer()); + myComboBox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + myActions.onSchemeChanged(getSelectedScheme()); + } + }); + myComboBox.setModel(new DefaultComboBoxModel() { + @Override + public void setSelectedItem(Object anObject) { + if (anObject instanceof OptionalSeparatorItem && ((OptionalSeparatorItem)anObject).isSeparator()) { + return; + } + super.setSelectedItem(anObject); + } + }); + } + + private SimpleTextAttributes getSchemeAttributes(@NotNull T scheme) { + return myActions.isDeleteAvailable(scheme) ? SimpleTextAttributes.REGULAR_ATTRIBUTES : SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES; + } + + public void resetSchemes(@NotNull Collection schemes) { + myComboBox.removeAllItems(); + DefaultSchemeActions.SchemeLevel currSchemeLevel = DefaultSchemeActions.SchemeLevel.IDE_Only; + for (T scheme : schemes) { + DefaultSchemeActions.SchemeLevel schemeLevel = myActions.getSchemeLevel(scheme); + if (!currSchemeLevel.equals(schemeLevel)) { + currSchemeLevel = schemeLevel; + if (!schemeLevel.equals(DefaultSchemeActions.SchemeLevel.IDE_Only)) { + myComboBox.addItem(new MySchemeItem(currSchemeLevel.toString())); + } + } + myComboBox.addItem(new MySchemeItem(scheme)); + } + } + + private class MyListCellRenderer extends ColoredListCellRenderer { + private ListCellRendererWrapper myWrapper = new ListCellRendererWrapper() { + @Override + public void customize(JList list, + MySchemeItem value, + int index, + boolean selected, + boolean hasFocus) { + if (value.isSeparator()) { + setText(" Stored in " + value.getPresentableText()); + setSeparator(); + } + } + }; + + @Override + public Component getListCellRendererComponent(JList list, + MySchemeItem value, + int index, + boolean selected, + boolean hasFocus) { + if (value.isSeparator()) { + Component c = myWrapper.getListCellRendererComponent(list, value, index, selected, hasFocus); + if (c instanceof TitledSeparator) { + ((TitledSeparator)c).getLabel().setForeground(JBColor.GRAY); + return c; + } + } + return super.getListCellRendererComponent(list, value, index, selected, hasFocus); + } + + @Override + protected void customizeCellRenderer(@NotNull JList list, + MySchemeItem value, + int index, + boolean selected, + boolean hasFocus) { + if (value.getScheme() != null) { + append(value.getPresentableText(), getSchemeAttributes(value.getScheme())); + DefaultSchemeActions.SchemeLevel schemeLevel = myActions.getSchemeLevel(value.getScheme()); + if (index == -1 && !DefaultSchemeActions.SchemeLevel.IDE_Only.equals(schemeLevel)) { + append(" " + schemeLevel.toString(), SimpleTextAttributes.GRAY_ATTRIBUTES); + } + } + } + } + + @Nullable + public T getSelectedScheme() { + int i = myComboBox.getSelectedIndex(); + return i >= 0 ? myComboBox.getItemAt(i).getScheme() : null; + } + + public void selectScheme(@Nullable T scheme) { + for (int i = 0; i < myComboBox.getItemCount(); i ++) { + if (myComboBox.getItemAt(i).getScheme() == scheme) { + myComboBox.setSelectedIndex(i); + break; + } + } + } + + public ComboBox getComboBox() { + return myComboBox; + } + + private interface OptionalSeparatorItem { + boolean isSeparator(); + } + + private final class MySchemeItem implements OptionalSeparatorItem { + private @Nullable T myScheme; + private @Nullable String myText; + + public MySchemeItem(@NotNull String text) { + myText = text; + } + + public MySchemeItem(@Nullable T scheme) { + myScheme = scheme; + } + + @Nullable + public String getSchemeName() { + return myScheme != null ? myScheme.getName() : null; + } + + @Nullable + public T getScheme() { + return myScheme; + } + + @NotNull + public String getPresentableText() { + return myScheme != null ? SchemeManager.getDisplayName(myScheme) : myText != null ? myText : ""; + } + + @Override + public boolean isSeparator() { + return myScheme == null; + } + } +} diff --git a/platform/platform-impl/src/com/intellij/ide/actions/QuickChangeColorSchemeAction.java b/platform/platform-impl/src/com/intellij/ide/actions/QuickChangeColorSchemeAction.java index 818a82fdf6e7..b6f83fa2b670 100644 --- a/platform/platform-impl/src/com/intellij/ide/actions/QuickChangeColorSchemeAction.java +++ b/platform/platform-impl/src/com/intellij/ide/actions/QuickChangeColorSchemeAction.java @@ -20,7 +20,7 @@ import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.actionSystem.DefaultActionGroup; import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.colors.EditorColorsScheme; -import com.intellij.openapi.editor.colors.impl.AbstractColorsScheme; +import com.intellij.openapi.options.SchemeManager; import com.intellij.openapi.project.DumbAwareAction; import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; @@ -42,7 +42,7 @@ public class QuickChangeColorSchemeAction extends QuickSwitchSchemeAction { final EditorColorsScheme current, final EditorColorsScheme scheme, final boolean addScheme) { - group.add(new DumbAwareAction(AbstractColorsScheme.getDisplayName(scheme), "", scheme == current ? ourCurrentAction : ourNotCurrentAction) { + group.add(new DumbAwareAction(SchemeManager.getDisplayName(scheme), "", scheme == current ? ourCurrentAction : ourNotCurrentAction) { @Override public void actionPerformed(@Nullable AnActionEvent e) { if (addScheme) { diff --git a/platform/projectModel-impl/src/com/intellij/openapi/options/SchemeManager.java b/platform/projectModel-impl/src/com/intellij/openapi/options/SchemeManager.java index d01e26bba802..58d1f8687354 100644 --- a/platform/projectModel-impl/src/com/intellij/openapi/options/SchemeManager.java +++ b/platform/projectModel-impl/src/com/intellij/openapi/options/SchemeManager.java @@ -122,4 +122,12 @@ public abstract class SchemeManager { public boolean isMetadataEditable(@NotNull T scheme) { return true; } + + public static String getDisplayName(@NotNull Scheme scheme) { + String schemeName = scheme.getName(); + return + schemeName.startsWith(EDITABLE_COPY_PREFIX) ? + schemeName.substring(EDITABLE_COPY_PREFIX.length()) : + schemeName; + } }