【问题标题】:ILightweightLabelDecorator's 'decorate' method is never calledILightweightLabelDecorator 的 'decorate' 方法永远不会被调用
【发布时间】:2014-06-13 00:46:26
【问题描述】:

我正在尝试将叠加图片添加到 TreeViewer 的特定项目。我已经阅读了 10 多篇关于 stackoverflow 的帖子以及关于 ILightweightLabelDecorator 的其他 Google 结果。

但我在 this guy 遇到了同样的问题:ILightweightLabelDecoratordecorate 方法永远不会被调用。

这是我所做的:

...
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 中的声明方式,但仍然从未调用过 decorate 方法。

<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>

我错过了什么吗?有人给点提示吗?

【问题讨论】:

    标签: java swt jface eclipse-plugin eclipse-pde


    【解决方案1】:

    您需要使用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 中的声明方式来执行此操作。但是编程方式确实失败了。谢谢你的耐心。
    • 添加了关于 DecorationOverlayIcon 的信息
    【解决方案2】:

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

    http://subhankarc.blogspot.in/2012/05/decorator-in-eclipse-jface-treeviewer.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多