Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top