5

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?

Lii
  • 11,553
  • 8
  • 64
  • 88
Martin Cowie
  • 2,788
  • 7
  • 38
  • 74

3 Answers3

10

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.

Fabian Steeg
  • 44,988
  • 7
  • 85
  • 112
8

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);
        }
    }
}
Erk
  • 1,159
  • 15
  • 9
  • I believe all recent versions of the workbench will never have more than one page, but I'm not sure about that. – Mike Daniels Apr 28 '11 at 18:19
  • I think WorkbenchPage is what you get if you drag an editor to "the side" of the other editors making eclipse split the editor area into two separate sections with their own area for editor tabs... – Erk May 13 '11 at 22:40
2

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;
  }
}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Is there a reason you're skipping the last editor in the array? To find all elements in an array you should do `for (int i=0; i – Erk Aug 22 '17 at 19:12
  • @Erk Not sure. Seven years ago, this example was from https://www.eclipse.org/forums/index.php/t/106220/ – VonC Aug 22 '17 at 19:47