1

如何以编程方式检查作为菜单项贡献的命令是否在处理程序“执行”方法中被选中/未选中(如果是 CHECK BOX 类型)、选中或未选中(如果是 RADIO 按钮类型) 。在此处查看快照https://docs.google.com/file/d/0B3pxBGD-v-ycWVFaeElnSGdyTE0/edit

4

2 回答 2

2

查看此博客:http ://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/

因此,首先确保您的命令具有适当的样式:

<extension point="org.eclipse.ui.menus">
  <menuContribution locationURI="...">
    <command commandId="org.eclipse.example.command.toggle"
              style="toggle" />
  </menuContribution>
</extension>

然后,您可以像这样检查状态:

ICommandService service =(ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = service.getCommand("org.eclipse.example.command.toggle");
State state = command.getState("org.eclipse.example.command.toggleState");
System.out.println(state.getValue());
//state.setValue(!(Boolean) state.getValue());

此外,请考虑查看 org.eclipse.ui.handlers.HandlerUtil,它有时可能会有所帮助。

希望这可以帮助。

于 2013-01-30T10:36:25.450 回答
1

我得到了解决方案,在处理程序执行方法方法中添加了这段代码

public Object execute(ExecutionEvent event) throws ExecutionException {

   Event selEvent = (Event) event.getTrigger();
   MenuItem item = (MenuItem) selEvent.widget;

   System.Out.Println(item.getSelection());     
   return null;
}
于 2013-01-31T07:56:05.900 回答