Salman Javed
Salman Javed

Reputation: 11

Eclipse Text editor get cursor location

I am developing a plugin that recommends code using eclipse plugin development environment (PDE). For now I'm working on designing the interface. The thing is I want to get the cursor location in the eclipse editor and open a JFrame at that position. I've tried to get the location with the help of documentation and forums and only able to get the offset till now or you can say line and column offset. I want to get it in a point(x,y) that represents a location. So any ideas how to get the cursor position?

Upvotes: 1

Views: 1277

Answers (1)

greg-449
greg-449

Reputation: 111142

Assuming you have the StyledText control for the editor use getCaretOffset to get the caret offset:

StyledText text = ... get editor styled text

int caret = text.getCaretOffset();

Then call getLocationAtOffset to get the x, y coordinates of the offset relative to the control:

Point point = text.getLocationAtOffset(caret);

If necessary you can convert this to be relative to the display:

point = text.toDisplay(point);

Note that Eclipse plugins normally use SWT, not Swing. It will be a lot more difficult to open a JFrame than a SWT control.

You can get the StyledText for an ITextEditor using:

StyledText text = (StyledText)editor.getAdapter(Control.class);

Upvotes: 4

Related Questions