1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui.action;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.swing.Action;
25 import javax.swing.JPopupMenu;
26 import org.xnap.commons.gui.Builder;
27
28 /***
29 *
30 * @author Steffen Pingel
31 */
32 public class ActionCollection {
33
34 List<Action> actions = new ArrayList<Action>();
35
36 public ActionCollection()
37 {
38 }
39
40 public void add(Action action)
41 {
42 actions.add(action);
43 }
44
45 public void addSeparator()
46 {
47 actions.add(null);
48 }
49
50 public JPopupMenu createPopupMenu()
51 {
52 JPopupMenu menu = new JPopupMenu();
53 for (Action action : actions) {
54 if (action != null) {
55 menu.add(Builder.createMenuItem(action));
56 }
57 else {
58 menu.addSeparator();
59 }
60 }
61 return menu;
62 }
63
64 public Action[] getActions()
65 {
66 List<Action> result = new ArrayList<Action>(actions.size());
67 for (Action action : result) {
68 if (action != null) {
69 result.add(action);
70 }
71 }
72 return result.toArray(new Action[0]);
73 }
74
75 public void setSelectedItems(Object[] items)
76 {
77 for (Action action : actions) {
78 if (action instanceof SelectionAction) {
79 ((SelectionAction)action).setSelectedItems(items);
80 }
81 }
82 }
83
84 public boolean isEmpty()
85 {
86 return actions.isEmpty();
87 }
88
89 }