Class AbstractToggleCommand

  • All Implemented Interfaces:
    java.awt.event.ActionListener, java.io.Serializable, java.lang.Cloneable, java.util.EventListener, javax.swing.Action

    public abstract class AbstractToggleCommand
    extends AbstractCommand
    An abstract base class for commands that toggle a state when selected. Note that whether a toggle command is selected or not is determined by the value of the action's SELECTED_KEY key. It is up to the subclass to set this key to an appropriate initial value. (The isSelected() method is simply a convenience that reads the value of this key and casts the result to a boolean value.)
    Since:
    3.0
    Author:
    Chris Jennings
    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from class javax.swing.AbstractAction

        changeSupport, enabled
      • Fields inherited from interface javax.swing.Action

        ACCELERATOR_KEY, ACTION_COMMAND_KEY, DEFAULT, DISPLAYED_MNEMONIC_INDEX_KEY, LARGE_ICON_KEY, LONG_DESCRIPTION, MNEMONIC_KEY, NAME, SELECTED_KEY, SHORT_DESCRIPTION, SMALL_ICON
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractToggleCommand()
      Creates a command with no predefined name or icon.
      AbstractToggleCommand​(java.lang.String nameKey)
      Defines a command using a name determined by the specified string key.
      AbstractToggleCommand​(java.lang.String nameKey, java.lang.String iconResource)
      Defines a command using a name determined by the specified string key and an icon loaded from the given icon resource.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isSelected()
      Returns true if the command is selected, that is, if the toggle state is currently enabled.
      void setSelected​(boolean selected)
      Sets whether the command is selected.
      void toggle()
      If the command is enabled, toggles the selected state of the command by generating a fake action event.
      • Methods inherited from class javax.swing.AbstractAction

        addPropertyChangeListener, clone, firePropertyChange, getKeys, getPropertyChangeListeners, getValue, isEnabled, putValue, removePropertyChangeListener, setEnabled
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface javax.swing.Action

        accept
      • Methods inherited from interface java.awt.event.ActionListener

        actionPerformed
    • Constructor Detail

      • AbstractToggleCommand

        public AbstractToggleCommand()
        Creates a command with no predefined name or icon.
      • AbstractToggleCommand

        public AbstractToggleCommand​(java.lang.String nameKey)
        Defines a command using a name determined by the specified string key.
        Parameters:
        nameKey - the localized text key for the command name
        See Also:
        AbstractCommand.setNameKey(java.lang.String)
      • AbstractToggleCommand

        public AbstractToggleCommand​(java.lang.String nameKey,
                                     java.lang.String iconResource)
        Defines a command using a name determined by the specified string key and an icon loaded from the given icon resource.
        Parameters:
        nameKey - the localized text key for the command name
        iconResource - the resource file to load an icon from
        See Also:
        AbstractCommand.setNameKey(java.lang.String), AbstractCommand.setIcon(javax.swing.Icon)
    • Method Detail

      • isSelected

        public final boolean isSelected()
        Returns true if the command is selected, that is, if the toggle state is currently enabled.
        Returns:
        true if the command is selected
      • setSelected

        public final void setSelected​(boolean selected)
        Sets whether the command is selected. This should be used to set the command's initial state. It does not cause the command to be performed.
        Parameters:
        selected - if true, the command will be selected
      • toggle

        public final void toggle()
        If the command is enabled, toggles the selected state of the command by generating a fake action event.