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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: CompositeAction.java,v 1.2 2005/10/10 18:02:40 rbair 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.awt.event.ActionEvent;
  23. import java.awt.event.ItemEvent;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import javax.swing.Action;
  28. import javax.swing.Icon;
  29. /**
  30.  * A class that represents an action which will fire a sequence of actions.
  31.  * The action ids are added to the internal list. When this action is invoked,
  32.  * the event will be dispatched to the actions in the internal list.
  33.  * <p>
  34.  * The action ids are represented by the value of the <code>Action.ACTION_COMMAND_KEY</code>
  35.  * and must be managed by the <code>ActionManager</code>. When this action is 
  36.  * invoked, then the actions are retrieved from the ActionManager in list order
  37.  * and invoked.
  38.  * 
  39.  * @see ActionManager
  40.  * @author Mark Davidson
  41.  */
  42. public class CompositeAction extends AbstractActionExt {
  43.      /**
  44.      * Keys for storing extended action attributes. May make public.
  45.      */
  46.     private static final String LIST_IDS = "action-list-ids";
  47.     public CompositeAction() {
  48. this("CompositeAction");
  49.     }
  50.     public CompositeAction(String name) {
  51. super(name);
  52.     }
  53.     /**
  54.      * @param name display name of the action
  55.      * @param command the value of the action command key
  56.      */
  57.     public CompositeAction(String name, String command) {
  58. super(name, command);
  59.     }
  60.     public CompositeAction(String name, Icon icon) {
  61. super(name, icon);
  62.     }
  63.     /**
  64.      * @param name display name of the action
  65.      * @param command the value of the action command key
  66.      * @param icon icon to display
  67.      */
  68.     public CompositeAction(String name, String command, Icon icon) {
  69. super(name, command, icon);
  70.     }
  71.     /**
  72.      * Add an action id to the action list. This action will be invoked 
  73.      * when this composite action is invoked.
  74.      */
  75.     public void addAction(String id) {
  76. List list = (List)getValue(LIST_IDS);
  77. if (list == null) {
  78.     list = new ArrayList();
  79.     putValue(LIST_IDS, list);
  80. }
  81. list.add(id);
  82.     }
  83.     /**
  84.      * Returns a list of action ids which indicates that this is a composite
  85.      * action. 
  86.      * @return a valid list of action ids or null
  87.      */
  88.     public List getActionIDs() {
  89. return (List)getValue(LIST_IDS);
  90.     }
  91.     /**
  92.      * Callback for composite actions. This method will redispatch the 
  93.      * ActionEvent to all the actions held in the list.
  94.      */
  95.     public void actionPerformed(ActionEvent evt) {
  96. ActionManager manager = ActionManager.getInstance();
  97.     
  98. Iterator iter = getActionIDs().iterator();
  99. while (iter.hasNext()) {
  100.     String id = (String)iter.next();
  101.     Action action = manager.getAction(id);
  102.     if (action != null) {
  103. action.actionPerformed(evt);
  104.     }
  105. }
  106.     }
  107.     /**
  108.      * Callback for toggle actions.
  109.      */
  110.     public void itemStateChanged(ItemEvent evt) {
  111. ActionManager manager = ActionManager.getInstance();
  112.     
  113. Iterator iter = getActionIDs().iterator();
  114. while (iter.hasNext()) {
  115.     String id = (String)iter.next();
  116.     Action action = manager.getAction(id);
  117.     if (action != null && action instanceof AbstractActionExt) {
  118. ((AbstractActionExt)action).itemStateChanged(evt);
  119.     }
  120. }
  121.     }
  122. }