John
John

Reputation: 815

How to access right-clicked file in Eclipse RCP using commands?

I implemented an entry in the context menu of my Eclipse RCP application. The function should export the right-clicked file in another format. I already implemented the transformation-function.

What I need, is the path and name of the right-clicked file. This is what I have:

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    Shell shell = new Shell();
    DirectoryDialog dialog = new DirectoryDialog(shell);
    String saveToPath = dialog.open();

    String filePath = // ... how to access the clicked file?

    exportOtherFormat(filePath, saveToPath);
    return null;
}

So basically I would like to know, how I can access the right-clicked file, specially the path and name.

Upvotes: 0

Views: 27

Answers (1)

greg-449
greg-449

Reputation: 111217

Get the current selection in your handler and adapt it to an IFile with:

ISelection sel = HandlerUtil.getCurrentSelection(event);

if (sel instanceof IStructuredSelection)
 {
   Object selObj = ((IStructuredSelection)sel).getFirstObject();

   IFile file = (IFile)Platform.getAdapterManager().getAdapter(selObj, IFile.class);

   // TODO your code
 }

Upvotes: 1

Related Questions