org.pietschy.command

Class ToggleCommand

Implemented Interfaces:
ActionCommandExecutor
Known Direct Subclasses:
EnableCommandGroup.EnableCommand, PageSelectCommand, SimpleToggle, UndoableToggleCommand, VisibleCommandGroup.VisibleCommand

public abstract class ToggleCommand
extends ActionCommand

The toggle command class implements a command that has a selected state. Each execution of the command will toggle the selected state of the command. Subclasses should override the handleSelection(boolean) method.

Field Summary

Fields inherited from class org.pietschy.command.ActionCommand

HINT_ACTION_EVENT, HINT_INVOKER, HINT_INVOKER_WINDOW, HINT_MODIFIERS

Fields inherited from class org.pietschy.command.Command

internalLog, listenerList, pcs

Constructor Summary

ToggleCommand()
Creates a new anonymous ToggleCommand.
ToggleCommand(String commandId)
Creates a new toggle command with the specified Id that is bound to CommandManager.defaultInstance.
ToggleCommand(CommandManager commandManager)
Creates a new anonymous toggle command bound to the specified CommandManager.defaultInstance().
ToggleCommand(CommandManager commandManager, String commandId)
Creates a new toggle command with the specified Id that is bound to the specified CommandManager.

Method Summary

void
addNotify(CommandGroup parent)
protected void
applySelection(boolean selected)
Applies the selected state to the toggle and updates all its buttons.
protected void
attemptSelection(boolean selected)
Attempts to set the selected state of the command.
protected void
configureButtonStates(AbstractButton button)
Overrides the default Command.configureButtonStates(AbstractButton) to include the configuration of the buttons selected state.
AbstractButton
createButton(ButtonFactory factory, String faceId)
Creates a toggle button for this command using the specified ButtonFactory and Face.
AbstractButton
createCheckBox()
Creates a checkbox for this command using the default button factory and the button face.
AbstractButton
createCheckBox(String faceName)
Creates a checkbox for this command using the default button factory and the specified face.
AbstractButton
createCheckBox(ButtonFactory factory)
Creates a checkbox for this command using the specifed button factory and the button face.
AbstractButton
createCheckBox(ButtonFactory factory, String faceId)
Creates a checkbox for this command using the specified ButtonFactory and Face.
JMenuItem
createMenuItem(MenuFactory factory, String faceId)
Creates a new JCheckBoxMenuItem that is bound to this command.
protected void
handleExecute()
This method is called whenever the Command is executed.
protected abstract void
handleSelection(boolean selected)
Entry for subclasses to handle the selection process.
boolean
isSelected()
void
removeNotify(CommandGroup parent)
void
requestDefautIn(RootPaneContainer container)
Warning this method does nothing as toggle commands are implmented using JToggleButtons and default buttons must be instances of JButton.
void
setSelected(boolean selected)
Attempts to set the selected state of the command.

Methods inherited from class org.pietschy.command.ActionCommand

addCommandListener, addInterceptor, areEqual, attach, configureButtonStates, detach, execute, execute, getActionAdapter, getActionAdapter, getActionCommand, getActionEvent, getHint, getHint, getHints, getInvoker, getInvokerWindow, getModifiers, handleExecute, installShortCut, installShortCut, postExecute, preExecute, putHint, putHints, removeCommandListener, removeInterceptor, requestDefautIn, setActionCommand, uninstallShortCut, uninstallShortCut

Methods inherited from class org.pietschy.command.Command

addFace, addHoverListener, addNewFace, addNotify, addPropertyChangeListener, addPropertyChangeListener, attach, buttonIterator, configureButtonAppearance, configureButtonAppearances, configureButtonAppearances, configureButtonStates, createButton, createButton, createButton, createButton, createMenuItem, createMenuItem, createMenuItem, createMenuItem, detach, export, faceExists, fireHoverEnded, fireHoverStarted, getAccelerator, getAlternativeFaceNames, getButtonFactory, getButtonIn, getCommandManager, getDefaultFace, getDefaultFace, getDescription, getFace, getFace, getIcon, getId, getLongDescription, getMenuFactory, getMnemonic, getMnemonicIndex, getProperty, getProperty, getPropertyNames, getSelectedIcon, getText, getTextPosition, getToolbarFactory, initCommandManager, installFace, isAnonymous, isAttachedTo, isEnabled, isVisible, putProperty, removeHoverListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, requestFocusIn, setAccelerator, setButtonFactory, setDescription, setEnabled, setIcon, setLongDescription, setMenuFactory, setMnemonic, setMnemonicIndex, setSelectedIcon, setText, setTextPosition, setToolbarFactory, setVisible, toString

Constructor Details

ToggleCommand

public ToggleCommand()
Creates a new anonymous ToggleCommand. Anonymous commands can't must be fully programatically generated and can't be exported to command containers.

ToggleCommand

public ToggleCommand(String commandId)
Parameters:
commandId - the id of the command.

ToggleCommand

public ToggleCommand(CommandManager commandManager)
Creates a new anonymous toggle command bound to the specified CommandManager.defaultInstance().

ToggleCommand

public ToggleCommand(CommandManager commandManager,
                     String commandId)
Creates a new toggle command with the specified Id that is bound to the specified CommandManager.
Parameters:
commandManager - the CommandManager to which the command belongs.
commandId - the id of the command.

Method Details

addNotify

public void addNotify(CommandGroup parent)
Overrides:
addNotify in interface Command

applySelection

protected void applySelection(boolean selected)
Applies the selected state to the toggle and updates all its buttons. This method also fires a property change event if the new selection has changed from the previous state.

This method should only be called by subclasses if the wish to by-pass all of the normal behaviour and explicitly set the state of this toggle (such as when reverting state after an undo request). This method completely ignores any exclusive group membership and will not update the state of any other toggles which may share membership with the command.

Parameters:
selected - the desired selection state of the command.

attemptSelection

protected final void attemptSelection(boolean selected)
            throws ToggleVetoException
Attempts to set the selected state of the command. This method delegats the selection request to handleSelection(boolean) and on return configures all attachements appropriately.
Parameters:
selected - the desired selection state of the command.
Throws:
ToggleVetoException - if a ToggleVetoException is thrown by handleSelection(boolean).

configureButtonStates

protected void configureButtonStates(AbstractButton button)
Overrides the default Command.configureButtonStates(AbstractButton) to include the configuration of the buttons selected state.
Overrides:
configureButtonStates in interface ActionCommand
Parameters:
button - the button to initialize.

createButton

public AbstractButton createButton(ButtonFactory factory,
                                   String faceId)
Creates a toggle button for this command using the specified ButtonFactory and Face.
Overrides:
createButton in interface Command
Returns:
a new JToggleButton for this command.

createCheckBox

public AbstractButton createCheckBox()
Creates a checkbox for this command using the default button factory and the button face.
Returns:
a new JCheckBox for this command.

createCheckBox

public AbstractButton createCheckBox(String faceName)
Creates a checkbox for this command using the default button factory and the specified face.
Returns:
a new JCheckBox for this command.

createCheckBox

public AbstractButton createCheckBox(ButtonFactory factory)
Creates a checkbox for this command using the specifed button factory and the button face.
Returns:
a new JCheckBox for this command.

createCheckBox

public AbstractButton createCheckBox(ButtonFactory factory,
                                     String faceId)
Creates a checkbox for this command using the specified ButtonFactory and Face.
Returns:
a new JCheckBox for this command.

createMenuItem

public JMenuItem createMenuItem(MenuFactory factory,
                                String faceId)
Creates a new JCheckBoxMenuItem that is bound to this command.
Overrides:
createMenuItem in interface Command
Parameters:
factory -
faceId -
Returns:
a new JCheckBoxMenuItem for this command.

handleExecute

protected void handleExecute()
Overrides:
handleExecute in interface ActionCommand

handleSelection

protected abstract void handleSelection(boolean selected)
            throws ToggleVetoException
Entry for subclasses to handle the selection process. When a request to change the selection is made, this method will be called.

To deny the selection request, subclassed must throw a ToggleVetoException.

Please note that the current state of isSelected() will not be updated until after this method has been called and so should not be used in this method.

Parameters:
selected - the requested selection state.

isSelected

public boolean isSelected()

removeNotify

public void removeNotify(CommandGroup parent)
Overrides:
removeNotify in interface Command

requestDefautIn

public void requestDefautIn(RootPaneContainer container)
Warning this method does nothing as toggle commands are implmented using JToggleButtons and default buttons must be instances of JButton.
Overrides:
requestDefautIn in interface ActionCommand

setSelected

public final void setSelected(boolean selected)
Attempts to set the selected state of the command. This method delegats the selection request to handleSelection(boolean) and configures all attachements appropriately based on the return value.

It isn't guarenteed that the final state of the command will be the value specified. The actual final state will be determined by handleSelection(boolean).

Parameters:
selected - true if the command is being selected, false if it is being deselected.