Class ActionManager

  • All Implemented Interfaces:
    java.io.Serializable

    public class ActionManager
    extends javax.swing.ActionMap
    The ActionManager manages sets of javax.swing.Actions for an application. There are convenience methods for getting and setting the state of the action. All of these elements have a unique id tag which is used by the ActionManager to reference the action. This id maps to the Action.ACTION_COMMAND_KEY on the Action.

    The ActionManager may be used to conveniently register callback methods on BoundActions.

    A typical use case of the ActionManager is:

       ActionManager manager = ActionManager.getInstance();
    
       // load Actions
       manager.addAction(action);
    
       // Change the state of the action:
       manager.setEnabled("new-action", newState);
     
    The ActionManager also supports Actions that can have a selected state associated with them. These Actions are typically represented by a JCheckBox or similar widget. For such actions the registered method is invoked with an additional parameter indicating the selected state of the widget. For example, for the callback handler:

      public class Handler {
          public void stateChanged(boolean newState);
       }
     
    The registration method would look similar:
      manager.registerCallback("select-action", new Handler(), "stateChanged");
     

    The stateChanged method would be invoked as the selected state of the widget changed. Additionally if you need to change the selected state of the Action use the ActionManager method setSelected.

    The ActionContainerFactory uses the managed Actions in a ActionManager to create user interface components. It uses the shared instance of ActionManager by default. For example, to create a JMenu based on an action-list id:

     ActionContainerFactory factory = new ActionContainerFactory();
     JMenu file = factory.createMenu(list);
     
    See Also:
    ActionContainerFactory, TargetableAction, BoundAction, Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static ActionManager INSTANCE
      Shared instance of the singleton ActionManager.
    • Constructor Summary

      Constructors 
      Constructor Description
      ActionManager()
      Creates the action manager.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      javax.swing.Action addAction​(java.lang.Object id, javax.swing.Action action)
      Adds an action to the ActionManager
      javax.swing.Action addAction​(javax.swing.Action action)  
      javax.swing.Action getAction​(java.lang.Object id)
      Retrieves the action corresponding to an action id.
      java.util.Set<java.lang.Object> getActionIDs()
      Returns the ids for all the managed actions.
      BoundAction getBoundAction​(java.lang.Object id)
      Convenience method for returning the BoundAction
      CompositeAction getCompositeAction​(java.lang.Object id)
      Convenience method for returning the CompositeAction
      static ActionManager getInstance()
      Return the instance of the ActionManger.
      ServerAction getServerAction​(java.lang.Object id)
      Convenience method for returning the ServerAction
      private AbstractActionExt getStateChangeAction​(java.lang.Object id)
      Convenience method for returning the StateChangeAction
      TargetableAction getTargetableAction​(java.lang.Object id)
      Convenience method for returning the TargetableAction
      boolean isBoundAction​(java.lang.Object id)
      Test to determine if the action is a BoundAction
      boolean isCompositeAction​(java.lang.Object id)
      Test to determine if the action is a BoundAction
      boolean isEnabled​(java.lang.Object id)
      Returns the enabled state of the Action.
      boolean isSelected​(java.lang.Object id)
      Gets the selected state of a toggle action.
      boolean isServerAction​(java.lang.Object id)
      Test to determine if the action is a ServerAction
      boolean isStateAction​(java.lang.Object id)
      Determines if the Action corresponding to the action id is a state changed action (toggle, group type action).
      boolean isTargetableAction​(java.lang.Object id)
      Test to determine if the action is a TargetableAction
      (package private) static void printAction​(java.io.PrintStream stream, javax.swing.Action action)
      A diagnostic which prints the Attributes of an action on the printStream
      void registerCallback​(java.lang.Object id, java.lang.Object handler, java.lang.String method)
      Convenience method to register a callback method on a BoundAction
      void setEnabled​(java.lang.Object id, boolean enabled)
      Enables or disables the state of the Action corresponding to the action id.
      static void setInstance​(ActionManager manager)
      Sets the ActionManager instance.
      void setSelected​(java.lang.Object id, boolean selected)
      Sets the selected state of a toggle action.
      • Methods inherited from class javax.swing.ActionMap

        allKeys, clear, get, getParent, keys, put, remove, setParent, size
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • INSTANCE

        private static ActionManager INSTANCE
        Shared instance of the singleton ActionManager.
    • Constructor Detail

      • ActionManager

        public ActionManager()
        Creates the action manager. Use this constuctor if the application should support many ActionManagers. Otherwise, using the getInstance method will return a singleton.
    • Method Detail

      • setInstance

        public static void setInstance​(ActionManager manager)
        Sets the ActionManager instance.
      • getActionIDs

        public java.util.Set<java.lang.Object> getActionIDs()
        Returns the ids for all the managed actions.

        An action id is a unique idenitfier which can be used to retrieve the corrspondng Action from the ActionManager. This identifier can also be used to set the properties of the action through the action manager like setting the state of the enabled or selected flags.

        Returns:
        a set which represents all the action ids
      • addAction

        public javax.swing.Action addAction​(javax.swing.Action action)
      • addAction

        public javax.swing.Action addAction​(java.lang.Object id,
                                            javax.swing.Action action)
        Adds an action to the ActionManager
        Parameters:
        id - value of the action id - which is value of the ACTION_COMMAND_KEY
        action - Action to be managed
        Returns:
        the action that was added
      • getAction

        public javax.swing.Action getAction​(java.lang.Object id)
        Retrieves the action corresponding to an action id.
        Parameters:
        id - value of the action id
        Returns:
        an Action or null if id
      • getTargetableAction

        public TargetableAction getTargetableAction​(java.lang.Object id)
        Convenience method for returning the TargetableAction
        Parameters:
        id - value of the action id
        Returns:
        the TargetableAction referenced by the named id or null
      • getBoundAction

        public BoundAction getBoundAction​(java.lang.Object id)
        Convenience method for returning the BoundAction
        Parameters:
        id - value of the action id
        Returns:
        the TargetableAction referenced by the named id or null
      • getServerAction

        public ServerAction getServerAction​(java.lang.Object id)
        Convenience method for returning the ServerAction
        Parameters:
        id - value of the action id
        Returns:
        the TargetableAction referenced by the named id or null
      • getCompositeAction

        public CompositeAction getCompositeAction​(java.lang.Object id)
        Convenience method for returning the CompositeAction
        Parameters:
        id - value of the action id
        Returns:
        the TargetableAction referenced by the named id or null
      • getStateChangeAction

        private AbstractActionExt getStateChangeAction​(java.lang.Object id)
        Convenience method for returning the StateChangeAction
        Parameters:
        id - value of the action id
        Returns:
        the StateChangeAction referenced by the named id or null
      • setEnabled

        public void setEnabled​(java.lang.Object id,
                               boolean enabled)
        Enables or disables the state of the Action corresponding to the action id. This method should be used by application developers to ensure that all components created from an action remain in synch with respect to their enabled state.
        Parameters:
        id - value of the action id
        enabled - true if the action is to be enabled; otherwise false
      • isEnabled

        public boolean isEnabled​(java.lang.Object id)
        Returns the enabled state of the Action. When enabled, any component associated with this object is active and able to fire this object's actionPerformed method.
        Parameters:
        id - value of the action id
        Returns:
        true if this Action is enabled; false if the action doesn't exist or disabled.
      • setSelected

        public void setSelected​(java.lang.Object id,
                                boolean selected)
        Sets the selected state of a toggle action. If the id doesn't correspond to a toggle action then it will fail silently.
        Parameters:
        id - the value of the action id
        selected - true if the action is to be selected; otherwise false.
      • isSelected

        public boolean isSelected​(java.lang.Object id)
        Gets the selected state of a toggle action. If the id doesn't correspond to a toggle action then it will fail silently.
        Parameters:
        id - the value of the action id
        Returns:
        true if the action is selected; false if the action doesn't exist or is disabled.
      • printAction

        static void printAction​(java.io.PrintStream stream,
                                javax.swing.Action action)
        A diagnostic which prints the Attributes of an action on the printStream
      • registerCallback

        public void registerCallback​(java.lang.Object id,
                                     java.lang.Object handler,
                                     java.lang.String method)
        Convenience method to register a callback method on a BoundAction
        Parameters:
        id - value of the action id - which is the value of the ACTION_COMMAND_KEY
        handler - the object which will be perform the action
        method - the name of the method on the handler which will be called.
        See Also:
        BoundAction.registerCallback(java.lang.Object, java.lang.String)
      • isStateAction

        public boolean isStateAction​(java.lang.Object id)
        Determines if the Action corresponding to the action id is a state changed action (toggle, group type action).
        Parameters:
        id - value of the action id
        Returns:
        true if the action id represents a multi state action; false otherwise
      • isTargetableAction

        public boolean isTargetableAction​(java.lang.Object id)
        Test to determine if the action is a TargetableAction
      • isBoundAction

        public boolean isBoundAction​(java.lang.Object id)
        Test to determine if the action is a BoundAction
      • isCompositeAction

        public boolean isCompositeAction​(java.lang.Object id)
        Test to determine if the action is a BoundAction
      • isServerAction

        public boolean isServerAction​(java.lang.Object id)
        Test to determine if the action is a ServerAction