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

4

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

我刚刚制作了自己的编辑器来打开XML文件并创建一些部分以显示数据。现在我想读取XML文件并将其放置在该部分内。

我认为我现在知道如何读取XML数据,但我不知道如何访问文件名以便打开它。

谢谢

2个回答

7

我知道这篇文章早已发布,但我在寻找解决同样问题时偶然发现了它。因此我想在 VonC 的答案中添加一条说明:

IFileEditorInput

该解决方案隐藏在org.eclipse.ui.ide插件中,因此为了使解决方案有效,您的插件需要声明它作为依赖项。


谢谢,这样就省去了我很多麻烦。 - Ashwinee K Jha

6
也许这种方法对你有用。
将编辑器输入转换为IFileEditorInput,并使用IFile调用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;
}

感谢您提供的代码,但是我在IFileEditorInput上遇到了一个错误。 它说IFileEditorInput无法解析为类型。 快速修复中也没有合适的解决方案。 - Iso

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接