Eclipse 3.5:如何从编辑器获取文件名?

Iso*_*Iso 4 xml eclipse editor

谁能告诉我如何从编辑器中获取文件名?

我只是创建了自己的编辑器来打开xml文件并创建了一些显示数据的部分.现在我想读取XML文件并将其放在该部分中.

我想我现在如何读取xml数据,但我不知道如何访问文件名,以便它可以打开.

谢谢

Von*_*onC 7

可能这种方法可能对你有用

将编辑器输入转换为IFileEditorInput并使用IFileto调用getLocation()getLocationURI().

至于说在这里,基本上

((IFileEditorInput)editorInput).getFile().getLocation() 足够.

另见此代码:

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;
}
Run Code Online (Sandbox Code Playgroud)


mdi*_*iin 7

我意识到这已经过时了,但是由于我在寻找完全相同问题的解决方案时偶然发现它,我想在VonC的答案中添加一个注释:

IFileEditorInput
Run Code Online (Sandbox Code Playgroud)

隐藏在org.eclipse.ui.ide插件中,因此为了使解决方案正常工作,您的插件需要将其声明为依赖项.