001/*
002 * $Id: CompositeAction.java 3972 2011-03-17 20:31: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 */
021package org.jdesktop.swingx.action;
022
023import java.awt.event.ActionEvent;
024import java.awt.event.ItemEvent;
025import java.util.ArrayList;
026import java.util.Iterator;
027import java.util.List;
028
029import javax.swing.Action;
030import 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 */
045public 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    @Override
110    public void actionPerformed(ActionEvent evt) {
111        ActionManager manager = ActionManager.getInstance();
112            
113        Iterator<String> iter = getActionIDs().iterator();
114        while (iter.hasNext()) {
115            String id = iter.next();
116            Action action = manager.getAction(id);
117            if (action != null) {
118            action.actionPerformed(evt);
119            }
120        }
121    }
122
123    /**
124     * Callback for toggle actions.
125     */
126    @Override
127    public void itemStateChanged(ItemEvent evt) {
128        ActionManager manager = ActionManager.getInstance();
129            
130        Iterator<String> iter = getActionIDs().iterator();
131        while (iter.hasNext()) {
132            String id = iter.next();
133            Action action = manager.getAction(id);
134            if (action != null && action instanceof AbstractActionExt) {
135            ((AbstractActionExt)action).itemStateChanged(evt);
136            }
137        }
138    }
139}