patternjavaMinor
Retrieving font preference setting in Eclipse JDT plugin
Viewed 0 times
eclipsesettingjdtpluginfontretrievingpreference
Problem
I have an Eclipse plugin which creates a
I would like to configure the font of the viewer to match the settings of the Java Editor Text Font preference:
My question is especially about the code block which retrieves the font from the theme manager:
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:
-
-
The comment could be a great method name here (
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
-
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.