View Javadoc

1   package org.appfuse.webapp.services.impl;
2   
3   import org.apache.tapestry5.Asset;
4   import org.apache.tapestry5.Field;
5   import org.apache.tapestry5.MarkupWriter;
6   import org.apache.tapestry5.ValidationDecorator;
7   import org.apache.tapestry5.ValidationTracker;
8   import org.apache.tapestry5.dom.Element;
9   import org.apache.tapestry5.services.Environment;
10  
11  /**
12   * Custom Implementation of the ValidationDecorator Interface
13   *
14   * @author Serge Eby
15   * @version $Id: ValidationDelegate.java 5 2008-08-30 09:59:21Z serge.eby $
16   */
17  public class ValidationDelegate implements ValidationDecorator {
18  
19      private final Environment environment;
20      private final Asset iconAsset;
21      private final MarkupWriter markupWriter;
22  
23      public ValidationDelegate(Environment environment, Asset iconAsset,
24                                MarkupWriter markupWriter) {
25          this.environment = environment;
26          this.markupWriter = markupWriter;
27          this.iconAsset = iconAsset;
28      }
29  
30      public void insideField(Field field) {
31  
32          if (inError(field)) {
33              if (field.isRequired() && isMissing(field)) {
34                  addErrorClassToCurrentElement("fieldMissing");
35                  return;
36              }
37              addErrorClassToCurrentElement("fieldInvalid");
38          }
39      }
40  
41      public void beforeLabel(Field field) {
42  
43      }
44  
45      public void insideLabel(Field field, Element labelElement) {
46          if (inError(field)) {
47              addErrorClassToCurrentElement("error");
48          }
49          /* http://www.nabble.com/Problem-using-BeanEditForm-with-a-POJO-td23349016s302.html" target="alexandria_uri">http://www.nabble.com/Problem-using-BeanEditForm-with-a-POJO-td23349016s302.html#a23352398
50          if (field.isRequired()) {
51              labelElement.raw("<span class=\"req\"> *</span>");
52          }*/
53  
54      }
55  
56      public void afterLabel(Field field) {
57      }
58  
59      public void beforeField(Field field) {
60          if (inError(field)) {
61              markupWriter.element("span", "class", "fieldError");
62              String iconId = field.getClientId() + ":icon";
63              markupWriter.element("img", "src", iconAsset.toClientURL(), "alt",
64                      "", "class", "validationWarning", "id", iconId);
65              markupWriter.end(); // img
66              markupWriter.writeRaw("&nbsp;");
67  
68              String error = getError(field);
69              if (error == null) {
70                  error = "";
71              }
72              markupWriter.writeRaw(error);
73              markupWriter.end(); // span
74          }
75  
76      }
77  
78      public void afterField(Field field) {
79      }
80  
81      private boolean inError(Field field) {
82          ValidationTracker tracker = environment
83                  .peekRequired(ValidationTracker.class);
84  
85          return tracker.inError(field);
86      }
87  
88      private String getError(Field field) {
89          ValidationTracker tracker = environment
90                  .peekRequired(ValidationTracker.class);
91          return tracker.getError(field);
92      }
93  
94      private boolean isMissing(Field field) {
95          return true; // FIXME: Determine if field wasn't populated
96      }
97  
98      private void addErrorClassToCurrentElement(String errorClass) {
99          markupWriter.getElement().addClassName(errorClass);
100     }
101 
102 }