Adding Color and Font preferences
Join the DZone community and get the full member experience.Join For Free
You had that nice looking editors and views. You loved the button color and the geeky font. But your boss didn't. Solution? Put a preference so that the user can choose which ever he likes ;-)
Ok. That may not be the real rationale, but for a valid reason, you wanted to have Color and Font preferences in the PreferencePage. The obvious answer for us (the plugin develoepers) would be to add ColorFieldEditor & FontFieldEditor in our existing PreferencePage. But its not obvious for the user to look into your plugins Preference Page for changing them. Eclipse already provides a PreferencePage for these customizations: Colors and Fonts page ( General->Appearance->Colors and Fonts ) Users might be looking into this place to change the colors & fonts.
As you can see in the above image it categorizes the available preferences; gives a filter to search; provides space for description and preview; and a tool tip for the current value of the preference. This is much more than the *FieldEditor right? In this tip let us see how to add our preferences into that and access them.
You need to extend the org.eclipse.ui.themes extension point for adding your content into this page. I would like to provide one category with one color preference and one font preference. The extension is:
<themeElementCategory id="com.eclipse-tips.blog.preferences.themeElementCategory" label="Eclipse Tips">
An example theme category
<colorDefinition categoryId="com.eclipse-tips.blog.preferences.themeElementCategory" id="com.eclipse-tips.blog.preferences.colorDefinition" label="Some Color" value="COLOR_DARK_BLUE">
Your description goes here
<fontDefinition categoryId="com.eclipse-tips.blog.preferences.themeElementCategory" id="com.eclipse-tips.blog.preferences.fontDefinition" label="Some Font" value="Lucida Sans-italic-18">
Description for this font
Guess all the elements are self explanatory - except for the value element. For colorDefinition the value element will be the COLOR_* constants defined in the SWT class. You can specify your custom colors thru comma separated RGB values like 125,245,96. For fontDefinition it would be fontname-style-height. Now our UI will look like:
To get the values stored in the preferences, you need to use the IThemeManager:
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();Once you get the current theme, you can get the preferences stored by giving the appropriate ids defined in the plugin.xml
ITheme currentTheme = themeManager.getCurrentTheme();
ColorRegistry colorRegistry = currentTheme.getColorRegistry();
Color color = colorRegistry.get("com.eclipse-tips.blog.preferences.colorDefinition");
FontRegistry fontRegistry = currentTheme.getFontRegistry();
Font font = fontRegistry.get("com.eclipse-tips.blog.preferences.fontDefinition");
What you have seen is just the tip of the iceberg. The themes extension point has much more functionalities (The Preview is one such example). I'll explain them later, but you can see the Extension Point description here.
This is an extract from http://blog.eclipse-tips.com/2008/08/adding-color-and-font-preferences.html
Opinions expressed by DZone contributors are their own.