Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "FAQ How do I find out what view or editor is selected?"

m
m (Fix syntax error)
 
Line 10: Line 10:
 
   //adding a listener
 
   //adding a listener
 
   IPartListener2 pl = new IPartListener2() {
 
   IPartListener2 pl = new IPartListener2() {
       public void partActivated(IWorkbenchPartReference ref)
+
       public void partActivated(IWorkbenchPartReference ref) {
 
         System.out.println("Active: "+ref.getTitle());
 
         System.out.println("Active: "+ref.getTitle());
 
       }
 
       }

Latest revision as of 08:03, 5 September 2016

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:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Back to the top