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 |
13 |
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 |
13 |
private transient int _focusPriority = -1; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
13 |
private final List _trackings = new ArrayList(); |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
13 |
private final Map _trackingMap = new HashMap(); |
56 |
|
|
57 |
|
public void clear() |
58 |
|
{ |
59 |
14 |
_currentComponent = null; |
60 |
14 |
_trackings.clear(); |
61 |
14 |
_trackingMap.clear(); |
62 |
14 |
} |
63 |
|
|
64 |
|
public void clearErrors() |
65 |
|
{ |
66 |
1 |
if (_trackings == null) |
67 |
0 |
return; |
68 |
|
|
69 |
1 |
Iterator i = _trackings.iterator(); |
70 |
2 |
while(i.hasNext()) |
71 |
|
{ |
72 |
1 |
FieldTracking ft = (FieldTracking) i.next(); |
73 |
1 |
ft.setErrorRenderer(null); |
74 |
1 |
ft.setRenderError(false); |
75 |
1 |
} |
76 |
1 |
} |
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 |
5 |
} |
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 |
31 |
return (FieldTracking) _trackingMap.get(_currentComponent.getName()); |
150 |
|
} |
151 |
|
|
152 |
|
public void setFormComponent(IFormComponent component) |
153 |
|
{ |
154 |
23 |
_currentComponent = component; |
155 |
23 |
} |
156 |
|
|
157 |
|
public boolean isInError() |
158 |
|
{ |
159 |
2 |
IFieldTracking tracking = getComponentTracking(); |
160 |
|
|
161 |
2 |
return tracking != null && tracking.isInError(); |
162 |
|
} |
163 |
|
|
164 |
|
public String getFieldInputValue() |
165 |
|
{ |
166 |
1 |
IFieldTracking tracking = getComponentTracking(); |
167 |
|
|
168 |
1 |
return tracking == null ? null : tracking.getInput(); |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
public List getFieldTracking() |
176 |
|
{ |
177 |
8 |
if (Tapestry.size(_trackings) == 0) return null; |
178 |
|
|
179 |
7 |
return Collections.unmodifiableList(_trackings); |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
public IFieldTracking getCurrentFieldTracking() |
184 |
|
{ |
185 |
0 |
return findCurrentTracking(); |
186 |
|
} |
187 |
|
|
188 |
|
public void reset() |
189 |
|
{ |
190 |
3 |
IFieldTracking tracking = getComponentTracking(); |
191 |
|
|
192 |
3 |
if (tracking != null) |
193 |
|
{ |
194 |
3 |
_trackings.remove(tracking); |
195 |
3 |
_trackingMap.remove(tracking.getFieldName()); |
196 |
|
} |
197 |
3 |
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
public void record(ValidatorException ex) |
207 |
|
{ |
208 |
8 |
IRender errorRenderer = ex.getErrorRenderer(); |
209 |
|
|
210 |
8 |
if (errorRenderer == null) |
211 |
7 |
record(ex.getMessage(), ex.getConstraint()); |
212 |
|
else |
213 |
1 |
record(errorRenderer, ex.getConstraint()); |
214 |
8 |
} |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
public void record(String message, ValidationConstraint constraint) |
222 |
|
{ |
223 |
11 |
record(new RenderString(message), constraint); |
224 |
11 |
} |
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 |
13 |
FieldTracking tracking = findCurrentTracking(); |
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
13 |
tracking.setErrorRenderer(errorRenderer); |
249 |
13 |
tracking.setConstraint(constraint); |
250 |
13 |
} |
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
public void record(IFormComponent field, String message) |
255 |
|
{ |
256 |
2 |
setFormComponent(field); |
257 |
|
|
258 |
2 |
record(message, null); |
259 |
2 |
} |
260 |
|
|
261 |
|
public void recordFieldInputValue(String input) |
262 |
|
{ |
263 |
11 |
FieldTracking tracking = findCurrentTracking(); |
264 |
|
|
265 |
11 |
tracking.setInput(input); |
266 |
11 |
} |
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
protected FieldTracking findCurrentTracking() |
277 |
|
{ |
278 |
24 |
FieldTracking result = null; |
279 |
|
|
280 |
24 |
if (_currentComponent == null) |
281 |
|
{ |
282 |
2 |
result = new FieldTracking(); |
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
2 |
_trackings.add(result); |
288 |
|
} |
289 |
|
else |
290 |
|
{ |
291 |
22 |
result = getComponentTracking(); |
292 |
|
|
293 |
22 |
if (result == null) |
294 |
|
{ |
295 |
14 |
String fieldName = _currentComponent.getName(); |
296 |
|
|
297 |
14 |
result = new FieldTracking(fieldName, _currentComponent); |
298 |
|
|
299 |
14 |
_trackings.add(result); |
300 |
14 |
_trackingMap.put(fieldName, result); |
301 |
|
} |
302 |
|
} |
303 |
|
|
304 |
24 |
return result; |
305 |
|
} |
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
public void writePrefix(IMarkupWriter writer, IRequestCycle cycle, |
312 |
|
IFormComponent component, IValidator validator) |
313 |
|
{ |
314 |
3 |
} |
315 |
|
|
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
public void writeAttributes(IMarkupWriter writer, IRequestCycle cycle, |
323 |
|
IFormComponent component, IValidator validator) |
324 |
|
{ |
325 |
3 |
IFieldTracking tracking = getFieldTracking(component); |
326 |
3 |
if (tracking == null || !tracking.getRenderError()) |
327 |
3 |
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 |
3 |
IFieldTracking tracking = getComponentTracking(); |
350 |
|
|
351 |
3 |
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 |
3 |
} |
360 |
|
|
361 |
|
public boolean getHasErrors() |
362 |
|
{ |
363 |
8 |
return getFirstError() != null; |
364 |
|
} |
365 |
|
|
366 |
|
|
367 |
|
|
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
|
373 |
|
public IRender getFirstError() |
374 |
|
{ |
375 |
15 |
if (Tapestry.size(_trackings) == 0) |
376 |
4 |
return null; |
377 |
|
|
378 |
11 |
Iterator i = _trackings.iterator(); |
379 |
|
|
380 |
16 |
while(i.hasNext()) |
381 |
|
{ |
382 |
13 |
IFieldTracking tracking = (IFieldTracking) i.next(); |
383 |
|
|
384 |
13 |
if (tracking.isInError()) return tracking.getErrorRenderer(); |
385 |
5 |
} |
386 |
|
|
387 |
3 |
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 |
3 |
String fieldName = component.getName(); |
411 |
|
|
412 |
3 |
return (IFieldTracking) _trackingMap.get(fieldName); |
413 |
|
} |
414 |
|
|
415 |
|
|
416 |
|
|
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
public List getAssociatedTrackings() |
425 |
|
{ |
426 |
1 |
int count = Tapestry.size(_trackings); |
427 |
|
|
428 |
1 |
if (count == 0) return null; |
429 |
|
|
430 |
1 |
List result = new ArrayList(count); |
431 |
|
|
432 |
3 |
for(int i = 0; i < count; i++) |
433 |
|
{ |
434 |
2 |
IFieldTracking tracking = (IFieldTracking) _trackings.get(i); |
435 |
|
|
436 |
2 |
if (tracking.getFieldName() == null) continue; |
437 |
|
|
438 |
1 |
result.add(tracking); |
439 |
|
} |
440 |
|
|
441 |
1 |
return result; |
442 |
|
} |
443 |
|
|
444 |
|
|
445 |
|
|
446 |
|
|
447 |
|
|
448 |
|
|
449 |
|
|
450 |
|
|
451 |
|
|
452 |
|
|
453 |
|
|
454 |
|
|
455 |
|
public List getUnassociatedTrackings() |
456 |
|
{ |
457 |
2 |
int count = Tapestry.size(_trackings); |
458 |
|
|
459 |
2 |
if (count == 0) return null; |
460 |
|
|
461 |
2 |
List result = new ArrayList(count); |
462 |
|
|
463 |
5 |
for(int i = 0; i < count; i++) |
464 |
|
{ |
465 |
3 |
IFieldTracking tracking = (IFieldTracking) _trackings.get(i); |
466 |
|
|
467 |
3 |
if (tracking.getFieldName() != null) continue; |
468 |
|
|
469 |
2 |
result.add(tracking); |
470 |
|
} |
471 |
|
|
472 |
2 |
return result; |
473 |
|
} |
474 |
|
|
475 |
|
public List getErrorRenderers() |
476 |
|
{ |
477 |
2 |
List result = new ArrayList(); |
478 |
|
|
479 |
2 |
Iterator i = _trackings.iterator(); |
480 |
4 |
while(i.hasNext()) |
481 |
|
{ |
482 |
2 |
IFieldTracking tracking = (IFieldTracking) i.next(); |
483 |
|
|
484 |
2 |
IRender errorRenderer = tracking.getErrorRenderer(); |
485 |
|
|
486 |
2 |
if (errorRenderer != null) |
487 |
1 |
result.add(errorRenderer); |
488 |
2 |
} |
489 |
|
|
490 |
2 |
return result; |
491 |
|
} |
492 |
|
|
493 |
|
|
494 |
|
|
495 |
|
public void registerForFocus(IFormComponent field, int priority) |
496 |
|
{ |
497 |
5 |
if (priority > _focusPriority) |
498 |
|
{ |
499 |
4 |
_focusField = field.getClientId(); |
500 |
4 |
_focusPriority = priority; |
501 |
|
} |
502 |
5 |
} |
503 |
|
|
504 |
|
|
505 |
|
|
506 |
|
|
507 |
|
|
508 |
|
|
509 |
|
public String getFocusField() |
510 |
|
{ |
511 |
1 |
return _focusField; |
512 |
|
} |
513 |
|
|
514 |
|
} |