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 import java.lang.reflect.InvocationTargetException;
19
20 import org.apache.commons.beanutils.BeanUtils;
21 import org.apache.commons.jelly.DynaBeanTagSupport;
22 import org.apache.commons.jelly.JellyException;
23 import org.apache.commons.jelly.JellyTagException;
24 import org.apache.commons.jelly.Tag;
25 import org.apache.commons.jelly.XMLOutput;
26 import org.apache.commons.jelly.impl.TagFactory;
27 import org.xml.sax.Attributes;
28
29 /*** This class represents a layout-manager constraints as passed in
30 * the second argument of {@link Container#add(Component,Object)}.
31 * <p>
32 * In essence, it looks really like nothing else than a bean-class...
33 * with {@link #getConstraintObject}.
34 * Probably a shorter java-source is do-able.
35 * <p>
36 * TODO: this class should probably be extended with special treatment for dimensios
37 * using the converter package.
38 */
39 public class ConstraintTag extends DynaBeanTagSupport {
40
41
42
43
44
45
46
47
48
49
50 protected Factory factory;
51 protected String var = null;
52 protected Object bean = null;
53
54 public static class HereFactory extends BeanFactory implements TagFactory {
55 public HereFactory(Class c) { super(c); }
56 public Tag createTag(String name, Attributes attributes) {
57 return new ConstraintTag ( this );
58
59 }
60 }
61 public static class ConstantFactory implements TagFactory, Factory {
62 public ConstantFactory(Object c) { this.constant = c;}
63 private Object constant;
64 public Object newInstance() { return constant; }
65 public Tag createTag(String name, Attributes attributes) throws JellyException {
66 return new ConstraintTag ( this );
67 }
68 }
69
70
71
72
73
74
75 public ConstraintTag (Factory factory) {
76 this.factory = factory;
77 }
78
79 protected void createBean ( Factory factory ) throws InstantiationException {
80 bean = factory.newInstance();
81 }
82
83
84
85 public void beforeSetAttributes ( ) throws JellyTagException {
86 try {
87 createBean(factory);
88 } catch (InstantiationException e) {
89 throw new JellyTagException(e.toString());
90 }
91 }
92
93
94 public void setAttribute ( String name, Object value ) throws JellyTagException {
95
96 if ( "var".equals(name) ) {
97 var = value.toString();
98 } else {
99
100 try {
101 BeanUtils.setProperty( bean, name, value );
102 } catch (IllegalAccessException e) {
103 throw new JellyTagException(e.toString());
104 } catch (InvocationTargetException e) {
105 throw new JellyTagException(e.toString());
106 }
107
108 }
109 }
110
111 /*** Children invocation... just nothing...
112 */
113 public void doTag ( XMLOutput output ) throws JellyTagException {
114 if ( var != null ) context.setVariable ( var, getBean() );
115 invokeBody ( output );
116
117 }
118
119
120 public Object getBean() {
121 return bean;
122 }
123
124 /*** Returns the attached constraint object.
125 */
126 public Object getConstraintObject() {
127 return getBean();
128 }
129 }