View Javadoc

1   package net.sf.tacos.form.translator;
2   
3   import java.util.Locale;
4   
5   import org.apache.tapestry.form.IFormComponent;
6   import org.apache.tapestry.form.ValidationMessages;
7   import org.apache.tapestry.form.AbstractFormComponentContributor;
8   import org.apache.tapestry.form.FormComponentContributorContext;
9   import org.apache.tapestry.form.translator.Translator;
10  import org.apache.tapestry.valid.ValidatorException;
11  import org.apache.tapestry.valid.ValidationConstants;
12  import org.apache.tapestry.IMarkupWriter;
13  import org.apache.tapestry.IRequestCycle;
14  import org.apache.tapestry.json.JSONObject;
15  import org.apache.hivemind.HiveMind;
16  
17  /**
18   * The generics version of Tapestry's AbstractTranslator.
19   */
20  public abstract class AbstractTranslator<T> extends AbstractFormComponentContributor implements Translator
21  {
22      private boolean _trim;
23  
24      private String _message;
25  
26      public AbstractTranslator()
27      {
28      }
29  
30      // Needed until HIVEMIND-134 fix is available
31      public AbstractTranslator(String initializer)
32      {
33          super(initializer);
34      }
35  
36      /**
37       * @see org.apache.tapestry.form.translator.Translator#format(org.apache.tapestry.form.IFormComponent,
38       *      Locale, java.lang.Object)
39       */
40      public String format(IFormComponent field, Locale locale, Object object)
41      {
42          if (object == null)
43              return "";
44  
45          return formatObject(field, locale, (T) object);
46      }
47  
48      /**
49       * @see org.apache.tapestry.form.translator.Translator#parse(org.apache.tapestry.form.IFormComponent,
50       *      ValidationMessages, java.lang.String)
51       */
52      public Object parse(IFormComponent field, ValidationMessages messages, String text)
53              throws ValidatorException
54      {
55          String value = text == null ? null : (_trim ? text.trim() : text);
56  
57          return HiveMind.isBlank(value) ? getValueForEmptyInput() : parseText(field, messages, value);
58      }
59  
60      protected abstract String formatObject(IFormComponent field, Locale locale, T object);
61  
62      protected abstract T parseText(IFormComponent field, ValidationMessages messages, String text)
63      throws ValidatorException;
64  
65      /**
66       * The value to be used when the value supplied in the request is blank (null or empty). The
67       * default value is null, but some subclasses may override.
68       *
69       * @see #parse(IFormComponent, ValidationMessages, String)
70       * @return null, subclasses may override
71       */
72      protected Object getValueForEmptyInput()
73      {
74          return null;
75      }
76  
77      protected String buildMessage(ValidationMessages messages, IFormComponent field, String key)
78      {
79          String label = field.getDisplayName();
80  
81          Object[] parameters = getMessageParameters(messages.getLocale(), label);
82  
83          return messages.formatValidationMessage(_message, key, parameters);
84      }
85  
86      protected Object[] getMessageParameters(Locale locale, String label)
87      {
88          return new Object[] { label };
89      }
90  
91      /**
92       * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter , org.apache.tapestry.IRequestCycle , org.apache.tapestry.form.FormComponentContributorContext , IFormComponent)
93       */
94      public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
95              FormComponentContributorContext context, IFormComponent field)
96      {
97          super.renderContribution(writer, cycle, context, field);
98  
99          if (_trim) {
100             JSONObject profile = context.getProfile();
101 
102             accumulateProperty(profile, ValidationConstants.TRIM, field.getClientId());
103         }
104     }
105 
106     public boolean isTrim()
107     {
108         return _trim;
109     }
110 
111     public void setTrim(boolean trim)
112     {
113         _trim = trim;
114     }
115 
116     public String getMessage()
117     {
118         return _message;
119     }
120 
121     public void setMessage(String message)
122     {
123         _message = message;
124     }
125 }