http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/
帖子中描述了在使用到toggle状态切换时候的用法,发现帖子中描述的不够全面,有一些遗漏的地方。
1、如果要初始按下去,注意初始默认值org.eclipse.ui.handlers.RegistryToggleState:true 确保为true
<command id="cn.com.ywave.fibermon.ui.handlers.CmdVpFilter"
name="Filter">
<state class="org.eclipse.ui.handlers.RegistryToggleState:true"
id="fibermon.vpwarning.filter.state">
</state>
</command>
2,handler要实现IElementUpdater
直接使用在plugin中定义好的stateid(fibermon.vpwarning.filter.state) 不要使用comments中的IMenuStateIds.STYLE
IStateListener 是状态改变时候的监听,提供了handleStateChange方法,可以在此方法中实现状态切换时候的处理。
public class HandlerVpFilter extends AbstractHandler implements IHandler, IStateListener, IElementUpdater {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
Command allShowCommand = event.getCommand();
State allState = allShowCommand.getState("fibermon.vpwarning.filter.state");
allState.setValue(!Boolean.parseBoolean(String.valueOf(allState.getValue())));
ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
service.refreshElements(event.getCommand().getId(), null);
return null;
}
@Override
public void handleStateChange(State state, Object oldValue) {
GlobalDataContainer.inst().getFilterDisableWarningDM().setValue((Boolean) state.getValue());
}
@Override
public void updateElement(UIElement element, Map parameters) {
ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = commandService.getCommand("cn.com.ywave.fibermon.ui.handlers.CmdVpFilter");
element.setChecked((Boolean) command.getState("fibermon.vpwarning.filter.state").getValue());
}