Execute command/handler of an existing plugin from a different plugin

假装没事ソ 提交于 2021-02-07 19:56:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!