001 /*
002 * $Id: CompositeAction.java,v 1.3 2005/10/26 14:29:55 kleopatra Exp $
003 *
004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020 */
021 package org.jdesktop.swingx.action;
022
023 import java.awt.event.ActionEvent;
024 import java.awt.event.ItemEvent;
025 import java.util.ArrayList;
026 import java.util.Iterator;
027 import java.util.List;
028
029 import javax.swing.Action;
030 import javax.swing.Icon;
031
032 /**
033 * A class that represents an action which will fire a sequence of actions.
034 * The action ids are added to the internal list. When this action is invoked,
035 * the event will be dispatched to the actions in the internal list.
036 * <p>
037 * The action ids are represented by the value of the <code>Action.ACTION_COMMAND_KEY</code>
038 * and must be managed by the <code>ActionManager</code>. When this action is
039 * invoked, then the actions are retrieved from the ActionManager in list order
040 * and invoked.
041 *
042 * @see ActionManager
043 * @author Mark Davidson
044 */
045 public class CompositeAction extends AbstractActionExt {
046
047 /**
048 * Keys for storing extended action attributes. May make public.
049 */
050 private static final String LIST_IDS = "action-list-ids";
051
052 public CompositeAction() {
053 this("CompositeAction");
054 }
055
056 public CompositeAction(String name) {
057 super(name);
058 }
059
060 /**
061 * @param name display name of the action
062 * @param command the value of the action command key
063 */
064 public CompositeAction(String name, String command) {
065 super(name, command);
066 }
067
068 public CompositeAction(String name, Icon icon) {
069 super(name, icon);
070 }
071
072 /**
073 * @param name display name of the action
074 * @param command the value of the action command key
075 * @param icon icon to display
076 */
077 public CompositeAction(String name, String command, Icon icon) {
078 super(name, command, icon);
079 }
080
081 /**
082 * Add an action id to the action list. This action will be invoked
083 * when this composite action is invoked.
084 */
085 public void addAction(String id) {
086 List list = (List)getValue(LIST_IDS);
087 if (list == null) {
088 list = new ArrayList();
089 putValue(LIST_IDS, list);
090 }
091 list.add(id);
092 }
093
094 /**
095 * Returns a list of action ids which indicates that this is a composite
096 * action.
097 * @return a valid list of action ids or null
098 */
099 public List getActionIDs() {
100 return (List)getValue(LIST_IDS);
101 }
102
103 /**
104 * Callback for composite actions. This method will redispatch the
105 * ActionEvent to all the actions held in the list.
106 */
107 public void actionPerformed(ActionEvent evt) {
108 ActionManager manager = ActionManager.getInstance();
109
110 Iterator iter = getActionIDs().iterator();
111 while (iter.hasNext()) {
112 String id = (String)iter.next();
113 Action action = manager.getAction(id);
114 if (action != null) {
115 action.actionPerformed(evt);
116 }
117 }
118 }
119
120 /**
121 * Callback for toggle actions.
122 */
123 public void itemStateChanged(ItemEvent evt) {
124 ActionManager manager = ActionManager.getInstance();
125
126 Iterator iter = getActionIDs().iterator();
127 while (iter.hasNext()) {
128 String id = (String)iter.next();
129 Action action = manager.getAction(id);
130 if (action != null && action instanceof AbstractActionExt) {
131 ((AbstractActionExt)action).itemStateChanged(evt);
132 }
133 }
134 }
135 }