如何在Eclipse 4.3中使用ParameterizedCommand注入参数? [英] How to inject parameter with ParameterizedCommand in Eclipse 4.3?
问题描述
我当前正在实现Eclipse 4.3应用程序并遇到问题。我尝试参数化命令以删除特定文件。我的方法对应于在Eclipse中获取参数化命令的参数RCP 4.2 ,但是我莫名其妙地无法正常工作。
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.
在我的Application.e4xmi中,我添加了带有参数的命令:
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);
}
一切正常,只有文件
未注入,它保持为 null
。当我在执行前检查 pcmd
变量时,它告诉我已将参数正确设置为 {cmd0 = test}
(使用 System.out.println(pcmd.getParameterMap());
)。当我删除 @Optional
时,根本不会调用execute方法。
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.
在某些地方参数 cmd0
丢失。
Somewhere the parameter cmd0
is lost. Where is the mistake in my code?
谢谢!
推荐答案
刚刚找到解决方案。使用 pcmd.executeWithChecks(null,null);
执行似乎无法正常工作。相反,我们需要注入的 EHandlerService
:
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.
这篇关于如何在Eclipse 4.3中使用ParameterizedCommand注入参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!