简体   繁体   中英

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.

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
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM