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