Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » General (non-technical) » Eclipse Foundation » Re: Get Selected Object in Eclipse - Help Please
Re: Get Selected Object in Eclipse - Help Please [message #34536] Wed, 10 May 2006 08:33
Eclipse UserFriend
Originally posted by:

It sounds like you're getting hold of the selected object, but then not being able to cast it into the type you are wanting/expecting. You should print out the type of object you're getting back with object.getClass() to find out what the type is, and if you're being defensive, you should check with instanceof before casting :-)

However, most objects that get selected don't necessarily have the type that you think they should (or want them to). They may just present themselves as an anonymous IResource, for example. In any case, objects in Eclipse most of the time support IAdaptable to perform dynamic casting -- if you're not familliar with it, I highly recommend the article by the hugely intelligent bloke at ;-)

The net effect is that you end up keeping a track of the selection via the selection listener. You can then see if the selection is an instanceof IStructuredSelection (which most of the time it is, but you need the check anyway), and then cast and obtain the object. If the object implements IAdaptable, you then cast to that and obtain a reference to the IResource or similar.

This shows an example of getting the objects -- though in this case, not using the IAdaptable pattern:

<a href=" pnews.ui/Source/Java/org/rcpapps/rcpnews/ui/actions/BrowseAc"></a>

Hope that helps,

Previous Topic:Help in migrating NetBeans to Eclipse!!!
Next Topic:Is "chmod +x ..." equiv. to IFileInfo.setAttribute(EFS.ATTRIBUTE_EXECUTABLE, ...)?
Goto Forum:

Current Time: Sun Apr 22 20:25:40 GMT 2018

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

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