Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
Checkbox |
|
| 1.6666666666666667;1.667 |
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="../../../../../ComponentReference/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 | 7 | 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 | 4 | renderDelegatePrefix(writer, cycle); |
40 | ||
41 | 4 | writer.beginEmpty("input"); |
42 | 4 | writer.attribute("type", "checkbox"); |
43 | ||
44 | 4 | writer.attribute("name", getName()); |
45 | ||
46 | 4 | if (isDisabled()) |
47 | 1 | writer.attribute("disabled", "disabled"); |
48 | ||
49 | // write out submitted input for case of validation errors |
|
50 | ||
51 | 4 | IValidationDelegate delegate = getForm().getDelegate(); |
52 | 4 | boolean checked = getValue(); |
53 | 4 | if (delegate != null && delegate.isInError()) { |
54 | ||
55 | 0 | checked = Boolean.valueOf(delegate.getFieldInputValue()).booleanValue(); |
56 | } |
|
57 | ||
58 | 4 | if (checked) { |
59 | 3 | writer.attribute("checked", "checked"); |
60 | } |
|
61 | ||
62 | 4 | renderIdAttribute(writer, cycle); |
63 | ||
64 | 4 | getValidatableFieldSupport().renderContributions(this, writer, cycle); |
65 | ||
66 | 4 | renderInformalParameters(writer, cycle); |
67 | ||
68 | 4 | writer.closeTag(); |
69 | ||
70 | 4 | renderDelegateSuffix(writer, cycle); |
71 | 4 | } |
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 | 3 | 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 | 3 | getValidatableFieldSupport().validate(this, writer, cycle, value); |
87 | ||
88 | 2 | setValue(value != null); |
89 | } |
|
90 | 1 | catch (ValidatorException e) |
91 | { |
|
92 | 1 | getForm().getDelegate().record(e); |
93 | 1 | getForm().getDelegate().recordFieldInputValue(Boolean.valueOf(value).toString()); |
94 | 2 | } |
95 | 3 | } |
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 | } |