From 49a102d8594dfc45f6f9200a8ce615e488306cf6 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Thu, 10 Mar 2016 15:07:17 +0300 Subject: [PATCH] ui: allow to pass custom color description panel --- .../colors/ColorAndFontDescriptionPanel.java | 102 ++++++++++-------- .../options/colors/OptionsPanelImpl.java | 43 ++++++-- 2 files changed, 94 insertions(+), 51 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/application/options/colors/ColorAndFontDescriptionPanel.java b/platform/lang-impl/src/com/intellij/application/options/colors/ColorAndFontDescriptionPanel.java index f53ca1379019..d65bacf64c05 100644 --- a/platform/lang-impl/src/com/intellij/application/options/colors/ColorAndFontDescriptionPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/colors/ColorAndFontDescriptionPanel.java @@ -26,6 +26,7 @@ import com.intellij.openapi.util.Pair; import com.intellij.ui.*; import com.intellij.ui.components.JBCheckBox; import com.intellij.util.BitUtil; +import com.intellij.util.EventDispatcher; import com.intellij.util.FontUtil; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.UIUtil; @@ -44,7 +45,8 @@ import java.util.Map; /** * @author cdr */ -public class ColorAndFontDescriptionPanel extends JPanel { +public class ColorAndFontDescriptionPanel extends JPanel implements OptionsPanelImpl.ColorDescriptionPanel { + private final EventDispatcher myDispatcher = EventDispatcher.create(Listener.class); private JPanel myPanel; @@ -119,15 +121,24 @@ public class ColorAndFontDescriptionPanel extends JPanel { myLabelFont.setVisible(false); // hide for now as it doesn't look that good } - protected void onHyperLinkClicked(HyperlinkEvent e) { + @NotNull + @Override + public JComponent getPanel() { + return this; } - protected void onSettingsChanged(ActionEvent e) { + private void onHyperLinkClicked(HyperlinkEvent e) { + myDispatcher.getMulticaster().onHyperLinkClicked(e); + } + + private void onSettingsChanged(ActionEvent e) { myErrorStripeColorChooser.setEnabled(myCbErrorStripe.isSelected()); myForegroundChooser.setEnabled(myCbForeground.isSelected()); myBackgroundChooser.setEnabled(myCbBackground.isSelected()); myEffectsColorChooser.setEnabled(myCbEffects.isSelected()); myEffectsCombo.setEnabled(myCbEffects.isSelected()); + + myDispatcher.getMulticaster().onSettingsChanged(e); } public void resetDefault() { @@ -161,7 +172,7 @@ public class ColorAndFontDescriptionPanel extends JPanel { colorPanel.setEnabled(isChecked); } - public void reset(ColorAndFontDescription description) { + public void reset(@NotNull ColorAndFontDescription description) { if (description.isFontEnabled()) { myLabelFont.setEnabled(true); myCbBold.setEnabled(true); @@ -252,50 +263,53 @@ public class ColorAndFontDescriptionPanel extends JPanel { myBackgroundChooser.setEditable(isEditEnabled); } - public void apply(ColorAndFontDescription description, EditorColorsScheme scheme) { - if (description != null) { - description.setInherited(myInheritAttributesBox.isSelected()); - if (description.isInherited()) { - TextAttributes baseAttributes = description.getBaseAttributes(); - if (baseAttributes != null) { - description.setFontType(baseAttributes.getFontType()); - description.setForegroundChecked(baseAttributes.getForegroundColor() != null); - description.setForegroundColor(baseAttributes.getForegroundColor()); - description.setBackgroundChecked(baseAttributes.getBackgroundColor() != null); - description.setBackgroundColor(baseAttributes.getBackgroundColor()); - description.setErrorStripeChecked(baseAttributes.getErrorStripeColor() != null); - description.setErrorStripeColor(baseAttributes.getErrorStripeColor()); - description.setEffectColor(baseAttributes.getEffectColor()); - description.setEffectType(baseAttributes.getEffectType()); - description.setEffectsColorChecked(baseAttributes.getEffectColor() != null); - } - else { - description.setInherited(false); - } - reset(description); + public void apply(@NotNull ColorAndFontDescription description, EditorColorsScheme scheme) { + description.setInherited(myInheritAttributesBox.isSelected()); + if (description.isInherited()) { + TextAttributes baseAttributes = description.getBaseAttributes(); + if (baseAttributes != null) { + description.setFontType(baseAttributes.getFontType()); + description.setForegroundChecked(baseAttributes.getForegroundColor() != null); + description.setForegroundColor(baseAttributes.getForegroundColor()); + description.setBackgroundChecked(baseAttributes.getBackgroundColor() != null); + description.setBackgroundColor(baseAttributes.getBackgroundColor()); + description.setErrorStripeChecked(baseAttributes.getErrorStripeColor() != null); + description.setErrorStripeColor(baseAttributes.getErrorStripeColor()); + description.setEffectColor(baseAttributes.getEffectColor()); + description.setEffectType(baseAttributes.getEffectType()); + description.setEffectsColorChecked(baseAttributes.getEffectColor() != null); } else { - setInheritanceInfo(description); - int fontType = Font.PLAIN; - if (myCbBold.isSelected()) fontType |= Font.BOLD; - if (myCbItalic.isSelected()) fontType |= Font.ITALIC; - description.setFontType(fontType); - description.setForegroundChecked(myCbForeground.isSelected()); - description.setForegroundColor(myForegroundChooser.getSelectedColor()); - description.setBackgroundChecked(myCbBackground.isSelected()); - description.setBackgroundColor(myBackgroundChooser.getSelectedColor()); - description.setErrorStripeChecked(myCbErrorStripe.isSelected()); - description.setErrorStripeColor(myErrorStripeColorChooser.getSelectedColor()); - description.setEffectsColorChecked(myCbEffects.isSelected()); - description.setEffectColor(myEffectsColorChooser.getSelectedColor()); - - if (myEffectsCombo.isEnabled()) { - String effectType = (String)myEffectsCombo.getModel().getSelectedItem(); - description.setEffectType(myEffectsMap.get(effectType)); - } + description.setInherited(false); } - description.apply(scheme); + reset(description); } + else { + setInheritanceInfo(description); + int fontType = Font.PLAIN; + if (myCbBold.isSelected()) fontType |= Font.BOLD; + if (myCbItalic.isSelected()) fontType |= Font.ITALIC; + description.setFontType(fontType); + description.setForegroundChecked(myCbForeground.isSelected()); + description.setForegroundColor(myForegroundChooser.getSelectedColor()); + description.setBackgroundChecked(myCbBackground.isSelected()); + description.setBackgroundColor(myBackgroundChooser.getSelectedColor()); + description.setErrorStripeChecked(myCbErrorStripe.isSelected()); + description.setErrorStripeColor(myErrorStripeColorChooser.getSelectedColor()); + description.setEffectsColorChecked(myCbEffects.isSelected()); + description.setEffectColor(myEffectsColorChooser.getSelectedColor()); + + if (myEffectsCombo.isEnabled()) { + String effectType = (String)myEffectsCombo.getModel().getSelectedItem(); + description.setEffectType(myEffectsMap.get(effectType)); + } + } + description.apply(scheme); + } + + @Override + public void addListener(@NotNull Listener listener) { + myDispatcher.addListener(listener); } private static class EffectsComboModel extends CollectionComboBoxModel { 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 dc61fe3cb31b..1ac7b419e059 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 @@ -18,12 +18,14 @@ package com.intellij.application.options.colors; import com.intellij.ide.DataManager; import com.intellij.ide.util.PropertiesComponent; +import com.intellij.openapi.editor.colors.EditorColorsScheme; import com.intellij.openapi.options.SearchableConfigurable; import com.intellij.openapi.options.ex.Settings; import com.intellij.openapi.util.ActionCallback; import com.intellij.ui.ScrollPaneFactory; import com.intellij.util.EventDispatcher; import com.intellij.util.ui.JBUI; +import org.jetbrains.annotations.NotNull; import javax.swing.*; import javax.swing.event.HyperlinkEvent; @@ -33,6 +35,7 @@ import javax.swing.text.BadLocationException; import javax.swing.text.Element; import java.awt.*; import java.awt.event.ActionEvent; +import java.util.EventListener; import java.util.HashSet; import java.util.Set; @@ -40,7 +43,7 @@ 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; + private final ColorDescriptionPanel myOptionsPanel; private final ColorAndFontOptions myOptions; private final SchemesPanel mySchemesProvider; @@ -53,21 +56,28 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel { public OptionsPanelImpl(ColorAndFontOptions options, SchemesPanel schemesProvider, String categoryName) { + this(options, schemesProvider, categoryName, new ColorAndFontDescriptionPanel()); + } + + public OptionsPanelImpl(ColorAndFontOptions options, + SchemesPanel schemesProvider, + String categoryName, + ColorDescriptionPanel optionsPanel) { super(new BorderLayout()); myOptions = options; mySchemesProvider = schemesProvider; myCategoryName = categoryName; myProperties = PropertiesComponent.getInstance(); - myOptionsPanel = new ColorAndFontDescriptionPanel() { + myOptionsPanel = optionsPanel; + myOptionsPanel.addListener(new ColorDescriptionPanel.Listener() { @Override - protected void onSettingsChanged(ActionEvent e) { - super.onSettingsChanged(e); + public void onSettingsChanged(ActionEvent e) { myDispatcher.getMulticaster().settingsChanged(); } @Override - protected void onHyperLinkClicked(HyperlinkEvent e) { + public void onHyperLinkClicked(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { Settings settings = Settings.KEY.getData(DataManager.getInstance().getDataContext(OptionsPanelImpl.this)); String attrName = e.getDescription(); @@ -87,7 +97,7 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel { } } } - }; + }); myOptionsTree = new ColorOptionsTree(myCategoryName); @@ -101,7 +111,7 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel { JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myOptionsTree); add(scrollPane, BorderLayout.CENTER); - add(myOptionsPanel, BorderLayout.EAST); + add(myOptionsPanel.getPanel(), BorderLayout.EAST); } @@ -181,4 +191,23 @@ public class OptionsPanelImpl extends JPanel implements OptionsPanel { } return result; } + + public interface ColorDescriptionPanel { + @NotNull + JComponent getPanel(); + + void resetDefault(); + + void reset(@NotNull ColorAndFontDescription description); + + void apply(@NotNull ColorAndFontDescription descriptor, EditorColorsScheme scheme); + + void addListener(@NotNull Listener listener); + + interface Listener extends EventListener { + void onSettingsChanged(ActionEvent e); + + void onHyperLinkClicked(HyperlinkEvent e); + } + } }