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