如何使用命令访问 Eclipse RCP 中的右键单击文件?

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

我在 Eclipse RCP 应用程序的上下文菜单中实现了一个条目。该函数应该以另一种格式导出右键单击的文件。我已经实现了转换功能。

我需要的是右键文件的路径和名称。这是我的:

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

所以基本上我想知道如何访问右键单击的文件,特别是路径和名称。

在您的处理程序中获取当前选择并将其调整为 IFile 具有:

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
 }