Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Checkbox |
|
| 1.8333333333333333;1.833 |
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.form; | |
16 | ||
17 | import org.apache.tapestry.IMarkupWriter; | |
18 | import org.apache.tapestry.IRequestCycle; | |
19 | import org.apache.tapestry.valid.IValidationDelegate; | |
20 | import org.apache.tapestry.valid.ValidatorException; | |
21 | ||
22 | /** | |
23 | * Implements a component that manages an HTML <input type=checkbox> form element. [ <a | |
24 | * href="../../../../../components/form/checkbox.html">Component Reference </a>] | |
25 | * <p> | |
26 | * As of 4.0, this component can be validated. | |
27 | * | |
28 | * @author Howard Lewis Ship | |
29 | * @author Paul Ferraro | |
30 | */ | |
31 | 0 | public abstract class Checkbox extends AbstractFormComponent implements ValidatableField |
32 | { | |
33 | /** | |
34 | * @see org.apache.tapestry.form.validator.AbstractRequirableField#renderRequirableFormComponent(org.apache.tapestry.IMarkupWriter, | |
35 | * org.apache.tapestry.IRequestCycle) | |
36 | */ | |
37 | protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) | |
38 | { | |
39 | 0 | renderDelegatePrefix(writer, cycle); |
40 | ||
41 | 0 | writer.beginEmpty("input"); |
42 | 0 | writer.attribute("type", "checkbox"); |
43 | ||
44 | 0 | writer.attribute("name", getName()); |
45 | ||
46 | 0 | if (isDisabled()) |
47 | 0 | writer.attribute("disabled", "disabled"); |
48 | ||
49 | // write out submitted input for case of validation errors | |
50 | ||
51 | 0 | IValidationDelegate delegate = getForm().getDelegate(); |
52 | 0 | boolean checked = getValue(); |
53 | 0 | if (delegate != null && delegate.isInError()) { |
54 | ||
55 | 0 | checked = Boolean.valueOf(delegate.getFieldInputValue()).booleanValue(); |
56 | } | |
57 | ||
58 | 0 | if (checked) { |
59 | 0 | writer.attribute("checked", "checked"); |
60 | } | |
61 | ||
62 | 0 | renderIdAttribute(writer, cycle); |
63 | ||
64 | 0 | getValidatableFieldSupport().renderContributions(this, writer, cycle); |
65 | ||
66 | 0 | renderInformalParameters(writer, cycle); |
67 | ||
68 | 0 | writer.closeTag(); |
69 | ||
70 | 0 | renderDelegateSuffix(writer, cycle); |
71 | 0 | } |
72 | ||
73 | /** | |
74 | * In traditional HTML, many checkboxes would have the same name but different values. Under | |
75 | * Tapestry, it makes more sense to have different names and a fixed value. For a checkbox, we | |
76 | * only care about whether the name appears as a request parameter. | |
77 | */ | |
78 | protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) | |
79 | { | |
80 | 0 | String value = cycle.getParameter(getName()); |
81 | ||
82 | try | |
83 | { | |
84 | // This is atypical validation - since this component does not explicitly bind to an object | |
85 | ||
86 | 0 | getValidatableFieldSupport().validate(this, writer, cycle, value); |
87 | ||
88 | 0 | setValue(value != null); |
89 | } | |
90 | 0 | catch (ValidatorException e) |
91 | { | |
92 | 0 | getForm().getDelegate().record(e); |
93 | 0 | getForm().getDelegate().recordFieldInputValue(Boolean.valueOf(value).toString()); |
94 | 0 | } |
95 | 0 | } |
96 | ||
97 | public abstract boolean getValue(); | |
98 | ||
99 | public abstract void setValue(boolean selected); | |
100 | ||
101 | /** | |
102 | * Injected. | |
103 | */ | |
104 | public abstract ValidatableFieldSupport getValidatableFieldSupport(); | |
105 | ||
106 | /** | |
107 | * @see org.apache.tapestry.form.AbstractFormComponent#isRequired() | |
108 | */ | |
109 | public boolean isRequired() | |
110 | { | |
111 | 0 | return getValidatableFieldSupport().isRequired(this); |
112 | } | |
113 | } |