开发者

How to get selected file in Package Explorer from Eclipse

I have an Action that implements IObjectActionDelegate. In this action I want to preform some operations over the file that is selected in Package Explorer when I selected my Action. I only have a run(IAction action) method and the ObjectAction filters the files so that the action only appears for the files I want.

I'm look开发者_运维技巧ing for some way to retrieve an IFile from the selection so I can execute my action over the file.

Thank you,


Here is the code for getting selection.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜