3
\$\begingroup\$

I have an Eclipse plugin which creates a JavaSourceViewer to visualize specific source code in a separate view. I would like to configure the font of the viewer to match the settings of the Java Editor Text Font preference:

JavaSourceViewer textViewer = new JavaSourceViewer(...);
...

// get the SWT component which displays the text
StyledText textControl = textViewer.getTextWidget();

// retrieve the font preference from the theme manager
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
ITheme currentTheme = themeManager.getCurrentTheme();
FontRegistry fontRegistry = currentTheme.getFontRegistry();
Font font = fontRegistry.get(PreferenceConstants.EDITOR_TEXT_FONT);

// set the font on the SWT component
textControl.setFont(font);

My question is especially about the code block which retrieves the font from the theme manager:

  • Is this the correct approach to get the font?
  • Is there anything else which needs to be considered?
\$\endgroup\$
2
\$\begingroup\$

I'm not familiar with Eclipse plugin development, so just two minor notes after going through javadoc of the relevant APIs:

  1. FontRegistry has an addListener method which you might want and/or need to listen to and change the font of the textControl when the user changes their font settings.

  2. The comment could be a great method name here (retrieveFontPreference, for example):

    // retrieve the font preference from the theme manager
    IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
    ITheme currentTheme = themeManager.getCurrentTheme();
    FontRegistry fontRegistry = currentTheme.getFontRegistry();
    Font font = fontRegistry.get(PreferenceConstants.EDITOR_TEXT_FONT);
    

    See also: Clean Code by Robert C. Martin, Bad Comments, p67: Don’t Use a Comment When You Can Use a Function or a Variable

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.