Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Selection of a IJavaElement - determine the associated project
Selection of a IJavaElement - determine the associated project [message #84920] Sun, 29 June 2003 22:40 Go to next message
Lars Ködderitzsch is currently offline Lars KödderitzschFriend
Messages: 31
Registered: July 2009
Member
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 #84935 is a reply to message #84920] Sun, 29 June 2003 23:28 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: adam.kiezun.gmx.net.remove

> For my plugin to work I need the project "in which the selected element
> lies".

IJavaElement:getJavaProject

a.
--
eclipse.org
Re: Selection of a IJavaElement - determine the associated project [message #85011 is a reply to message #84935] Mon, 30 June 2003 05:07 Go to previous messageGo to next message
Lars Ködderitzsch is currently offline Lars KödderitzschFriend
Messages: 31
Registered: July 2009
Member
Adam Kiezun wrote:

> > For my plugin to work I need the project \"in which the selected element
> > lies\".

> IJavaElement:getJavaProject
> a.
> --
> eclipse.org

Thanks for yout anwer, but as I noted in my first post
IJavaElement.getJavaProject works not as I awaited it. It can give you the
wrong project, for instance if the selected java element lies in an
external archive and the same archived is used in more than one project.
It depends on in which order these project are opened, the first opened
project \"adds the archive contents to the java model\". If I select the
element in the archive in the second project and call getJavaProject on
the element, the first project is returned.

Regards,
Lars
Re: Selection of a IJavaElement - determine the associated project [message #85069 is a reply to message #85011] Mon, 30 June 2003 08:44 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: adam_kiezun.ch.ibm.spam.protection.com

> Thanks for yout anwer, but as I noted in my first post

yup, i missed that parts of your post - sorry

a.
Re: Selection of a IJavaElement - determine the associated project [message #85161 is a reply to message #84920] Mon, 30 June 2003 10:10 Go to previous messageGo to next message
Jerome Lanneluc is currently offline Jerome LannelucFriend
Messages: 572
Registered: July 2009
Senior Member
Lars,

If the IJavaElement belongs to an external jar files, then it has no
IProject (since it is external to the workspace). The only relationship it
has with the Java model is through a classpath. One or more Java projects
can refer to it through their classpaths. It then belongs to one or more
Java projects.

Jerome

"Lars K
Re: Selection of a IJavaElement - determine the associated project [message #85176 is a reply to message #85161] Mon, 30 June 2003 10:20 Go to previous messageGo to next message
Lars Ködderitzsch is currently offline Lars KödderitzschFriend
Messages: 31
Registered: July 2009
Member
Hi Jerome,

thanks, but given the selected java element (through IStructuredSelection)
how can I determine the \\\"selected project\\\"?
I need something like a selection path...

Regards,
Lars

Jerome Lanneluc wrote:

> Lars,

> If the IJavaElement belongs to an external jar files, then it has no
> IProject (since it is external to the workspace). The only relationship it
> has with the Java model is through a classpath. One or more Java projects
> can refer to it through their classpaths. It then belongs to one or more
> Java projects.

> Jerome

> \\\"Lars Ködderitzsch\\\" <rookie_no2@web.de> wrote in message
> news:bdnpt0$gt6$1@rogue.oti.com...
> > 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 #85206 is a reply to message #85176] Mon, 30 June 2003 10:31 Go to previous messageGo to next message
Jerome Lanneluc is currently offline Jerome LannelucFriend
Messages: 572
Registered: July 2009
Senior Member
Sorry Lars, but I don't know anything about the UI part, so someone else
would have to help you.

Jerome

"Lars K
Re: Selection of a IJavaElement - determine the associated project [message #85453 is a reply to message #85206] Mon, 30 June 2003 14:39 Go to previous messageGo to next message
Stefan is currently offline StefanFriend
Messages: 27
Registered: July 2009
Junior Member
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 #85554 is a reply to message #85453] Mon, 30 June 2003 15:53 Go to previous messageGo to next message
Lars Ködderitzsch is currently offline Lars KödderitzschFriend
Messages: 31
Registered: July 2009
Member
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 #86235 is a reply to message #85554] Tue, 01 July 2003 09:14 Go to previous messageGo to next message
Stefan is currently offline StefanFriend
Messages: 27
Registered: July 2009
Junior Member
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
Re: Selection of a IJavaElement - determine the associated project [message #86294 is a reply to message #86235] Tue, 01 July 2003 10:58 Go to previous message
Lars Ködderitzsch is currently offline Lars KödderitzschFriend
Messages: 31
Registered: July 2009
Member
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
Previous Topic:1.4.2 compiler options?
Next Topic:export a gcj makefile or similar?
Goto Forum:
  


Current Time: Wed Jan 15 07:49:39 GMT 2025

Powered by FUDForum. Page generated in 0.09852 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top