How to set the color of the Eclipse / RCP decorator?

I added a decorator in the Eclipse / RCP application to my tree view elements using 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 a decorative suffix that 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, which has no effect. The suffix has the same color as the label in the tree: black.

How can I set the color of the decorative suffix?

+4
source share
4 answers

I just managed to get a colored decoration using the TreeElementDecoratingLabelProvider wrapper class 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); } } 
+1
source

I just had success with formatting other colored text using org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider , which wraps IStyledLabelProvider and ILabelDecorator .

I think the key is the getStyledText method, which allows you to customize the text style

+3
source

I think you should try to reorder - first set setForegroundColor () and then add the suffix.

Hint: in order not to initialize any color yourself, you can use Display.getDefault (). getSystemColor (SWT.COLOR_GREEN); Then you need to take care of the disposal of this color - it is freed by the system.

+1
source

Your decorator needs to implement org.eclipse.jface.viewers.IColorDecorator if he needs to provide different colors

+1
source

Source: https://habr.com/ru/post/1338419/


All Articles