How to set the color of an Eclipse/RCP decorator?
Asked Answered
B

5

5

I added a decorator in a Eclipse/RCP application to my tree viewer items by plugin.xml:

<extension point="org.eclipse.ui.decorators">
      <decorator
            adaptable="true"
            class="sernet.verinice.samt.rcp.TopicDecorator"
            id="sernet.verinice.samt.rcp.TopicDecorator"
            label="ISA Topic decorator"
            lightweight="true"
            location="BOTTOM_LEFT"
            state="true">
         <enablement>
            <objectClass name="sernet.verinice.model.samt.SamtTopic"/>        
         </enablement>
      </decorator>

In the decorator class i set the decoration suffix which works fine:

public class TopicDecorator extends LabelProvider implements ILightweightLabelDecorator, {
  ControlMaturityService maturityService = new ControlMaturityService();    
  @Override
  public void decorate(Object element, IDecoration decoration) {
     decoration.addSuffix( new StringBuilder().append(" [")
       .append(maturityService.getWeightedMaturity((IControl)element))
       .append("]").toString() );   
     decoration.setForegroundColor(new Color(Display.getCurrent(), 150,90,90));     
   }

As you can see i also tried to set the foreground color of the suffic which has no effect. Suffix has the same color as the label in the tree: black.

How can i set the color of the decoration suffix?

Banda answered 4/2, 2011 at 12:4 Comment(0)
F
1

I have just had success getting a different coloured text decoration using a wrapper class TreeElementDecoratingLabelProvider for org.eclipse.jface.viewers.DecoratingLabelProvider:

public class TreeElementDecoratingLabelProvider extends DecoratingLabelProvider {
   public TreeElementDecoratingLabelProvider(ILabelProvider provider,   ILabelDecorator decorator) {
      super(provider, decorator);
   }

    @Override
    public Color getForeground(Object element) {
      //return your color for element...
      return Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
   }
}
Flog answered 9/12, 2016 at 10:53 Comment(0)
M
3

I have just had success getting a different coloured text decoration using a org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider that wrapps an IStyledLabelProvider, and an ILabelDecorator.

I think the key is the getStyledText method of the LabelProvider, that allows custom styling of the text

Megilp answered 21/12, 2011 at 10:19 Comment(1)
Just wanted to add that the DelegatingStyledCellLabelProvider can be used to wrap a IStyledLabelProvider in cases where one does not want a decorator, but rather just a label provider which creates styled labels directly.Tan
R
1

I guess you should try to change the order - set setForegroundColor() first and then add a suffix.

Hint: to not initialize any colour by yourself, you may use Display.getDefault().getSystemColor(SWT.COLOR_GREEN); Then you need to care about disposing of this colour - it's freed by the system.

Rockel answered 9/3, 2011 at 7:47 Comment(1)
It makes no difference whether you call setForegroundColor(..) first or after addSuffix(..). Suffix has still the same color as the rest of the text.Scrip
D
1

Your decorator needs to implement org.eclipse.jface.viewers.IColorDecorator if it needs to provide various colors

Dahabeah answered 23/3, 2011 at 5:6 Comment(2)
My TopicDecorator now also implements IColorDecorator. Implemented methods decorateBackground and decorateForeground both returns statically one different color but decorator string ist still black with white backround.Scrip
Make sure "Use Mixed fonts and colors" preference is checked (Prefereces->General->Apperance) and disable all other decorators (General->Appearance->Label decorators) and see whether it worksDahabeah
F
1

I have just had success getting a different coloured text decoration using a wrapper class TreeElementDecoratingLabelProvider for org.eclipse.jface.viewers.DecoratingLabelProvider:

public class TreeElementDecoratingLabelProvider extends DecoratingLabelProvider {
   public TreeElementDecoratingLabelProvider(ILabelProvider provider,   ILabelDecorator decorator) {
      super(provider, decorator);
   }

    @Override
    public Color getForeground(Object element) {
      //return your color for element...
      return Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
   }
}
Flog answered 9/12, 2016 at 10:53 Comment(0)
C
1

You just have to implement org.eclipse.jface.viewers.IColorProvider in your LabelProvider

public class MyLabelProvider extends LabelProvider implements IColorProvider {

    public String getText(Object element){
        return String.valueOf(element)
    }

    public Color getForeground(Object element){
        Display display = Display.getDefault();
        return display.getSystemColor(SWT.COLOR_GRAY);
    }

    public Color getBackground(Object element){
        return null;
    }
}

Then you can create a DecoratingLabelProvider. Usually you use the workbench's decorator, because it detects the decorator registered by the extension-point org.eclipse.ui.decorators. See The Java Developer's Guide to Eclipse.

ILabelProvider baseLabelProvider = new MyLabelProvider();
IDecoratorManager decoratorManager = PlatformUI.getWorkbench().getDecoratorManager();
ILabelDecorator decorator = decoratorManager.getLabelDecorator();
DecoratingLabelProvider decoratingLabelProvider = new DecoratingLabelProvider(baseLabelProvider, decorator);

and use it as normal

TableViewer viewer = ...;
viewer.setLabelProvider(decoratingLabelProvider);

The DecoratingLabelProvider automatically detects if the LabelProvider it uses is an

For more sophisticated decorations take a look at WorkbenchLabelProvider.

I often use the WorkbenchLabelProvider in combination with the DelegatingStyledCellLabelProvider, because they perfectly integrate with the workbench.

I see two benefits with this approach:

  1. WorkbenchAdapter is easier to use than the LableProviders
  2. WorkbenchLabelProvider manages the system resources like Colors for you. So don't forget to dispose the WorkbenchLabelProvider.

Here is a code snippet that I often use:

ILabelDecorator labelDecorator = PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator();
DecoratingStyledCellLabelProvider labelProvider = new DecoratingStyledCellLabelProvider(
            new WorkbenchLabelProvider(), labelDecorator, null);
tableViewer.setLabelProvider(labelProvider);
tableViewer.getTable().addDisposeListener((e) -> labelProvider.dispose());
Callisthenics answered 8/2, 2020 at 6:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.