1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.enhance; |
16 |
| |
17 |
| import java.lang.reflect.Modifier; |
18 |
| import java.util.Iterator; |
19 |
| |
20 |
| import org.apache.hivemind.ErrorLog; |
21 |
| import org.apache.hivemind.Location; |
22 |
| import org.apache.hivemind.service.BodyBuilder; |
23 |
| import org.apache.hivemind.service.MethodSignature; |
24 |
| import org.apache.hivemind.util.Defense; |
25 |
| import org.apache.tapestry.IBinding; |
26 |
| import org.apache.tapestry.IComponent; |
27 |
| import org.apache.tapestry.binding.BindingSource; |
28 |
| import org.apache.tapestry.event.PageDetachListener; |
29 |
| import org.apache.tapestry.spec.IComponentSpecification; |
30 |
| import org.apache.tapestry.spec.IPropertySpecification; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public class SpecifiedPropertyWorker implements EnhancementWorker |
40 |
| { |
41 |
| private ErrorLog _errorLog; |
42 |
| |
43 |
| private BindingSource _bindingSource; |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
422
| public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
|
53 |
| { |
54 |
422
| Iterator i = spec.getPropertySpecificationNames().iterator();
|
55 |
| |
56 |
422
| while (i.hasNext())
|
57 |
| { |
58 |
121
| String name = (String) i.next();
|
59 |
121
| IPropertySpecification ps = spec.getPropertySpecification(name);
|
60 |
| |
61 |
121
| try
|
62 |
| { |
63 |
121
| performEnhancement(op, ps);
|
64 |
| } |
65 |
| catch (RuntimeException ex) |
66 |
| { |
67 |
1
| _errorLog.error(
|
68 |
| EnhanceMessages.errorAddingProperty(name, op.getBaseClass(), ex), |
69 |
| ps.getLocation(), |
70 |
| ex); |
71 |
| } |
72 |
| } |
73 |
| } |
74 |
| |
75 |
121
| private void performEnhancement(EnhancementOperation op, IPropertySpecification ps)
|
76 |
| { |
77 |
121
| Defense.notNull(ps, "ps");
|
78 |
| |
79 |
121
| String propertyName = ps.getName();
|
80 |
121
| String specifiedType = ps.getType();
|
81 |
121
| boolean persistent = ps.isPersistent();
|
82 |
121
| String initialValue = ps.getInitialValue();
|
83 |
121
| Location location = ps.getLocation();
|
84 |
| |
85 |
121
| addProperty(op, propertyName, specifiedType, persistent, initialValue, location);
|
86 |
| } |
87 |
| |
88 |
121
| public void addProperty(EnhancementOperation op, String propertyName, String specifiedType,
|
89 |
| boolean persistent, String initialValue, Location location) |
90 |
| { |
91 |
121
| Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, specifiedType);
|
92 |
| |
93 |
120
| op.claimProperty(propertyName);
|
94 |
| |
95 |
120
| String field = "_$" + propertyName;
|
96 |
| |
97 |
120
| op.addField(field, propertyType);
|
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
120
| EnhanceUtils.createSimpleAccessor(op, field, propertyName, propertyType, location);
|
104 |
| |
105 |
120
| addMutator(op, propertyName, propertyType, field, persistent, location);
|
106 |
| |
107 |
120
| if (initialValue == null)
|
108 |
114
| addReinitializer(op, propertyType, field);
|
109 |
| else |
110 |
6
| addInitialValue(op, propertyName, propertyType, field, initialValue, location);
|
111 |
| } |
112 |
| |
113 |
114
| private void addReinitializer(EnhancementOperation op, Class propertyType, String fieldName)
|
114 |
| { |
115 |
114
| String defaultFieldName = fieldName + "$default";
|
116 |
| |
117 |
114
| op.addField(defaultFieldName, propertyType);
|
118 |
| |
119 |
| |
120 |
| |
121 |
114
| op.extendMethodImplementation(
|
122 |
| IComponent.class, |
123 |
| EnhanceUtils.FINISH_LOAD_SIGNATURE, |
124 |
| defaultFieldName + " = " + fieldName + ";"); |
125 |
| |
126 |
| |
127 |
| |
128 |
114
| op.extendMethodImplementation(
|
129 |
| PageDetachListener.class, |
130 |
| EnhanceUtils.PAGE_DETACHED_SIGNATURE, |
131 |
| fieldName + " = " + defaultFieldName + ";"); |
132 |
| } |
133 |
| |
134 |
6
| private void addInitialValue(EnhancementOperation op, String propertyName, Class propertyType,
|
135 |
| String fieldName, String initialValue, Location location) |
136 |
| { |
137 |
6
| String description = EnhanceMessages.initialValueForProperty(propertyName);
|
138 |
| |
139 |
6
| InitialValueBindingCreator creator = new InitialValueBindingCreator(_bindingSource,
|
140 |
| description, initialValue, location); |
141 |
| |
142 |
6
| String creatorField = op.addInjectedField(
|
143 |
| fieldName + "$initialValueBindingCreator", |
144 |
| InitialValueBindingCreator.class, |
145 |
| creator); |
146 |
| |
147 |
6
| String bindingField = fieldName + "$initialValueBinding";
|
148 |
6
| op.addField(bindingField, IBinding.class);
|
149 |
| |
150 |
6
| BodyBuilder builder = new BodyBuilder();
|
151 |
| |
152 |
6
| builder.addln("{0} = {1}.createBinding(this);", bindingField, creatorField);
|
153 |
| |
154 |
6
| op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, builder
|
155 |
| .toString()); |
156 |
| |
157 |
6
| builder.clear();
|
158 |
| |
159 |
6
| builder.addln("{0} = {1};", fieldName, EnhanceUtils.createUnwrapExpression(
|
160 |
| op, |
161 |
| bindingField, |
162 |
| propertyType)); |
163 |
| |
164 |
6
| String code = builder.toString();
|
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
6
| op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code);
|
170 |
6
| op.extendMethodImplementation(
|
171 |
| PageDetachListener.class, |
172 |
| EnhanceUtils.PAGE_DETACHED_SIGNATURE, |
173 |
| code); |
174 |
| |
175 |
| } |
176 |
| |
177 |
120
| private void addMutator(EnhancementOperation op, String propertyName, Class propertyType,
|
178 |
| String fieldName, boolean persistent, Location location) |
179 |
| { |
180 |
120
| String methodName = EnhanceUtils.createMutatorMethodName(propertyName);
|
181 |
| |
182 |
120
| BodyBuilder body = new BodyBuilder();
|
183 |
| |
184 |
120
| body.begin();
|
185 |
| |
186 |
120
| if (persistent)
|
187 |
| { |
188 |
20
| body.add("org.apache.tapestry.Tapestry#fireObservedChange(this, ");
|
189 |
20
| body.addQuoted(propertyName);
|
190 |
20
| body.addln(", ($w) $1);");
|
191 |
| } |
192 |
| |
193 |
120
| body.addln(fieldName + " = $1;");
|
194 |
| |
195 |
120
| body.end();
|
196 |
| |
197 |
120
| MethodSignature sig = new MethodSignature(void.class, methodName, new Class[]
|
198 |
| { propertyType }, null); |
199 |
| |
200 |
120
| op.addMethod(Modifier.PUBLIC, sig, body.toString(), location);
|
201 |
| } |
202 |
| |
203 |
42
| public void setErrorLog(ErrorLog errorLog)
|
204 |
| { |
205 |
42
| _errorLog = errorLog;
|
206 |
| } |
207 |
| |
208 |
42
| public void setBindingSource(BindingSource bindingSource)
|
209 |
| { |
210 |
42
| _bindingSource = bindingSource;
|
211 |
| } |
212 |
| } |