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

无人久伴 提交于 2019-12-23 12:10:11

问题


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?


回答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);
   }
}



回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/4897750/how-to-set-the-color-of-an-eclipse-rcp-decorator

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