如何从Eclipse插件中获取包资源管理器中的选定节点 [英] How to get the selected node in the package explorer from an Eclipse plugin

查看:151
本文介绍了如何从Eclipse插件中获取包资源管理器中的选定节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Eclipse命令插件,并希望在包资源管理器视图中检索当前选定的节点。我想要从返回的结果中获取绝对文件路径,其中所选节点驻留在文件系统(即c:\eclipse\test.html)上。



我该如何做?

解决方案

第一步是获得选择服务,例如从这样的任何视图或编辑器:

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

或者,

然后,获取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()); 

您可以在这里找到有关选择服务的更多信息:使用选择服务


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:

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.

Then, get the selection for the Package Explorer and cast it to an IStructuredSelection:

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

From that, you can get your selected IFile:

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

Now to get the full path, you will have to get the location for the 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.

这篇关于如何从Eclipse插件中获取包资源管理器中的选定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆