KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > actions > CompositeAction


1 /*
2  * $Id: CompositeAction.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 /**
9  * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
10  *
11  * Redistribution and use in source and binary forms, with or
12  * without modification, are permitted provided that the following
13  * conditions are met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistribution in binary form must reproduce the above
19  * copyright notice, this list of conditions and the following
20  * disclaimer in the documentation and/or other materials
21  * provided with the distribution.
22  *
23  * Neither the name of Sun Microsystems, Inc. or the names of
24  * contributors may be used to endorse or promote products derived
25  * from this software without specific prior written permission.
26  *
27  * This software is provided "AS IS," without a warranty of any
28  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
29  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
31  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
32  * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
33  * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
34  * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
35  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
36  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
37  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
38  * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
39  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
40  *
41  * You acknowledge that this software is not designed, licensed or
42  * intended for use in the design, construction, operation or
43  * maintenance of any nuclear facility.
44  *
45  */

46 package org.jdesktop.swing.actions;
47
48 import java.awt.event.ActionEvent JavaDoc;
49 import java.awt.event.ItemEvent JavaDoc;
50
51 import java.util.ArrayList JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54
55 import javax.swing.Action JavaDoc;
56 import javax.swing.Icon JavaDoc;
57
58 import org.jdesktop.swing.Application;
59
60 /**
61  * A class that represents an action which will fire a sequence of actions.
62  * The action ids are added to the internal list. When this action is invoked,
63  * the event will be dispatched to the actions in the internal list.
64  * <p>
65  * The action ids are represented by the value of the <code>Action.ACTION_COMMAND_KEY</code>
66  * and must be managed by the <code>ActionManager</code>. When this action is
67  * invoked, then the actions are retrieved from the ActionManager in list order
68  * and invoked.
69  *
70  * @see ActionManager
71  * @author Mark Davidson
72  */

73 public class CompositeAction extends AbstractActionExt {
74
75      /**
76      * Keys for storing extended action attributes. May make public.
77      */

78     private static final String JavaDoc LIST_IDS = "action-list-ids";
79
80     public CompositeAction() {
81     this("CompositeAction");
82     }
83
84     public CompositeAction(String JavaDoc name) {
85     super(name);
86     }
87
88     /**
89      * @param name display name of the action
90      * @param command the value of the action command key
91      */

92     public CompositeAction(String JavaDoc name, String JavaDoc command) {
93     super(name, command);
94     }
95
96     public CompositeAction(String JavaDoc name, Icon JavaDoc icon) {
97     super(name, icon);
98     }
99
100     /**
101      * @param name display name of the action
102      * @param command the value of the action command key
103      * @param icon icon to display
104      */

105     public CompositeAction(String JavaDoc name, String JavaDoc command, Icon JavaDoc icon) {
106     super(name, command, icon);
107     }
108
109     /**
110      * Add an action id to the action list. This action will be invoked
111      * when this composite action is invoked.
112      */

113     public void addAction(String JavaDoc id) {
114     List JavaDoc list = (List JavaDoc)getValue(LIST_IDS);
115     if (list == null) {
116         list = new ArrayList JavaDoc();
117         putValue(LIST_IDS, list);
118     }
119     list.add(id);
120     }
121
122     /**
123      * Returns a list of action ids which indicates that this is a composite
124      * action.
125      * @return a valid list of action ids or null
126      */

127     public List JavaDoc getActionIDs() {
128     return (List JavaDoc)getValue(LIST_IDS);
129     }
130
131     /**
132      * Callback for composite actions. This method will redispatch the
133      * ActionEvent to all the actions held in the list.
134      */

135     public void actionPerformed(ActionEvent JavaDoc evt) {
136     ActionManager manager = Application.getInstance().getActionManager();
137         
138     Iterator JavaDoc iter = getActionIDs().iterator();
139     while (iter.hasNext()) {
140         String JavaDoc id = (String JavaDoc)iter.next();
141         Action JavaDoc action = manager.getAction(id);
142         if (action != null) {
143         action.actionPerformed(evt);
144         }
145     }
146     }
147
148     /**
149      * Callback for toggle actions.
150      */

151     public void itemStateChanged(ItemEvent JavaDoc evt) {
152     ActionManager manager = Application.getInstance().getActionManager();
153         
154     Iterator JavaDoc iter = getActionIDs().iterator();
155     while (iter.hasNext()) {
156         String JavaDoc id = (String JavaDoc)iter.next();
157         Action JavaDoc action = manager.getAction(id);
158         if (action != null && action instanceof AbstractActionExt) {
159         ((AbstractActionExt)action).itemStateChanged(evt);
160         }
161     }
162     }
163 }
164
Popular Tags