KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > ValidField


1 // Copyright 2004, 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.valid;
16
17 import org.apache.tapestry.IMarkupWriter;
18 import org.apache.tapestry.IRequestCycle;
19 import org.apache.tapestry.Tapestry;
20 import org.apache.tapestry.form.AbstractFormComponent;
21 import org.apache.tapestry.form.Form;
22
23 /**
24  * A {@link Form}component that creates a text field that allows for validation of user input and
25  * conversion between string and object values. [ <a
26  * HREF="../../../../../ComponentReference/ValidField.html">Component Reference </a>]
27  * <p>
28  * A ValidatingTextField uses an {@link IValidationDelegate} to track errors and an
29  * {@link IValidator}to convert between strings and objects (as well as perform validations). The
30  * validation delegate is shared by all validating text fields in a form, the validator may be
31  * shared my multiple elements as desired.
32  *
33  * @author Howard Lewis Ship
34  */

35
36 public abstract class ValidField extends AbstractFormComponent
37 {
38     public abstract boolean isHidden();
39
40     public abstract boolean isDisabled();
41
42     public abstract Object JavaDoc getValue();
43
44     public abstract void setValue(Object JavaDoc value);
45
46     /** Parameter */
47
48     public abstract String JavaDoc getDisplayName();
49
50     /**
51      * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
52      * org.apache.tapestry.IRequestCycle)
53      */

54     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
55     {
56         IValidationDelegate delegate = getForm().getDelegate();
57
58         delegate.writePrefix(writer, cycle, this, null);
59
60         writer.beginEmpty("input");
61
62         writer.attribute("type", isHidden() ? "password" : "text");
63
64         if (isDisabled())
65             writer.attribute("disabled", "disabled");
66
67         writer.attribute("name", getName());
68
69         String JavaDoc value = readValue();
70         if (value != null)
71             writer.attribute("value", value);
72
73         renderIdAttribute(writer, cycle);
74
75         renderInformalParameters(writer, cycle);
76
77         delegate.writeAttributes(writer, cycle, this, null);
78
79         IValidator validator = getValidator();
80
81         if (validator == null)
82             throw Tapestry.createRequiredParameterException(this, "validator");
83
84         validator.renderValidatorContribution(this, writer, cycle);
85
86         writer.closeTag();
87
88         delegate.writeSuffix(writer, cycle, this, null);
89     }
90
91     /**
92      * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter,
93      * org.apache.tapestry.IRequestCycle)
94      */

95     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
96     {
97         String JavaDoc value = cycle.getParameter(getName());
98
99         updateValue(value);
100     }
101
102     protected String JavaDoc readValue()
103     {
104         IValidator validator = getValidator();
105         if (validator == null)
106             throw Tapestry.createRequiredParameterException(this, "validator");
107
108         IValidationDelegate delegate = getForm().getDelegate();
109
110         if (delegate.isInError())
111             return delegate.getFieldInputValue();
112
113         Object JavaDoc value = getValue();
114
115         String JavaDoc result = validator.toString(this, value);
116
117         return result;
118     }
119
120     protected void updateValue(String JavaDoc value)
121     {
122         Object JavaDoc objectValue = null;
123
124         IValidator validator = getValidator();
125         if (validator == null)
126             throw Tapestry.createRequiredParameterException(this, "validator");
127
128         IValidationDelegate delegate = getForm().getDelegate();
129
130         delegate.recordFieldInputValue(value);
131
132         try
133         {
134             objectValue = validator.toObject(this, value);
135         }
136         catch (ValidatorException ex)
137         {
138             delegate.record(ex);
139             return;
140         }
141
142         setValue(objectValue);
143     }
144
145     public abstract IValidator getValidator();
146 }
Popular Tags