【问题标题】:How to get selected file in Package Explorer from Eclipse如何从 Eclipse 中获取 Package Explorer 中的选定文件
【发布时间】:2011-04-14 10:12:14
【问题描述】:
我有一个实现 IObjectActionDelegate 的动作。在此操作中,当我选择我的操作时,我想对在包资源管理器中选择的文件执行一些操作。我只有一个 run(IAction action) 方法,并且 ObjectAction 过滤文件,以便该操作仅针对我想要的文件显示。
我正在寻找某种方法从选择中检索 IFile,以便我可以对文件执行我的操作。
谢谢,
【问题讨论】:
标签:
eclipse
interface
eclipse-plugin
【解决方案1】:
这是获取选择的代码。
if (selection instanceof IStructuredSelection) {
IStructuredSelection ssel = (IStructuredSelection) selection;
Object obj = ssel.getFirstElement();
IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj, IFile.class);
if (file == null) {
if (obj instanceof IAdaptable) {
file = (IFile) ((IAdaptable) obj).getAdapter(IFile.class);
}
}
if (file != null) {
//Deal with the file
}
}