Eclipse Plugin: Creating a dynamic menu and corresponding handler?

好久不见. 提交于 2019-12-04 14:46:51

I finally found the answer to this myself. Within plugin.xml it is possible to specify parameters for each command, e.g.

<commandParameter
    id="commandParameterID"
    name="Name of the Parameter"
    optional="false">
</commandParameter>

Now, when I am dynamically creating each menu item I can just add my parameter to the parameters Map of the CommandContributionItemParameter object.

CommandContributionItemParameter param = new CommandContributionItemParameter(PlatformUI.getWorkbench(), null, "CommandID", CommandContributionItem.STYLE_PUSH);
param.parameters = new HashMap<String, String>();
param.parameters.put("commandParameterID", "TheValue");

The parameters that are added this way are accessible in the handler class as follows:

public Object execute(ExecutionEvent event) throws ExecutionException {
    System.out.println(event.getParameter("commandParameterID"));
    return null;
}

Possibly this simple menu creator helps you a step further (or the surrounding classes within the project underlying the link) or maybe the plugin.xml of the popup menu within the same project

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