1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.valid; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.Collections; |
19 |
| import java.util.HashMap; |
20 |
| import java.util.Iterator; |
21 |
| import java.util.List; |
22 |
| import java.util.Map; |
23 |
| |
24 |
| import org.apache.tapestry.IMarkupWriter; |
25 |
| import org.apache.tapestry.IRender; |
26 |
| import org.apache.tapestry.IRequestCycle; |
27 |
| import org.apache.tapestry.Tapestry; |
28 |
| import org.apache.tapestry.form.IFormComponent; |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class ValidationDelegate implements IValidationDelegate |
39 |
| { |
40 |
| private static final long serialVersionUID = 6215074338439140780L; |
41 |
| |
42 |
| private transient IFormComponent _currentComponent; |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| private final List _trackings = new ArrayList(); |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| private final Map _trackingMap = new HashMap(); |
55 |
| |
56 |
25
| public void clear()
|
57 |
| { |
58 |
25
| _currentComponent = null;
|
59 |
25
| _trackings.clear();
|
60 |
25
| _trackingMap.clear();
|
61 |
| } |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
0
| public void writeLabelPrefix(IFormComponent component, IMarkupWriter writer, IRequestCycle cycle)
|
70 |
| { |
71 |
0
| if (isInError(component))
|
72 |
| { |
73 |
0
| writer.begin("font");
|
74 |
0
| writer.attribute("color", "red");
|
75 |
| } |
76 |
| } |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
0
| public void writeLabelSuffix(IFormComponent component, IMarkupWriter writer, IRequestCycle cycle)
|
85 |
| { |
86 |
0
| if (isInError(component))
|
87 |
| { |
88 |
0
| writer.end();
|
89 |
| } |
90 |
| } |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
72
| protected FieldTracking getComponentTracking()
|
107 |
| { |
108 |
72
| return (FieldTracking) _trackingMap.get(_currentComponent.getName());
|
109 |
| } |
110 |
| |
111 |
162
| public void setFormComponent(IFormComponent component)
|
112 |
| { |
113 |
162
| _currentComponent = component;
|
114 |
| } |
115 |
| |
116 |
50
| public boolean isInError()
|
117 |
| { |
118 |
50
| IFieldTracking tracking = getComponentTracking();
|
119 |
| |
120 |
50
| return tracking != null && tracking.isInError();
|
121 |
| } |
122 |
| |
123 |
0
| public String getFieldInputValue()
|
124 |
| { |
125 |
0
| IFieldTracking tracking = getComponentTracking();
|
126 |
| |
127 |
0
| return tracking == null ? null : tracking.getInput();
|
128 |
| } |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
7
| public List getFieldTracking()
|
135 |
| { |
136 |
7
| if (Tapestry.size(_trackings) == 0)
|
137 |
1
| return null;
|
138 |
| |
139 |
6
| return Collections.unmodifiableList(_trackings);
|
140 |
| } |
141 |
| |
142 |
| |
143 |
0
| public IFieldTracking getCurrentFieldTracking()
|
144 |
| { |
145 |
0
| return findCurrentTracking();
|
146 |
| } |
147 |
| |
148 |
3
| public void reset()
|
149 |
| { |
150 |
3
| IFieldTracking tracking = getComponentTracking();
|
151 |
| |
152 |
3
| if (tracking != null)
|
153 |
| { |
154 |
3
| _trackings.remove(tracking);
|
155 |
3
| _trackingMap.remove(tracking.getFieldName());
|
156 |
| } |
157 |
| } |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
8
| public void record(ValidatorException ex)
|
166 |
| { |
167 |
8
| IRender errorRenderer = ex.getErrorRenderer();
|
168 |
| |
169 |
8
| if (errorRenderer == null)
|
170 |
7
| record(ex.getMessage(), ex.getConstraint());
|
171 |
| else |
172 |
1
| record(errorRenderer, ex.getConstraint());
|
173 |
| } |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
8
| public void record(String message, ValidationConstraint constraint)
|
181 |
| { |
182 |
8
| record(new RenderString(message), constraint);
|
183 |
| } |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
10
| public void record(IRender errorRenderer, ValidationConstraint constraint)
|
199 |
| { |
200 |
10
| FieldTracking tracking = findCurrentTracking();
|
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
10
| tracking.setErrorRenderer(errorRenderer);
|
206 |
10
| tracking.setConstraint(constraint);
|
207 |
| } |
208 |
| |
209 |
10
| public void recordFieldInputValue(String input)
|
210 |
| { |
211 |
10
| FieldTracking tracking = findCurrentTracking();
|
212 |
| |
213 |
10
| tracking.setInput(input);
|
214 |
| } |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
20
| protected FieldTracking findCurrentTracking()
|
225 |
| { |
226 |
20
| FieldTracking result = null;
|
227 |
| |
228 |
20
| if (_currentComponent == null)
|
229 |
| { |
230 |
1
| result = new FieldTracking();
|
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
1
| _trackings.add(result);
|
236 |
| } |
237 |
| else |
238 |
| { |
239 |
19
| result = getComponentTracking();
|
240 |
| |
241 |
19
| if (result == null)
|
242 |
| { |
243 |
12
| String fieldName = _currentComponent.getName();
|
244 |
| |
245 |
12
| result = new FieldTracking(fieldName, _currentComponent);
|
246 |
| |
247 |
12
| _trackings.add(result);
|
248 |
12
| _trackingMap.put(fieldName, result);
|
249 |
| } |
250 |
| } |
251 |
| |
252 |
20
| return result;
|
253 |
| } |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
13
| public void writePrefix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component,
|
260 |
| IValidator validator) |
261 |
| { |
262 |
| } |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
15
| public void writeAttributes(IMarkupWriter writer, IRequestCycle cycle,
|
269 |
| IFormComponent component, IValidator validator) |
270 |
| { |
271 |
| } |
272 |
| |
273 |
| |
274 |
| |
275 |
| |
276 |
| |
277 |
| |
278 |
12
| public void writeSuffix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component,
|
279 |
| IValidator validator) |
280 |
| { |
281 |
12
| if (isInError())
|
282 |
| { |
283 |
0
| writer.printRaw(" ");
|
284 |
0
| writer.begin("font");
|
285 |
0
| writer.attribute("color", "red");
|
286 |
0
| writer.print("**");
|
287 |
0
| writer.end();
|
288 |
| } |
289 |
| } |
290 |
| |
291 |
6
| public boolean getHasErrors()
|
292 |
| { |
293 |
6
| return getFirstError() != null;
|
294 |
| } |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
12
| public IRender getFirstError()
|
303 |
| { |
304 |
12
| if (Tapestry.size(_trackings) == 0)
|
305 |
4
| return null;
|
306 |
| |
307 |
8
| Iterator i = _trackings.iterator();
|
308 |
| |
309 |
8
| while (i.hasNext())
|
310 |
| { |
311 |
10
| IFieldTracking tracking = (IFieldTracking) i.next();
|
312 |
| |
313 |
10
| if (tracking.isInError())
|
314 |
6
| return tracking.getErrorRenderer();
|
315 |
| } |
316 |
| |
317 |
2
| return null;
|
318 |
| } |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
0
| protected boolean isInError(IFormComponent component)
|
327 |
| { |
328 |
| |
329 |
| |
330 |
0
| String fieldName = component.getName();
|
331 |
| |
332 |
0
| IFieldTracking tracking = (IFieldTracking) _trackingMap.get(fieldName);
|
333 |
| |
334 |
0
| return tracking != null && tracking.isInError();
|
335 |
| } |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
| |
342 |
| |
343 |
| |
344 |
| |
345 |
| |
346 |
1
| public List getAssociatedTrackings()
|
347 |
| { |
348 |
1
| int count = Tapestry.size(_trackings);
|
349 |
| |
350 |
1
| if (count == 0)
|
351 |
0
| return null;
|
352 |
| |
353 |
1
| List result = new ArrayList(count);
|
354 |
| |
355 |
1
| for (int i = 0; i < count; i++)
|
356 |
| { |
357 |
2
| IFieldTracking tracking = (IFieldTracking) _trackings.get(i);
|
358 |
| |
359 |
2
| if (tracking.getFieldName() == null)
|
360 |
1
| continue;
|
361 |
| |
362 |
1
| result.add(tracking);
|
363 |
| } |
364 |
| |
365 |
1
| return result;
|
366 |
| } |
367 |
| |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
| |
373 |
| |
374 |
| |
375 |
| |
376 |
| |
377 |
1
| public List getUnassociatedTrackings()
|
378 |
| { |
379 |
1
| int count = Tapestry.size(_trackings);
|
380 |
| |
381 |
1
| if (count == 0)
|
382 |
0
| return null;
|
383 |
| |
384 |
1
| List result = new ArrayList(count);
|
385 |
| |
386 |
1
| for (int i = 0; i < count; i++)
|
387 |
| { |
388 |
2
| IFieldTracking tracking = (IFieldTracking) _trackings.get(i);
|
389 |
| |
390 |
2
| if (tracking.getFieldName() != null)
|
391 |
1
| continue;
|
392 |
| |
393 |
1
| result.add(tracking);
|
394 |
| } |
395 |
| |
396 |
1
| return result;
|
397 |
| } |
398 |
| |
399 |
2
| public List getErrorRenderers()
|
400 |
| { |
401 |
2
| List result = new ArrayList();
|
402 |
| |
403 |
2
| Iterator i = _trackings.iterator();
|
404 |
2
| while (i.hasNext())
|
405 |
| { |
406 |
2
| IFieldTracking tracking = (IFieldTracking) i.next();
|
407 |
| |
408 |
2
| IRender errorRenderer = tracking.getErrorRenderer();
|
409 |
| |
410 |
2
| if (errorRenderer != null)
|
411 |
1
| result.add(errorRenderer);
|
412 |
| } |
413 |
| |
414 |
2
| return result;
|
415 |
| } |
416 |
| } |