KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > tapestry > ValidationDelegate


1 package org.appfuse.webapp.tapestry;
2
3 import org.apache.tapestry.IMarkupWriter;
4 import org.apache.tapestry.IRender;
5 import org.apache.tapestry.IRequestCycle;
6 import org.apache.tapestry.form.IFormComponent;
7 import org.apache.tapestry.valid.IFieldTracking;
8 import org.apache.tapestry.valid.IValidator;
9 import org.apache.tapestry.valid.RenderString;
10
11 /**
12  * Custom Validation Delegate - based on one found in Tapestry in Action.
13  *
14  * @author Matt Raible
15  */

16 public class ValidationDelegate extends org.apache.tapestry.valid.ValidationDelegate {
17     private static final long serialVersionUID = 6658594142293597652L;
18
19     public void writeLabelPrefix(IFormComponent component,
20                                  IMarkupWriter writer, IRequestCycle cycle) {
21         // does nothing put prevent <font color="red"> from getting written
22
}
23
24     public void writeLabelSuffix(IFormComponent component,
25                                  IMarkupWriter writer, IRequestCycle cycle) {
26         if (component.isRequired()) {
27             writer.begin("span");
28             writer.attribute("class", "req");
29             writer.printRaw(" *");
30             writer.end();
31         }
32     }
33
34     public void writeAttributes(IMarkupWriter writer, IRequestCycle cycle,
35                                 IFormComponent component, IValidator validator) {
36         if (isInError()) {
37             String JavaDoc cssClass = ((component.getBinding("class") != null) ?
38                                 component.getBinding("class").getObject().toString() : "");
39             writer.attribute("class", cssClass + " error");
40         }
41     }
42
43     public void writePrefix(IMarkupWriter writer, IRequestCycle cycle,
44                             IFormComponent component, IValidator validator) {
45         if (isInError(component)) {
46             writer.begin("span");
47             writer.attribute("class", "fieldError");
48
49             writer.begin("img");
50             String JavaDoc ctxPath = cycle.getInfrastructure().getContextPath();
51             writer.attribute("src", ctxPath + "/images/iconWarning.gif");
52             writer.attribute("class", "validationWarning");
53             writer.attribute("alt", cycle.getPage().getMessages().getMessage("icon.warning"));
54             writer.end("img");
55
56             writer.printRaw("&nbsp;");
57
58             IFieldTracking tracking = getComponentTracking();
59             IRender render = tracking.getErrorRenderer();
60             String JavaDoc error = "";
61
62             if (render instanceof RenderString) {
63                 error = ((RenderString) render).getString();
64             }
65
66             writer.printRaw(error);
67             writer.end("span");
68         }
69     }
70
71     public void writeSuffix(IMarkupWriter writer, IRequestCycle cycle,
72                             IFormComponent component, IValidator validator) {
73         // prevent <font> tags from getting written when there's an error
74
}
75 }
76
Popular Tags