How to get “active editor” in Eclipse plugin?

浪尽此生 提交于 2019-11-30 01:28:31

问题


In my Eclipse plugin, I need to know when the editor that is visible on the screen has changed. I am currently getting the active editor as follows:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()

This works for most cases except for when the green Continue button is pressed:

If I use the F8 shortcut then the active editor is updated as expected.

It seems that the active editor property is not updated until the editor tab gets focus (which doesn't happen when the Continue button is pressed).

Is there any other route that I can take to get the "visible editor"?

Thanks in advance.

Alan


回答1:


  1. An editor is active only when it has focus, so what you are getting is the right output by the API. The user of your plugin will not be running it in debug mode, so not a concern for the end user
  2. Alternatively, to get all open editors you can do the following:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()




回答2:


The IWorkbenchPage interface has anisPartVisible()` method which indicates if the specified part is visible. The result does not depend on whether the specified part is currently active, i.e. has focus, or not.

To find a visible but currently not active editor it may however not be sufficient to simply call this method on the active workbench page. Instead you might have to iterate over all workbench windows and check the visibility of your editor on the page of each of them.




回答3:


The question is similar to the question posted in the link below. One way to achieve this is to keep track of which editor was previously opened by creating a Part Listener. Eclipse Plugin - How to get the last worked on editor



来源:https://stackoverflow.com/questions/9348767/how-to-get-active-editor-in-eclipse-plugin

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