001 // Copyright 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.form.translator;
016
017 import java.util.Locale;
018
019 import org.apache.hivemind.HiveMind;
020 import org.apache.tapestry.IMarkupWriter;
021 import org.apache.tapestry.IRequestCycle;
022 import org.apache.tapestry.form.AbstractFormComponentContributor;
023 import org.apache.tapestry.form.FormComponentContributorContext;
024 import org.apache.tapestry.form.IFormComponent;
025 import org.apache.tapestry.form.ValidationMessages;
026 import org.apache.tapestry.valid.ValidatorException;
027
028 /**
029 * Abstract {@link Translator} implementation that provides default behavior for trimming, null
030 * object, and empty text handling.
031 *
032 * @author Paul Ferraro
033 * @since 4.0
034 */
035 public abstract class AbstractTranslator extends AbstractFormComponentContributor implements
036 Translator
037 {
038 private boolean _trim;
039
040 private String _message;
041
042 public AbstractTranslator()
043 {
044 }
045
046 // Needed until HIVEMIND-134 fix is available
047 public AbstractTranslator(String initializer)
048 {
049 super(initializer);
050 }
051
052 /**
053 * @see org.apache.tapestry.form.translator.Translator#format(org.apache.tapestry.form.IFormComponent,
054 * Locale, java.lang.Object)
055 */
056 public String format(IFormComponent field, Locale locale, Object object)
057 {
058 if (object == null)
059 return "";
060
061 return formatObject(field, locale, object);
062 }
063
064 /**
065 * @see org.apache.tapestry.form.translator.Translator#parse(org.apache.tapestry.form.IFormComponent,
066 * ValidationMessages, java.lang.String)
067 */
068 public Object parse(IFormComponent field, ValidationMessages messages, String text)
069 throws ValidatorException
070 {
071 String value = text == null ? null : (_trim ? text.trim() : text);
072
073 return HiveMind.isBlank(value) ? getValueForEmptyInput()
074 : parseText(field, messages, value);
075 }
076
077 protected abstract String formatObject(IFormComponent field, Locale locale, Object object);
078
079 protected abstract Object parseText(IFormComponent field, ValidationMessages messages,
080 String text) throws ValidatorException;
081
082 /**
083 * The value to be used when the value supplied in the request is blank (null or empty). The
084 * default value is null, but some subclasses may override.
085 *
086 * @see #parse(IFormComponent, ValidationMessages, String)
087 * @return null, subclasses may override
088 */
089 protected Object getValueForEmptyInput()
090 {
091 return null;
092 }
093
094 protected String buildMessage(ValidationMessages messages, IFormComponent field, String key)
095 {
096 String label = field.getDisplayName();
097
098 Object[] parameters = getMessageParameters(messages.getLocale(), label);
099
100 return messages.formatValidationMessage(_message, key, parameters);
101 }
102
103 protected Object[] getMessageParameters(Locale locale, String label)
104 {
105 return new Object[]
106 { label };
107 }
108
109 /**
110 * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IRequestCycle,
111 * org.apache.tapestry.form.IFormComponent)
112 */
113 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
114 FormComponentContributorContext context, IFormComponent field)
115 {
116 super.renderContribution(writer, cycle, context, field);
117
118 if (_trim)
119 context.addSubmitHandler("function (event) { Tapestry.trim_field_value('"
120 + field.getClientId() + "'); }");
121 }
122
123 public boolean isTrim()
124
125 {
126 return _trim;
127 }
128
129 public void setTrim(boolean trim)
130 {
131 _trim = trim;
132 }
133
134 public String getMessage()
135 {
136 return _message;
137 }
138
139 public void setMessage(String message)
140 {
141 _message = message;
142 }
143 }