|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ValidField.java | 88.9% | 95.7% | 100% | 94.2% |
|
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.hivemind.ApplicationRuntimeException;
|
|
18 |
import org.apache.hivemind.HiveMind;
|
|
19 |
import org.apache.tapestry.IForm;
|
|
20 |
import org.apache.tapestry.IMarkupWriter;
|
|
21 |
import org.apache.tapestry.IRequestCycle;
|
|
22 |
import org.apache.tapestry.PageRenderSupport;
|
|
23 |
import org.apache.tapestry.Tapestry;
|
|
24 |
import org.apache.tapestry.TapestryUtils;
|
|
25 |
import org.apache.tapestry.form.AbstractTextField;
|
|
26 |
import org.apache.tapestry.form.Form;
|
|
27 |
import org.apache.tapestry.form.IFormComponent;
|
|
28 |
import org.apache.tapestry.html.Body;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* A {@link Form}component that creates a text field that allows for validation of user input and
|
|
32 |
* conversion between string and object values. [ <a
|
|
33 |
* href="../../../../../ComponentReference/ValidField.html">Component Reference </a>]
|
|
34 |
* <p>
|
|
35 |
* A ValidatingTextField uses an {@link IValidationDelegate}to track errors and an
|
|
36 |
* {@link IValidator}to convert between strings and objects (as well as perform validations). The
|
|
37 |
* validation delegate is shared by all validating text fields in a form, the validator may be
|
|
38 |
* shared my multiple elements as desired.
|
|
39 |
*
|
|
40 |
* @author Howard Lewis Ship
|
|
41 |
*/
|
|
42 |
|
|
43 |
public abstract class ValidField extends AbstractTextField implements IFormComponent |
|
44 |
{ |
|
45 |
public abstract Object getValue();
|
|
46 |
|
|
47 |
public abstract void setValue(Object value); |
|
48 |
|
|
49 |
public abstract String getDisplayName();
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Renders the component, which involves the {@link IValidationDelegate delegate}.
|
|
53 |
* <p>
|
|
54 |
* During a render, the <em>first</em> field rendered that is either in error, or required but
|
|
55 |
* null gets special treatment. JavaScript is added to select that field (such that the cursor
|
|
56 |
* jumps right to the field when the page loads).
|
|
57 |
*/
|
|
58 |
|
|
59 | 17 |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
60 |
{ |
|
61 | 17 |
IForm form = getForm(cycle); |
62 |
|
|
63 | 17 |
if (form.wasPrerendered(writer, this)) |
64 | 5 |
return;
|
65 |
|
|
66 | 12 |
IValidationDelegate delegate = form.getDelegate(); |
67 | 12 |
IValidator validator = getValidator(); |
68 |
|
|
69 | 12 |
if (validator == null) |
70 | 0 |
throw Tapestry.createRequiredParameterException(this, "validator"); |
71 |
|
|
72 | 12 |
boolean rendering = !cycle.isRewinding();
|
73 |
|
|
74 | 12 |
if (rendering)
|
75 | 7 |
delegate.writePrefix(writer, cycle, this, validator);
|
76 |
|
|
77 | 12 |
super.renderComponent(writer, cycle);
|
78 |
|
|
79 | 11 |
if (rendering)
|
80 | 6 |
delegate.writeSuffix(writer, cycle, this, validator);
|
81 |
|
|
82 |
// If rendering and there's either an error in the field,
|
|
83 |
// then we may have identified the default field (which will
|
|
84 |
// automatically receive focus).
|
|
85 |
|
|
86 | 11 |
if (rendering && delegate.isInError())
|
87 | 1 |
addSelect(cycle); |
88 |
|
|
89 |
// That's OK, but an ideal situation would know about non-validating
|
|
90 |
// text fields, and also be able to put the cursor in the
|
|
91 |
// first field, period (even if there are no required or error fields).
|
|
92 |
// Still, this pretty much rocks!
|
|
93 |
|
|
94 |
} |
|
95 |
|
|
96 |
/**
|
|
97 |
* Invokes
|
|
98 |
* {@link IValidationDelegate#writeAttributes(IMarkupWriter,IRequestCycle, IFormComponent,IValidator)}.
|
|
99 |
*/
|
|
100 |
|
|
101 | 7 |
protected void beforeCloseTag(IMarkupWriter writer, IRequestCycle cycle) |
102 |
{ |
|
103 | 7 |
IValidator validator = getValidator(); |
104 |
|
|
105 | 7 |
validator.renderValidatorContribution(this, writer, cycle);
|
106 |
|
|
107 | 6 |
getForm().getDelegate().writeAttributes(writer, cycle, this, validator);
|
108 |
} |
|
109 |
|
|
110 |
private static final String SELECTED_ATTRIBUTE_NAME = "org.apache.tapestry.component.html.valid.SelectedFieldSet"; |
|
111 |
|
|
112 |
/**
|
|
113 |
* Creates JavaScript to set the cursor on the first required or error field encountered while
|
|
114 |
* rendering. This only works if the text field is wrapped by a {@link Body} component
|
|
115 |
* (which is almost always true).
|
|
116 |
*/
|
|
117 |
|
|
118 | 3 |
protected void addSelect(IRequestCycle cycle) |
119 |
{ |
|
120 |
// If some other field has taken the honors, then let it.
|
|
121 |
|
|
122 | 3 |
if (cycle.getAttribute(SELECTED_ATTRIBUTE_NAME) != null) |
123 | 1 |
return;
|
124 |
|
|
125 | 2 |
PageRenderSupport pageRenderSupport = TapestryUtils.getOptionalPageRenderSupport(cycle); |
126 |
|
|
127 |
// If not wrapped by a Body, then do nothing.
|
|
128 |
|
|
129 | 2 |
if (pageRenderSupport == null) |
130 | 0 |
return;
|
131 |
|
|
132 | 2 |
IForm form = Form.get(cycle); |
133 |
|
|
134 | 2 |
String formName = form.getName(); |
135 | 2 |
String textFieldName = getName(); |
136 |
|
|
137 | 2 |
String fullName = "document." + formName + "." + textFieldName; |
138 |
|
|
139 | 2 |
pageRenderSupport.addInitializationScript(fullName + ".focus();");
|
140 | 2 |
pageRenderSupport.addInitializationScript(fullName + ".select();");
|
141 |
|
|
142 |
// Put a marker in, indicating that the selected field is known.
|
|
143 |
|
|
144 | 2 |
cycle.setAttribute(SELECTED_ATTRIBUTE_NAME, Boolean.TRUE); |
145 |
} |
|
146 |
|
|
147 | 7 |
protected String readValue()
|
148 |
{ |
|
149 | 7 |
IValidationDelegate delegate = getForm().getDelegate(); |
150 |
|
|
151 | 7 |
if (delegate.isInError())
|
152 | 1 |
return delegate.getFieldInputValue();
|
153 |
|
|
154 | 6 |
Object value = getValue(); |
155 | 6 |
String result = getValidator().toString(this, value);
|
156 |
|
|
157 | 6 |
if (HiveMind.isBlank(result) && getValidator().isRequired())
|
158 | 2 |
addSelect(getPage().getRequestCycle()); |
159 |
|
|
160 | 6 |
return result;
|
161 |
} |
|
162 |
|
|
163 | 5 |
protected void updateValue(String value) |
164 |
{ |
|
165 | 5 |
Object objectValue = null;
|
166 | 5 |
IValidationDelegate delegate = getForm().getDelegate(); |
167 |
|
|
168 | 5 |
delegate.recordFieldInputValue(value); |
169 |
|
|
170 | 5 |
try
|
171 |
{ |
|
172 | 5 |
objectValue = getValidator().toObject(this, value);
|
173 |
} |
|
174 |
catch (ValidatorException ex)
|
|
175 |
{ |
|
176 | 1 |
delegate.record(ex); |
177 | 1 |
return;
|
178 |
} |
|
179 |
|
|
180 | 4 |
setValue(objectValue); |
181 |
} |
|
182 |
|
|
183 |
public abstract IValidator getValidator();
|
|
184 |
} |
|