Eclipse Plugin - How to get the last worked on editor

廉价感情. 提交于 2019-12-19 03:58:43

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23389432/eclipse-plugin-how-to-get-the-last-worked-on-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!