can not retrieve the file name / java path selected in the eclipse plugin

I am developing a plugin that requires retrieval of path/filename of java files.

The code that i have written, successfully retrieves the filenames/path of the xml or manifest files, but is unable to retrieve the path of the Java files in the packages.

the code I have used is :-

if (selection instanceof IStructuredSelection)

    {
        Object o = ((IStructuredSelection) selection).getFirstElement();
        if (o instanceof IFile)
        {
            IPath loc = ((IFile) o).getLocation();
            if (loc != null)
            {
                    selectedFile = loc.toOSString();

            }
            else
                selectedFile = "Error at loc";
        }
        else
            selectedFile = "Error at O";
    }

please help me in this, asap. anxiously waiting. thank you.


With your help I was able to figure out a sloution:

Object o =  ((IStructuredSelection) selection).getFirstElement();

then

IPath loc = ((ICompilationUnit) o).getPath();

then

selectedFile = loc.toOSString();

With this, you will be able to get the path of the java file that you selected. and also, this shows the pathname starting from the project name.