Daniel
Daniel

Reputation: 24815

Toggling the state of a menu item

I have an Eclipse RCP app I'm working on. It has some view-specific menus and one of the menu items is an item which I would like to display a tick next to when the corresponding functionality is enabled. Similarly, the next time the item is selected, the item should become unticked to reflect that the corresponding functionality is disabled.

My question is this: how do I set the toggle state of these menu items? I have an IHandler to deal with the event when the menu item is selected but I'm unsure how to update the GUI element itself.

Does the StackOverflow community have any thoughts on how I might solve this?

Upvotes: 3

Views: 3035

Answers (2)

Don Kirkby
Don Kirkby

Reputation: 56240

Maybe things have changed, maybe there's a difference between RCP and an Eclipse plug-in, but I just set the style of my action to toggle, and it handled the toggling automatically. You can see the source code on github.com.

     <action
           class="live_py.EnableAction"
           id="live-py.enable.action"
           label="Li&amp;ve Coding"
           menubarPath="pyGeneralMenu/pyNavigateGroup"
           state="false"
           style="toggle">

To see whether the feature is currently enabled, I just look up the action by its id, and then call isChecked().

Upvotes: 1

Daniel
Daniel

Reputation: 24815

The solution involves having the command handler implement the IElementUpdater interface. The UI element can then be updated as so:

public void updateElement(UIElement element, Map parameters) 
{
    element.setChecked(isSelected);     
}

updateElement is called as part of a UI refresh which can be invoked from the handler's execute command as so:

     ICommandService service = (ICommandService) HandlerUtil
       .getActiveWorkbenchWindowChecked(event).getService(
           ICommandService.class);
   service.refreshElements(event.getCommand().getId(), null);

Lots more info here (see Radio Button Command and Update checked state entries)

Upvotes: 1

Related Questions