Tips on developing Eclipse plugins – III. – Project Decorators

Today I would like to say few words about Project decorators which are useful for altering the text and/or icon of resources in Package Explorer and Navigator. So don’t hesitate and read the rest of the article :)

Project decorators

– also see Tutorial

To create sample decorator decorating displayed name of all IProjects in workspace put the following fragment to your plugin.xml first:

[xml]


Some description of this decorator



[/xml]

Next step is to create class that does the actual decorating (com.dolejsky.MyDecorator in our example):

[java]
public class MyDecorator extends LabelProvider implements ILabelDecorator {
public static final String ID = “com.dolejsky.MyDecorator”;

public static MyDecorator getInstance() {
IDecoratorManager decoratorManager =
Activator.getDefault().getWorkbench().getDecoratorManager();

//check that this decorator is enabled
if (decoratorManager.getEnabled(ID)) {
return (MyDecorator ) decoratorManager.getLabelDecorator(ID);
}

//we are disabled
return null;
}

public Image decorateImage(Image image, Object element) {
//we could decorate image here…
return null;
}

public String decorateText(String text, Object element) {
//we decorate name of the project here…
return “decoration “+text;
}
}
[/java]

Sometimes it is needed to force the refresh of the decorations (e.g. when they depend on some internal state which has changed). It can be achieved by firing fireLabelProviderChanged() event of the decorator (if it is enabled). This has to be done in UI thread (as described here).

Tags: , , , ,

Comments are closed.