From a21d6784f7030bbcc9110f4f162bf1d8c75e4beb Mon Sep 17 00:00:00 2001 From: Roman Chernyatchik Date: Tue, 11 Nov 2008 01:26:04 +0300 Subject: [PATCH] API for custom color schemes was extracted Two ruby dark schemes schemes were bundled --- .../options/colors/ColorAndFontOptions.java | 17 +++-- .../application/options/colors/FontOptions.java | 22 ++---- .../application/options/colors/SchemesPanel.java | 18 +---- .../diff/impl/settings/DiffOptionsPanel.java | 8 ++- .../editor/colors/impl/DefaultColorsScheme.java | 2 +- .../colors/impl/EditorColorsManagerImpl.java | 84 ++++++++++++++++++---- .../src/META-INF/PlatformExtensionPoints.xml | 2 + .../src/messages/ApplicationBundle.properties | 4 +- .../src/messages/OptionsBundle.properties | 2 + 9 files changed, 102 insertions(+), 57 deletions(-) diff --git a/lang-impl/src/com/intellij/application/options/colors/ColorAndFontOptions.java b/lang-impl/src/com/intellij/application/options/colors/ColorAndFontOptions.java index 5561943b78..0b438f9d64 100644 --- a/lang-impl/src/com/intellij/application/options/colors/ColorAndFontOptions.java +++ b/lang-impl/src/com/intellij/application/options/colors/ColorAndFontOptions.java @@ -18,6 +18,7 @@ import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.editor.colors.ex.DefaultColorSchemesManager; import com.intellij.openapi.editor.colors.impl.DefaultColorsScheme; import com.intellij.openapi.editor.colors.impl.EditorColorsSchemeImpl; +import com.intellij.openapi.editor.colors.impl.ReadOnlyColorsScheme; import com.intellij.openapi.editor.markup.EffectType; import com.intellij.openapi.editor.markup.TextAttributes; import com.intellij.openapi.options.Configurable; @@ -125,16 +126,16 @@ public class ColorAndFontOptions extends SearchableConfigurable.Parent.Abstract return mySelectedScheme.getDescriptors(); } - public static boolean isDefault(EditorColorsScheme scheme) { - return ((MyColorScheme)scheme).isDefault(); + public static boolean isReadOnly(final EditorColorsScheme scheme) { + return ((MyColorScheme)scheme).isReadOnly(); } public String[] getSchemeNames() { ArrayList schemes = new ArrayList(mySchemes.values()); Collections.sort(schemes, new Comparator() { public int compare(MyColorScheme o1, MyColorScheme o2) { - if (isDefault(o1) && !isDefault(o2)) return -1; - if (!isDefault(o1) && isDefault(o2)) return 1; + if (isReadOnly(o1) && !isReadOnly(o2)) return -1; + if (!isReadOnly(o1) && isReadOnly(o2)) return 1; return o1.getName().compareToIgnoreCase(o2.getName()); } @@ -684,8 +685,8 @@ public class ColorAndFontOptions extends SearchableConfigurable.Parent.Abstract myRootSchemesPanel = null; } - public boolean currentSchemeIsDefault() { - return mySelectedScheme.isDefault(); + public boolean currentSchemeIsReadOnly() { + return isReadOnly(mySelectedScheme); } public boolean currentSchemeIsShared() { @@ -909,6 +910,10 @@ public class ColorAndFontOptions extends SearchableConfigurable.Parent.Abstract return myParentScheme instanceof DefaultColorsScheme; } + public boolean isReadOnly() { + return myParentScheme instanceof ReadOnlyColorsScheme; + } + public boolean isModified() { if (isFontModified()) return true; diff --git a/lang-impl/src/com/intellij/application/options/colors/FontOptions.java b/lang-impl/src/com/intellij/application/options/colors/FontOptions.java index 500675b460..176d9bcb07 100644 --- a/lang-impl/src/com/intellij/application/options/colors/FontOptions.java +++ b/lang-impl/src/com/intellij/application/options/colors/FontOptions.java @@ -7,7 +7,6 @@ import com.intellij.openapi.editor.ex.EditorSettingsExternalizable; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.ui.FixedSizeButton; -import com.intellij.openapi.ui.Messages; import com.intellij.ui.DocumentAdapter; import com.intellij.ui.IdeBorderFactory; import com.intellij.util.EventDispatcher; @@ -55,7 +54,7 @@ public class FontOptions extends JPanel implements OptionsPanel{ myEditorFontSizeField.setText(Integer.toString(getCurrentScheme().getEditorFontSize())); myFontNameField.setText(getCurrentScheme().getEditorFontName()); - if (ColorAndFontOptions.isDefault(myOptions.getSelectedScheme())) { + if (ColorAndFontOptions.isReadOnly(myOptions.getSelectedScheme())) { myLineSpacingField.setEnabled(false); myEditorFontSizeField.setEditable(false); myFontNameField.setEnabled(false); @@ -136,8 +135,8 @@ public class FontOptions extends JPanel implements OptionsPanel{ myFontNameButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { EditorColorsScheme current = getCurrentScheme(); - if (ColorAndFontOptions.isDefault(current) || ColorSettingsUtil.isSharedScheme(current)) { - showReadOnlyMessage(FontOptions.this, ColorSettingsUtil.isSharedScheme(current)); + if (ColorAndFontOptions.isReadOnly(current) || ColorSettingsUtil.isSharedScheme(current)) { + ColorAndFontPanel.showReadOnlyMessage(FontOptions.this, ColorSettingsUtil.isSharedScheme(current)); return; } @@ -189,17 +188,6 @@ public class FontOptions extends JPanel implements OptionsPanel{ return editorFontPanel; } - public static void showReadOnlyMessage(JComponent parent, final boolean sharedScheme) { - if (!sharedScheme) { - Messages.showMessageDialog(parent, ApplicationBundle.message("error.default.scheme.cannot.be.modified"), - ApplicationBundle.message("title.cannot.modify.default.scheme"), Messages.getInformationIcon()); - } - else { - Messages.showMessageDialog(parent, ApplicationBundle.message("error.shared.scheme.cannot.be.modified"), - ApplicationBundle.message("title.cannot.modify.default.scheme"), Messages.getInformationIcon()); - } - } - private void selectFont() { initFontTables(); @@ -239,8 +227,8 @@ public class FontOptions extends JPanel implements OptionsPanel{ public boolean updateDescription(boolean modified) { EditorColorsScheme scheme = myOptions.getSelectedScheme(); - if (modified && (ColorAndFontOptions.isDefault(scheme) || ColorSettingsUtil.isSharedScheme(scheme))) { - showReadOnlyMessage(this, ColorSettingsUtil.isSharedScheme(scheme)); + if (modified && (ColorAndFontOptions.isReadOnly(scheme) || ColorSettingsUtil.isSharedScheme(scheme))) { + ColorAndFontPanel.showReadOnlyMessage(this, ColorSettingsUtil.isSharedScheme(scheme)); return false; } diff --git a/lang-impl/src/com/intellij/application/options/colors/SchemesPanel.java b/lang-impl/src/com/intellij/application/options/colors/SchemesPanel.java index 810abb03fc..4cdf1d2d6c 100644 --- a/lang-impl/src/com/intellij/application/options/colors/SchemesPanel.java +++ b/lang-impl/src/com/intellij/application/options/colors/SchemesPanel.java @@ -9,7 +9,6 @@ import com.intellij.openapi.editor.colors.EditorColorsScheme; import com.intellij.openapi.editor.colors.impl.EditorColorsManagerImpl; import com.intellij.openapi.editor.colors.impl.EditorColorsSchemeImpl; import com.intellij.openapi.options.SchemesManager; -import com.intellij.openapi.ui.Messages; import com.intellij.util.EventDispatcher; import org.jetbrains.annotations.Nullable; @@ -45,7 +44,7 @@ public class SchemesPanel extends JPanel { public void actionPerformed(ActionEvent e) { if (mySchemeComboBox.getSelectedIndex() != -1) { EditorColorsScheme selected = myOptions.selectScheme((String)mySchemeComboBox.getSelectedItem()); - if (ColorAndFontOptions.isDefault(selected)) { + if (ColorAndFontOptions.isReadOnly(selected)) { myDeleteButton.setEnabled(false); if (myExportButton != null) { myExportButton.setEnabled(false); @@ -88,17 +87,6 @@ public class SchemesPanel extends JPanel { return null; } - public static void showReadOnlyMessage(JComponent parent, final boolean sharedScheme) { - if (!sharedScheme) { - Messages.showMessageDialog(parent, ApplicationBundle.message("error.default.scheme.cannot.be.modified"), - ApplicationBundle.message("title.cannot.modify.default.scheme"), Messages.getInformationIcon()); - } - else { - Messages.showMessageDialog(parent, ApplicationBundle.message("error.shared.scheme.cannot.be.modified"), - ApplicationBundle.message("title.cannot.modify.default.scheme"), Messages.getInformationIcon()); - } - } - private JPanel createSchemePanel() { JPanel panel = new JPanel(new GridBagLayout()); @@ -200,8 +188,8 @@ public class SchemesPanel extends JPanel { public boolean updateDescription(boolean modified) { EditorColorsScheme scheme = myOptions.getSelectedScheme(); - if (modified && (ColorAndFontOptions.isDefault(scheme) || ColorSettingsUtil.isSharedScheme(scheme))) { - showReadOnlyMessage(this, ColorSettingsUtil.isSharedScheme(scheme)); + if (modified && (ColorAndFontOptions.isReadOnly(scheme) || ColorSettingsUtil.isSharedScheme(scheme))) { + ColorAndFontPanel.showReadOnlyMessage(this, ColorSettingsUtil.isSharedScheme(scheme)); return false; } diff --git a/lang-impl/src/com/intellij/openapi/diff/impl/settings/DiffOptionsPanel.java b/lang-impl/src/com/intellij/openapi/diff/impl/settings/DiffOptionsPanel.java index 257f717734..aa6315a0c9 100644 --- a/lang-impl/src/com/intellij/openapi/diff/impl/settings/DiffOptionsPanel.java +++ b/lang-impl/src/com/intellij/openapi/diff/impl/settings/DiffOptionsPanel.java @@ -197,9 +197,11 @@ public class DiffOptionsPanel implements OptionsPanel { } private boolean checkModifiableScheme() { - boolean isDefault = myOptions.currentSchemeIsDefault(); - if (isDefault) ColorAndFontPanel.showReadOnlyMessage(myWholePanel, myOptions.currentSchemeIsShared()); - return !isDefault; + boolean isReadOnly = myOptions.currentSchemeIsReadOnly(); + if (isReadOnly) { + ColorAndFontPanel.showReadOnlyMessage(myWholePanel, myOptions.currentSchemeIsShared()); + } + return !isReadOnly; } private MyColorAndFontDescription getSelectedDescription() { diff --git a/platform-impl/src/com/intellij/openapi/editor/colors/impl/DefaultColorsScheme.java b/platform-impl/src/com/intellij/openapi/editor/colors/impl/DefaultColorsScheme.java index b181ef932f..cc76eeeb64 100644 --- a/platform-impl/src/com/intellij/openapi/editor/colors/impl/DefaultColorsScheme.java +++ b/platform-impl/src/com/intellij/openapi/editor/colors/impl/DefaultColorsScheme.java @@ -13,7 +13,7 @@ import org.jdom.Element; import java.awt.*; -public class DefaultColorsScheme extends AbstractColorsScheme { +public class DefaultColorsScheme extends AbstractColorsScheme implements ReadOnlyColorsScheme { private String myName; public DefaultColorsScheme(DefaultColorSchemesManager defaultColorSchemesManager) { diff --git a/platform-impl/src/com/intellij/openapi/editor/colors/impl/EditorColorsManagerImpl.java b/platform-impl/src/com/intellij/openapi/editor/colors/impl/EditorColorsManagerImpl.java index 088e429fb8..15aa6cb424 100644 --- a/platform-impl/src/com/intellij/openapi/editor/colors/impl/EditorColorsManagerImpl.java +++ b/platform-impl/src/com/intellij/openapi/editor/colors/impl/EditorColorsManagerImpl.java @@ -3,7 +3,9 @@ */ package com.intellij.openapi.editor.colors.impl; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.PathManager; +import com.intellij.openapi.application.ex.DecodeDefaultsUtil; import com.intellij.openapi.components.ExportableApplicationComponent; import com.intellij.openapi.components.RoamingType; import com.intellij.openapi.diagnostic.Logger; @@ -12,10 +14,8 @@ import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.colors.EditorColorsScheme; import com.intellij.openapi.editor.colors.ex.DefaultColorSchemesManager; import com.intellij.openapi.options.*; -import com.intellij.openapi.util.DefaultJDOMExternalizer; -import com.intellij.openapi.util.InvalidDataException; -import com.intellij.openapi.util.NamedJDOMExternalizable; -import com.intellij.openapi.util.WriteExternalException; +import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.util.*; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; @@ -24,6 +24,7 @@ import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -54,15 +55,8 @@ public class EditorColorsManagerImpl extends EditorColorsManager new SchemeProcessor() { public EditorColorsSchemeImpl readScheme(final Document document) throws InvalidDataException, IOException, JDOMException { - Element root = document.getRootElement(); - if (root == null || !SCHEME_NODE_NAME.equals(root.getName())) { - throw new InvalidDataException(); - } - - EditorColorsSchemeImpl scheme = new EditorColorsSchemeImpl(null, DefaultColorSchemesManager.getInstance()); - scheme.readExternal(root); - return scheme; + return loadSchemeFromDocument(document, true); } public Document writeScheme(final EditorColorsSchemeImpl scheme) throws WriteExternalException { @@ -83,7 +77,7 @@ public class EditorColorsManagerImpl extends EditorColorsManager } public boolean shouldBeSaved(final EditorColorsSchemeImpl scheme) { - return true; + return !(scheme instanceof ReadOnlyColorsScheme); } public void initScheme(final EditorColorsSchemeImpl scheme) { @@ -104,10 +98,74 @@ public class EditorColorsManagerImpl extends EditorColorsManager addDefaultSchemes(); + // Load default schemes from providers + loadAdditionalDefaultSchemes(); loadAllSchemes(); + setGlobalScheme(myDefaultColorSchemesManager.getAllSchemes()[0]); } + private void loadAdditionalDefaultSchemes() { + //Get color schemes from EPs + for (BundledColorSchemesProvider provider : BundledColorSchemesProvider.EP_NAME.getExtensions()) { + final String[] schemesPaths = provider.getBundledSchemesRelativePaths(); + + for (final String schemePath : schemesPaths) { + try { + final InputStream inputStream = DecodeDefaultsUtil.getDefaultsInputStream(provider, schemePath); + if (inputStream == null) { + final String msg = OptionsBundle.message("options.color.schemes.load.read.error", schemePath); + LOG.info(msg); + Messages.showErrorDialog(msg, OptionsBundle.message("options.color.schemes.load.settings.title")); + continue; + } + + final Document document; + try { + document = JDOMUtil.loadDocument(inputStream); + } + catch (JDOMException e) { + LOG.info("Error reading scheme from " + schemePath + ": " + e.getLocalizedMessage()); + throw e; + } + final EditorColorsSchemeImpl scheme = loadSchemeFromDocument(document, false); + mySchemesManager.addNewScheme(scheme, false); + } + catch (final Exception e) { + ApplicationManager.getApplication().invokeLater( + new Runnable(){ + public void run() { + final String msg = OptionsBundle.message("options.color.schemes.load.read.error", schemePath + ": " + e.getLocalizedMessage()); + LOG.info(msg, e); + Messages.showErrorDialog(msg, OptionsBundle.message("options.color.schemes.load.settings.title")); + } + } + ); + + } + } + } + } + + private EditorColorsSchemeImpl loadSchemeFromDocument(final Document document, + final boolean isEditable) + throws InvalidDataException { + + final Element root = document.getRootElement(); + + if (root == null || !SCHEME_NODE_NAME.equals(root.getName())) { + throw new InvalidDataException(); + } + + final EditorColorsSchemeImpl scheme = isEditable + // editable scheme + ? new EditorColorsSchemeImpl(null, DefaultColorSchemesManager.getInstance()) + //not editable scheme + : new ReadOnlyColorsSchemeImpl(null, DefaultColorSchemesManager.getInstance()); + scheme.readExternal(root); + return scheme; + } + // ------------------------------------------------------------------------- // ApplicationComponent interface implementation // ------------------------------------------------------------------------- diff --git a/platform-resources/src/META-INF/PlatformExtensionPoints.xml b/platform-resources/src/META-INF/PlatformExtensionPoints.xml index 4fea349f29..5e6adc980e 100644 --- a/platform-resources/src/META-INF/PlatformExtensionPoints.xml +++ b/platform-resources/src/META-INF/PlatformExtensionPoints.xml @@ -77,4 +77,6 @@ + + diff --git a/platform-resources_eng/src/messages/ApplicationBundle.properties b/platform-resources_eng/src/messages/ApplicationBundle.properties index c05ae20601..94c1d5983f 100644 --- a/platform-resources_eng/src/messages/ApplicationBundle.properties +++ b/platform-resources_eng/src/messages/ApplicationBundle.properties @@ -437,9 +437,9 @@ editbox.font.size=Size: editbox.line.spacing=Line spacing: combobox.scheme.name=Scheme name: progress.analyzing.fonts=Analyzing Fonts -error.default.scheme.cannot.be.modified=Default scheme cannot be modified. Please do "Save As..." first. +error.readonly.scheme.cannot.be.modified=Read-only scheme cannot be modified. Please do "Save As..." first. error.shared.scheme.cannot.be.modified=Shared scheme cannot be modified. Please do "Save As..." first. -title.cannot.modify.default.scheme=Cannot Modify Default Scheme +title.cannot.modify.readonly.scheme=Cannot Modify read-only Scheme title.save.color.scheme.as=Save Color Scheme As title.path.variables=Path Variables editbox.path.macro.value=Value: diff --git a/platform-resources_eng/src/messages/OptionsBundle.properties b/platform-resources_eng/src/messages/OptionsBundle.properties index 40669106eb..542f9f1f3d 100644 --- a/platform-resources_eng/src/messages/OptionsBundle.properties +++ b/platform-resources_eng/src/messages/OptionsBundle.properties @@ -147,6 +147,8 @@ project.settings.display.name=Project Settings [{0}] template.project.settings.short.name=Template Project project.settings.short.name=Project options.color.schemes.presentable.name=Color schemes +options.color.schemes.load.settings.title=Load Settings +options.color.schemes.load.read.error=Cannot read scheme from {0} options.editor.settings.presentable.name=Editor settings options.java.attribute.descriptor.info=Info options.java.attribute.descriptor.server.problems=Problem from server -- 2.11.4.GIT