|
Toggle Commands and Groups
Toggle commands are a simple extension of regular commands that provide the notion of selection. The buttons and menu items produced by toggle commands are rendered as checkboxes for individual commands or radio buttons when a number of commands are combined into an exclusive group. The following shows a very simple toggle command implementation.
public class MyToggleCommand
extends ToggleCommand
{
public MyToggleCommand
{
super("toggle.one");
}
public void handleSelection(boolean selected) throws ToggleVetoException
{
if (selected && isNotDark())
throw new ToggleVetoException("Light should only be used when dark");
light.setOn(selected)
}
}
Most the work of the toggle command is managed behind the scenes, but the command specified selection behavior is delegated to the handleSelection method. If selection request can't be honored, the method may throw a ToggleVetoException, this allows toggle commands to refuse both selection and de selection. This behavior is respected by exclusive groups and a change in selection state will only be made if the currently selected command relinquishes selection.
Toggle Groups
Toggle groups are an extension of standard groups that allow you to control the collective behavior of the toggle commands it contains . The following show typical a toggle group that ensures that only one toggle is selected at any given time.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE commands SYSTEM "commands.dtd">
<commands version="1.1.0">
<toggle-group id="my.toggle.group" exclusive="true">
<face>
<text mnemonic="g">My Group</text>
<icon type="classpath">images/my-group-icon.gif</icon>
</face>
<members>
<member command-id="toggle.one"/>
<member command-id="toggle.two"/>
<member command-id="toggle.three"/>
</members>
</toggle-group>
</commands>
Toggle commands can only belong to one toggle group at any given time, attempting to add them to a second will raise a runtime exception. While it is possible to add a toggle command to a toggle group and one or more regular groups at once, the typical practice would be to add the whole toggle group as an "inline" member of any other groups. The following is an example.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE commands SYSTEM "commands.dtd">
<commands version="1.1.0">
<group id="my.group">
<face>
<text mnemonic="g">My Group</text>
</face>
<members>
<member command-id="command.one"/>
<member command-id="command.two"/>
<separator/>
<member command-id="my.toggle.group" inline="true"/>
</members>
</group>
</commands>
|