无法使用Eclipse ShowInSystemExplorerHandler API打开多个选定的文件和文件夹 [英] Unable to open multiple selected files and folders using Eclipse ShowInSystemExplorerHandler API

查看:186
本文介绍了无法使用Eclipse ShowInSystemExplorerHandler API打开多个选定的文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用Eclipse ShowInSystemExplorerHandler API,如果我选择一个文件或文件夹,则可以正常工作。但是它不适用于文件或文件夹的多重选择。我在下面的代码段中提供。请帮助我解决问题,以便我应该能够在特定于OS的资源管理器中打开多个文件夹/文件。顺便说一下,我正在使用 structuredSelection.forEach 以便打开所有文件和文件夹。

Hi I am using Eclipse ShowInSystemExplorerHandler API, it is working fine if I select a single file or folder. But it does not work for multiple selection of files or folders. I provide below the code snippet. Please help me how to resolve so that I should be able to open multiple folders/files in OS specific explorer. By the way I am using structuredSelection.forEach so that I can open all the files and folders.

在代码下方查找。

@SuppressWarnings("restriction")
public class OpenExplorerHandler extends AbstractHandler {
    
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        ISelectionService service = window.getSelectionService();
        IStructuredSelection structuredSelection = (IStructuredSelection) service.getSelection();

        structuredSelection.forEach( selctionElement -> {
            if (selctionElement instanceof IAdaptable) {
                IResource resource = (IResource) ((IAdaptable) selctionElement).getAdapter(IResource.class);
                File selectedFileFolder = resource.getLocation().toFile();
                String filePath = selectedFileFolder.getAbsolutePath();
                
                ECommandService commandService = PlatformUI.getWorkbench().getService(ECommandService.class);
                EHandlerService handlerService = PlatformUI.getWorkbench().getService(EHandlerService.class);
                Command command = commandService.getCommand(ShowInSystemExplorerHandler.ID);
                
                if (command.isDefined()) {
                    ParameterizedCommand parameterizedCommand = commandService
                        .createCommand(ShowInSystemExplorerHandler.ID, Collections.singletonMap(
                                        ShowInSystemExplorerHandler.RESOURCE_PATH_PARAMETER, filePath));
                    if (handlerService.canExecute(parameterizedCommand)) {
                        handlerService.executeHandler(parameterizedCommand);
                    }
                }
            }
        });
        
        return null;
    }
}


推荐答案

ShowInSystemExplorerHandler 命令处理程序的实现非常奇怪。虽然可以通过资源开作为一个参数(你正在做的),它还是着眼于当前选择,以确定是否处理程序启用的项目数,并且在选择只有一个项目才会启用。

The implementation of the ShowInSystemExplorerHandler command handler turns out to be rather odd. Although you can pass the resource to open as a parameter (which you are doing) it still looks at the number of items currently selected to determine if the handler is enabled and will only be enabled when exactly one item is selected.

如果调试代码,您会看到 handlerService.canExecute(parameterizedCommand)返回false,因为选择了一项以上。

If you debug the code you will see that handlerService.canExecute(parameterizedCommand) is returning false because more that one item is selected.

所以看起来您不能直接在选择了多个项目的情况下使用它。

So it doesn't look like you can use this directly with multiple items selected.

您可以做的是定义自己的调用相同命令和处理程序的命令码。像这样的东西:

What you can do is define your own command and handler that calls the same code. Something like:

   <extension
      point="org.eclipse.ui.commands">
   <command
         categoryId="org.eclipse.ui.category.navigate"
         name="show in explorer"
         id="my.showInSystemExplorer"
         description="Show in Explorer">
      <commandParameter
            id="org.eclipse.ui.ide.showInSystemExplorer.path"
            name="Resource path"
            optional="false">
      </commandParameter>
   </command>
</extension>


 <extension
      point="org.eclipse.ui.handlers">
   <handler
         class="org.eclipse.ui.internal.ide.handlers.ShowInSystemExplorerHandler"
         commandId="my.showInSystemExplorer">
    </handler>
 </extension>

然后将ID中的 ShowInSystemExplorerHandler.ID 替换为您的命令(在示例中为 my.showInSystemExplorer )。

Then replace ShowInSystemExplorerHandler.ID in the code with the id of your command (my.showInSystemExplorer in the example).

这篇关于无法使用Eclipse ShowInSystemExplorerHandler API打开多个选定的文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆