如何使用Eclipse JFace中的IDecorationContext api

时间:2010-01-08 12:57:43

标签: java eclipse api swt jface

是否有使用IDecorationContext标签装饰的例子?

从它的外观来看,IDecorationContext类似乎提供某种上下文装饰支持,但对于我的生活,我找不到使用此功能的任何示例代码......

是否有人实际使用了装饰上下文功能?若然,它解决了哪些用例?


PS:我正在寻找一种方法将图像装饰应用于对象标签,并根据对象的显示位置,基本图标大小不同(例如表格中的传统“小”图标和树项目和内容标题的更大图标。)

应用于原始图标的装饰应相应地选择合适的尺寸装饰。

IDecorationContext似乎符合我所需的条件,但是文档与开源库的一个小功能一样稀疏,并且没有找到示例。

搜索“IDecorationContext”并没有发现任何有趣的东西,所以我转向StackOverflow众智,希望下一个得到问题的人能够更快地得到答案;)

1 个答案:

答案 0 :(得分:7)

我没有使用IDecorationContext,但您可以在org.eclipse.jface.viewers.LabelDecorator中看到它。

this thread中也讨论过(即使没有答案,至少可以给你一个起点)

我目前的做法是使用a扩展org.eclipse.ui.decorators ILightweightLabelDecorator为各自添加替换叠加层 图标:

public class ProjectLabelDecorator extends LabelProvider 
   implements ILightweightLabelDecorator {

   ...

   public void decorate(Object element, IDecoration decoration) {
      if (element instanceof IFolder) {
         IFolder folder = (IFolder) element;
     try {
            if (folder.getProject().hasNature("rttdt.nature")) {
                if (ProjectNature.isTestcase(folder)) {
                   IDecorationContext context = 
                      decoration.getDecorationContext();
                   if (context instanceof DecorationContext) {
                      ((DecorationContext) context).putProperty(
                         IDecoration.ENABLE_REPLACE, Boolean.TRUE);
                   }
                   decoration.addOverlay(fTestcaseOverlay,
                      IDecoration.REPLACE);
                }
         } catch (CoreException e) {
         }
      }
   }

   ...
}