我正在编写Eclipse命令插件,并希望在包资源管理器视图中检索当前选定的节点。我希望能够从返回的结果中获取绝对文件路径,其中所选节点位于文件系统上(即c:\ eclipse \ test.html)。
我该怎么做?
答案 0 :(得分:32)
第一步是获得选择服务,例如从这样的任何观点或编辑:
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。
答案 1 :(得分:15)
代码如下:
IWorkbenchWindow window =
PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = window.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");
您可以在像LuaFileWizardAction class这样的动作中查看示例。