Skip to main content

Notice: This Wiki is now read only and edits are no longer 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 access the active project?"

 
m
Line 1: Line 1:
This question is often asked by newcomers to Eclipse, probably as a result of  
+
This question is often asked by newcomers to Eclipse, probably as a result of switching from another IDE with a different interpretation of the term <i>project</i>. Similarly, people often ask how to access the &#147;active&#148; file.  In Eclipse there is no such thing as an active project or file.  Projects can be opened or closed, but many projects may be open at any given time.
switching from another IDE with a different interpretation of the term <i>project</i>.
+
Similarly, people often ask how to access the &#147;active&#148; file.  In Eclipse
+
there is no such thing as an active project or file.  Projects can be opened or closed,
+
but many projects may be open at any given time.
+
  
 
+
Often people are really asking for the currently selected project, folder, or file.  The selection can be queried using the UI&#146;s <tt>ISelectionService</tt>.
Often people are really asking for the currently selected project,
+
folder, or file.  The selection can be queried using the UI&#146;s <tt>ISelectionService</tt>.
+
  
 
Once you have the selection, you can extract the selected resource as follows:
 
Once you have the selection, you can extract the selected resource as follows:
Line 14: Line 8:
 
       if (!(sel instanceof IStructuredSelection))
 
       if (!(sel instanceof IStructuredSelection))
 
         return null;
 
         return null;
       IStructuredSelection ss = (IStructuredSelection)sel;
+
       IStructuredSelection ss = (IStructuredSelection) sel;
 
       Object element = ss.getFirstElement();
 
       Object element = ss.getFirstElement();
 
       if (element instanceof IResource)
 
       if (element instanceof IResource)
Line 26: Line 20:
 
</pre>
 
</pre>
  
 
+
If you are looking for the active editor, you can determine that from the <tt>IPartService</tt>. If an editor is active, you can extract the resource, if available, like this:
If you are looking for the active editor, you can determine that from the <tt>IPartService</tt>.
+
If an editor is active, you can extract the resource, if available, like this:
+
 
<pre>
 
<pre>
 
   IResource extractResource(IEditorPart editor) {
 
   IResource extractResource(IEditorPart editor) {
Line 37: Line 29:
 
   }
 
   }
 
</pre>
 
</pre>
 
  
 
== See Also: ==
 
== See Also: ==
 +
*[[FAQ How do I find out what object is selected?]]
  
[[FAQ_How_do_I_find_out_what_object_is_selected%3F]]
+
{{Template:FAQ_Tagline}}
 
+
<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 00:35, 9 June 2006

This question is often asked by newcomers to Eclipse, probably as a result of switching from another IDE with a different interpretation of the term project. Similarly, people often ask how to access the &#147;active&#148; file. In Eclipse there is no such thing as an active project or file. Projects can be opened or closed, but many projects may be open at any given time.

Often people are really asking for the currently selected project, folder, or file. The selection can be queried using the UI&#146;s ISelectionService.

Once you have the selection, you can extract the selected resource as follows:

   IResource extractSelection(ISelection sel) {
      if (!(sel instanceof IStructuredSelection))
         return null;
      IStructuredSelection ss = (IStructuredSelection) sel;
      Object element = ss.getFirstElement();
      if (element instanceof IResource)
         return (IResource) element;
      if (!(element instanceof IAdaptable))
         return null;
      IAdaptable adaptable = (IAdaptable)element;
      Object adapter = adaptable.getAdapter(IResource.class);
      return (IResource) adapter;
   }

If you are looking for the active editor, you can determine that from the IPartService. If an editor is active, you can extract the resource, if available, like this:

   IResource extractResource(IEditorPart editor) {
      IEditorInput input = editor.getInput();
      if (!(input instanceof IFileEditorInput))
         return null;
      return ((IFileEditorInput)input).getFile();
   }

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