Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Radio |
|
| 3.2;3.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.form; | |
16 | ||
17 | import org.apache.hivemind.ApplicationRuntimeException; | |
18 | import org.apache.tapestry.IForm; | |
19 | import org.apache.tapestry.IMarkupWriter; | |
20 | import org.apache.tapestry.IRequestCycle; | |
21 | import org.apache.tapestry.Tapestry; | |
22 | ||
23 | /** | |
24 | * Implements a component that manages an HTML <input type=radio> form element. | |
25 | * Such a component must be wrapped (possibly indirectly) | |
26 | * inside a {@link RadioGroup} component. | |
27 | * | |
28 | * [<a href="../../../../../components/form/radio.html">Component Reference</a>] | |
29 | * | |
30 | * | |
31 | * <p>{@link Radio} and {@link RadioGroup} are generally not used (except | |
32 | * for very special cases). Instead, a {@link PropertySelection} component is used. | |
33 | * | |
34 | * | |
35 | * @author Howard Lewis Ship | |
36 | * | |
37 | **/ | |
38 | ||
39 | 0 | public abstract class Radio extends AbstractFormComponent |
40 | { | |
41 | /** | |
42 | * Renders the form element, or responds when the form containing the element | |
43 | * is submitted (by checking {@link Form#isRewinding()}. | |
44 | * | |
45 | * | |
46 | **/ | |
47 | ||
48 | protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) | |
49 | { | |
50 | 0 | RadioGroup group = RadioGroup.get(cycle); |
51 | ||
52 | 0 | if (group == null) |
53 | 0 | throw new ApplicationRuntimeException(Tapestry.getMessage("Radio.must-be-contained-by-group"), |
54 | this, | |
55 | null, | |
56 | null); | |
57 | ||
58 | 0 | int option = group.getNextOptionId(); |
59 | ||
60 | 0 | setClientId(group.getName()+option); |
61 | 0 | setName(group.getName()); |
62 | ||
63 | 0 | writer.beginEmpty("input"); |
64 | ||
65 | 0 | writer.attribute("type", "radio"); |
66 | ||
67 | 0 | writer.attribute("name", getName()); |
68 | ||
69 | 0 | renderIdAttribute(writer, cycle); |
70 | ||
71 | // As the group if the value for this Radio matches the selection | |
72 | // for the group as a whole; if so this is the default radio and is checked. | |
73 | ||
74 | 0 | if (group.isSelection(getValue())) |
75 | 0 | writer.attribute("checked", "checked"); |
76 | ||
77 | 0 | if (isDisabled() || group.isDisabled()) |
78 | 0 | writer.attribute("disabled", "disabled"); |
79 | ||
80 | // The value for the Radio matches the option number (provided by the RadioGroup). | |
81 | // When the form is submitted, the RadioGroup will know which option was, | |
82 | // in fact, selected by the user. | |
83 | ||
84 | 0 | writer.attribute("value", option); |
85 | ||
86 | // don't make it a reserved parameter to preserve backwards compatibility | |
87 | 0 | if ( !isParameterBound("onclick") ) |
88 | { | |
89 | 0 | String onclickCall = "tapestry.byId('"+group.getClientId()+"').onChange("+option+");"; |
90 | 0 | writer.attribute("onclick", onclickCall); |
91 | } | |
92 | ||
93 | 0 | renderInformalParameters(writer, cycle); |
94 | ||
95 | 0 | writer.closeTag(); |
96 | 0 | } |
97 | ||
98 | protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) | |
99 | { | |
100 | 0 | RadioGroup group = RadioGroup.get(cycle); |
101 | ||
102 | 0 | if (group == null) |
103 | 0 | throw new ApplicationRuntimeException( |
104 | Tapestry.getMessage("Radio.must-be-contained-by-group"), | |
105 | this, | |
106 | null, | |
107 | null); | |
108 | ||
109 | 0 | int option = group.getNextOptionId(); |
110 | ||
111 | 0 | setClientId(group.getName()+option); |
112 | 0 | setName(group.getName()); |
113 | ||
114 | // If not disabled and this is the selected button within the radio group, | |
115 | // then update set the selection from the group to the value for this | |
116 | // radio button. This will update the selected parameter of the RadioGroup. | |
117 | ||
118 | 0 | if (!isDisabled() && !group.isDisabled() && group.isSelected(option)) |
119 | 0 | group.updateSelection(getValue()); |
120 | 0 | } |
121 | ||
122 | /** | |
123 | * Overridden to do nothing so that special {@link RadioGroup} semantics are handled properly. | |
124 | * @param form The form to set the name on. | |
125 | */ | |
126 | protected void setName(IForm form) | |
127 | { | |
128 | 0 | } |
129 | ||
130 | public abstract boolean isDisabled(); | |
131 | ||
132 | public abstract Object getValue(); | |
133 | } |