1 package ca.spaz.gui;
2
3 import java.awt.Toolkit;
4 import java.awt.event.*;
5 import java.io.*;
6 import java.lang.reflect.Method;
7 import java.util.*;
8
9 import javax.swing.*;
10 import javax.xml.parsers.*;
11
12 import org.w3c.dom.*;
13
14
15 /***
16 * A JMenuBar that can be created from an XML description.
17 * Reflection is used to automatically dispatch the menu selections
18 * without need of registering listeners for each item.
19 *
20 *
21 * @author Aaron Davidson
22 * @date September 2002
23 */
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class SpazMenuBar extends JMenuBar implements ActionListener {
38 private Hashtable actions;
39 private Object listener;
40
41 public SpazMenuBar(InputStream file, Object listener) {
42 super();
43 this.listener = listener;
44 this.actions = new Hashtable();
45 loadXML(file);
46 }
47
48 public SpazMenuBar(String file, Object listener) {
49 super();
50 this.listener = listener;
51 this.actions = new Hashtable();
52
53 InputStream xmlFile = getClass().getResourceAsStream(file);
54 loadXML(xmlFile);
55 }
56
57
58 public void loadXML(InputStream file) {
59 try {
60 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
61 dbf.setNamespaceAware(true);
62 DocumentBuilder db = dbf.newDocumentBuilder();
63
64 Document d = db.parse(file);
65 loadXML(d);
66 } catch (Exception e) {
67 e.printStackTrace();
68 }
69 }
70
71 public void loadXML(Document d) {
72 try {
73 Element e = d.getDocumentElement();
74 NodeList nl = e.getElementsByTagName("menu");
75 for (int n=0; n<nl.getLength(); n++) {
76 Element elm = (Element)(nl.item(n));
77 loadXMLMenu(elm);
78 }
79 } catch (Exception e) {
80 e.printStackTrace();
81 }
82 }
83
84 public void loadXMLMenu(Element elm) {
85 String title = elm.getAttribute("title");
86 String key = elm.getAttribute("key");
87 JMenu menu = new JMenu(title);
88 this.add(menu);
89 NodeList nl = elm.getElementsByTagName("item");
90 for (int n=0; n<nl.getLength(); n++) {
91 Element e = (Element)(nl.item(n));
92 loadXMLMenuItem(e, menu);
93 }
94 if (key != null && key.length() > 0) {
95 menu.setMnemonic(key.charAt(0));
96 }
97 }
98
99 public void loadXMLMenuItem(Element elm, JMenu jm) {
100 String separator = elm.getAttribute("separator");
101 if (separator != null) {
102 if (separator.equals("true")) {
103 jm.addSeparator();
104 return;
105 }
106 }
107
108 String title = elm.getAttribute("title");
109 String tooltip = elm.getAttribute("tooltip");
110 String action = elm.getAttribute("action");
111 String key = elm.getAttribute("key");
112 String acc = elm.getAttribute("acc");
113 String checkbox = elm.getAttribute("checkbox");
114 JMenuItem item = new JMenuItem(title);
115 if (checkbox != null) {
116 if (checkbox.equals("true")) {
117 item = new JCheckBoxMenuItem(title);
118 }
119 }
120 jm.add(item);
121 item.addActionListener(this);
122 actions.put(item, action);
123 if (key != null && key.length() > 0) {
124 item.setMnemonic(key.charAt(0));
125 }
126 if (acc != null && acc.length() > 0) {
127 try {
128 Class c = KeyEvent.class;
129 item.setAccelerator(
130 KeyStroke.getKeyStroke(c.getDeclaredField(acc).getInt(c),
131 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
132 } catch (Exception e) {
133 e.printStackTrace();
134 }
135 }
136 if (tooltip != null) {
137 item.setToolTipText(tooltip);
138 }
139 }
140
141 public void setEnabled(String action, boolean on) {
142 JMenuItem item = (JMenuItem)getItem(action);
143 if (item != null) {
144 item.setEnabled(on);
145 }
146 }
147
148 public JMenuItem getItem(String action) {
149 Enumeration e = actions.keys();
150 while (e.hasMoreElements()) {
151 JMenuItem item = (JMenuItem)e.nextElement();
152 String s = (String)actions.get(item);
153 if (s.equals(action)) {
154 return item;
155 }
156 }
157 return null;
158 }
159
160
161 public boolean isSelected(String name) {
162 JCheckBoxMenuItem item = (JCheckBoxMenuItem)getItem(name);
163 return item.isSelected();
164 }
165
166 public void actionPerformed(ActionEvent e) {
167 String methodName = (String)actions.get(e.getSource());
168 if (methodName != null) {
169 try {
170 Class[] params = null;
171 Method m = listener.getClass().getMethod(methodName, params);
172 Object[] params2 = null;
173 m.invoke(listener, params2);
174 } catch (NoSuchMethodException me) {
175 JOptionPane.showMessageDialog(null, "No Menu Handler for '"+methodName+"'");
176
177
178
179 } catch (Exception ex) {
180 ex.printStackTrace();
181 }
182 }
183 }
184
185
186 }