update font in custom panel when changed in colors and fonts..

Answered

So here's the deal: I have this custom JPanel that I added in a ToolWindow -- all is well there. The panel holds a JTree component that I populate with some nodes. However, I'd like the font for the text on those nodes to reflect the current "Primary font", as specified in the 'Editor > Colors&Fonts > Font' page. So if it's set to Menlo, then I want the font in my JTree to be Menlo. And if someone comes along and changes it to Monaco, then I want to update the font for the JTree to be Monaco.

In perusing the OpenAPI I stumbled across this (promising) listener: 

public interface ColorAndFontSettingsListener extends EventListener {
void selectedOptionChanged(final Object selected);
void schemeChanged(final Object source);
void settingsChanged();
void selectionInPreviewChanged(final String typeToSelect);

void fontChanged();
}

though I'm unsure how to go about using it for my situation.. Any suggestions on how the above listener can be utilized? Or is what I'm trying to do simply not possible?

 

 

 

0
6 comments
Avatar
Permanently deleted user

Don't use ColorAndFontSettingsListener is not a part of OpenAPI.

This code can help you:

    ApplicationManager.getApplication().getMessageBus().connect().subscribe(EditorColorsManager.TOPIC, new EditorColorsListener() {
      @Override
      public void globalSchemeChange(EditorColorsScheme scheme) {
        System.out.println(EditorColorsManager.getInstance().getGlobalScheme().getEditorFontName());
      }
    });

1

Hm, it appears globalSchemeChange only fires when the global scheme changes (duh) but not for normal font changes... 

So perhaps there really isn't a good way to adjust font on custom panels whenever the primary font is changed for the current scheme.. ?

 

 

0
Avatar
Permanently deleted user

globalSchemeChange() is being triggered when one changes font settings in current scheme.

0

Hmm,

Just to clarify, here's the code that I have in placeright now:

EditorColorsManager.getInstance().addEditorColorsListener(new EditorColorsListener() {
@Override
public void globalSchemeChange(EditorColorsScheme scheme) {
System.out.println(EditorColorsManager.getInstance().getGlobalScheme().getEditorFontName());
}
});

if I put a breakpoint on the system.out, it only gets triggered when one selects a different scheme from the "Scheme" dropbox in the 'Editor > Colors & Fonts > Font' menu.

I haven't observed it firing when I just change the primary font. 

But going back to the code you gave me in a previous post, I'm not sure where to put that or what a TOPIC is. I found the class Topic (and its #create(..) methods) but didn't have much luck getting that working.

So you're saying if I get the code that you gave me working, then globalSchemeChange() will fire when one changes the font? Also: Why is the method called globalSchemeChange in that case..? Seems like what you're saying should happen would be for a method called globalFontChange()..

0
Avatar
Permanently deleted user

com.intellij.openapi.editor.colors.EditorColorsManager.TOPIC is public constant

As for naming... well, font is just one piece of scheme as well as other ones (see any of 'Editor > Colors&Fonts > XXX' related to some certain scheme) and single 'common' listener is enough to react on changes.

1

Ah! I see. I was running an older version of the IDE and EditorColorsManager#TOPIC is fairly new addition! I've updated and it is indeed working as you said it would--with the code you suggested.

Thanks for the help and thanks for being patient!

0

Please sign in to leave a comment.