Enumerating all my Eclipse editors?

怎甘沉沦 提交于 2019-12-03 16:29:22

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.

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);
        }
    }
}

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