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 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
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 |
0 |
super(); |
43 |
0 |
this.listener = listener; |
44 |
0 |
this.actions = new Hashtable(); |
45 |
0 |
loadXML(file); |
46 |
0 |
} |
47 |
|
|
48 |
|
public SpazMenuBar(String file, Object listener) { |
49 |
0 |
super(); |
50 |
0 |
this.listener = listener; |
51 |
0 |
this.actions = new Hashtable(); |
52 |
|
|
53 |
0 |
InputStream xmlFile = getClass().getResourceAsStream(file); |
54 |
0 |
loadXML(xmlFile); |
55 |
0 |
} |
56 |
|
|
57 |
|
|
58 |
|
public void loadXML(InputStream file) { |
59 |
|
try { |
60 |
0 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
61 |
0 |
dbf.setNamespaceAware(true); |
62 |
0 |
DocumentBuilder db = dbf.newDocumentBuilder(); |
63 |
|
|
64 |
0 |
Document d = db.parse(file); |
65 |
0 |
loadXML(d); |
66 |
0 |
} catch (Exception e) { |
67 |
0 |
e.printStackTrace(); |
68 |
0 |
} |
69 |
0 |
} |
70 |
|
|
71 |
|
public void loadXML(Document d) { |
72 |
|
try { |
73 |
0 |
Element e = d.getDocumentElement(); |
74 |
0 |
NodeList nl = e.getElementsByTagName("menu"); |
75 |
0 |
for (int n=0; n<nl.getLength(); n++) { |
76 |
0 |
Element elm = (Element)(nl.item(n)); |
77 |
0 |
loadXMLMenu(elm); |
78 |
|
} |
79 |
0 |
} catch (Exception e) { |
80 |
0 |
e.printStackTrace(); |
81 |
0 |
} |
82 |
0 |
} |
83 |
|
|
84 |
|
public void loadXMLMenu(Element elm) { |
85 |
0 |
String title = elm.getAttribute("title"); |
86 |
0 |
String key = elm.getAttribute("key"); |
87 |
0 |
JMenu menu = new JMenu(title); |
88 |
0 |
this.add(menu); |
89 |
0 |
NodeList nl = elm.getElementsByTagName("item"); |
90 |
0 |
for (int n=0; n<nl.getLength(); n++) { |
91 |
0 |
Element e = (Element)(nl.item(n)); |
92 |
0 |
loadXMLMenuItem(e, menu); |
93 |
|
} |
94 |
0 |
if (key != null && key.length() > 0) { |
95 |
0 |
menu.setMnemonic(key.charAt(0)); |
96 |
|
} |
97 |
0 |
} |
98 |
|
|
99 |
|
public void loadXMLMenuItem(Element elm, JMenu jm) { |
100 |
0 |
String separator = elm.getAttribute("separator"); |
101 |
0 |
if (separator != null) { |
102 |
0 |
if (separator.equals("true")) { |
103 |
0 |
jm.addSeparator(); |
104 |
0 |
return; |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
0 |
String title = elm.getAttribute("title"); |
109 |
0 |
String tooltip = elm.getAttribute("tooltip"); |
110 |
0 |
String action = elm.getAttribute("action"); |
111 |
0 |
String key = elm.getAttribute("key"); |
112 |
0 |
String acc = elm.getAttribute("acc"); |
113 |
0 |
String checkbox = elm.getAttribute("checkbox"); |
114 |
0 |
JMenuItem item = new JMenuItem(title); |
115 |
0 |
if (checkbox != null) { |
116 |
0 |
if (checkbox.equals("true")) { |
117 |
0 |
item = new JCheckBoxMenuItem(title); |
118 |
|
} |
119 |
|
} |
120 |
0 |
jm.add(item); |
121 |
0 |
item.addActionListener(this); |
122 |
0 |
actions.put(item, action); |
123 |
0 |
if (key != null && key.length() > 0) { |
124 |
0 |
item.setMnemonic(key.charAt(0)); |
125 |
|
} |
126 |
0 |
if (acc != null && acc.length() > 0) { |
127 |
|
try { |
128 |
0 |
Class c = KeyEvent.class; |
129 |
0 |
item.setAccelerator( |
130 |
|
KeyStroke.getKeyStroke(c.getDeclaredField(acc).getInt(c), |
131 |
|
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); |
132 |
0 |
} catch (Exception e) { |
133 |
0 |
e.printStackTrace(); |
134 |
0 |
} |
135 |
|
} |
136 |
0 |
if (tooltip != null) { |
137 |
0 |
item.setToolTipText(tooltip); |
138 |
|
} |
139 |
0 |
} |
140 |
|
|
141 |
|
public void setEnabled(String action, boolean on) { |
142 |
0 |
JMenuItem item = (JMenuItem)getItem(action); |
143 |
0 |
if (item != null) { |
144 |
0 |
item.setEnabled(on); |
145 |
|
} |
146 |
0 |
} |
147 |
|
|
148 |
|
public JMenuItem getItem(String action) { |
149 |
0 |
Enumeration e = actions.keys(); |
150 |
0 |
while (e.hasMoreElements()) { |
151 |
0 |
JMenuItem item = (JMenuItem)e.nextElement(); |
152 |
0 |
String s = (String)actions.get(item); |
153 |
0 |
if (s.equals(action)) { |
154 |
0 |
return item; |
155 |
|
} |
156 |
0 |
} |
157 |
0 |
return null; |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
public boolean isSelected(String name) { |
162 |
0 |
JCheckBoxMenuItem item = (JCheckBoxMenuItem)getItem(name); |
163 |
0 |
return item.isSelected(); |
164 |
|
} |
165 |
|
|
166 |
|
public void actionPerformed(ActionEvent e) { |
167 |
0 |
String methodName = (String)actions.get(e.getSource()); |
168 |
0 |
if (methodName != null) { |
169 |
|
try { |
170 |
0 |
Class[] params = null; |
171 |
0 |
Method m = listener.getClass().getMethod(methodName, params); |
172 |
0 |
Object[] params2 = null; |
173 |
0 |
m.invoke(listener, params2); |
174 |
0 |
} catch (NoSuchMethodException me) { |
175 |
0 |
JOptionPane.showMessageDialog(null, "No Menu Handler for '"+methodName+"'"); |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
0 |
} catch (Exception ex) { |
180 |
0 |
ex.printStackTrace(); |
181 |
0 |
} |
182 |
|
} |
183 |
0 |
} |
184 |
|
|
185 |
|
|
186 |
|
} |