eclipseeclipse-plugintext-editoreclipse-pde

How to get opened Editors in right Order?


I can get opened editors

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

this way, but they are unordered(always returned the same way, it doesnt matter which window is first and which second). For plugin I implement its important for me to get them in order they are opened it, is there any way to do that?


Solution

  • There's some indication here that you can't get what you want directly from the API.

    But how about this: register an IPartListener (or, better yet, IPartListener2) with the page's IPartService. Then you ought to get part-opened and part-closed messages. From that you can keep your own ordering of editor parts (IEditorPart). You can use that directly, or combine it with what you get from getEditorReferences().

    So I'm talking about something like:

    PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener(
       new IPartListener2() {
          private Stack<IWorkbenchPartReference> partStack = new Stack<IworkbenchPartReference>();
    
          public void partOpened(IWorkbenchPartReference ref) {
              partStack.push(ref);
          }
    
          public void partClosed(IWorkbenchPartReference ref) {
              partStack.pop(ref);
          }
    
          /* you'll need to implement or stub out the other methods of IPartListener2 */
          public void partActivated(IWorkbenchpartReference ref) {}
          public void partDeactivated(IWorkbenchpartReference ref) {}
          /* etc */
    
       }
    );
    

    Then you'll access that stack in your plugin.