1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.apache.commons.jelly.tags.swing; |
17 |
|
|
18 |
|
|
19 |
|
import java.awt.Component; |
20 |
|
import java.awt.Container; |
21 |
|
import java.awt.Dimension; |
22 |
|
import java.awt.Font; |
23 |
|
import java.awt.LayoutManager; |
24 |
|
import java.awt.Point; |
25 |
|
import java.awt.Window; |
26 |
|
import java.awt.event.FocusListener; |
27 |
|
import java.awt.event.KeyListener; |
28 |
|
import java.awt.event.WindowListener; |
29 |
|
import java.lang.reflect.InvocationTargetException; |
30 |
|
import java.util.Map; |
31 |
|
|
32 |
|
import javax.swing.Action; |
33 |
|
import javax.swing.JFrame; |
34 |
|
import javax.swing.JMenu; |
35 |
|
import javax.swing.JMenuBar; |
36 |
|
import javax.swing.JScrollPane; |
37 |
|
import javax.swing.JSplitPane; |
38 |
|
import javax.swing.RootPaneContainer; |
39 |
|
import javax.swing.border.Border; |
40 |
|
|
41 |
|
import org.apache.commons.beanutils.BeanUtils; |
42 |
|
import org.apache.commons.beanutils.ConvertUtils; |
43 |
|
import org.apache.commons.jelly.JellyTagException; |
44 |
|
import org.apache.commons.jelly.MissingAttributeException; |
45 |
|
import org.apache.commons.jelly.XMLOutput; |
46 |
|
import org.apache.commons.jelly.tags.core.UseBeanTag; |
47 |
|
import org.apache.commons.jelly.tags.swing.converters.DebugGraphicsConverter; |
48 |
|
import org.apache.commons.logging.Log; |
49 |
|
import org.apache.commons.logging.LogFactory; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
5 |
public class ComponentTag extends UseBeanTag implements ContainerTag { |
65 |
|
|
66 |
|
|
67 |
4 |
private static final Log log = LogFactory.getLog(ComponentTag.class); |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
2 |
private static final DebugGraphicsConverter debugGraphicsConverter = new DebugGraphicsConverter(); |
74 |
|
|
75 |
|
|
76 |
|
private Factory factory; |
77 |
|
|
78 |
1 |
public ComponentTag() { |
79 |
1 |
} |
80 |
|
|
81 |
12 |
public ComponentTag(Factory factory) { |
82 |
12 |
this.factory = factory; |
83 |
12 |
} |
84 |
|
|
85 |
|
public String toString() { |
86 |
0 |
String componentName = getComponent().getName(); |
87 |
0 |
if (componentName == null || componentName.length() == 0) |
88 |
0 |
componentName = getComponent().toString(); |
89 |
0 |
return "ComponentTag with bean " + componentName; |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
public void setAction(Action action) throws JellyTagException { |
96 |
0 |
Component component = getComponent(); |
97 |
0 |
if ( component != null ) { |
98 |
|
|
99 |
|
try { |
100 |
0 |
BeanUtils.setProperty( component, "action", action ); |
101 |
0 |
} catch (IllegalAccessException e) { |
102 |
0 |
throw new JellyTagException(e); |
103 |
|
} catch (InvocationTargetException e) { |
104 |
0 |
throw new JellyTagException(e); |
105 |
|
} |
106 |
|
} |
107 |
0 |
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
public void setFont(Font font) throws JellyTagException { |
113 |
0 |
Component component = getComponent(); |
114 |
0 |
if ( component != null ) { |
115 |
|
|
116 |
|
try { |
117 |
0 |
BeanUtils.setProperty( component, "font", font ); |
118 |
0 |
} |
119 |
|
catch (IllegalAccessException e) { |
120 |
0 |
throw new JellyTagException(e); |
121 |
|
} |
122 |
|
catch (InvocationTargetException e) { |
123 |
0 |
throw new JellyTagException(e); |
124 |
|
} |
125 |
|
} |
126 |
0 |
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
public void setBorder(Border border) throws JellyTagException { |
132 |
0 |
Component component = getComponent(); |
133 |
0 |
if ( component != null ) { |
134 |
|
try { |
135 |
|
|
136 |
0 |
BeanUtils.setProperty( component, "border", border ); |
137 |
0 |
} |
138 |
|
catch (IllegalAccessException e) { |
139 |
0 |
throw new JellyTagException(e); |
140 |
|
} |
141 |
|
catch (InvocationTargetException e) { |
142 |
0 |
throw new JellyTagException(e); |
143 |
|
} |
144 |
|
} |
145 |
0 |
} |
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
public void setLayout(LayoutManager layout) throws JellyTagException { |
151 |
2 |
Component component = getComponent(); |
152 |
2 |
if ( component != null ) { |
153 |
2 |
if ( component instanceof RootPaneContainer ) { |
154 |
2 |
RootPaneContainer rpc = (RootPaneContainer) component; |
155 |
2 |
component = rpc.getContentPane(); |
156 |
|
} |
157 |
|
|
158 |
|
try { |
159 |
|
|
160 |
2 |
BeanUtils.setProperty( component, "layout", layout ); |
161 |
2 |
} |
162 |
|
catch (IllegalAccessException e) { |
163 |
0 |
throw new JellyTagException(e); |
164 |
|
} |
165 |
|
catch (InvocationTargetException e) { |
166 |
0 |
throw new JellyTagException(e); |
167 |
|
} |
168 |
|
} |
169 |
2 |
} |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
public void addWindowListener(WindowListener listener) throws JellyTagException { |
175 |
0 |
Component component = getComponent(); |
176 |
0 |
if ( component instanceof Window ) { |
177 |
0 |
Window window = (Window) component; |
178 |
0 |
window.addWindowListener(listener); |
179 |
|
} |
180 |
0 |
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
public void addFocusListener(FocusListener listener) throws JellyTagException { |
186 |
0 |
Component component = getComponent(); |
187 |
0 |
component.addFocusListener(listener); |
188 |
0 |
} |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
public void addKeyListener(KeyListener listener) throws JellyTagException { |
194 |
0 |
Component component = getComponent(); |
195 |
0 |
component.addKeyListener(listener); |
196 |
0 |
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
public Component getComponent() { |
205 |
26 |
Object bean = getBean(); |
206 |
26 |
if ( bean instanceof Component ) { |
207 |
24 |
return (Component) bean; |
208 |
|
} |
209 |
2 |
return null; |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
public void addChild(Component component, Object constraints) throws JellyTagException { |
220 |
4 |
Object parent = getBean(); |
221 |
4 |
if ( parent instanceof JFrame && component instanceof JMenuBar ) { |
222 |
0 |
JFrame frame = (JFrame) parent; |
223 |
0 |
frame.setJMenuBar( (JMenuBar) component ); |
224 |
|
} |
225 |
4 |
else if ( parent instanceof RootPaneContainer ) { |
226 |
3 |
RootPaneContainer rpc = (RootPaneContainer) parent; |
227 |
3 |
if (constraints != null) { |
228 |
2 |
rpc.getContentPane().add( component, constraints ); |
229 |
|
} |
230 |
|
else { |
231 |
1 |
rpc.getContentPane().add( component); |
232 |
|
} |
233 |
|
} |
234 |
1 |
else if ( parent instanceof JScrollPane ) { |
235 |
0 |
JScrollPane scrollPane = (JScrollPane) parent; |
236 |
0 |
scrollPane.setViewportView( component ); |
237 |
|
} |
238 |
1 |
else if ( parent instanceof JSplitPane) { |
239 |
0 |
JSplitPane splitPane = (JSplitPane) parent; |
240 |
0 |
if ( splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT ) { |
241 |
0 |
if ( splitPane.getTopComponent() == null ) { |
242 |
0 |
splitPane.setTopComponent( component ); |
243 |
|
} |
244 |
|
else { |
245 |
0 |
splitPane.setBottomComponent( component ); |
246 |
|
} |
247 |
|
} |
248 |
|
else { |
249 |
0 |
if ( splitPane.getLeftComponent() == null ) { |
250 |
0 |
splitPane.setLeftComponent( component ); |
251 |
|
} |
252 |
|
else { |
253 |
0 |
splitPane.setRightComponent( component ); |
254 |
|
} |
255 |
|
} |
256 |
|
} |
257 |
1 |
else if ( parent instanceof JMenuBar && component instanceof JMenu ) { |
258 |
0 |
JMenuBar menuBar = (JMenuBar) parent; |
259 |
0 |
menuBar.add( (JMenu) component ); |
260 |
|
} |
261 |
1 |
else if ( parent instanceof Container ) { |
262 |
1 |
Container container = (Container) parent; |
263 |
1 |
if (constraints != null) { |
264 |
0 |
container.add( component, constraints ); |
265 |
|
} |
266 |
|
else { |
267 |
1 |
container.add( component ); |
268 |
|
} |
269 |
|
} |
270 |
4 |
} |
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
protected Class convertToClass(Object classObject) throws MissingAttributeException, ClassNotFoundException { |
280 |
13 |
if (classObject == null) { |
281 |
13 |
return null; |
282 |
|
} |
283 |
|
else { |
284 |
0 |
return super.convertToClass(classObject); |
285 |
|
} |
286 |
|
} |
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
protected Object newInstance(Class theClass, Map attributes, XMLOutput output) throws JellyTagException { |
292 |
|
try { |
293 |
12 |
if (theClass != null ) { |
294 |
0 |
return theClass.newInstance(); |
295 |
|
} else { |
296 |
12 |
return factory.newInstance(); |
297 |
|
} |
298 |
|
} catch (IllegalAccessException e) { |
299 |
0 |
throw new JellyTagException(e); |
300 |
|
} catch (InstantiationException e) { |
301 |
0 |
throw new JellyTagException(e); |
302 |
|
} |
303 |
|
} |
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
protected void processBean(String var, Object bean) throws JellyTagException { |
310 |
11 |
if (var != null) { |
311 |
4 |
context.setVariable(var, bean); |
312 |
|
} |
313 |
11 |
Component component = getComponent(); |
314 |
11 |
if ( component != null ) { |
315 |
10 |
ContainerTag parentTag = (ContainerTag) findAncestorWithClass( ContainerTag.class ); |
316 |
10 |
if ( parentTag != null ) { |
317 |
7 |
parentTag.addChild(component, getConstraint()); |
318 |
|
} |
319 |
|
else { |
320 |
3 |
if (var == null) { |
321 |
0 |
throw new JellyTagException( "The 'var' attribute must be specified or this tag must be nested inside a JellySwing container tag like a widget or a layout" ); |
322 |
|
} |
323 |
|
} |
324 |
|
} |
325 |
11 |
} |
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
protected void setBeanProperties(Object bean, Map attributes) throws JellyTagException { |
331 |
|
|
332 |
13 |
Component component = getComponent(); |
333 |
13 |
if (component != null) { |
334 |
12 |
if (attributes.containsKey("location")) { |
335 |
2 |
Object value = attributes.get("location"); |
336 |
2 |
Point p = null; |
337 |
2 |
if (value instanceof Point) { |
338 |
0 |
p = (Point) value; |
339 |
|
} |
340 |
2 |
else if (value != null) { |
341 |
2 |
p = |
342 |
|
(Point) ConvertUtils.convert( |
343 |
|
value.toString(), |
344 |
|
Point.class); |
345 |
|
} |
346 |
2 |
component.setLocation(p); |
347 |
2 |
addIgnoreProperty("location"); |
348 |
|
} |
349 |
|
|
350 |
12 |
if (attributes.containsKey("size")) { |
351 |
2 |
Object value = attributes.get("size"); |
352 |
2 |
Dimension d = null; |
353 |
2 |
if (value instanceof Dimension) { |
354 |
0 |
d = (Dimension) value; |
355 |
|
} |
356 |
2 |
else if (value != null) { |
357 |
2 |
d = |
358 |
|
(Dimension) ConvertUtils.convert( |
359 |
|
value.toString(), |
360 |
|
Dimension.class); |
361 |
|
} |
362 |
2 |
component.setSize(d); |
363 |
2 |
addIgnoreProperty("size"); |
364 |
|
} |
365 |
|
|
366 |
12 |
if (attributes.containsKey("debugGraphicsOptions")) { |
367 |
|
try { |
368 |
1 |
Object o = debugGraphicsConverter.convert(attributes.get("debugGraphicsOptions")); |
369 |
1 |
attributes.put("debugGraphicsOptions", o); |
370 |
1 |
} catch (IllegalArgumentException e) { |
371 |
0 |
throw new JellyTagException(e); |
372 |
|
} |
373 |
|
} |
374 |
|
|
375 |
12 |
if (attributes.containsKey("debugGraphics")) { |
376 |
|
try { |
377 |
1 |
Object o = debugGraphicsConverter.convert(attributes.get("debugGraphics")); |
378 |
1 |
attributes.put("debugGraphicsOptions", o); |
379 |
1 |
} catch (IllegalArgumentException e) { |
380 |
0 |
throw new JellyTagException(e); |
381 |
|
} |
382 |
|
|
383 |
1 |
addIgnoreProperty("debugGraphics"); |
384 |
|
} |
385 |
|
|
386 |
12 |
super.setBeanProperties(bean, attributes); |
387 |
|
} |
388 |
12 |
} |
389 |
|
|
390 |
|
protected Object getConstraint() { |
391 |
7 |
return null; |
392 |
|
} |
393 |
|
|
394 |
|
|
395 |
|
|
396 |
|
|
397 |
|
|
398 |
|
|
399 |
|
public void doTag(XMLOutput output) throws JellyTagException { |
400 |
13 |
super.doTag(output); |
401 |
11 |
clearBean(); |
402 |
11 |
} |
403 |
|
|
404 |
|
|
405 |
|
|
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
protected void clearBean() { |
410 |
11 |
setBean(null); |
411 |
11 |
} |
412 |
|
} |