如何从Eclipse中的Package Explorer中获取所选文件

时间:2010-09-19 20:32:44

标签: eclipse interface eclipse-plugin

我有一个实现IObjectActionDelegate的Action。在这个操作中,我想在选择我的Action时在Package Explorer中选择的文件上执行一些操作。我只有一个run(IAction动作)方法,而ObjectAction会过滤文件,以便仅为我想要的文件显示动作。

我正在寻找一些从选择中检索IFile的方法,以便我可以对文件执行操作。

谢谢,

1 个答案:

答案 0 :(得分:0)

以下是获取选择的代码。

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
    }
}