Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adding Color and Font preferences

DZone's Guide to

Adding Color and Font preferences

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's 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:

<extension point="org.eclipse.ui.themes">
<themeElementCategory id="com.eclipse-tips.blog.preferences.themeElementCategory" label="Eclipse Tips">
<description>
An example theme category
</description>
</themeElementCategory>
<colorDefinition categoryId="com.eclipse-tips.blog.preferences.themeElementCategory" id="com.eclipse-tips.blog.preferences.colorDefinition" label="Some Color" value="COLOR_DARK_BLUE">
<description>
Your description goes here
</description>
</colorDefinition>
<fontDefinition categoryId="com.eclipse-tips.blog.preferences.themeElementCategory" id="com.eclipse-tips.blog.preferences.fontDefinition" label="Some Font" value="Lucida Sans-italic-18">
<description>
Description for this font
</description>
</fontDefinition>
</extension>

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();
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");
Once you get the current theme, you can get the preferences stored by giving the appropriate ids defined in the plugin.xml

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

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}