Java ILightweightLabelDecorator&x27;s';装饰';方法从未被调用

Java ILightweightLabelDecorator&x27;s';装饰';方法从未被调用,java,swt,jface,eclipse-plugin,eclipse-pde,Java,Swt,Jface,Eclipse Plugin,Eclipse Pde,我正在尝试向TreeViewer的特定项添加覆盖图片。我已经阅读了10多篇关于stackoverflow和其他Google结果的帖子,这些帖子都是关于ILightweightLabelDecorator的 但是我遇到了同样的问题:从未调用ILightWeightLabelCorator的装饰方法 <decorator lightweight="true" location="TOP_RIGHT" label="ViewLabelProvider Decorator"

我正在尝试向
TreeViewer
的特定项添加覆盖图片。我已经阅读了10多篇关于stackoverflow和其他Google结果的帖子,这些帖子都是关于ILightweightLabelDecorator的

但是我遇到了同样的问题:从未调用
ILightWeightLabelCorator
装饰方法

<decorator
    lightweight="true"
    location="TOP_RIGHT"
    label="ViewLabelProvider Decorator"
    class="com.myplugin.MyViewLabelProvider"
    id="com.myplugin.viewlabelprovider.decorator"
    state="true">
 <enablement>
    <objectClass name="com.myplugin.model.MyObject"/>        
 </enablement>
</decorator>
以下是我所做的:

...
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new MyViewLabelProvider());
...

//the MyViewLabelProvider class
public class MyViewLabelProvider extends LabelProvider implements ILightweightLabelDecorator{

    public void decorate(Object element, IDecoration decoration) {
        if(element instanceof MyObject){
            decoration.addOverlay(ImageUtil.getImageDescriptor("icons/decorate.gif"), IDecoration.TOP_RIGHT);
        }
    }

    ...
在完成所有这些工作之后,它仍然不起作用,因此我甚至在plugin.xml中尝试了声明方式,但是
decoration
方法仍然没有被调用

<decorator
    lightweight="true"
    location="TOP_RIGHT"
    label="ViewLabelProvider Decorator"
    class="com.myplugin.MyViewLabelProvider"
    id="com.myplugin.viewlabelprovider.decorator"
    state="true">
 <enablement>
    <objectClass name="com.myplugin.model.MyObject"/>        
 </enablement>
</decorator>


我错过什么了吗?有人给你一些提示吗?

你需要为你的标签提供者使用
org.eclipse.jface.viewers.DecoratingLabelProvider
,如下所示:

viewer.setLabelProvider(new DecoratingLabelProvider(your label provider,
             PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator()));
通常,您的标签提供程序只扩展了
LabelProvider
,您使用一个单独的类来实现
ILightweightLabelDecorator

更新:

如果您只想在标签提供程序中的图像上添加覆盖,可以使用
org.eclipse.jface.viewers.DecorationOverlayIcon
。比如:

DecorationOverlayIcon decoratedImage = new DecorationOverlayIcon(img, overlay, IDecoration.BOTTOM_RIGHT);

注意:你需要处理你用这种方式创建的任何图像。

检查我的这篇文章是否有用。它有一个使用装饰器的逐步过程


这只是一个拼写错误,我使用了您的代码,但还没有被调用。我的代码与plugin.xml中的声明一起工作。检查您的装饰器是否显示在“首选项>常规>外观>标签装饰”中,并且它是否已启用。不好。在我的情况下,我认为我不需要plugin.xml中的声明方式来实现这一点。但编程方式确实失败了。谢谢你的耐心。添加了关于装饰的信息