HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Retrieving font preference setting in Eclipse JDT plugin

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
eclipsesettingjdtpluginfontretrievingpreference

Problem

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?

Solution

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

-
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.

-
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

Code Snippets

// 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);

Context

StackExchange Code Review Q#29554, answer score: 2

Revisions (0)

No revisions yet.