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:
<decorator
id="myDecorator"
label="My sample project decorator"
state="true"
class= "com.dolejsky.MyDecorator"
objectClass="org.eclipse.core.resources.IProject"
adaptable="true">
<description>
Some description of this decorator
</description>
</decorator>
</extension>
Next step is to create class that does the actual decorating (com.dolejsky.MyDecorator in our example):
public static MyDecorator getInstance() { //check that this decorator is enabled //we are disabled public Image decorateImage(Image image, Object element) { public String decorateText(String text, Object element) {
public static final String ID = "com.dolejsky.MyDecorator";
IDecoratorManager decoratorManager =
Activator.getDefault().getWorkbench().getDecoratorManager();
if (decoratorManager.getEnabled(ID)) {
return (MyDecorator ) decoratorManager.getLabelDecorator(ID);
}
return null;
}
//we could decorate image here...
return null;
}
//we decorate name of the project here...
return "decoration "+text;
}
}
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).