Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 2.06 KB

FAQ_How_do_I_find_out_what_view_or_editor_is_selected.md

File metadata and controls

28 lines (21 loc) · 2.06 KB

FAQ How do I find out what view or editor is selected?

To find out what view or editor is selected, use the IPartService. As with ISelectionService, you can add a listener to this service to track the active part or simply query it whenever you need to know. Note, saying that the part is active does not imply that it has focus. If a dialog opens on top of the workbench window, the active part does not change, even though the active part loses focus. The part service will also notify you when parts are closed, hidden, brought to the top of a stack, and during other lifecycle events.

Two types of listeners can be added to the part service: IPartListener and the poorly named IPartListener2. You should always use this second one as it can handle part-change events on parts that have not yet been created because they are hidden in a stack behind another part. This listener will also tell you when a part is made visible or hidden or when an editor's input is changed:

      IWorkbenchPage page = ...;
      //the active part
      IWorkbenchPart active = page.getActivePart();
      //adding a listener
      IPartListener2 pl = new IPartListener2() {
         public void partActivated(IWorkbenchPartReference ref) {
            System.out.println("Active: "+ref.getTitle());
         }
         ... other listener methods ...
      };
      page.addPartListener(pl);

IWorkbenchPage implements IPartService directly. You can also access a activation service by using IWorkbenchWindow.getPartService.

See Also: