如何从Eclipse插件中获取包资源管理器中的选定节点 [英] How to get the selected node in the package explorer from an Eclipse plugin
问题描述
我该如何做?
第一步是获得选择服务,例如从这样的任何视图或编辑器:
ISelectionService service = getSite()。getWorkbenchWindow()
.getSelectionService() ;
或者, 然后,获取Package Explorer的选择并将其转换为IStructuredSelection: 从中可以得到您选择的IFile: 现在要获得完整路径,您将必须获取IFile的位置: 然后你最终可以使用它来获取文件的真正完整路径(除其他外): 您可以在这里找到有关选择服务的更多信息:使用选择服务。 I'm writing an Eclipse command plugin and want to retrieve the currently selected node in the package explorer view. I want to be able to get the absolute filepath, where the selected node resides on the filesystem (i.e. c:\eclipse\test.html), from the returned result. How do I do this ? The first step is to get a selection service, e.g. from any view or editor like this: Or, as VonC wrote, you could get it via the PlatformUI, if you are neither in a view or an editor. Then, get the selection for the Package Explorer and cast it to an IStructuredSelection: From that, you can get your selected IFile: Now to get the full path, you will have to get the location for the IFile: Which you then can finally use to get the real full path to your file (among other things): You can find more information on the selection service here: Using the Selection Service. 这篇关于如何从Eclipse插件中获取包资源管理器中的选定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
IStructuredSelection structured =(IStructuredSelection)service
.getSelection(org.eclipse.jdt.ui.PackageExplorer);
IFile file =(IFile)structured.getFirstElement();
IPath path = file.getLocation();
System.out.println(path.toPortableString());
ISelectionService service = getSite().getWorkbenchWindow()
.getSelectionService();
IStructuredSelection structured = (IStructuredSelection) service
.getSelection("org.eclipse.jdt.ui.PackageExplorer");
IFile file = (IFile) structured.getFirstElement();
IPath path = file.getLocation();
System.out.println(path.toPortableString());