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 User
Originally posted by: alex_blewitt.yahoo.com

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 http://www.eclipsezone.com/articles/what-is-iadaptable/ ;-)

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=" http://cvs.sourceforge.net/viewcvs.py/rcpapps/org.rcpapps.rc pnews.ui/Source/Java/org/rcpapps/rcpnews/ui/actions/BrowseAc tion.java?view=markup">BrowseAction.java</a>

Hope that helps,

Alex.
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 Oct 26 03:55:53 GMT 2014

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

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