programmatically change the background color in eclipse

本小妞迷上赌 提交于 2019-12-23 16:27:37

问题


I have a question related to eclipse plugin development. Is there any means by which I can programmatically change the background color in eclipse. I am able to change the text foreground color by calling setTextColor(color, offset, length, controlRedraw) in ITextViewer but I don't find any function by which I can change the background color of the text. If anyone has been through this kindly share your thoughts.

Thanks arav


回答1:


I am not sure this can be done easily, short of extending your own version of a Text Editor, here you provide a Configuration Class which inturn accepts a PresentationReconciler Class which is like a Rule Class that tells you if you need to put a Foreground or a Background Color

See this document

PresentationReconciler

  • IPresentationDamager: define dirty region given a text change
  • IPresentationRepairer: recreate presentation for dirty region
  • DefaultDamagerRepairer does both, based on a token scanner
  • ITokenScanner: parse text into a token stream
  • RuleBasedScanner uses simple rules

Extract from the presentation

From Text Editor Recipes, Season’s recipes for your text editor
Tom Eicher, IBM Eclipse Team

Here, the null background color means, takes the default system background for that widget. (so here: white).
But you could specify whatever color you want, based on the partitioning of your document and on the rules that would apply.




回答2:


I know this was asked a while ago, but in case anyone is looking for another solution, I thought I would post the following:

Since you are able to use the setTextColor method, then you should be able to use the changeTextPresentation method as well.

In the case of my plug-in, I have a TextListener that calls the TextChanged method I overwrote. I did the following to add background color using the changeTextPresentation method. In doing so, I was able to get a Green background with Black foreground. Not that I would want this, of course, but just for testing purposes.

public void TextChanged(TextEvent event){
...
TextPresentation presentation = new TextPresentation();
TextAttribute attr = new TextAttribute(new ColorManager().getColor(MyConstants.BLACK),
      new ColorManager().getColor(MyConstants.GREEN), style);
presentation.addStyleRange(new StyleRange(startOffset, tokLength, attr.getForeground(),
      attr.getBackground());
sourceViewer.changeTextPresentation(presentation, true); //sourceViewer is a global variable passed to my TextListener class constructor.
}


来源:https://stackoverflow.com/questions/2009750/programmatically-change-the-background-color-in-eclipse

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