001    /*
002     * $Id: CompositeAction.java 3132 2008-12-05 14:34:58Z kschaefe $
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        @SuppressWarnings("unchecked")
086        public void addAction(String id) {
087            List<String> list = (List<String>) getValue(LIST_IDS);
088            if (list == null) {
089                list = new ArrayList<String>();
090                putValue(LIST_IDS, list);
091            }
092            list.add(id);
093        }
094    
095        /**
096         * Returns a list of action ids which indicates that this is a composite
097         * action. 
098         * @return a valid list of action ids or null
099         */
100        @SuppressWarnings("unchecked")
101        public List<String> getActionIDs() {
102            return (List<String>) getValue(LIST_IDS);
103        }    
104    
105        /**
106         * Callback for composite actions. This method will redispatch the 
107         * ActionEvent to all the actions held in the list.
108         */
109        public void actionPerformed(ActionEvent evt) {
110            ActionManager manager = ActionManager.getInstance();
111                
112            Iterator<String> iter = getActionIDs().iterator();
113            while (iter.hasNext()) {
114                String id = iter.next();
115                Action action = manager.getAction(id);
116                if (action != null) {
117                action.actionPerformed(evt);
118                }
119            }
120        }
121    
122        /**
123         * Callback for toggle actions.
124         */
125        public void itemStateChanged(ItemEvent evt) {
126            ActionManager manager = ActionManager.getInstance();
127                
128            Iterator<String> iter = getActionIDs().iterator();
129            while (iter.hasNext()) {
130                String id = iter.next();
131                Action action = manager.getAction(id);
132                if (action != null && action instanceof AbstractActionExt) {
133                ((AbstractActionExt)action).itemStateChanged(evt);
134                }
135            }
136        }
137    }