2

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 looking for some way to retrieve an IFile from the selection so I can execute my action over the file.

Thank you,

Leonardo Marques
  • 3,721
  • 7
  • 36
  • 50
  • 4
    possible duplicate of [How to get the selected node in the package explorer from an Eclipse plugin](http://stackoverflow.com/questions/585802/how-to-get-the-selected-node-in-the-package-explorer-from-an-eclipse-plugin) – Fabian Steeg Sep 19 '10 at 21:32
  • 1
    Please mark as answered, so it doesn't shows up as Unanswered, thanks – Ramiro González Maciel May 07 '12 at 22:10

1 Answers1

0

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
    }
}
António Almeida
  • 9,620
  • 8
  • 59
  • 66