Part 3: Use decorators to identify marked IResources
Within Eclipse, decorators are used to add visual information to objects in the workbench. Usually, they display the object type and any key properties currently associated with that object. Figure 3 shows how decorators are displayed in the package explorer to the user. The decorators show which items are Java source files or packages, and shows marker icons for source and packages that contain warnings or errors. Here decorators are also used to add team details, such as whether the files are in or out of sync with the repository.
Figure 3. Packages and source decorated with symbolic icons
The first step to add our decorator is to extend the
org.eclipse.ui.decorators extension point.
It enables the definition of a new decorator, and selects what kind of
objects it will decorate.
The important fields here are:
- class, — which must be the fully qualified
name of a class that implements
ILightweightLabelDecorator(as lightweight is set to true). - enablement, — which contains the list of Eclipse objects to which the decorator applies.
Listing 10. Decorator definition from the plugin.xml
<extension point="org.eclipse.ui.decorators"> <decorator id="com.ibm.example.filedecorator" label="MyMarker Decorator" state="true" class= "com.ibm.example.mymarker.FileDecorator" adaptable="true" lightweight="true"> <enablement> <objectClass name="org.eclipse.core.resources.IResource"/> </enablement> </decorator> </extension> |
See Resources for more documentation on extension point from Eclipse.org.
Note: Lightweight vs. Non-Lightweight: According to the API, non-lightweight decorators may become deprecated in future versions of Eclipse.
We need to implement the class
FileDecorator to determine the behavior of
our decorator. This class has to implement
ILightweightLabelDecorator. It is good idea
to have it extend LabelProvider as this
will allow us to only override the method in which we are interested,
that is, decorate().
A basic implementation of decorate() is
shown in Listing 11.
Listing 11. Basic implementation of
decorate()
public void decorate(Object resource, IDecoration decoration) {
decoration.addOverlay(ImageDescriptor.createFromFile(FileDecorator.class,
"/icons/sample.gif"), IDecoration.TOP_RIGHT);
decoration.addPrefix("My Prefix ");
decoration.addSuffix(" My Suffix");
}
|
The IDecoration object also allows
customization of the font and text/background color.
Figure 4. A screenshot of the code in Listing 11 decorating IResources
The first argument of decorate() can be
used to filter the resources we want to decorate. If we only want to
decorate resources that contain specific markers, we use code exampled
in Listing 12.
Listing 12. Decorate resources with specific markers
public void decorate(Object resource, IDecoration decoration) {
if(resource instanceof IResource){
List<IMarker> markers = MyMarkerFactory.findMarkers((IResource) resource);
if (markers.size() > 0) {
decoration.addSuffix(" Marker !!");
}
}
}
|
You have followed this tutorial, what's next?
Advanced improvements can include:
- Add editable properties to markers. Allow the user to change the state of the markers
- Automate the creation and deletion of markers. Use background processing jobs to create, update, and delete markers automatically.
- Customize the marker hovers. Use advance marker hovers to support HTML or multimedia content
In this tutorial, we used Eclipse to easily create and customize markers and perform advanced resource marking. Developers are encouraged to use this simple, yet powerful tool to perfectly integrate their plugins into the Eclipse IDE. Keep in mind, however, that this feature can become obtrusive to the user if implemented too extensively. Furthermore, it is the responsibility of the developer to maintain the Eclipse look and feel by taking into consideration the Eclipse User Interface Guidelines, see Resources.





