【发布时间】:2010-10-09 19:19:58
【问题描述】:
我正在编写一个 Eclipse 命令插件,并希望在包资源管理器视图中检索当前选定的节点。我希望能够从返回的结果中获取所选节点驻留在文件系统上的绝对文件路径(即 c:\eclipse\test.html)。
我该怎么做?
【问题讨论】:
标签: java eclipse user-interface eclipse-rcp rcp
我正在编写一个 Eclipse 命令插件,并希望在包资源管理器视图中检索当前选定的节点。我希望能够从返回的结果中获取所选节点驻留在文件系统上的绝对文件路径(即 c:\eclipse\test.html)。
我该怎么做?
【问题讨论】:
标签: java eclipse user-interface eclipse-rcp rcp
第一步是获得选择服务,例如从像这样的任何视图或编辑器:
ISelectionService service = getSite().getWorkbenchWindow()
.getSelectionService();
或者,as VonC wrote,如果您不在视图或编辑器中,您可以通过 PlatformUI 获取它。
然后,获取 Package Explorer 的选择并将其转换为 IStructuredSelection:
IStructuredSelection structured = (IStructuredSelection) service
.getSelection("org.eclipse.jdt.ui.PackageExplorer");
从中,您可以获得您选择的 IFile:
IFile file = (IFile) structured.getFirstElement();
现在要获取完整路径,您必须获取 IFile 的位置:
IPath path = file.getLocation();
然后您最终可以使用它来获取文件的真实完整路径(除其他外):
System.out.println(path.toPortableString());
您可以在此处找到有关选择服务的更多信息:Using the Selection Service。
【讨论】:
代码如下:
IWorkbenchWindow window =
PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = window.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");
您在类似 LuaFileWizardAction class 的 Action 中查看示例。
【讨论】: