Unable to inspect File object in selectionChanged() [message #287077] |
Thu, 23 June 2005 14:41  |
Eclipse User |
|
|
|
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 #287080 is a reply to message #287079] |
Thu, 23 June 2005 14:56   |
Eclipse User |
|
|
|
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 #287137 is a reply to message #287130] |
Fri, 24 June 2005 10:58  |
Eclipse User |
|
|
|
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.
>
|
|
|
Powered by
FUDForum. Page generated in 0.25192 seconds