Items decorations in a TreeViewer

一个人想着一个人 提交于 2019-12-05 17:23:13

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!