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 object is selected?"

m
 
Line 32: Line 32:
 
== See Also: ==
 
== See Also: ==
 
*[[FAQ How do I find out what view or editor is selected?]]
 
*[[FAQ How do I find out what view or editor is selected?]]
*[[FAQ How do I find the active workbench_page?]]
+
*[[FAQ How do I find the active workbench page?]]
 
*[[FAQ How do I make a view respond to selection changes in another view?]]
 
*[[FAQ How do I make a view respond to selection changes in another view?]]
 
*[[FAQ How do I access the active project?]]
 
*[[FAQ How do I access the active project?]]
  
 
{{Template:FAQ_Tagline}}
 
{{Template:FAQ_Tagline}}

Latest revision as of 16:53, 13 November 2009

The ISelectionService tracks all selection changes within the views and editors of a workbench window or page. By adding a listener to this service, you will be notified whenever the selection changes. Selections in views are typically returned as IStructuredSelection instances, and selections in editors typically implement ITextSelection. You should avoid any expensive computation from within a selection listener, because this event fires quite frequently as the user is moving around in the UI and typing in editors. A more efficient approach is to avoid adding a listener, and simply asking the selection service for the current selection when you need it.

You can also ask for the selection in a particular view by passing the view ID as a parameter to the getSelection method:

   IWorkbenchPage page = ...;
   //the current selection in the entire page
   ISelection selection = page.getSelection();
   //the current selection in the navigator view
   selection = page.getSelection(IPageLayout.ID_RES_NAV);
   //add a listener
   ISelectionListener sl = new ISelectionListener() {
      public void selectionChanged(IWorkbenchPart part, ISelection sel) {
         System.out.println("Selection is: " + sel);
      }
   };
   page.addSelectionListener(sl);
   //add a listener to selection changes only
   //in the navigator view
   page.addSelectionListener(sl, IPageLayout.ID_RES_NAV);

IWorkbenchPage implements ISelectionService directly. You can also access a selection service to track selection within a workbench window by using IWorkbenchWindow.getSelectionService.

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