我正在使用自定义编辑器构建一个eclipse插件。我已经实现了文本悬停功能,用户将鼠标悬停在某些文本上,然后该文本将显示在工具提示中,如javadocs。
现在我想在编辑器中更改悬停文本的背景颜色。我该如何实现呢?我尝试了一些代码。但我没有工作。
Color blueColor = new Color(PlatformUI.getWorkbench().getDisplay(),0,0,255);
textViewer.setTextColor(blueColor, startRegion, finalStr.length(), false);
这里的finalStr是一个字符串,当我将鼠标悬停在某些文本上时,我会得到它。我想改变finalStr的背景和前景色。
答案 0 :(得分:0)
我解决了我的问题。上面的代码对我不起作用,因为我在SWT线程之外访问它。所以我必须创建一个新线程并在该线程中编写代码。以下是我用来使其工作的代码。
new Thread(new Runnable() {
@SuppressWarnings("static-access")
public void run() {
try {Thread.sleep(10); } catch (Exception e) { }
PlatformUI.getWorkbench().getDisplay().getDefault().asyncExec(new Runnable() {
public void run() {
textViewer.invalidateTextPresentation();
Color lightColor = PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_GRAY);
Color blueColor=new Color(PlatformUI.getWorkbench().getDisplay(),0,0,255);
TextPresentation presentation = new TextPresentation();
TextAttribute attr = new TextAttribute(blueColor,lightColor , 0);
presentation.addStyleRange(new StyleRange(startRegion, finalStr.length(), attr.getForeground(),attr.getBackground()));
textViewer.changeTextPresentation(presentation, true);
}
});
}
}).start();