问题
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.
Find below the code.
@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;
}
}
回答1:
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.
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>
Then replace ShowInSystemExplorerHandler.ID
in the code with the id of your command (my.showInSystemExplorer
in the example).
来源:https://stackoverflow.com/questions/62865684/unable-to-open-multiple-selected-files-and-folders-using-eclipse-showinsystemexp