How would one use IDecorationContext api from Eclipse JFace

为君一笑 提交于 2019-11-26 23:35:54

问题


Is there an example out there for using IDecorationContext for label decorations?

By the looks of it, IDecorationContext class seems to provide some sort of contextual decoration support, but for the life of me, I can not find any sample code using this feature...

Has anybody actually used decoration context feature and if so, what use cases did it solve?


PS: I am looking for a way to apply image decorations to object labels and depending on where the object is displayed, the base icon size varies (e.g. traditional "small" icons in table- and tree items and larger icons for content headers).

The decorations applied to the original icons should choose appropriate size decorations accordingly.

IDecorationContext seems to fit the bill for what I need it for, but the documentation is as sparse as one can expect from a minor feature of an open source library and there are no examples to be found.

Googling for the "IDecorationContext" did not reveal anything interesting either, so I turn to StackOverflow crowd wisdom in hopes next guy getting the question would be able to get their answer faster ;)


回答1:


I did not use IDecorationContext, but you can see it used in org.eclipse.jface.viewers.LabelDecorator.

It is also discussed in this thread (even if there are no answer, that can at least give you a starting point)

My current approach is to extend org.eclipse.ui.decorators using a ILightweightLabelDecorator to add a replace overlay to the respective icons:

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) {
         }
      }
   }

   ...
}


来源:https://stackoverflow.com/questions/2027649/how-would-one-use-idecorationcontext-api-from-eclipse-jface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!