TreeViewer 中的项目装饰

发布于 2024-11-13 18:21:53 字数 319 浏览 4 评论 0原文

我有以下问题: 我正在 Eclipse 中准备一个编辑器,其中一个选项卡包含 TreeViewer 来显示树中的项目。每个项目都有一个名称和一个值,并且可以编辑。 我需要向用户指示该值不正确(例如超出给定范围)的问题。我的想法是用警告或错误图标装饰不正确的单元格,编辑完成后也会显示该图标。

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

预先感谢,

Marcin

PS。我仅限于 Eclipse 3.4

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望她远 2024-11-20 18:21:53

有两种方法可以做到这一点。如果您的 TreeViewer 显示的对象是 EObject 实例(由 EMF 生成。如果您不理解这部分,请跳到下一段:)),您可以更改这些 EObject 的“XyzItemProvider”,以便它们的“getImage”方法返回装饰图像而不是“普通”图像......这就是 EMF 对象的情况,不需要更改其他任何内容。

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

然后您需要的是“如何装饰图像”,这是通过如下代码完成的:

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;
    }
    [...]
}

然后您需要为树查看器提供此标签提供程序:

TreeViewer treeViewer = new TreeViewer(...);
treeViewer.setLabelProvider(new MyLabelProvider(new LabelProvider()); // new LabelProvider()... or your previous label provider if you have one.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文