3

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

2 Answers2

2

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

http://web.archive.org/web/20140715222227/http://img266.i_mageshack.us/img266/5465/setrules2.png

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.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
1

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.
}
Paul Reisert
  • 135
  • 8