1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.menu;
21
22 import java.awt.Component;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25
26 import javax.swing.Action;
27 import javax.swing.JLabel;
28 import javax.swing.JMenu;
29 import javax.swing.event.PopupMenuEvent;
30 import javax.swing.event.PopupMenuListener;
31
32 import org.xnap.gui.util.IconHelper;
33
34 public abstract class AbstractDynamicMenu extends JMenu
35 {
36
37
38
39
40
41 private LinkedList temporaries = new LinkedList();
42
43
44
45 public AbstractDynamicMenu(String name)
46 {
47 super(name);
48
49 initialize();
50 }
51
52 public AbstractDynamicMenu(Action action)
53 {
54 super(action);
55
56 String filename = (String)action.getValue(IconHelper.XNAP_ICON);
57 setIcon(IconHelper.getMenuIcon(filename));
58
59 initialize();
60 }
61
62
63
64 private void initialize()
65 {
66 getPopupMenu().addPopupMenuListener(new PopupListener());
67 }
68
69 /***
70 * Adds <code>c</code>.
71 *
72 * @see #removeAllTemporaries()
73 */
74 public void addTemporary(Component c)
75 {
76 add(c);
77 temporaries.add(c);
78 }
79
80 public void addTemporary(String s)
81 {
82 addTemporary(new JLabel(" " + s));
83 }
84
85 /***
86 * Adds <code>c</code>.
87 *
88 * @see #removeAllTemporaries()
89 */
90 public void addTemporary(Component c, int index)
91 {
92 add(c, index);
93 temporaries.add(c);
94 }
95
96 /***
97 * Removes all components that were added by
98 * {@link #addTemporary(Component)}.
99 */
100 public void removeAllTemporaries()
101 {
102 for (Iterator i = temporaries.iterator(); i.hasNext();) {
103 remove((Component)i.next());
104 }
105 temporaries.clear();
106 }
107
108 protected abstract void willBecomeVisible();
109
110
111
112 protected class PopupListener implements PopupMenuListener {
113
114 public void popupMenuCanceled(PopupMenuEvent e)
115 {
116 }
117
118 public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
119 {
120 }
121
122 public void popupMenuWillBecomeVisible(PopupMenuEvent e)
123 {
124 willBecomeVisible();
125 }
126 }
127
128 }