Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Items decorations in a TreeViewer

I have the following problem: I'm preparing an editor in Eclipse and one of the tab contains TreeViewer to show items in the tree. Each item has a name and a value, which is editable. The problem I need to indicate to user that value is incorrect (e.g. exceeds a given range). My idea is to decorate incorrect cells with a warning or error icon which will be shown also after editing is complete.

Does anybody have an idea how to decorate items in the tree? I was experimenting with ControlDecoration class but without success.

Thanks in advance,

Marcin

PS. I'm limited to Eclipse 3.4

like image 591
Marcin Avatar asked Feb 23 '23 23:02

Marcin


1 Answers

There are two ways that this can be done. If your TreeViewer displays objects that are instances of EObject (generated by EMF. If your don't understand this part, skip to the next paragraph :)), you can change these EObject's "XyzItemProvider" so that their "getImage" method return a decorated image instead of the "plain" image... and that's it for EMF objects, nothing else needs to be changed.

If you're displaying "classic" Java Objects, you'll have to change your TreeViewer's LabelProvider in order to decorate the Image. This is done through the TreeViewer#setLabelProvider() method.

What you will need then is "how to decorate an Image", which is done through code such as this :

public class MyLabelProvider extends DecoratingLabelProvider {
    public Image getImage(Object element) {
        Image image = super.getImage(element);

        List<Object> images = new ArrayList<Object>(2);
        images.add(image);
        images.add(<Image of the decorator>);
        labelImage = new ComposedImage(images); // This will put the second of the "images" list (the decorator) above the first (the element's image)

        return decoratedImage;
    }
    [...]
}

You then need to give your tree viewer this label provider :

TreeViewer treeViewer = new TreeViewer(...);
treeViewer.setLabelProvider(new MyLabelProvider(new LabelProvider()); // new LabelProvider()... or your previous label provider if you have one.
like image 97
Kellindil Avatar answered Mar 06 '23 20:03

Kellindil