简体   繁体   English

如何从Eclipse插件中获取包浏览器中的选定节点

[英]How to get the selected node in the package explorer from an Eclipse plugin

I'm writing an Eclipse command plugin and want to retrieve the currently selected node in the package explorer view. 我正在编写Eclipse命令插件,并希望在包资源管理器视图中检索当前选定的节点。 I want to be able to get the absolute filepath, where the selected node resides on the filesystem (ie c:\\eclipse\\test.html), from the returned result. 我希望能够从返回的结果中获取绝对文件路径,其中所选节点位于文件系统上(即c:\\ eclipse \\ test.html)。

How do I do this ? 我该怎么做呢 ?

The first step is to get a selection service, eg from any view or editor like this: 第一步是获取选择服务,例如从任何视图或编辑器,如下所示:

ISelectionService service = getSite().getWorkbenchWindow()
            .getSelectionService();

Or, as VonC wrote , you could get it via the PlatformUI, if you are neither in a view or an editor. 或者, 正如VonC所写 ,如果您既不在视图中也不在编辑器中,您可以通过PlatformUI获取它。

Then, get the selection for the Package Explorer and cast it to an IStructuredSelection: 然后,获取Package Explorer的选择并将其转换为IStructuredSelection:

IStructuredSelection structured = (IStructuredSelection) service
            .getSelection("org.eclipse.jdt.ui.PackageExplorer");

From that, you can get your selected IFile: 从那里,您可以获得您选择的IFile:

IFile file = (IFile) structured.getFirstElement();

Now to get the full path, you will have to get the location for the IFile: 现在要获得完整路径,您必须获取IFile的位置:

IPath path = file.getLocation();

Which you then can finally use to get the real full path to your file (among other things): 然后,您最终可以使用它来获取文件的真实完整路径(以及其他内容):

System.out.println(path.toPortableString());

You can find more information on the selection service here: Using the Selection Service . 您可以在此处找到有关选择服务的更多信息: 使用选择服务

The code would be like: 代码如下:

IWorkbenchWindow window =
    PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = window.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");

You view an example in an Action like this LuaFileWizardAction class . 您可以在像这个LuaFileWizardAction类的Action中查看示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM