From f8a22f70a5598efeaaa3c03aabaa0cf2942f75ae Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Fri, 2 Dec 2016 17:39:46 +0300 Subject: [PATCH] Color schemes panel UI refactoring --- .../options/DefaultSchemeActions.java | 11 ++ .../options/colors/ColorSchemeActions.java | 128 +++++++++++++++++ .../options/colors/SchemesPanel.java | 134 ++---------------- 3 files changed, 151 insertions(+), 122 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/application/options/colors/ColorSchemeActions.java diff --git a/platform/lang-impl/src/com/intellij/application/options/DefaultSchemeActions.java b/platform/lang-impl/src/com/intellij/application/options/DefaultSchemeActions.java index 30067598dd3e..060ba5029f22 100644 --- a/platform/lang-impl/src/com/intellij/application/options/DefaultSchemeActions.java +++ b/platform/lang-impl/src/com/intellij/application/options/DefaultSchemeActions.java @@ -81,6 +81,13 @@ public abstract class DefaultSchemeActions { public void actionPerformed(AnActionEvent e) { doReset(); } + + @Override + public void update(AnActionEvent e) { + Presentation p = e.getPresentation(); + T currentScheme = getCurrentScheme(); + p.setEnabled(currentScheme != null && isResetAvailable(currentScheme)); + } } @@ -188,6 +195,10 @@ public abstract class DefaultSchemeActions { protected abstract boolean isDeleteAvailable(@NotNull T scheme); + protected boolean isResetAvailable(@NotNull T scheme) { + return true; + } + protected abstract void doExport(@NotNull T scheme, @NotNull String exporterName); @Nullable 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 new file mode 100644 index 000000000000..4e9903ff224e --- /dev/null +++ b/platform/lang-impl/src/com/intellij/application/options/colors/ColorSchemeActions.java @@ -0,0 +1,128 @@ +/* + * 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.colors; + +import com.intellij.application.options.DefaultSchemeActions; +import com.intellij.application.options.SaveSchemeDialog; +import com.intellij.openapi.application.ApplicationBundle; +import com.intellij.openapi.editor.colors.EditorColorsScheme; +import com.intellij.openapi.editor.colors.impl.AbstractColorsScheme; +import com.intellij.openapi.editor.colors.impl.EditorColorsSchemeImpl; +import com.intellij.openapi.editor.colors.impl.EmptyColorScheme; +import com.intellij.openapi.editor.colors.impl.ReadOnlyColorsScheme; +import com.intellij.openapi.options.*; +import com.intellij.openapi.project.DefaultProjectFactory; +import com.intellij.openapi.ui.MessageType; +import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; +import java.util.List; + +public abstract class ColorSchemeActions extends DefaultSchemeActions { + private final ColorAndFontOptions myOptions; + private JComponent myParentComponent; + + public ColorSchemeActions(@NotNull JComponent parentComponent, @NotNull ColorAndFontOptions options) { + myOptions = options; + myParentComponent = parentComponent; + } + + @Override + protected void doImport(@NotNull String importerName) { + final SchemeImporter importer = SchemeImporterEP.getImporter(importerName, EditorColorsScheme.class); + if (importer != null) { + VirtualFile importSource = SchemeImportUtil.selectImportSource(importer.getSourceExtensions(), myParentComponent, null); + if (importSource != null) { + try { + EditorColorsScheme imported = + importer.importScheme(DefaultProjectFactory.getInstance().getDefaultProject(), importSource, myOptions.getSelectedScheme(), + name -> { + String newName = myOptions.getUniqueName(name != null ? name : "Unnamed"); + AbstractColorsScheme newScheme = new EditorColorsSchemeImpl(EmptyColorScheme.INSTANCE); + newScheme.setName(newName); + newScheme.setDefaultMetaInfo(EmptyColorScheme.INSTANCE); + return newScheme; + }); + if (imported != null) { + myOptions.addImportedScheme(imported); + } + } + catch (SchemeImportException e) { + SchemeImportUtil.showStatus(myParentComponent, "Import failed: " + e.getMessage(), MessageType.ERROR); + } + } + } + } + + @Override + protected void doReset() { + EditorColorsScheme currentScheme = getCurrentScheme(); + if (currentScheme != null) { + if (Messages + .showOkCancelDialog(ApplicationBundle.message("color.scheme.reset.message"), + ApplicationBundle.message("color.scheme.reset.title"), Messages.getQuestionIcon()) == Messages.OK) { + myOptions.resetSchemeToOriginal(currentScheme.getName()); + } + } + } + + @Override + protected void doSaveAs() { + List names = ContainerUtil.newArrayList(myOptions.getSchemeNames()); + String selectedName = AbstractColorsScheme.getDisplayName(myOptions.getSelectedScheme()); + SaveSchemeDialog dialog = + new SaveSchemeDialog(myParentComponent, ApplicationBundle.message("title.save.color.scheme.as"), names, selectedName); + if (dialog.showAndGet()) { + myOptions.saveSchemeAs(dialog.getSchemeName()); + } + } + + @Override + protected void doDelete() { + EditorColorsScheme currentScheme = getCurrentScheme(); + if (currentScheme != null) { + myOptions.removeScheme(currentScheme.getName()); + } + } + + @Override + protected boolean isDeleteAvailable(@NotNull EditorColorsScheme scheme) { + return !ColorAndFontOptions.isReadOnly(scheme) && ColorAndFontOptions.canBeDeleted(scheme); + } + + @Override + protected boolean isResetAvailable(@NotNull EditorColorsScheme scheme) { + AbstractColorsScheme originalScheme = + scheme instanceof AbstractColorsScheme ? ((AbstractColorsScheme)scheme).getOriginal() : null; + return + !ColorAndFontOptions.isReadOnly(scheme) && + scheme.getName().startsWith(SchemeManager.EDITABLE_COPY_PREFIX) && + originalScheme instanceof ReadOnlyColorsScheme; + } + + @Override + protected void doExport(@NotNull EditorColorsScheme scheme, @NotNull String exporterName) { + // Unsupported for now. + } + + @Override + protected Class getSchemeType() { + return EditorColorsScheme.class; + } +} 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 ea5812a23553..699970f53884 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 @@ -16,24 +16,14 @@ package com.intellij.application.options.colors; -import com.intellij.application.options.ImportSourceChooserDialog; -import com.intellij.application.options.SaveSchemeDialog; import com.intellij.application.options.SkipSelfSearchComponent; import com.intellij.openapi.application.ApplicationBundle; import com.intellij.openapi.editor.colors.EditorColorsScheme; import com.intellij.openapi.editor.colors.impl.AbstractColorsScheme; -import com.intellij.openapi.editor.colors.impl.EditorColorsSchemeImpl; -import com.intellij.openapi.editor.colors.impl.EmptyColorScheme; -import com.intellij.openapi.editor.colors.impl.ReadOnlyColorsScheme; import com.intellij.openapi.extensions.Extensions; -import com.intellij.openapi.options.*; -import com.intellij.openapi.project.DefaultProjectFactory; import com.intellij.openapi.ui.ComboBox; -import com.intellij.openapi.ui.MessageType; -import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.ui.ManageSchemesComboAction; import com.intellij.util.EventDispatcher; -import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.JBInsets; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -42,16 +32,12 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.util.List; public class SchemesPanel extends JPanel implements SkipSelfSearchComponent { private final ColorAndFontOptions myOptions; private ComboBox mySchemeComboBox; - - private JButton myDeleteButton; - private JButton myResetButton; - private JButton myImportButton; + private JLabel myHintLabel; private final EventDispatcher myDispatcher = EventDispatcher.create(ColorAndFontSettingsListener.class); @@ -73,18 +59,10 @@ public class SchemesPanel extends JPanel implements SkipSelfSearchComponent { String selectedName = getSelectedSchemeName(); if (selectedName != null) { EditorColorsScheme selected = myOptions.selectScheme(selectedName); - final boolean readOnly = ColorAndFontOptions.isReadOnly(selected); - myDeleteButton.setEnabled(!readOnly && ColorAndFontOptions.canBeDeleted(selected)); - myHintLabel.setVisible(readOnly); + myHintLabel.setVisible(ColorAndFontOptions.isReadOnly(selected)); if (areSchemesLoaded()) { myDispatcher.getMulticaster().schemeChanged(SchemesPanel.this); } - AbstractColorsScheme originalScheme = - selected instanceof AbstractColorsScheme ? ((AbstractColorsScheme)selected).getOriginal() : null; - myResetButton.setEnabled( - !readOnly && - selectedName.startsWith(SchemeManager.EDITABLE_COPY_PREFIX) && - originalScheme instanceof ReadOnlyColorsScheme); } } }); @@ -107,61 +85,20 @@ public class SchemesPanel extends JPanel implements SkipSelfSearchComponent { mySchemeComboBox = new ComboBox<>(); panel.add(mySchemeComboBox, - new GridBagConstraints(gridx++, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new JBInsets(0, 0, 5, 10), + new GridBagConstraints(gridx++, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new JBInsets(0, 0, 5, 10), 0, 0)); - - JButton saveAsButton = new JButton(ApplicationBundle.message("button.save.as")); - saveAsButton.addActionListener(new ActionListener() { + ManageSchemesComboAction schemesComboAction = new ManageSchemesComboAction(new ColorSchemeActions(this, myOptions) { + @Nullable @Override - public void actionPerformed(@NotNull ActionEvent e) { - showSaveAsDialog(); + protected EditorColorsScheme getCurrentScheme() { + return myOptions.getScheme(getSelectedSchemeName()); } }); - panel.add(saveAsButton, - new GridBagConstraints(gridx++, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new JBInsets(0, 0, 5, 5), + JButton manageButton = schemesComboAction.createCombo(); + panel.add(manageButton, + new GridBagConstraints(gridx++, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new JBInsets(0, 0, 5, 5), 0, 0)); - - myDeleteButton = new JButton(ApplicationBundle.message("button.delete")); - myDeleteButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(@NotNull ActionEvent e) { - String selectedName = getSelectedSchemeName(); - if (selectedName != null) { - myOptions.removeScheme(selectedName); - } - } - }); - panel.add(myDeleteButton, - new GridBagConstraints(gridx++, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new JBInsets(0, 0, 5, 5), 0, - 0)); - myResetButton = new JButton(ApplicationBundle.message("color.scheme.reset")); - myResetButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(@NotNull ActionEvent e) { - String selectedName = getSelectedSchemeName(); - if (selectedName != null) { - if (Messages - .showOkCancelDialog(ApplicationBundle.message("color.scheme.reset.message"), - ApplicationBundle.message("color.scheme.reset.title"), Messages.getQuestionIcon()) == Messages.OK) { - myOptions.resetSchemeToOriginal(selectedName); - } - } - } - }); - panel.add(myResetButton, - new GridBagConstraints(gridx++, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new JBInsets(0, 0, 5, 5), 0, - 0)); - myImportButton = new JButton("Import..."); - myImportButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - chooseAndImport(); - } - }); - myImportButton.setVisible(isImportAvailable()); - panel.add(myImportButton, - new GridBagConstraints(gridx++, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new JBInsets(0, 0, 5, 5), 0, - 0)); + myHintLabel = new JLabel(ApplicationBundle.message("hint.readonly.scheme.cannot.be.modified")); myHintLabel.setEnabled(false); panel.add(myHintLabel, @@ -189,15 +126,6 @@ public class SchemesPanel extends JPanel implements SkipSelfSearchComponent { return panel; } - private void showSaveAsDialog() { - List names = ContainerUtil.newArrayList(myOptions.getSchemeNames()); - String selectedName = AbstractColorsScheme.getDisplayName(myOptions.getSelectedScheme()); - SaveSchemeDialog dialog = new SaveSchemeDialog(this, ApplicationBundle.message("title.save.color.scheme.as"), names, selectedName); - if (dialog.showAndGet()) { - myOptions.saveSchemeAs(dialog.getSchemeName()); - } - } - @Deprecated public boolean updateDescription(boolean modified) { EditorColorsScheme scheme = myOptions.getSelectedScheme(); @@ -245,44 +173,6 @@ public class SchemesPanel extends JPanel implements SkipSelfSearchComponent { myDispatcher.addListener(listener); } - private void chooseAndImport() { - ImportSourceChooserDialog importSourceChooserDialog = - new ImportSourceChooserDialog<>(this, EditorColorsScheme.class); - if (importSourceChooserDialog.showAndGet()) { - final String selectedImporterName = importSourceChooserDialog.getSelectedSourceName(); - if (selectedImporterName != null) { - final SchemeImporter importer = SchemeImporterEP.getImporter(selectedImporterName, EditorColorsScheme.class); - if (importer != null) { - VirtualFile importSource = SchemeImportUtil.selectImportSource(importer.getSourceExtensions(), this, null); - if (importSource != null) { - try { - EditorColorsScheme imported = - importer.importScheme(DefaultProjectFactory.getInstance().getDefaultProject(), importSource, myOptions.getSelectedScheme(), - name -> { - String newName = myOptions.getUniqueName(name); - AbstractColorsScheme newScheme = new EditorColorsSchemeImpl(EmptyColorScheme.INSTANCE); - newScheme.setName(newName); - newScheme.setDefaultMetaInfo(EmptyColorScheme.INSTANCE); - return newScheme; - }); - if (imported != null) { - myOptions.addImportedScheme(imported); - } - - } - catch (SchemeImportException e) { - SchemeImportUtil.showStatus(myImportButton, "Import failed: " + e.getMessage(), MessageType.ERROR); - } - } - } - } - } - } - - private static boolean isImportAvailable() { - return !SchemeImporterEP.getExtensions(EditorColorsScheme.class).isEmpty(); - } - private final static class MySchemeItem { private EditorColorsScheme myScheme;