TreeViewer中的项目装饰 [英] Items decorations in a TreeViewer

查看:45
本文介绍了TreeViewer中的项目装饰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题: 我正在准备在Eclipse中使用编辑器,并且其中一个标签包含 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.

有人知道如何装饰树中的项目吗?我正在尝试使用 ControlDecoration 类,但没有成功.

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

预先感谢

Marcin

PS.我仅限于Eclipse 3.4

PS. I'm limited to Eclipse 3.4

推荐答案

有两种方法可以完成此操作.如果您的TreeViewer显示的对象是EObject的实例(由EMF生成.如果您不理解此部分,请跳至下一段:)),您可以更改这些EObject的"XyzItemProvider",以便其"getImage"方法返回一个装饰的图像,而不是普通"图像...仅此而已,对于EMF对象,无需进行其他任何更改.

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.

如果要显示经典" Java对象,则必须更改TreeViewer的LabelProvider才能装饰图像.这是通过TreeViewer#setLabelProvider()方法完成的.

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.

这篇关于TreeViewer中的项目装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆