Class ActionManager

java.lang.Object
javax.swing.ActionMap
org.jdesktop.swingx.action.ActionManager
All Implemented Interfaces:
Serializable

public class ActionManager extends 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:
  • Field Details

    • INSTANCE

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

    • 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 Details

    • getInstance

      public static ActionManager getInstance()
      Return the instance of the ActionManger. If this has not been explicity set then it will be created.
      Returns:
      the ActionManager instance.
      See Also:
    • setInstance

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

      public Set<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 Action addAction(Action action)
    • addAction

      public Action addAction(Object id, 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 Action getAction(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(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(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(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(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(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(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(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(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(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(PrintStream stream, Action action)
      A diagnostic which prints the Attributes of an action on the printStream
    • registerCallback

      public void registerCallback(Object id, Object handler, 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:
    • isStateAction

      public boolean isStateAction(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(Object id)
      Test to determine if the action is a TargetableAction
    • isBoundAction

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

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

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