1 |
|
package ca.spaz.gui; |
2 |
|
|
3 |
|
import java.awt.event.*; |
4 |
|
import java.io.File; |
5 |
|
import java.lang.reflect.Method; |
6 |
|
import java.util.*; |
7 |
|
|
8 |
|
import javax.swing.*; |
9 |
|
import javax.swing.border.*; |
10 |
|
import javax.xml.parsers.*; |
11 |
|
|
12 |
|
import org.w3c.dom.*; |
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
public class SpazCanvas extends JPanel implements ActionListener { |
22 |
|
protected Hashtable actions, components; |
23 |
|
protected Object listener; |
24 |
|
|
25 |
|
public SpazCanvas(String file) { |
26 |
0 |
super(); |
27 |
0 |
init(file); |
28 |
0 |
} |
29 |
|
|
30 |
|
public SpazCanvas(String file, Object listener) { |
31 |
0 |
super(); |
32 |
0 |
this.listener = listener; |
33 |
0 |
init(file); |
34 |
0 |
} |
35 |
|
|
36 |
|
private void init(String file) { |
37 |
0 |
this.actions = new Hashtable(); |
38 |
0 |
this.components = new Hashtable(); |
39 |
0 |
this.setLayout(new SpazLayout()); |
40 |
0 |
loadXML(file); |
41 |
0 |
} |
42 |
|
|
43 |
|
public void setListener(Object l) { |
44 |
0 |
this.listener = l; |
45 |
0 |
} |
46 |
|
|
47 |
|
public void loadXML(String fname) { |
48 |
|
try { |
49 |
0 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
50 |
0 |
dbf.setNamespaceAware(true); |
51 |
0 |
DocumentBuilder db = dbf.newDocumentBuilder(); |
52 |
0 |
File f = new File(fname); |
53 |
0 |
Document d = db.parse(f); |
54 |
0 |
Element e = d.getDocumentElement(); |
55 |
0 |
parseChildren(e); |
56 |
0 |
} catch (Exception e) { |
57 |
0 |
e.printStackTrace(); |
58 |
0 |
} |
59 |
0 |
} |
60 |
|
|
61 |
|
public SpazPosition getPosition(JComponent c) { |
62 |
0 |
return ((SpazLayout)this.getLayout()).getPosition(c); |
63 |
|
} |
64 |
|
|
65 |
|
public String getAction(JComponent c) { |
66 |
0 |
return (String)actions.get(c); |
67 |
|
} |
68 |
|
|
69 |
|
public JComponent getComponent(String idStr) { |
70 |
0 |
return (JComponent)components.get(idStr); |
71 |
|
} |
72 |
|
|
73 |
|
public Enumeration components() { |
74 |
0 |
return components.elements(); |
75 |
|
} |
76 |
|
|
77 |
|
public void parseChildren(Element e) { |
78 |
|
try { |
79 |
0 |
NodeList nl = e.getChildNodes(); |
80 |
0 |
for (int i = 0; i < nl.getLength(); i++) { |
81 |
|
|
82 |
0 |
if ((nl.item(i).getNodeType() == Node.ELEMENT_NODE)) { |
83 |
0 |
Element elm = (Element)nl.item(i); |
84 |
0 |
String tag = elm.getTagName(); |
85 |
0 |
if (tag.equals("canvas")) { |
86 |
0 |
parseChildren(elm); |
87 |
0 |
} else if (tag.equals("label")) { |
88 |
0 |
parseLabel(elm); |
89 |
0 |
} else if (tag.equals("textfield")) { |
90 |
0 |
parseTextField(elm); |
91 |
0 |
} else if (tag.equals("button")) { |
92 |
0 |
parseButton(elm); |
93 |
0 |
} else if (tag.equals("textarea")) { |
94 |
0 |
parseTextArea(elm); |
95 |
0 |
} else if (tag.equals("progressbar")) { |
96 |
0 |
parseProgressBar(elm); |
97 |
|
} |
98 |
|
} |
99 |
|
} |
100 |
0 |
} catch (Exception ex) { |
101 |
0 |
ex.printStackTrace(); |
102 |
0 |
} |
103 |
0 |
} |
104 |
|
|
105 |
|
public void parseTextArea(Element elm) { |
106 |
0 |
JTextPane text = new JTextPane(); |
107 |
0 |
JScrollPane jsp = |
108 |
|
new JScrollPane( |
109 |
|
text, |
110 |
|
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, |
111 |
|
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
112 |
|
|
113 |
0 |
jsp.setAutoscrolls(true); |
114 |
0 |
SpazPosition lp = parseSpazPosition(elm); |
115 |
0 |
if (lp == null) |
116 |
0 |
return; |
117 |
0 |
this.add(jsp, lp); |
118 |
0 |
parseBorder(jsp, elm); |
119 |
0 |
parseAttributes(elm, jsp); |
120 |
0 |
} |
121 |
|
|
122 |
|
public void parseLabel(Element elm) { |
123 |
0 |
JLabel label = new JLabel(); |
124 |
0 |
String title = elm.getAttribute("title"); |
125 |
0 |
if (title != null) |
126 |
0 |
label.setText(title); |
127 |
0 |
SpazPosition lp = parseSpazPosition(elm); |
128 |
0 |
if (lp == null) |
129 |
0 |
return; |
130 |
0 |
this.add(label, lp); |
131 |
0 |
parseBorder(label, elm); |
132 |
0 |
parseAttributes(elm, label); |
133 |
0 |
} |
134 |
|
|
135 |
|
public void parseProgressBar(Element elm) { |
136 |
0 |
JProgressBar bar = new JProgressBar(); |
137 |
0 |
SpazPosition lp = parseSpazPosition(elm); |
138 |
0 |
if (lp == null) |
139 |
0 |
return; |
140 |
0 |
this.add(bar, lp); |
141 |
0 |
parseBorder(bar, elm); |
142 |
0 |
parseAttributes(elm, bar); |
143 |
0 |
} |
144 |
|
|
145 |
|
public void parseButton(Element elm) { |
146 |
0 |
JButton btn = new JButton(); |
147 |
0 |
String title = elm.getAttribute("title"); |
148 |
0 |
if (title != null) |
149 |
0 |
btn.setText(title); |
150 |
0 |
SpazPosition lp = parseSpazPosition(elm); |
151 |
0 |
if (lp == null) |
152 |
0 |
return; |
153 |
0 |
this.add(btn, lp); |
154 |
0 |
parseBorder(btn, elm); |
155 |
0 |
parseAttributes(elm, btn); |
156 |
0 |
String action = elm.getAttribute("action"); |
157 |
0 |
if (action.length() > 0) { |
158 |
0 |
actions.put(btn, action); |
159 |
0 |
btn.addActionListener(this); |
160 |
|
} |
161 |
|
|
162 |
0 |
} |
163 |
|
|
164 |
|
public void parseTextField(Element elm) { |
165 |
0 |
String title = ""; |
166 |
0 |
JTextField textField = new JTextField(title); |
167 |
0 |
SpazPosition lp = parseSpazPosition(elm); |
168 |
0 |
if (lp == null) |
169 |
0 |
return; |
170 |
0 |
this.add(textField, lp); |
171 |
0 |
parseBorder(textField, elm); |
172 |
0 |
parseAttributes(elm, textField); |
173 |
0 |
} |
174 |
|
|
175 |
|
public void parseAttributes(Element elm, JComponent comp) { |
176 |
0 |
String enabled = elm.getAttribute("enabled"); |
177 |
0 |
if (enabled != null) |
178 |
0 |
comp.setEnabled(!enabled.equals("false")); |
179 |
0 |
String id = elm.getAttribute("id"); |
180 |
0 |
if (id.length() > 0) { |
181 |
0 |
components.put(id, comp); |
182 |
0 |
comp.setName(id); |
183 |
|
} |
184 |
0 |
String tooltip = elm.getAttribute("tooltip"); |
185 |
0 |
if (tooltip.length() > 0) { |
186 |
0 |
comp.setToolTipText(tooltip); |
187 |
|
} |
188 |
0 |
} |
189 |
|
|
190 |
|
public SpazPosition parseSpazPosition(Element elm) { |
191 |
0 |
if (elm.getTagName().equals("position")) { |
192 |
0 |
SpazPosition lp = new SpazPosition(); |
193 |
0 |
double left_rel = Double.parseDouble(elm.getAttribute("left_rel")); |
194 |
0 |
double right_rel = Double.parseDouble(elm.getAttribute("right_rel")); |
195 |
0 |
double top_rel = Double.parseDouble(elm.getAttribute("top_rel")); |
196 |
0 |
double bottom_rel = Double.parseDouble(elm.getAttribute("bottom_rel")); |
197 |
0 |
int left_abs = Integer.parseInt(elm.getAttribute("left_abs")); |
198 |
0 |
int right_abs = Integer.parseInt(elm.getAttribute("right_abs")); |
199 |
0 |
int top_abs = Integer.parseInt(elm.getAttribute("top_abs")); |
200 |
0 |
int bottom_abs = Integer.parseInt(elm.getAttribute("bottom_abs")); |
201 |
0 |
lp.setHPos(left_rel, left_abs, right_rel, right_abs); |
202 |
0 |
lp.setVPos(top_rel, top_abs, bottom_rel, bottom_abs); |
203 |
0 |
return lp; |
204 |
|
} else { |
205 |
0 |
NodeList nl = elm.getElementsByTagName("position"); |
206 |
0 |
for (int n = 0; n < nl.getLength(); n++) { |
207 |
0 |
SpazPosition lp = parseSpazPosition((Element) (nl.item(n))); |
208 |
0 |
return lp; |
209 |
|
} |
210 |
|
} |
211 |
0 |
return null; |
212 |
|
} |
213 |
|
|
214 |
|
public void parseBorder(JComponent c, Element elm) { |
215 |
0 |
if (elm.getTagName().equals("border")) { |
216 |
0 |
String type = elm.getAttribute("type"); |
217 |
0 |
if (type.equals("empty")) { |
218 |
0 |
c.setBorder(new EmptyBorder(2, 2, 2, 2)); |
219 |
0 |
} else if (type.equals("titled")) { |
220 |
0 |
String title = elm.getAttribute("title"); |
221 |
0 |
c.setBorder(new TitledBorder(title)); |
222 |
0 |
} else if (type.equals("beveled")) { |
223 |
0 |
int btype = Integer.parseInt(elm.getAttribute("bevel")); |
224 |
0 |
c.setBorder(new BevelBorder(btype)); |
225 |
|
} |
226 |
0 |
} else { |
227 |
0 |
NodeList nl = elm.getElementsByTagName("border"); |
228 |
0 |
for (int n = 0; n < nl.getLength(); n++) { |
229 |
0 |
parseBorder(c, (Element)nl.item(n)); |
230 |
|
} |
231 |
|
} |
232 |
0 |
} |
233 |
|
|
234 |
|
public void actionPerformed(ActionEvent e) { |
235 |
0 |
if (listener == null) |
236 |
0 |
return; |
237 |
0 |
String methodName = (String)actions.get(e.getSource()); |
238 |
0 |
if (methodName != null) { |
239 |
|
try { |
240 |
0 |
Class[] params = null; |
241 |
0 |
Method m = listener.getClass().getMethod(methodName, params); |
242 |
0 |
Object[] params2 = null; |
243 |
0 |
m.invoke(listener, params2); |
244 |
0 |
} catch (NoSuchMethodException me) { |
245 |
0 |
JOptionPane.showMessageDialog(this, |
246 |
|
"No Action Handler for '" + methodName + "'"); |
247 |
0 |
} catch (Exception ex) { |
248 |
0 |
ex.printStackTrace(); |
249 |
0 |
} |
250 |
|
} |
251 |
0 |
} |
252 |
|
|
253 |
|
} |