以编程方式更改eclipse中的背景颜色

时间:2010-01-05 22:50:58

标签: eclipse editor colors plugins background

我有一个与eclipse插件开发相关的问题。有什么办法吗? 通过它我可以以编程方式更改eclipse中的背景颜色。      我可以通过调用来更改文本前景色 ITextViewer中的setTextColor(color,offset,length,controlRedraw) 但我没有找到任何可以改变背景的功能 文字的颜色。        如果有人通过这种方式分享你的想法。

由于 ARAV

2 个答案:

答案 0 :(得分:2)

我不确定这是否可以轻松完成,只需扩展您自己的文本编辑器版本,在这里提供一个Configuration类,其中接受PresentationReconciler类,类似于{{ 1}}类,告诉您是否需要放置前景色或背景色

请参阅this document

<强> PresentationReconciler

  • Rule:在文本更改时定义脏区
  • IPresentationDamager:为脏区重新制作演示文稿
  • IPresentationRepairer根据令牌扫描程序执行这两项操作
  • DefaultDamagerRepairer:将文本解析为令牌流
  • ITokenScanner使用简单的规则

从演示文稿中摘录

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

来自文本编辑器食谱,季节的文本编辑器配方
Tom Eicher ,IBM Eclipse团队

此处,空背景颜色表示该窗口小部件的默认系统背景。 (所以这里:白色) 但您可以根据文档的分区和适用的规则指定所需的颜色。

答案 1 :(得分:1)

我知道这是前一段时间被问到的,但是如果有人在寻找其他解决方案,我想我会发布以下内容:

由于您可以使用setTextColor方法,因此您也应该能够使用changeTextPresentation方法。

对于我的插件,我有一个TextListener,它调用我覆盖的TextChanged方法。我使用changeTextPresentation方法执行以下操作来添加背景颜色。通过这样做,我能够获得带有黑色前景的绿色背景。当然,这不是我想要的,而是出于测试目的。

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.
}