Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse 3.5: How to get file name from Editor?

Can anyone tell me how to get file name from editor?

I just made my own editor to open xml file and create a few section to display the data. Now I want to read the XML file and place it inside the section.

I think I now how to read xml data but I don't know how to access the file name so that it can be open.

Thanks

like image 919
Iso Avatar asked Dec 14 '22 04:12

Iso


2 Answers

I realise this is old, but since I stumbled upon it while searching for a solution to the exact same problem, I want to add a note to the answer by VonC:

IFileEditorInput

is hidden in the org.eclipse.ui.ide plug-in, so in order for the solution to work your plug-in needs to state that as a dependency.

like image 112
mdiin Avatar answered Dec 28 '22 12:12

mdiin


May be this approach could be useful in you casre

cast the editor input to IFileEditorInput and use the IFile to call getLocation() or getLocationURI().

As said here, basically

((IFileEditorInput)editorInput).getFile().getLocation() is enough.

See also this code:

public static String getCurrentFileRealPath(){
        IWorkbenchWindow win = PlatformUI.getWorkbench
().getActiveWorkbenchWindow();

        IWorkbenchPage page = win.getActivePage();
        if (page != null) {
            IEditorPart editor = page.getActiveEditor();
            if (editor != null) {
                IEditorInput input = editor.getEditorInput();
                if (input instanceof IFileEditorInput) {
                    return ((IFileEditorInput)input).getFile
().getLocation().toOSString();
                }
            }
        }
        return null;
}
like image 44
VonC Avatar answered Dec 28 '22 12:12

VonC