Thursday, September 10, 2015

Eclipse RCP : How to check state(toggle) of a menu items or toolbar items inside handlers

In Eclipse RCP there are at-least two different ways we  can check the state of a menu item or  a toolbar item inside a handlers execute method :

Option 1 :


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());



Option 2:


   @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
   
   
        Event selEvent = (Event) event.getTrigger();
        Widget widget =  selEvent.widget;
        if (widget instanceof ToolItem) {        
        isSelected = ((ToolItem)widget).getSelection();
}else {
isSelected = ((MenuItem)widget).getSelection();
}
}


No comments:

Post a Comment

5 Strategies for Getting More Work Done in Less Time

Summary.    You’ve got more to do than could possibly get done with your current work style. You’ve prioritized. You’ve planned. You’ve dele...