Eclipse preferences customization

romain.bioteau's picture
romain.bioteau
Blog Categories: 

This post will show how to customize Eclipse RCP preferences. This came from the need to have a user friendly interface in our product instead of the rough default tree view of Eclipse preferences.

Default Eclipse preferences TreeView

Default Eclipse preferences TreeView

If you are not familiar with Eclipse preferences, you can have an in-depth look of how preferences works in this article by Lars Vogel.

First remove all the preferences you don't need in your RCP. Some of these were brought in by 3rd party plug-ins such as JDT.

You'll have to override the postWindowOpen() method of the WorkbenchWindowAdvisor application:

[cc lang="java"]

//Clean the preferences

PreferenceManager preferenceManager = PlatformUI.getWorkbench().getPreferenceManager();

for(Object elem : preferenceManager.getElements(PreferenceManager.POST_ORDER)){

if(elem instanceof IPreferenceNode){

//FILTER REGARDING THE NODE ID

if(((IPreferenceNode)elem).getId().equals("....")) {

preferenceManager.remove((IPreferenceNode) elem) ;

}

....

[/cc]

This gives you better visibility of your RCP preferences.

Next the idea is to build a new UI for this dialog, keeping the Eclipse PreferencePage mechanism. To show a quick preview of the result I made a mockup of this dialog using the WindowBuilder tool. With this tool, you can draw your UI very easily, though a background knowledge of SWT helps a lot.

WindowBuilder overview

Using mockups is a good practice to test your user interface and collect your team's feedback. Here we clearly want to have an "OS X style" for our preferences. Once the mockup shows the interface the way you want it, you can  go ahead with your implementation using the code generated by WindowBuilder. The generated code is pretty clean and easy to modify.

Eclipse preference API

Next the goal is to integrate the PreferencePages to the new Dialog. In Eclipse you can define a preference page using an extension point. Thus a PreferencePage is bound to an ID. To find them you can use the PreferenceManager like this :

[cc lang="java"]

/**

  • Find the IPreferenceNode from its id

**/

public static IPreferenceNode findNodeMatching(String nodeId) {

List nodes = PlatformUI.getWorkbench().getPreferenceManager().getElements(PreferenceManager.POST_ORDER);

for (Iterator i = nodes.iterator(); i.hasNext();) {

IPreferenceNode node = (IPreferenceNode) i.next();

if (node.getId().equals(nodeId)) {

return node;

}

}

return null;

}

[/cc]

Then, once you retrieve the IPreferenceNode, you can create the page in a Composite:

[cc lang="java"]

protected void openPreferencePage(String pageId,Composite parentComposite) {

IPreferenceNode node = PreferenceUtil.findNodeMatching(pageId) ;

node.createPage();

node.getPage().createControl(parentComposite) ;

}

[/cc]

In addition, I found it useful to reuse the Eclipse keyword contributions linked to those pages. To achieve this, I implemented my own listener inspired from the one in the default preference tree filter.

Here is a quick preview of the result:

I'll be sharing more cool Eclipse stuff soon!

Notifications