| Selection of a IJavaElement - determine the associated project [message #84920] | 
Sun, 29 June 2003 18:40   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Hi, 
 
I'm writing a plugin that uses the "org.eclipse.ui.popupMenus"-Extension 
Point to add an action to the java views. 
The action is enabled for IClassFile, ICompilationUnit and IType. 
 
For my plugin to work I need the project "in which the selected element 
lies". 
 
How do I get this correctly. 
Thanks,  
Lars 
 
NOTE: IJavaElement.getJavaProject().getProject() works not correctly if 
the "same" java element (maybe from an external jar file) occurs in more 
than one open project. I believe it to be an issue with the java model...
 |  
 |  
  | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
| Re: Selection of a IJavaElement - determine the associated project [message #85554 is a reply to message #85453] | 
Mon, 30 June 2003 11:53    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Hi Stefan, 
 
thank you very much for your answer. 
It will work, if the selection was made in the resource navigator, because 
only resources are shown therein. 
However, if the selection occurs in one of the java views (java browsing, 
package explorer) the selected element is probably of IJavaElement. 
A java element can deliver its associated java project, but im my case, 
when the java element comes from an archive that is used in more than one 
open project the returned java project is not always the project where the 
selection occured.  
Kind of complicated situation, huh? 
 
Thanks again, 
Lars 
 
 
Stefan wrote: 
 
> Hi ! 
 
> I don't know if this is correct way, but perhaps you could navigate 
> the tree using : 
 
> ISelection selection = wPart.getSite().getSelectionProvider. 
> ().getSelection(); 
> if (selection instanceof StructuredSelection) 
> { 
>   if (!ssel.isEmpty()) 
>   { 
>     Object o = ssel.getFirstElement(); 
 
>     if (o instanceof Project) 
>     { 
>       Project project = (Project) o; 
>       // use project as you like 
>     } 
>     else if (o instanceof Resource) 
>     { 
>     // if resource is selected         
>     // navigate the resource tree using  getParent method 
>     // until you reach the Project or null (workspace root), but should 
>     // hit the project first 
>     } 
> } 
 
> I take only first resource that is selected, but it is possible, 
> that there are multiple files selected in different projects. 
 
 
> regards, Stefan
 |  
 |  
  | 
 | 
| Re: Selection of a IJavaElement - determine the associated project [message #86294 is a reply to message #86235] | 
Tue, 01 July 2003 06:58   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Hi Stefan, 
 
thank you very much again. 
You might want to check the bug report #39468 I filed yesterday. 
There your assumption is confirmed, but sadly no satisfing solution 
provided so far. 
I guess I have to let the user choose which project he meant by selecting 
the darned element ;-) 
 
Regards, 
Lars 
 
 
Stefan wrote: 
 
> Hi again 
 
 
> You are right, and I tested it with this fragment of code: 
 
> StructuredSelection ssel = (StructuredSelection) selection; 
> if (!ssel.isEmpty()) 
> { 
>   Object o = ssel.getFirstElement(); 
>   while(o != null && o instanceof JavaElement) 
>   { 
>     System.out.println(((JavaElement)o).getElementName()); 
>     o = ((JavaElement)o).getParent(); 
>   } 
> } 
 
> If I select the class or package which is of type JarPackageFragment 
> I get the wrong project name  but if 
 
> I select the Jar file itself (rt.jar) which is JarPackageFragmentRoot 
> it gives me the correct project name.  
 
> After little experimenting I noticed, that you get the project that you  
> expand first in the package explorer, so my guess is that there is one 
> instance of rt.jar per project , but only one instance of given 
> JarPackageFragment per workspace (to save memory?). 
 
 
> regards, Stefan
 |  
 |  
  | 
Powered by 
FUDForum. Page generated in 0.65881 seconds