1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.util;
21
22 import java.awt.event.ActionEvent;
23
24 import java.util.ArrayList;
25 import java.util.Iterator;
26
27 import javax.swing.AbstractAction;
28 import javax.swing.Action;
29 import javax.swing.Icon;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class ActionList extends AbstractAction
53 {
54 private static final long serialVersionUID = 1L;
55
56
57
58
59
60 public static final String SEPARATOR = "_group_separator_";
61
62
63
64
65
66
67
68 public static final String MENU_CONTEXT = "menuContext";
69
70
71 public static final String MENU_CONTEXT_BUTTON = "button";
72
73
74 public static final String MENU_CONTEXT_RADIO = "radio";
75
76
77 public static String ACTIONS = "actions";
78 private ArrayList<Action> actions = null;
79
80
81
82
83 public ActionList()
84 {
85 this(null, null);
86 }
87
88
89
90
91
92
93 public ActionList(String name)
94 {
95 this(name, null);
96 }
97
98
99
100
101
102
103
104 public ActionList(String name, Icon icon)
105 {
106 this(name, icon, null);
107 }
108
109
110
111
112
113
114
115
116 public ActionList(String name, Icon icon, Action[] actions)
117 {
118 super(name, icon);
119 this.actions = new ArrayList<Action>();
120
121 if (actions != null) {
122 for (int i = 0; i < actions.length; i++) {
123 this.actions.add(actions[i]);
124 }
125
126 putValue(ACTIONS, this.actions.toArray(new Action[size()]));
127 }
128
129 putValue(MENU_CONTEXT, MENU_CONTEXT_BUTTON);
130 }
131
132
133
134
135
136
137 public void addAction(Action action)
138 {
139 addAction(size(), action);
140 }
141
142
143
144
145
146
147
148
149 public boolean contains(Action action)
150 {
151 return actions.contains(action);
152 }
153
154
155
156
157
158
159
160
161 public boolean containsActionWithName(String name)
162 {
163 return (firstIndexOfActionWithName(name) < 0);
164 }
165
166
167
168
169
170
171
172
173 public int firstIndexOfActionWithName(String name)
174 {
175 for (Iterator iter = actions.iterator(); iter.hasNext();) {
176 Action element = (Action)iter.next();
177
178 if ((element != null) && element.getValue(NAME).equals(name)) {
179 return actions.indexOf(element);
180 }
181 }
182
183 return -1;
184 }
185
186
187
188
189
190
191
192 public void addAll(ActionList action)
193 {
194 Action[] a = action.toArray();
195
196 for (int i = 0; i < a.length; i++) {
197 addAction(a[i]);
198 }
199 }
200
201
202
203
204 public synchronized void addAction(int index, Action action)
205 {
206 actions.add(index, action);
207 putValue(ACTIONS, actions.toArray(new Action[size()]));
208 }
209
210
211
212
213
214
215
216
217 public synchronized Action getAction(int index)
218 {
219 return (Action)actions.get(index);
220 }
221
222
223
224
225
226
227
228
229
230
231 public synchronized Action getAction(String ID)
232 {
233 Iterator it = actions.iterator();
234
235 while (it.hasNext()) {
236 Action a = (Action)it.next();
237
238 if (ID.equals(a.getValue(ACTION_COMMAND_KEY))) {
239 return a;
240 }
241 }
242
243 return null;
244 }
245
246
247
248
249 public int indexOf(Action action)
250 {
251 return actions.indexOf(action);
252 }
253
254
255
256
257 public void removeAction(Action action)
258 {
259 actions.remove(action);
260 putValue(ACTIONS, actions.toArray(new Action[size()]));
261 }
262
263
264
265
266 public Action removeAction(int index)
267 {
268 Action a = (Action)actions.remove(index);
269 putValue(ACTIONS, actions.toArray(new Action[size()]));
270
271 return a;
272 }
273
274
275
276
277
278
279 public int size()
280 {
281 return actions.size();
282 }
283
284
285
286
287
288 public void actionPerformed(ActionEvent e)
289 {
290 }
291
292
293
294
295
296
297 public Action[] toArray()
298 {
299 return (Action[])actions.toArray(new Action[actions.size()]);
300 }
301
302
303
304
305
306
307 public Iterator iterator()
308 {
309 return actions.iterator();
310 }
311
312
313
314
315
316 public void clear()
317 {
318 actions.clear();
319 }
320 }
321
322