View Javadoc

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