Question

I am writing an eclipse plugin which exposes a view to the user with several buttons. On the click of any button, I would like to paste a certain comment into the editor window where the user is currently working and at the cursor location he is pointing to.

Once the user clicks the button, the editor window no longer has focus and the following code does not work.

workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor()

How can I detect the editor window where the user was working and the cursor location before the user clicked the button?

Was it helpful?

Solution

Use an IPartListener to listen to part activation events.

Set the listener up in your view initialization:

getSite().getPage().addPartListener(partListener);

(and don't forget to remove the listener in the view dispose).

Among other things this will give you part activation events:

public void partActivated(IWorkbenchPart part)

If part is an instance of IEditorPart then it is an editor being activated. So you just need to keep track of this activated editor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top