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