21.01.2009
Toggle Handler for Eclipse Commands
Based on Moritz Posts blog post Toggling a Command contribution I built a base class for a command handler which automatically handles the toggle state. To use it add the base class to your project:
Add a state element to your command contribution:
<command id="somecommand" name="SomeCommand">
<state class="org.eclipse.jface.commands.ToggleState" id="STYLE"/>
</command>
Contribute your handler using the style="toggle" attribute:
<extension point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:de.ralfebert.somemenu">
<command commandId="somecommand" style="toggle"/>
</menuContribution>
</extension>
And use ToggleHandler to implement your Handler class:
public class SomeCommandHandler extends ToggleHandler {
@Override
protected void executeToggle(ExecutionEvent event, boolean newState) {
System.out.println(newState);
}
}
That’s everything required to get toggling commands:



I think this functionality has been added to HandlerUtil recently. Please also see how the toggling handler can drive other contributions:
http://philondev.blogspot.com/2009/12/toggle-commands-toggle-other.html