Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Unable to inspect File object in selectionChanged()
Unable to inspect File object in selectionChanged() [message #287077] Thu, 23 June 2005 14:41 Go to next message
Eclipse UserFriend
Originally posted by: dhicks.i-hicks.org

I'm trying to build an action plugin to provide a new submenu on the
file selection popup menu. I've got some code in selecionChanged() that
I want to inspect the selected file and decide whether to make the menu
item available or not. However, I keep getting a ClassCastException
when I try to get to the File object in the provided StructuredSelection
object. I can see in the debugger that the first element is a "File"
type, which I assume is "org.eclipse.core.internal.resources.File", but
when I try to cast the object to such type I get an exception thrown.

Any help?
Thanks,
Dave
Re: Unable to inspect File object in selectionChanged() [message #287079 is a reply to message #287077] Thu, 23 June 2005 14:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

First, that is an internal class (it has .internal in the package name)
and so should not be cast to. You should be using IFile if the selection
is of type IFile. The selection could be for an IFolder, or an IProject,
or IJavaElement. You will need to test for this and not just blindly
cast. Unless you told in your plugin.xml that you are only interested in
selections of type IFile, then you wouldn't need to cast.



--
Thanks,
Rich Kulp
Re: Unable to inspect File object in selectionChanged() [message #287080 is a reply to message #287079] Thu, 23 June 2005 14:56 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: dhicks.i-hicks.org

Rich, thanks for the feedback. Using IFile didn't help matters though.
I'm still getting a ClassCastException. Here is my code, as it stands...

public void selectionChanged(IAction action, ISelection selection) {
if (!selection.isEmpty()) {
Shell shell = new Shell();
StructuredSelection ss = (StructuredSelection)selection;
try {
IFile f = (IFile)ss.getFirstElement();
MessageDialog.openInformation(shell,"EmsServiceCreator
Plug-in","Selected file:" + f.toString() );
} catch (Exception e) {
e.printStackTrace();
}
}
}

My plugin.xml specifies that this plugin should only be called for
*.java files and is based on the IFile resource.

Rich Kulp wrote:
> First, that is an internal class (it has .internal in the package name)
> and so should not be cast to. You should be using IFile if the selection
> is of type IFile. The selection could be for an IFolder, or an IProject,
> or IJavaElement. You will need to test for this and not just blindly
> cast. Unless you told in your plugin.xml that you are only interested in
> selections of type IFile, then you wouldn't need to cast.
Re: Unable to inspect File object in selectionChanged() [message #287092 is a reply to message #287080] Thu, 23 June 2005 17:39 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

What is the returned if it cannot be cast to IFile? You may not be
selecting an IFile, you may be selecting an IJavaElement.

--
Thanks,
Rich Kulp
Re: Unable to inspect File object in selectionChanged() [message #287109 is a reply to message #287092] Thu, 23 June 2005 22:32 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: dhicks.i-hicks.org

Rich Kulp wrote:
> What is the returned if it cannot be cast to IFile? You may not be
> selecting an IFile, you may be selecting an IJavaElement.
>

I determined that the element in question is a "File" object by setting
a breakpoint and debugging the plugin at the point where I'm getting the
selection change event. Perhaps it's an "IJavaElement" that I'm really
after - I do intend to have this plugin activate against a Java class.
So far, I haven't found where that particular element is defined, though.

Thanks for the input, thus far.
Dave
Re: Unable to inspect File object in selectionChanged() [message #287130 is a reply to message #287109] Fri, 24 June 2005 09:57 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

You would get an IJavaElement if you selected a class from the Package
Explorer. You would get an IFile if you selected a file in the Navigator.

I'm taling about org.eclipse.core.resources.IFile,
org.eclipse.internal.core.resources.File implements that IFile, so it
must be compatible if it is a File.

David Hicks wrote:
> Rich Kulp wrote:
>
>> What is the returned if it cannot be cast to IFile? You may not be
>> selecting an IFile, you may be selecting an IJavaElement.
>>
>
> I determined that the element in question is a "File" object by setting
> a breakpoint and debugging the plugin at the point where I'm getting the
> selection change event. Perhaps it's an "IJavaElement" that I'm really
> after - I do intend to have this plugin activate against a Java class.
> So far, I haven't found where that particular element is defined, though.
>
> Thanks for the input, thus far.
> Dave

--
Thanks,
Rich Kulp
Re: Unable to inspect File object in selectionChanged() [message #287137 is a reply to message #287130] Fri, 24 June 2005 10:58 Go to previous message
Eclipse UserFriend
Originally posted by: dhicks.i-hicks.org

I see. Thanks for the clarification. Sorry if my questions seem silly,
but this is my first plug-in attempt, and I clearly have a lot to learn.
:-)

Rich Kulp wrote:
> You would get an IJavaElement if you selected a class from the Package
> Explorer. You would get an IFile if you selected a file in the Navigator.
>
> I'm taling about org.eclipse.core.resources.IFile,
> org.eclipse.internal.core.resources.File implements that IFile, so it
> must be compatible if it is a File.
>
Previous Topic:removing DialogSettings?
Next Topic:enable/disable plugins from the workbench programmatically
Goto Forum:
  


Current Time: Sat Jul 12 15:41:42 EDT 2025

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

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

Back to the top