【问题标题】:Unable to open multiple selected files and folders using Eclipse ShowInSystemExplorerHandler API无法使用 Eclipse ShowInSystemExplorerHandler API 打开多个选定的文件和文件夹
【发布时间】:2020-11-02 01:09:56
【问题描述】:

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

在代码下方查找。

@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 命令处理程序仅支持打开单个路径。
  • 先生,如果我选择一个文件或文件夹,它可以工作。但是如果我选择多个文件或文件夹,它不会打开。顺便说一句,我正在使用structuredSelection.forEach( selctionElement -> { ...} 进行迭代并尝试打开文件夹。

标签: eclipse eclipse-plugin swt eclipse-rcp jface


【解决方案1】:

ShowInSystemExplorerHandler 命令处理程序的实现结果相当奇怪。尽管您可以将要打开的资源作为参数传递(您正在这样做),但它仍然会查看当前选择的项目数以确定处理程序是否已启用,并且仅在仅选择一项时启用。

如果您调试代码,您将看到 handlerService.canExecute(parameterizedCommand) 返回 false,因为选择了多个项目。

所以看起来你不能直接使用它来选择多个项目。

您可以做的是定义自己的命令和处理程序来调用相同的代码。比如:

   <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>

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

【讨论】:

  • 是的,我已经看到了。先生,有什么方法可以让我每次都激活处理程序以便它执行吗?
  • 您必须安排每次调用处理程序时只选择一项。
  • 我添加了一个解决方法,使用不同的命令和处理程序调用相同的代码
  • 让我试试先生,我会回来找你的。
  • 先生,我试过了,即使我在一个项目中选择了多个文件夹,它也只打开一个文件夹。
猜你喜欢
  • 1970-01-01
  • 2018-03-02
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 2012-10-13
  • 2018-08-12
相关资源
最近更新 更多