|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ValidField.java | 71.4% | 87.8% | 100% | 84.7% |
|
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 getValue(); | |
43 | ||
44 | public abstract void setValue(Object value); | |
45 | ||
46 | /** Parameter */ | |
47 | ||
48 | public abstract String getDisplayName(); | |
49 | ||
50 | /** | |
51 | * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter, | |
52 | * org.apache.tapestry.IRequestCycle) | |
53 | */ | |
54 | 4 | protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
55 | { | |
56 | 4 | IValidationDelegate delegate = getForm().getDelegate(); |
57 | ||
58 | 4 | delegate.writePrefix(writer, cycle, this, null); |
59 | ||
60 | 4 | writer.beginEmpty("input"); |
61 | ||
62 | 4 | writer.attribute("type", isHidden() ? "password" : "text"); |
63 | ||
64 | 4 | if (isDisabled()) |
65 | 0 | writer.attribute("disabled", "disabled"); |
66 | ||
67 | 4 | writer.attribute("name", getName()); |
68 | ||
69 | 4 | String value = readValue(); |
70 | 4 | if (value != null) |
71 | 3 | writer.attribute("value", value); |
72 | ||
73 | 4 | renderIdAttribute(writer, cycle); |
74 | ||
75 | 4 | renderInformalParameters(writer, cycle); |
76 | ||
77 | 4 | delegate.writeAttributes(writer, cycle, this, null); |
78 | ||
79 | 4 | IValidator validator = getValidator(); |
80 | ||
81 | 4 | if (validator == null) |
82 | 0 | throw Tapestry.createRequiredParameterException(this, "validator"); |
83 | ||
84 | 4 | validator.renderValidatorContribution(this, writer, cycle); |
85 | ||
86 | 4 | writer.closeTag(); |
87 | ||
88 | 4 | 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 | 2 | protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
96 | { | |
97 | 2 | String value = cycle.getParameter(getName()); |
98 | ||
99 | 2 | updateValue(value); |
100 | } | |
101 | ||
102 | 4 | protected String readValue() |
103 | { | |
104 | 4 | IValidator validator = getValidator(); |
105 | 4 | if (validator == null) |
106 | 0 | throw Tapestry.createRequiredParameterException(this, "validator"); |
107 | ||
108 | 4 | IValidationDelegate delegate = getForm().getDelegate(); |
109 | ||
110 | 4 | if (delegate.isInError()) |
111 | 2 | return delegate.getFieldInputValue(); |
112 | ||
113 | 2 | Object value = getValue(); |
114 | ||
115 | 2 | String result = validator.toString(this, value); |
116 | ||
117 | 2 | return result; |
118 | } | |
119 | ||
120 | 2 | protected void updateValue(String value) |
121 | { | |
122 | 2 | Object objectValue = null; |
123 | ||
124 | 2 | IValidator validator = getValidator(); |
125 | 2 | if (validator == null) |
126 | 1 | throw Tapestry.createRequiredParameterException(this, "validator"); |
127 | ||
128 | 1 | IValidationDelegate delegate = getForm().getDelegate(); |
129 | ||
130 | 1 | delegate.recordFieldInputValue(value); |
131 | ||
132 | 1 | try |
133 | { | |
134 | 1 | objectValue = validator.toObject(this, value); |
135 | } | |
136 | catch (ValidatorException ex) | |
137 | { | |
138 | 0 | delegate.record(ex); |
139 | 0 | return; |
140 | } | |
141 | ||
142 | 1 | setValue(objectValue); |
143 | } | |
144 | ||
145 | public abstract IValidator getValidator(); | |
146 | } |
|