问题
I have an RCP application with an existing command and handler to switch perspective programmatically. And I also have and new plugin consumed by the existing RCP application. I want this new plugin to execute the command/handler of my RCP application, what can be the possible solution for this?
回答1:
You may need to define a handler for that command (not sure), but executing commands programmatically looks like this:
Command command = ((ICommandService) getSite().getService(ICommandService.class)).getCommand(commandId);
...
final Event trigger = new Event();
ExecutionEvent executionEvent = ((IHandlerService) getSite().getService(IHandlerService.class)).createExecutionEvent(command, trigger);
command.executeWithChecks(executionEvent);
回答2:
There are many ways to execute a command. @Bela presented one - I usually use the following code:
ICommandService commandService = (ICommandService) locationService.getService(ICommandService.class);
IHandlerService hs = (IHandlerService) locationService.getService(IHandlerService.class);
ParameterizedCommand pc = commandService.deserialize("<cmd-id>(<p-id>=<value>)");
hs.executeCommand(pc, null);
The advantage of this method is primary that it allows you to add parameters to a command - e.g. the newWizardId
of the org.eclipse.ui.newWizard
.
来源:https://stackoverflow.com/questions/9666818/execute-command-handler-of-an-existing-plugin-from-a-different-plugin