How to inject parameter with ParameterizedCommand in Eclipse 4.3?

大憨熊 提交于 2020-01-13 12:06:57

问题


I am currently implementing an Eclipse 4.3 application and running into a problem. I try to parametrize a command to delete specific files. My approach is corresponding to Getting parameter of parameterized command in Eclipse RCP 4.2, but i somehow don't get it working right.

In my Application.e4xmi I have added a command with a parameter:

<commands xmi:id="_K1MVgDGKEeOO8o2ChqdHMA" elementId="first.application.command.deleteproject" commandName="deleteProjectCommand">
<parameters xmi:id="_Hr4FEDGTEeOO8o2ChqdHMA" elementId="cmd0" name="cmd0" typeId="" optional="false"/>
</commands>

At one point in my code I create the command, set the parameter, and execute it:

Map<String, String> parameters = new HashMap<String, String>();
parameters.put("cmd0", "test");
final Command command =commandService.getCommand("first.application.command.deleteproject");
final ParameterizedCommand pcmd = ParameterizedCommand.generateCommand(command, parameters);
pcmd.executeWithChecks(null, null);

I have a handler that is linked with the command, that has the following execute method:

@Execute
public void execute(@Optional @Named("cmd0") String file) {
  System.out.println("delete project " + file);
}

Everything works fine, only the file is not injected, it stays null. When I check the pcmd variable before I execute it, it tells me that it has set the parameters correctly to {cmd0=test} (using System.out.println(pcmd.getParameterMap());). When I remove @Optional, the execute method is not called at all.

Somewhere the parameter cmd0 is lost. Where is the mistake in my code?

Thanks!


回答1:


Just found the solution. Executing with pcmd.executeWithChecks(null, null); seems not to work as expected. Instead, we need the EHandlerService that we inject:

@Inject
private EHandlerService handlerService;

And now we execute the command with the service like this:

handlerService.executeHandler(pcmd);

Voila!

I hope this can help someone, too.



来源:https://stackoverflow.com/questions/19293503/how-to-inject-parameter-with-parameterizedcommand-in-eclipse-4-3

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