org.pietschy.command
Class ToggleCommand

java.lang.Object
  extended by org.pietschy.command.Command
      extended by org.pietschy.command.ActionCommand
          extended by org.pietschy.command.ToggleCommand
All Implemented Interfaces:
ActionCommandExecutor
Direct Known 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(CommandManager commandManager)
          Creates a new anonymous toggle command bound to the specified CommandManager.defaultInstance().
ToggleCommand(CommandManager commandManager, java.lang.String commandId)
          Creates a new toggle command with the specified Id that is bound to the specified CommandManager.
ToggleCommand(java.lang.String commandId)
          Creates a new toggle command with the specified Id that is bound to CommandManager.defaultInstance.
 
Method Summary
 void addNotify(CommandGroup parent)
          Invoked whenever a command is added to a group.
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(javax.swing.AbstractButton button)
          Overrides the default Command.configureButtonStates(javax.swing.AbstractButton) to include the configuration of the buttons selected state.
 javax.swing.AbstractButton createButton(ButtonFactory factory, java.lang.String faceId)
          Creates a toggle button for this command using the specified ButtonFactory and Face.
 javax.swing.AbstractButton createCheckBox()
          Creates a checkbox for this command using the default button factory and the button face.
 javax.swing.AbstractButton createCheckBox(ButtonFactory factory)
          Creates a checkbox for this command using the specifed button factory and the button face.
 javax.swing.AbstractButton createCheckBox(ButtonFactory factory, java.lang.String faceId)
          Creates a checkbox for this command using the specified ButtonFactory and Face.
 javax.swing.AbstractButton createCheckBox(java.lang.String faceName)
          Creates a checkbox for this command using the default button factory and the specified face.
 javax.swing.JMenuItem createMenuItem(MenuFactory factory, java.lang.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)
          Invoked whenever a command is remvoved from a group.
 void requestDefautIn(javax.swing.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, detach, execute, execute, getActionAdapter, getActionAdapter, getActionCommand, getActionEvent, getHint, getHint, getHints, getInvoker, getInvokerWindow, getModifiers, installShortCut, installShortCut, postExecute, preExecute, putHint, putHints, removeCommandListener, removeInterceptor, setActionCommand, uninstallShortCut, uninstallShortCut
 
Methods inherited from class org.pietschy.command.Command
addFace, addHoverListener, addNewFace, addPropertyChangeListener, addPropertyChangeListener, buttonIterator, configureButtonAppearance, configureButtonAppearances, configureButtonAppearances, createButton, createButton, createButton, createMenuItem, createMenuItem, createMenuItem, 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, removePropertyChangeListener, removePropertyChangeListener, requestFocusIn, setAccelerator, setButtonFactory, setDescription, setEnabled, setIcon, setLongDescription, setMenuFactory, setMnemonic, setMnemonicIndex, setSelectedIcon, setText, setTextPosition, setToolbarFactory, setVisible, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.pietschy.command.ActionCommandExecutor
addPropertyChangeListener, addPropertyChangeListener, isEnabled, removePropertyChangeListener, removePropertyChangeListener
 

Constructor Detail

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(java.lang.String commandId)
Creates a new toggle command with the specified Id that is bound to CommandManager.defaultInstance.

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,
                     java.lang.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 Detail

configureButtonStates

protected void configureButtonStates(javax.swing.AbstractButton button)
Overrides the default Command.configureButtonStates(javax.swing.AbstractButton) to include the configuration of the buttons selected state.

Overrides:
configureButtonStates in class ActionCommand
Parameters:
button - the button to initialize.

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.

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).

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.

isSelected

public boolean isSelected()

handleExecute

protected void handleExecute()
This method is called whenever the Command is executed. If this command is a member of and exclusive group, then the selection request is delegated to the group by calling ToggleGroupController.handleSelectionRequest(org.pietschy.command.ToggleCommand, boolean), otherwise setSelected(boolean) is called with the value of !isSelected().

Specified by:
handleExecute in class 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.
Throws:
ToggleVetoException

createMenuItem

public javax.swing.JMenuItem createMenuItem(MenuFactory factory,
                                            java.lang.String faceId)
Creates a new JCheckBoxMenuItem that is bound to this command.

Overrides:
createMenuItem in class Command
Parameters:
factory -
faceId -
Returns:
a new JCheckBoxMenuItem for this command.

createButton

public javax.swing.AbstractButton createButton(ButtonFactory factory,
                                               java.lang.String faceId)
Creates a toggle button for this command using the specified ButtonFactory and Face.

Overrides:
createButton in class Command
Returns:
a new JToggleButton for this command.

createCheckBox

public javax.swing.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 javax.swing.AbstractButton createCheckBox(java.lang.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 javax.swing.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 javax.swing.AbstractButton createCheckBox(ButtonFactory factory,
                                                 java.lang.String faceId)
Creates a checkbox for this command using the specified ButtonFactory and Face.

Returns:
a new JCheckBox for this command.

requestDefautIn

public void requestDefautIn(javax.swing.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 class ActionCommand
Parameters:
container - the root pane container to check.

addNotify

public void addNotify(CommandGroup parent)
Description copied from class: Command
Invoked whenever a command is added to a group. Please note that Commands may belong to more than one group at any given time. This method does nothing by default, subclasses may override to perform special configuration as required.

Overrides:
addNotify in class Command
Parameters:
parent - the parent to which the command was added.

removeNotify

public void removeNotify(CommandGroup parent)
Description copied from class: Command
Invoked whenever a command is remvoved from a group. Please note that Commands may belong to more than one group at any given time. This method does nothing by default, subclasses may override to perform special configuration as required.

Overrides:
removeNotify in class Command
Parameters:
parent - the parent from which the command was removed.