Jump to: navigation, search

Difference between revisions of "FAQ How do I find out what object is selected?"

 
m
Line 9: Line 9:
 
service for the current selection when you need it.
 
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 <tt>getSelection</tt> method:
 
+
You can also ask for the selection  
+
in a particular view by passing the view ID as a parameter to the <tt>getSelection</tt> method:
+
  
 
<pre>
 
<pre>
Line 33: Line 30:
 
       IPageLayout.ID_RES_NAV);
 
       IPageLayout.ID_RES_NAV);
 
</pre>
 
</pre>
<tt>IWorkbenchPage</tt> implements <tt>ISelectionService</tt>
+
<tt>IWorkbenchPage</tt> implements <tt>ISelectionService</tt> directly. You can also access a selection service to track selection within a workbench window by using <tt>IWorkbenchWindow.getSelectionService</tt>.
directly. You can also access a selection service to track selection within a
+
workbench window by using <tt>IWorkbenchWindow.getSelectionService</tt>.
+
 
+
 
+
 
+
 
+
 
+
  
 
== See Also: ==
 
== See Also: ==
 
+
* [[FAQ_How_do_I_find_out_what_view_or_editor_is_selected%3F]]
 
+
* [[FAQ_How_do_I_find_the_active_workbench_page%3F]]
[[FAQ_How_do_I_find_out_what_view_or_editor_is_selected%3F]]
+
* [[FAQ_How_do_I_make_a_view_respond_to_selection_changes_in_another_view%3F]]
 
+
* [[FAQ_How_do_I_access_the_active_project%3F]]
 
+
[[FAQ_How_do_I_find_the_active_workbench_page%3F]]
+
 
+
 
+
[[FAQ_How_do_I_make_a_view_respond_to_selection_changes_in_another_view%3F]]
+
 
+
 
+
[[FAQ_How_do_I_access_the_active_project%3F]]
+
  
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>

Revision as of 18:51, 29 May 2006

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.