Enumerating all my Eclipse editors?

那年仲夏 提交于 2019-12-05 00:05:54

问题


I have built a simple Eclipse plugin where a user may use a TableViewer of database resources to open an editor on any of those resources.

Users may therefore have zero upwards instances of the editor running.

Is there an API available to get a list of those editor instances?


回答1:


You can get references to all open editors with:

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

And then check these to select the ones that reference instances of your editor type.




回答2:


According to the javadoc for the API a workbench can have several windows, and a window can have several pages, and they do not share editors.

So, in order to get all and every open editor, you should do something along these lines (error checking etc excluded):

List<IEditorReference> editors = new ArrayList<IEditorReference>();
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
    for (IWorkbenchPage page : window.getPages()) {
        for (IEditorReference editor : page.getEditorReferences()) {
            editors.add(editor);
        }
    }
}



回答3:


Be aware the such an enumeration will not respect the tab order

Here is an example of an enumeration of editors:

IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
IWorkbenchPage page = window.getActivePage();
IEditorPart actEditor = page.getActiveEditor();
IEditorReference[] editors = page.getEditorReferences();
for (int i=0; i<editors.length-1; i++) {
  if (editors[i].getEditor(true) == actEditor) {
    page.activate(editors[i+1].getEditor(true));
    return null;
  }
}


来源:https://stackoverflow.com/questions/516704/enumerating-all-my-eclipse-editors

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