This article will teach you how to decorate your tree-viewer nodes with another icon overlayed on the original icon. It has three simple steps. I am explaining them in a step by step manner so that the user can also do a hands on while reading.
- At first we have to add the extension org.eclipse.ui.decorators in the plugin where our tree viewer is. Now add a decorator in it and specify the following attributes, I have added a snapshot for better understanding.
- id : any valid decorator id.
- label: any valid string
- class: name of the class which implements the decorator logic.
- Now we have to implement the decorator class. The decorator class implements ILightweightLabelDecorator. It has a few methods to be overridden. But mainly we need to override the decorate method to implement the image decoration. The code snippet is given for better understanding. Basically we check for all the elements in the tree-viewer and based on a property of the node we can decorate the node icon.
@Override
public void decorate(Object element, IDecoration decoration) {
if (element instanceof XYZ) {
ImageDescriptor imageDescriptor = Activator.getDefault().getImageRegistry().getDescriptor("constant_img");
decoration.addOverlay(imageDescriptor, IDecoration.TOP_LEFT);
}
}
- Now that your decorator is ready to use, create your treeviewer and set the decorator on it. Code snippet will be something like this.
myTreeViewer.setLabelProvider(new DecoratingLabelProvider(new MyLabelProvider(), decorator));
That's it! We are done with it and now you can see your tree-viewer node icon has got decorated with your overlayed icon beautifully. One tips would be to use smaller icon for decoration than that of the original icon.
cheers,
Subhankar
No comments:
Post a Comment