1 package com.explosion.expf.menusandtools.menu;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.awt.event.KeyEvent;
23 import java.util.Iterator;
24 import java.util.Vector;
25
26 import javax.swing.JMenu;
27 import javax.swing.JMenuBar;
28 import javax.swing.JMenuItem;
29 import javax.swing.KeyStroke;
30 import javax.swing.UIManager;
31
32 import com.explosion.expf.Application;
33 import com.explosion.expf.ExpConstants;
34 import com.explosion.expf.ExpListener;
35 import com.explosion.expf.ExpLookAndFeel;
36 import com.explosion.expf.menusandtools.menu.segmented.ExpMenuSegment;
37 import com.explosion.utilities.exception.ExceptionManagerFactory;
38
39 /***
40 * This class has a hashtable called elements. Every menu, menuItem or JCheckBoxMenuItem added
41 * using the Add.. functions of this class are inserted into this Hashtable. These
42 * elements are all accessible via the getElement() method. The getMenu method is a convenience method
43 * whioch references the same hashtable. The elements returned fromthe hashtable aregoing to
44 * be one of three types. it is up to the user to decide what they are going to be.
45 */
46
47 public class ExpMenuBar extends JMenuBar
48 {
49 private ExpMenu menuFile;
50 private ExpMenu menuHelp;
51 private ExpMenu menuConfigure;
52
53 private Vector topLevelMenus = new Vector();
54
55 private MenuHelper menuHelper;
56
57 public ExpMenuBar(ExpListener listener) throws Exception
58 {
59 menuHelper = new MenuHelper(listener);
60
61 JMenuItem item = null;
62
63
64 this.setName(ExpConstants.COMPNAME_EXPMENUBAR);
65
66
67 menuFile = createExpMenu("File", 'f', ExpConstants.MENU_FILE, 1, true);
68 menuHelp = createExpMenu("Help", 'h', ExpConstants.MENU_HELP, 1, true);
69 menuConfigure = createExpMenu("Tools", 't', ExpConstants.MENU_TOOLS, 1, true);
70
71
72 topLevelMenus.addElement(menuFile);
73
74
75 ExpMenuSegment exitSegment = menuFile.createNewSegment(ExpMenuSegment.ALWAYS_LAST_SEGMENT);
76 item = menuHelper.createMenuItem("Exit", 'x', ExpConstants.MENU_EXIT, KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.CTRL_MASK), 1);
77 exitSegment.addElement(item);
78
79
80 ExpMenuSegment helpSegment = menuHelp.createNewSegment(ExpMenuSegment.ANY_POSITION);
81 helpSegment.addElement(menuHelper.createMenuItem("Help Contents", 'h', ExpConstants.MENU_HELP_CONTENTS, KeyStroke.getKeyStroke(KeyEvent.VK_F1, KeyEvent.CTRL_MASK), 1));
82 helpSegment.addElement(menuHelper.createMenuItem("About", (char) 1, ExpConstants.MENU_ABOUT, null, 1));
83
84
85 ExpMenu menuLAF = createExpMenu("Look And Feel", 'l', ExpConstants.MENU_LAF, 1, false);
86 ExpMenuSegment LAFSegment = menuLAF.createNewSegment(ExpMenuSegment.ANY_POSITION);
87
88 for (Iterator it = ExpConstants.looksList.iterator(); it.hasNext();)
89 {
90 ExpLookAndFeel lf = (ExpLookAndFeel) ExpConstants.looksMap.get(it.next());
91 LAFSegment.addElement(menuHelper.createMenuItem(lf.getDisplayName(), (char)-1, lf.getActionName(), null, 1));
92 if (UIManager.getLookAndFeel().getClass().getName().equals(lf.getClassName()))
93 {
94 Application.ensureGlobalCookie(lf.getActionName(),0);
95 }
96 else
97 {
98 Application.ensureGlobalCookie(lf.getActionName(),1);
99 }
100 }
101
102
103 ExpMenuSegment configSegment = menuConfigure.createNewSegment(ExpMenuSegment.ALWAYS_LAST_SEGMENT);
104 configSegment.addElement(menuLAF);
105 configSegment.addElement(menuHelper.createMenuItem("Preferences", '0', ExpConstants.MENU_PROPERTIES, null, 1));
106
107
108 Application.addToGlobalCookie(ExpConstants.MENU_FILE,1);
109 Application.addToGlobalCookie(ExpConstants.MENU_HELP,1);
110 Application.addToGlobalCookie(ExpConstants.MENU_TOOLS,1);
111 Application.addToGlobalCookie(ExpConstants.MENU_EXIT,1);
112 Application.addToGlobalCookie(ExpConstants.MENU_ABOUT,1);
113 Application.addToGlobalCookie(ExpConstants.MENU_HELP_CONTENTS,1);
114
115 Application.addToGlobalCookie(ExpConstants.MENU_PROPERTIES,1);
116 Application.addToGlobalCookie(ExpConstants.MENU_LAF,1);
117
118 menuHelper.checkEnabled();
119 }
120
121 /***
122 * Runs through the menus and adds them in recieved order or mapped order. File ..... Config Help
123 */
124 public void constructMenus()
125 {
126
127
128
129
130 Vector menuMap = Application.getMenuMap();
131 if (menuMap != null )
132 {
133 topLevelMenus = new Vector();
134 for (int i = 0; i < menuMap.size(); i++)
135 {
136 Object item = menuHelper.getElement((String)menuMap.elementAt(i));
137 if (item == null)
138 ExceptionManagerFactory.getExceptionManager().manageException(new Exception("The menumap contains a reference ('"+menuMap.elementAt(i)+"') to an object which has not been created. This object will not be added to the top level menus."),null);
139 else if (!(item instanceof JMenu))
140 ExceptionManagerFactory.getExceptionManager().manageException(new Exception("The menumap contains a reference ('"+menuMap.elementAt(i)+"') to an object which is not an instance of JMenu. The object is an instance the '"+item.getClass().getName()+"' class. This object will not be added to the top level menus."),null);
141 else
142 {
143 JMenu menu = (JMenu) item;
144 this.add(menu);
145 topLevelMenus.addElement(menu);
146 }
147 }
148 }
149 else
150 {
151 topLevelMenus.addElement(menuConfigure);
152 topLevelMenus.addElement(menuHelp);
153 this.add(menuHelp);
154
155 for (int i = 0; i < topLevelMenus.size(); i++)
156 {
157 ExpMenu menu = (ExpMenu) topLevelMenus.elementAt(i);
158 this.add(menu);
159 }
160 }
161 }
162
163
164 public Vector getTopLevelMenus()
165 {
166 return topLevelMenus;
167 }
168
169 /***
170 * This method adds a JMenu to a MenuBar
171 */
172 public ExpMenu createExpMenu(String menuText, char mnemonic, String actionCommand, int enabledThreshold, boolean isTopLevel)
173 {
174 ExpMenu menu = menuHelper.createExpMenu(menuText, mnemonic, actionCommand, enabledThreshold);
175
176
177 if (isTopLevel)
178 topLevelMenus.addElement(menu);
179
180 return menu;
181 }
182
183 /***
184 * Returns the MenuHelper for this MenuBar
185 * @return
186 */
187 public MenuHelper getMenuHelper()
188 {
189 return this.menuHelper;
190 }
191 }