views:

58

answers:

1

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

+2  A: 

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

   ...
}
VonC
Note to self: this appears to be my 2000th answer on SO.
VonC