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 |
52
| public void clear()
|
61 |
| { |
62 |
52
| _currentComponent = null;
|
63 |
52
| _trackings.clear();
|
64 |
52
| _trackingMap.clear();
|
65 |
| } |
66 |
| |
67 |
2
| public void clearErrors()
|
68 |
| { |
69 |
2
| if (_trackings == null)
|
70 |
0
| return;
|
71 |
| |
72 |
2
| Iterator i = _trackings.iterator();
|
73 |
2
| while (i.hasNext())
|
74 |
| { |
75 |
2
| FieldTracking ft = (FieldTracking) i.next();
|
76 |
2
| 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 |
10
| 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 |
110
| protected FieldTracking getComponentTracking()
|
132 |
| { |
133 |
110
| return (FieldTracking) _trackingMap.get(_currentComponent.getName());
|
134 |
| } |
135 |
| |
136 |
336
| public void setFormComponent(IFormComponent component)
|
137 |
| { |
138 |
336
| _currentComponent = component;
|
139 |
| } |
140 |
| |
141 |
58
| public boolean isInError()
|
142 |
| { |
143 |
58
| IFieldTracking tracking = getComponentTracking();
|
144 |
| |
145 |
58
| return tracking != null && tracking.isInError();
|
146 |
| } |
147 |
| |
148 |
2
| public String getFieldInputValue()
|
149 |
| { |
150 |
2
| IFieldTracking tracking = getComponentTracking();
|
151 |
| |
152 |
2
| return tracking == null ? null : tracking.getInput();
|
153 |
| } |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
16
| public List getFieldTracking()
|
160 |
| { |
161 |
16
| if (Tapestry.size(_trackings) == 0)
|
162 |
2
| return null;
|
163 |
| |
164 |
14
| return Collections.unmodifiableList(_trackings);
|
165 |
| } |
166 |
| |
167 |
| |
168 |
0
| public IFieldTracking getCurrentFieldTracking()
|
169 |
| { |
170 |
0
| return findCurrentTracking();
|
171 |
| } |
172 |
| |
173 |
6
| public void reset()
|
174 |
| { |
175 |
6
| IFieldTracking tracking = getComponentTracking();
|
176 |
| |
177 |
6
| if (tracking != null)
|
178 |
| { |
179 |
6
| _trackings.remove(tracking);
|
180 |
6
| _trackingMap.remove(tracking.getFieldName());
|
181 |
| } |
182 |
| } |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
16
| public void record(ValidatorException ex)
|
191 |
| { |
192 |
16
| IRender errorRenderer = ex.getErrorRenderer();
|
193 |
| |
194 |
16
| if (errorRenderer == null)
|
195 |
14
| record(ex.getMessage(), ex.getConstraint());
|
196 |
| else |
197 |
2
| record(errorRenderer, ex.getConstraint());
|
198 |
| } |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
22
| public void record(String message, ValidationConstraint constraint)
|
206 |
| { |
207 |
22
| record(new RenderString(message), constraint);
|
208 |
| } |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
26
| public void record(IRender errorRenderer, ValidationConstraint constraint)
|
224 |
| { |
225 |
26
| FieldTracking tracking = findCurrentTracking();
|
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
26
| tracking.setErrorRenderer(errorRenderer);
|
231 |
26
| tracking.setConstraint(constraint);
|
232 |
| } |
233 |
| |
234 |
| |
235 |
| |
236 |
4
| public void record(IFormComponent field, String message)
|
237 |
| { |
238 |
4
| setFormComponent(field);
|
239 |
| |
240 |
4
| record(message, null);
|
241 |
| } |
242 |
| |
243 |
22
| public void recordFieldInputValue(String input)
|
244 |
| { |
245 |
22
| FieldTracking tracking = findCurrentTracking();
|
246 |
| |
247 |
22
| tracking.setInput(input);
|
248 |
| } |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
48
| protected FieldTracking findCurrentTracking()
|
259 |
| { |
260 |
48
| FieldTracking result = null;
|
261 |
| |
262 |
48
| if (_currentComponent == null)
|
263 |
| { |
264 |
4
| result = new FieldTracking();
|
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
4
| _trackings.add(result);
|
270 |
| } |
271 |
| else |
272 |
| { |
273 |
44
| result = getComponentTracking();
|
274 |
| |
275 |
44
| if (result == null)
|
276 |
| { |
277 |
28
| String fieldName = _currentComponent.getName();
|
278 |
| |
279 |
28
| result = new FieldTracking(fieldName, _currentComponent);
|
280 |
| |
281 |
28
| _trackings.add(result);
|
282 |
28
| _trackingMap.put(fieldName, result);
|
283 |
| } |
284 |
| } |
285 |
| |
286 |
48
| return result;
|
287 |
| } |
288 |
| |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
26
| public void writePrefix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component,
|
294 |
| IValidator validator) |
295 |
| { |
296 |
| } |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
26
| public void writeAttributes(IMarkupWriter writer, IRequestCycle cycle,
|
303 |
| IFormComponent component, IValidator validator) |
304 |
| { |
305 |
| } |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
| |
312 |
24
| public void writeSuffix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component,
|
313 |
| IValidator validator) |
314 |
| { |
315 |
24
| 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 |
64
| public boolean getHasErrors()
|
326 |
| { |
327 |
64
| return getFirstError() != null;
|
328 |
| } |
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
| |
334 |
| |
335 |
| |
336 |
78
| public IRender getFirstError()
|
337 |
| { |
338 |
78
| if (Tapestry.size(_trackings) == 0)
|
339 |
56
| return null;
|
340 |
| |
341 |
22
| Iterator i = _trackings.iterator();
|
342 |
| |
343 |
22
| while (i.hasNext())
|
344 |
| { |
345 |
26
| IFieldTracking tracking = (IFieldTracking) i.next();
|
346 |
| |
347 |
26
| if (tracking.isInError())
|
348 |
16
| return tracking.getErrorRenderer();
|
349 |
| } |
350 |
| |
351 |
6
| 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 |
2
| public List getAssociatedTrackings()
|
381 |
| { |
382 |
2
| int count = Tapestry.size(_trackings);
|
383 |
| |
384 |
2
| if (count == 0)
|
385 |
0
| return null;
|
386 |
| |
387 |
2
| List result = new ArrayList(count);
|
388 |
| |
389 |
2
| for (int i = 0; i < count; i++)
|
390 |
| { |
391 |
4
| IFieldTracking tracking = (IFieldTracking) _trackings.get(i);
|
392 |
| |
393 |
4
| if (tracking.getFieldName() == null)
|
394 |
2
| continue;
|
395 |
| |
396 |
2
| result.add(tracking);
|
397 |
| } |
398 |
| |
399 |
2
| return result;
|
400 |
| } |
401 |
| |
402 |
| |
403 |
| |
404 |
| |
405 |
| |
406 |
| |
407 |
| |
408 |
| |
409 |
| |
410 |
| |
411 |
4
| public List getUnassociatedTrackings()
|
412 |
| { |
413 |
4
| int count = Tapestry.size(_trackings);
|
414 |
| |
415 |
4
| if (count == 0)
|
416 |
0
| return null;
|
417 |
| |
418 |
4
| List result = new ArrayList(count);
|
419 |
| |
420 |
4
| for (int i = 0; i < count; i++)
|
421 |
| { |
422 |
6
| IFieldTracking tracking = (IFieldTracking) _trackings.get(i);
|
423 |
| |
424 |
6
| if (tracking.getFieldName() != null)
|
425 |
2
| continue;
|
426 |
| |
427 |
4
| result.add(tracking);
|
428 |
| } |
429 |
| |
430 |
4
| return result;
|
431 |
| } |
432 |
| |
433 |
4
| public List getErrorRenderers()
|
434 |
| { |
435 |
4
| List result = new ArrayList();
|
436 |
| |
437 |
4
| Iterator i = _trackings.iterator();
|
438 |
4
| while (i.hasNext())
|
439 |
| { |
440 |
4
| IFieldTracking tracking = (IFieldTracking) i.next();
|
441 |
| |
442 |
4
| IRender errorRenderer = tracking.getErrorRenderer();
|
443 |
| |
444 |
4
| if (errorRenderer != null)
|
445 |
2
| result.add(errorRenderer);
|
446 |
| } |
447 |
| |
448 |
4
| return result;
|
449 |
| } |
450 |
| |
451 |
| |
452 |
| |
453 |
40
| public void registerForFocus(IFormComponent field, int priority)
|
454 |
| { |
455 |
40
| if (priority > _focusPriority)
|
456 |
| { |
457 |
28
| _focusField = field.getClientId();
|
458 |
28
| _focusPriority = priority;
|
459 |
| } |
460 |
| } |
461 |
| |
462 |
| |
463 |
| |
464 |
| |
465 |
| |
466 |
| |
467 |
88
| public String getFocusField()
|
468 |
| { |
469 |
88
| return _focusField;
|
470 |
| } |
471 |
| |
472 |
| } |