views:

72

answers:

2

I am trying to draw some shapes (boxed ans arrows) into, i.e., "over" the text in an eclipse editor. To get started, I wrote the following code:

     IWorkbenchPage activePage = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
     final Shell shell2 = activePage.getActiveEditor().getSite().getShell();
     shell2.addPaintListener(new PaintListener(){
            public void paintControl(PaintEvent e){
                Rectangle clientArea = shell2.getClientArea();
             e.gc.drawLine(0,0,clientArea.width,clientArea.height);
            }
        });

The problem with this code is twofold: (1) The line is drawn not across the editor but across the entire workbench, i.e., Eclipse window, and (2) the line is drawn behind (!) all other controls like toolbars and editors. This causes the line to be almost invisible: it only shows at some pixels between other controls.

How can I draw a line across a control like a text editor in Eclipse?

A: 

The problem that you have is that you are getting the Shell, not the actual component for the editor. The Shell is the whole window where Eclipse is being shown.

I think the only solution is to create your own Editor implementation, and then in the createPartControl() method you can create a text area and then add the paint listener to it.

You can get started with:

http://www.realsolve.co.uk/site/tech/jface-text.php

And then, looking at the source code of AbstractTextEditor, you can find the "real" SWT component that you want to draw to. You would need to override the method that creates the UI components, copy the original code and add your custom painting.

Mario Ortegón
A: 

I'm not sure if it works, but you need to extend the TextEditor:

public class MyEditor extends TextEditor {
    protected StyledText createTextWidget(Composite parent, int styles) {
        StyledText widget = super.createTextWidget( parent, styles );
        widget.addPaintListener( <yourPaintlistener> );
        return widget;
    }
}

That should at least get you the basic text-drawing control of the editor. Still, it's a PITA to work with these classes, as it is very internal stuff from eclipse, and neither documented nor really extensible.

Good luck with that :)

derBiggi