Search code examples
eclipseeclipse-pluginhighlighting

How to Change background Color while Hovering in Eclipse Editor


I am building a eclipse plugin with custom editor. I have implemented text hovering functionality in which user hover on some text then that text will be shown on tooltip like javadocs.

Now i want to change the background color of that hovering text in editor. How i can implement that? I tried some code. But i didn't working.

Color blueColor = new Color(PlatformUI.getWorkbench().getDisplay(),0,0,255);
textViewer.setTextColor(blueColor, startRegion, finalStr.length(), false);

Here finalStr is a String that i will get when i hover on some text. I want to change the background and foreground color of finalStr.


Solution

  • I resolved my question. Above code wasn't working for me because i was accessing it outside SWT thread. So i have to make a new thread and write code in that thread. Following is the code i used to make it work.

    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();