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 access the active project?"

 
(See Also:)
(4 intermediate revisions by 2 users not shown)
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 38: Line 30:
 
</pre>
 
</pre>
  
 +
The code above has a minor error:
 +
IEditorInput input = editor.getEditorInput();
  
== See Also: ==
+
To obtain the project from the resource use IResource.getProject().
 +
Beware that while Eclipse uses "selected" rather than "active" for the active project, it uses "active" rather than "selected" for the active editor. Or is that "selected editor" ;-).  For example,
  
[[FAQ_How_do_I_find_out_what_object_is_selected%3F]]
+
IWorkbench iworkbench = PlatformUI.getWorkbench();
 +
if (iworkbench == null)...
 +
IWorkbenchWindow iworkbenchwindow = iworkbench.getActiveWorkbenchWindow();
 +
if (iworkbenchwindow == null) ...
 +
IWorkbenchPage iworkbenchpage = iworkbenchwindow.getActivePage();
 +
if (iworkbenchpage == null) ...
 +
IEditorPart ieditorpart = iworkbenchpage.getActiveEditor();
  
<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>
+
== See Also: ==
 +
*[[FAQ How do I find out what object is selected?]]
 +
*[http://dev.eclipse.org/mhonarc/lists/cdt-dev/msg11850.html Sample code to find selected project]
 +
{{Template:FAQ_Tagline}}

Revision as of 09:29, 23 January 2013

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();
   }

The code above has a minor error:

IEditorInput input = editor.getEditorInput();

To obtain the project from the resource use IResource.getProject(). Beware that while Eclipse uses "selected" rather than "active" for the active project, it uses "active" rather than "selected" for the active editor. Or is that "selected editor" ;-). For example,

IWorkbench iworkbench = PlatformUI.getWorkbench();
if (iworkbench == null)...
IWorkbenchWindow iworkbenchwindow = iworkbench.getActiveWorkbenchWindow();
if (iworkbenchwindow == null) ...
IWorkbenchPage iworkbenchpage = iworkbenchwindow.getActivePage();
if (iworkbenchpage == null) ...
IEditorPart ieditorpart = iworkbenchpage.getActiveEditor();

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.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.