Creating EditorColorsScheme

Hei,

I'm trying to copy Default colors scheme and change color for the Java's keywords.

I was using clone() which would do proper copying but it has the same problem as creating EditorColorsSchemeImpl - changes to "My New Default Scheme 2" gets propagated to "Default" theme. So, I change keyword color to yellow in my scheme, and I get it yellow it both. I tried even cloning TextAttributes but it didn't help. How can I just create a copy and _detach_ it from "Default" completely?

    public void initComponent() {         EditorColorsScheme defaultScheme = getColorsScheme("Default");         if (defaultScheme != null) {             EditorColorsScheme newScheme = new EditorColorsSchemeImpl(defaultScheme, DefaultColorSchemesManager.getInstance());             newScheme.setName("My New Default Scheme 2");             ColorSettingsPage javaColorPage = getColorPage("Java");             if (javaColorPage != null) {                 for (AttributesDescriptor colorKey : javaColorPage.getAttributeDescriptors()) {                     if (colorKey.getDisplayName().equals("Keyword")) {                         TextAttributes attributes = newScheme.getAttributes(colorKey.getKey()).clone();                         attributes.setForegroundColor(Color.yellow);                         newScheme.setAttributes(colorKey.getKey(), attributes);                     }                 }             }             EditorColorsManager.getInstance().addColorsScheme(newScheme);         }     }     @Nullable     private ColorSettingsPage getColorPage(String name) {         for (ColorSettingsPage settingsPage : Extensions.getExtensions(ColorSettingsPage.EP_NAME)) {             if (settingsPage.getDisplayName().equals("Java"))                 return settingsPage;         }         return null;     }     @Nullable     private EditorColorsScheme getColorsScheme(@NotNull String name) {         EditorColorsManager colorsManager = EditorColorsManager.getInstance();         EditorColorsScheme[] schemes = colorsManager.getAllSchemes();         for (EditorColorsScheme scheme : schemes) {             if (scheme.getName().equals(name))                 return scheme;         }         return null;     }

4 comments
Comment actions Permalink

Hi Sergiy,

You can check how new scheme is created and registered when you press 'Save As' button at the 'IDE Settings | Editor | Colors & Fonts' - com.intellij.application.options.colors.ColorAndFontOptions.saveSchemeAs()

Regards, Denis

0
Comment actions Permalink

Thanks Denis. Yes I checked it. However I didn't want to copy code from there. Public API in my opinion should do that and if it doesn't do that then there is a bug and here is why.

I can clone color scheme. There is a public function for that. However when I change attribute in the new scheme, it also gets changed in the scheme which I cloned. It's kinda weird, because cloning typically means creating another copy but let it be cloning which does it only partially. May be those 2 instances share the same TextAttributes, so I create new one and add it only to my cloned color scheme - it still gets changed in the original one. Is there a reason for doing it like that?

0
Comment actions Permalink

Fair enough. Will check the processing there and update current thread with the results.

Denis

0
Comment actions Permalink

I found where the problem is.

public TextAttributes clone() {     TextAttributes cloned = new TextAttributes();     cloned.myAttrs = myAttrs;     return cloned; }



So, when I clone TextAttribute I use the same AttributesFlyweight for both schemes. Not very nice, but I don't have to copy 100 lines. Btw, I think Color Scheme option page can be smaller, because it can just use clone() when "Save As" is used.
0

Please sign in to leave a comment.