1 package com.explosion.expf.menusandtools.menu;
2
3 import java.util.Enumeration;
4 import java.util.Hashtable;
5
6 import javax.swing.JCheckBoxMenuItem;
7 import javax.swing.JMenu;
8 import javax.swing.JMenuItem;
9 import javax.swing.KeyStroke;
10
11 import com.explosion.expf.Application;
12 import com.explosion.expf.ExpListener;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 /***
35 * @author Stephen Cowx
36 * Created on 05-Mar-2005
37 */
38 public class MenuHelper
39 {
40 private Hashtable elements = new Hashtable();
41 private ExpListener listener = null;
42
43
44 /***
45 * @param listener
46 */
47 public MenuHelper(ExpListener listener)
48 {
49 super();
50 this.listener = listener;
51 }
52 /***
53 * This method adds a JMenu to a MenuBar
54 */
55 public ExpMenu createExpMenu(String menuText, char mnemonic, String actionCommand, int enabledThreshold)
56 {
57
58 ExpMenu menu = new ExpMenu(menuText, actionCommand, enabledThreshold);
59
60
61 menu.setName(actionCommand);
62
63
64 menu.setName(menuText);
65
66
67 if (mnemonic != -1)
68 menu.setMnemonic(mnemonic);
69
70
71 elements.put(actionCommand, menu);
72
73 return menu;
74 }
75
76 /***
77 * This method adds a menuItem to a MenusAndTools
78 */
79 public JMenuItem createMenuItem(String itemText, char mnemonic, String actionCommand, KeyStroke accelerator, int enabledThreshold)
80 {
81
82 ExpMenuItem menuItem = new ExpMenuItem(itemText, actionCommand, enabledThreshold);
83
84
85 if (mnemonic != -1)
86 menuItem.setMnemonic(mnemonic);
87
88
89 menuItem.setActionCommand(actionCommand);
90
91
92 menuItem.setName(itemText);
93
94
95 if (accelerator != null)
96 menuItem.setAccelerator(accelerator);
97
98
99 menuItem.addActionListener(listener);
100
101
102 elements.put(actionCommand, menuItem);
103
104 return menuItem;
105 }
106
107 /***
108 * This method adds a menuItem to a MenusAndTools
109 */
110 public JCheckBoxMenuItem createCheckBoxMenuItem(String itemText, char mnemonic, String actionCommand, KeyStroke accelerator, int enabledThreshold)
111 {
112
113 ExpCheckBoxMenuItem menuItem = new ExpCheckBoxMenuItem(itemText, actionCommand, enabledThreshold);
114
115
116 if (mnemonic != -1)
117 menuItem.setMnemonic(mnemonic);
118
119
120 menuItem.setActionCommand(actionCommand);
121
122
123 if (accelerator != null)
124 menuItem.setAccelerator(accelerator);
125
126
127 menuItem.addActionListener(listener);
128
129
130 elements.put(actionCommand, menuItem);
131
132 return menuItem;
133 }
134
135
136 public JMenu getMenu(String menuName)
137 {
138 return (JMenu) elements.get(menuName);
139 }
140
141 public Object getElement(String elementName)
142 {
143 return elements.get(elementName);
144 }
145
146 /***
147 * This method runs through the list of menu's and their children and asks each of them to enable or diable themselves
148 */
149 public void checkEnabled()
150 {
151 try
152 {
153 Enumeration en = elements.elements();
154 while (en.hasMoreElements())
155 {
156 Object element = en.nextElement();
157 if (element instanceof ExpMenu)
158 ((ExpMenu) element).checkEnabled(Application.getActiveComponentCookies(), Application.getLocalCookies(),Application.getGlobalCookies());
159 else if (element instanceof ExpMenuItem)
160 ((ExpMenuItem) element).checkEnabled(Application.getActiveComponentCookies(), Application.getLocalCookies(),Application.getGlobalCookies());
161 else if (element instanceof ExpCheckBoxMenuItem)
162 ((ExpCheckBoxMenuItem) element).checkEnabled(Application.getActiveComponentCookies(), Application.getLocalCookies(),Application.getGlobalCookies());
163 }
164 }
165 catch (Exception e)
166 {
167 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while checking menuItems to see if they should be enabled or not.");
168 }
169 }
170 }