ActionManager.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:13k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: ActionManager.java,v 1.4 2005/10/13 08:59:55 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.action;
  22. import java.io.PrintStream;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import javax.swing.AbstractAction;
  27. import javax.swing.Action;
  28. /**
  29.  * The ActionManager manages sets of <code>javax.swing.Action</code>s for an
  30.  * application. There are convenience methods for getting and setting the state
  31.  * of the action.
  32.  * All of these elements have a unique id tag which is used by the ActionManager
  33.  * to reference the action. This id maps to the <code>Action.ACTION_COMMAND_KEY</code>
  34.  * on the Action.
  35.  * <p>
  36.  * The ActionManager may be used to conveniently register callback methods
  37.  * on BoundActions.
  38.  * <p>
  39.  * A typical use case of the ActionManager is:
  40.  * <p>
  41.  *  <pre>
  42.  *   ActionManager manager = ActionManager.getInstance();
  43.  *
  44.  *   // load Actions
  45.  *   manager.addAction(action);
  46.  *
  47.  *   // Change the state of the action:
  48.  *   manager.setEnabled("new-action", newState);
  49.  * </pre>
  50.  *
  51.  * The ActionManager also supports Actions that can have a selected state
  52.  * associated with them. These Actions are typically represented by a
  53.  * JCheckBox or similar widget. For such actions the registered method is
  54.  * invoked with an additional parameter indicating the selected state of
  55.  * the widget. For example, for the callback handler:
  56.  *<p>
  57.  * <pre>
  58.  *  public class Handler {
  59.  *      public void stateChanged(boolean newState);
  60.  *   }
  61.  * </pre>
  62.  * The registration method would look similar:
  63.  * <pre>
  64.  *  manager.registerCallback("select-action", new Handler(), "stateChanged");
  65.  * </pre>
  66.  *<p>
  67.  * The stateChanged method would be invoked as the selected state of
  68.  * the widget changed. Additionally if you need to change the selected
  69.  * state of the Action use the ActionManager method <code>setSelected</code>.
  70.  * <p>
  71.  * The <code>ActionContainerFactory</code> uses the managed Actions in the
  72.  * ActionManager to create
  73.  * user interface components. For example, to create a JMenu based on an
  74.  * action-list id:
  75.  * <pre>
  76.  * JMenu file = manager.getFactory().createMenu(list);
  77.  * </pre>
  78.  *
  79.  * @see ActionContainerFactory
  80.  * @see TargetableAction
  81.  * @see BoundAction
  82.  * @author Mark Davidson
  83.  */
  84. public class ActionManager {
  85.     // Internal data structures which manage the actions.
  86.     // key: value of ID_ATTR, value instanceof AbstractAction
  87.     private Map actionMap;
  88.     // Container factory instance for this ActionManager
  89.     private ActionContainerFactory factory;
  90.     /**
  91.      * Shared instance of the singleton ActionManager.
  92.      */
  93.     private static ActionManager INSTANCE;
  94.     // To enable debugging:
  95.     //   Pass -Ddebug=true to the vm on start up.
  96.     // or
  97.     //   set System.setProperty("debug", "true"); before constructing this Object
  98.     private static boolean DEBUG = false;
  99.     /**
  100.      * Creates the action manager. Use this constuctor if the application should
  101.      * support many ActionManagers. Otherwise, using the getInstance method will
  102.      * return a singleton.
  103.      */
  104.     public ActionManager() {
  105.     }
  106.     /**
  107.      * Return the Action Container Factory associated with this ActionManager
  108.      * instance. Will always return a factory instance.
  109.      */
  110.     public ActionContainerFactory getFactory() {
  111.         if (factory == null) {
  112.             factory = new ActionContainerFactory(this);
  113.         }
  114.         return factory;
  115.     }
  116.     /**
  117.      * This method should be used to associate a subclassed ActionContainerFactory
  118.      * with this ActionManager.
  119.      */
  120.     public void setFactory(ActionContainerFactory factory) {
  121.         this.factory = factory;
  122.     }
  123.     /**
  124.      * Return the instance of the ActionManger. If this has not been explicity
  125.      * set then it will be created.
  126.      *
  127.      * @return the ActionManager instance.
  128.      * @see #setInstance
  129.      */
  130.     public static ActionManager getInstance() {
  131.         if (INSTANCE == null) {
  132.             INSTANCE = new ActionManager();
  133.         }
  134.         return INSTANCE;
  135.     }
  136.     /**
  137.      * Sets the ActionManager instance.
  138.      */
  139.     public static void setInstance(ActionManager manager) {
  140.         INSTANCE = manager;
  141.     }
  142.     /**
  143.      * Returns the ids for all the managed actions.
  144.      * <p>
  145.      * An action id is a unique idenitfier which can
  146.      * be used to retrieve the corrspondng Action from the ActionManager.
  147.      * This identifier can also
  148.      * be used to set the properties of the action through the action
  149.      * manager like setting the state of the enabled or selected flags.
  150.      *
  151.      * @return a set which represents all the action ids
  152.      */
  153.     public Set getActionIDs() {
  154.         if (actionMap == null) {
  155.             return null;
  156.         }
  157.         return actionMap.keySet();
  158.     }
  159.     public Action addAction(Action action) {
  160.         return addAction(action.getValue(Action.ACTION_COMMAND_KEY), action);
  161.     }
  162.     /**
  163.      * Adds an action to the ActionManager
  164.      * @param id value of the action id - which is value of the ACTION_COMMAND_KEY
  165.      * @param action Action to be managed
  166.      * @return the action that was added
  167.      */
  168.     public Action addAction(Object id, Action action)  {
  169.         if (actionMap == null) {
  170.             actionMap = new HashMap();
  171.         }
  172.         actionMap.put(id, action);
  173.         return action;
  174.     }
  175.     /**
  176.      * Retrieves the action corresponding to an action id.
  177.      *
  178.      * @param id value of the action id
  179.      * @return an Action or null if id
  180.      */
  181.     public Action getAction(Object id)  {
  182.         if (actionMap != null) {
  183.             return (Action)actionMap.get(id);
  184.         }
  185.         return null;
  186.     }
  187.     /**
  188.      * Convenience method for returning the TargetableAction
  189.      *
  190.      * @param id value of the action id
  191.      * @return the TargetableAction referenced by the named id or null
  192.      */
  193.     public TargetableAction getTargetableAction(Object id) {
  194.         Action a = getAction(id);
  195.         if (a instanceof TargetableAction) {
  196.             return (TargetableAction)a;
  197.         }
  198.         return null;
  199.     }
  200.     /**
  201.      * Convenience method for returning the BoundAction
  202.      *
  203.      * @param id value of the action id
  204.      * @return the TargetableAction referenced by the named id or null
  205.      */
  206.     public BoundAction getBoundAction(Object id) {
  207.         Action a = getAction(id);
  208.         if (a instanceof BoundAction) {
  209.             return (BoundAction)a;
  210.         }
  211.         return null;
  212.     }
  213.     /**
  214.      * Convenience method for returning the ServerAction
  215.      *
  216.      * @param id value of the action id
  217.      * @return the TargetableAction referenced by the named id or null
  218.      */
  219.     public ServerAction getServerAction(Object id) {
  220.         Action a = getAction(id);
  221.         if (a instanceof ServerAction) {
  222.             return (ServerAction)a;
  223.         }
  224.         return null;
  225.     }
  226.     /**
  227.      * Convenience method for returning the CompositeAction
  228.      *
  229.      * @param id value of the action id
  230.      * @return the TargetableAction referenced by the named id or null
  231.      */
  232.     public CompositeAction getCompositeAction(Object id) {
  233.         Action a = getAction(id);
  234.         if (a instanceof CompositeAction) {
  235.             return (CompositeAction)a;
  236.         }
  237.         return null;
  238.     }
  239.     /**
  240.      * Convenience method for returning the StateChangeAction
  241.      *
  242.      * @param id value of the action id
  243.      * @return the StateChangeAction referenced by the named id or null
  244.      */
  245.     private AbstractActionExt getStateChangeAction(Object id) {
  246.         Action a = getAction(id);
  247.         if (a != null && a instanceof AbstractActionExt) {
  248.             AbstractActionExt aa = (AbstractActionExt)a;
  249.             if (aa.isStateAction()) {
  250.                 return aa;
  251.             }
  252.         }
  253.         return null;
  254.     }
  255.     /**
  256.      * Enables or disables the state of the Action corresponding to the
  257.      * action id. This method should be used
  258.      * by application developers to ensure that all components created from an
  259.      * action remain in synch with respect to their enabled state.
  260.      *
  261.      * @param id value of the action id
  262.      * @param enabled true if the action is to be enabled; otherwise false
  263.      */
  264.     public void setEnabled(Object id, boolean enabled) {
  265.         Action action = getAction(id);
  266.         if (action != null) {
  267.             action.setEnabled(enabled);
  268.         }
  269.     }
  270.     /**
  271.      * Returns the enabled state of the <code>Action</code>. When enabled,
  272.      * any component associated with this object is active and
  273.      * able to fire this object's <code>actionPerformed</code> method.
  274.      *
  275.      * @param id value of the action id
  276.      * @return true if this <code>Action</code> is enabled; false if the
  277.      *         action doesn't exist or disabled.
  278.      */
  279.     public boolean isEnabled(Object id) {
  280.         Action action = getAction(id);
  281.         if (action != null) {
  282.             return action.isEnabled();
  283.         }
  284.         return false;
  285.     }
  286.     /**
  287.      * Sets the selected state of a toggle action. If the id doesn't
  288.      * correspond to a toggle action then it will fail silently.
  289.      *
  290.      * @param id the value of the action id
  291.      * @param selected true if the action is to be selected; otherwise false.
  292.      */
  293.     public void setSelected(Object id, boolean selected) {
  294.         AbstractActionExt action = getStateChangeAction(id);
  295.         if (action != null) {
  296.             action.setSelected(selected);
  297.         }
  298.     }
  299.     /**
  300.      * Gets the selected state of a toggle action. If the id doesn't
  301.      * correspond to a toggle action then it will fail silently.
  302.      *
  303.      * @param id the value of the action id
  304.      * @return true if the action is selected; false if the action
  305.      *         doesn't exist or is disabled.
  306.      */
  307.     public boolean isSelected(Object id) {
  308.         AbstractActionExt action = getStateChangeAction(id);
  309.         if (action != null) {
  310.             return action.isSelected();
  311.         }
  312.         return false;
  313.     }
  314.     /**
  315.      * A diagnostic which prints the Attributes of an action
  316.      * on the printStream
  317.      */
  318.     static void printAction(PrintStream stream, Action action) {
  319.         stream.println("Attributes for " + action.getValue(Action.ACTION_COMMAND_KEY));
  320.         if (action instanceof AbstractAction) {
  321.             Object[] keys = ((AbstractAction)action).getKeys();
  322.             for (int i = 0; i < keys.length; i++) {
  323.                 stream.println("tkey: " + keys[i] + "tvalue: " +
  324.                                action.getValue((String)keys[i]));
  325.             }
  326.         }
  327.     }
  328.     /**
  329.      * Convenience method to register a callback method on a <code>BoundAction</code>
  330.      *
  331.      * @see BoundAction#registerCallback
  332.      * @param id value of the action id - which is the value of the ACTION_COMMAND_KEY
  333.      * @param handler the object which will be perform the action
  334.      * @param method the name of the method on the handler which will be called.
  335.      */
  336.     public void registerCallback(Object id, Object handler, String method) {
  337.         BoundAction action = getBoundAction(id);
  338.         if (action != null) {
  339.             action.registerCallback(handler, method);
  340.         }
  341.     }
  342.     //
  343.     // Convenience methods for determining the type of action.
  344.     //
  345.     /**
  346.      * Determines if the Action corresponding to the action id is a state changed
  347.      * action (toggle, group type action).
  348.      *
  349.      * @param id value of the action id
  350.      * @return true if the action id represents a multi state action; false otherwise
  351.      */
  352.     public boolean isStateAction(Object id) {
  353.         Action action = getAction(id);
  354.         if (action != null && action instanceof AbstractActionExt) {
  355.             return ((AbstractActionExt)action).isStateAction();
  356.         }
  357.         return false;
  358.     }
  359.     /**
  360.      * Test to determine if the action is a <code>TargetableAction</code>
  361.      */
  362.     public boolean isTargetableAction(Object id) {
  363.         return (getTargetableAction(id) != null);
  364.     }
  365.     /**
  366.      * Test to determine if the action is a <code>BoundAction</code>
  367.      */
  368.     public boolean isBoundAction(Object id) {
  369.         return (getBoundAction(id) != null);
  370.     }
  371.     /**
  372.      * Test to determine if the action is a <code>BoundAction</code>
  373.      */
  374.     public boolean isCompositeAction(Object id) {
  375.         return (getCompositeAction(id) != null);
  376.     }
  377.     /**
  378.      * Test to determine if the action is a <code>ServerAction</code>
  379.      */
  380.     public boolean isServerAction(Object id) {
  381.         return (getServerAction(id) != null);
  382.     }
  383. }