KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > ValidatableFieldSupportImpl


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form;
16
17 import java.util.Iterator JavaDoc;
18 import java.util.Locale JavaDoc;
19
20 import org.apache.hivemind.HiveMind;
21 import org.apache.hivemind.service.ThreadLocale;
22 import org.apache.tapestry.IMarkupWriter;
23 import org.apache.tapestry.IRequestCycle;
24 import org.apache.tapestry.coerce.ValueConverter;
25 import org.apache.tapestry.form.validator.Validator;
26 import org.apache.tapestry.valid.IValidationDelegate;
27 import org.apache.tapestry.valid.ValidatorException;
28
29 /**
30  * Default {@link VadidatableFieldSupport} implementation. This implementation generates calls to a
31  * static javascript function during render if client-side validation is enabled.
32  *
33  * @author Paul Ferraro
34  * @since 4.0
35  */

36 public class ValidatableFieldSupportImpl implements ValidatableFieldSupport
37 {
38     private ValueConverter _converter;
39
40     private ThreadLocale _threadLocale;
41
42     public void setValueConverter(ValueConverter converter)
43     {
44         _converter = converter;
45     }
46
47     public void setThreadLocale(ThreadLocale threadLocale)
48     {
49         _threadLocale = threadLocale;
50     }
51
52     protected Iterator JavaDoc getValidatorsIterator(ValidatableField component)
53     {
54         return (Iterator JavaDoc) _converter.coerceValue(component.getValidators(), Iterator JavaDoc.class);
55     }
56
57     /**
58      * @see org.apache.tapestry.form.ValidatableFieldSupport#render(org.apache.tapestry.form.ValidatableField,
59      * org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
60      */

61     public void render(ValidatableField component, IMarkupWriter writer, IRequestCycle cycle)
62     {
63         IValidationDelegate delegate = component.getForm().getDelegate();
64
65         String JavaDoc value = delegate.isInError() ? delegate.getFieldInputValue()
66                 : getTranslatedValue(component);
67
68         component.render(writer, cycle, value);
69     }
70
71     /**
72      * @see org.apache.tapestry.form.ValidatableFieldSupport#renderContributions(org.apache.tapestry.form.ValidatableField,
73      * org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
74      */

75     public void renderContributions(ValidatableField component, IMarkupWriter writer,
76             IRequestCycle cycle)
77     {
78         if (component.getForm().isClientValidationEnabled())
79         {
80             Locale JavaDoc locale = component.getPage().getLocale();
81             FormComponentContributorContext context = new FormComponentContributorContextImpl(
82                     locale, cycle, component);
83
84             component.getTranslator().renderContribution(writer, cycle, context, component);
85
86             Iterator JavaDoc validators = getValidatorsIterator(component);
87
88             while (validators.hasNext())
89             {
90                 Validator validator = (Validator) validators.next();
91
92                 validator.renderContribution(writer, cycle, context, component);
93             }
94         }
95     }
96
97     protected String JavaDoc getTranslatedValue(ValidatableField component)
98     {
99         Object JavaDoc value = component.readValue();
100
101         return (value != null) ? component.getTranslator().format(component, value) : "";
102     }
103
104     /**
105      * @see org.apache.tapestry.form.ValidatableFieldSupport#bind(org.apache.tapestry.form.ValidatableField,
106      * org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle, java.lang.String)
107      */

108     public void bind(ValidatableField component, IMarkupWriter writer, IRequestCycle cycle,
109             String JavaDoc value)
110     {
111         IValidationDelegate delegate = component.getForm().getDelegate();
112
113         delegate.recordFieldInputValue(value);
114
115         try
116         {
117             Object JavaDoc object = HiveMind.isBlank(value) ? null : component.getTranslator().parse(
118                     component,
119                     value);
120
121             boolean isNonNull = object != null;
122
123             Iterator JavaDoc validators = getValidatorsIterator(component);
124
125             ValidationMessages messages = new ValidationMessagesImpl(_threadLocale.getLocale());
126
127             while (validators.hasNext())
128             {
129                 Validator validator = (Validator) validators.next();
130
131                 if (isNonNull || validator.getAcceptsNull())
132                     validator.validate(component, messages, object);
133             }
134
135             // Only get here if it translated OK and then validated OK.
136

137             // NOTE: May need to have the Translator implement a provideNullValue() method, i.e.,
138
// to return 0 if the translator is concerned with numbers.
139

140             component.writeValue(object);
141         }
142         catch (ValidatorException ex)
143         {
144             delegate.record(ex);
145         }
146     }
147 }
148
Popular Tags