有人能告诉我如何从编辑器中获取文件名吗?
我刚刚制作了自己的编辑器来打开XML文件并创建一些部分以显示数据。现在我想读取XML文件并将其放置在该部分内。
我认为我现在知道如何读取XML数据,但我不知道如何访问文件名以便打开它。
谢谢
我知道这篇文章早已发布,但我在寻找解决同样问题时偶然发现了它。因此我想在 VonC 的答案中添加一条说明:
IFileEditorInput
该解决方案隐藏在org.eclipse.ui.ide插件中,因此为了使解决方案有效,您的插件需要声明它作为依赖项。
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;
}