|
|||||||||||||||||||
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 | |||||||||||||||
RadioGroup.java | 78.6% | 91.4% | 100% | 90% |
|
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.IMarkupWriter; | |
19 | import org.apache.tapestry.IRequestCycle; | |
20 | import org.apache.tapestry.Tapestry; | |
21 | import org.apache.tapestry.valid.ValidationStrings; | |
22 | import org.apache.tapestry.valid.ValidatorException; | |
23 | ||
24 | /** | |
25 | * A special type of form component that is used to contain {@link Radio}components. The Radio and | |
26 | * {@link Radio}group components work together to update a property of some other object, much like | |
27 | * a more flexible version of a {@link PropertySelection}. [ <a | |
28 | * href="../../../../../ComponentReference/RadioGroup.html">Component Reference </a>] | |
29 | * | |
30 | * As of 4.0, RadioGroup can indicate that it is required. | |
31 | * | |
32 | * @author Howard Lewis Ship | |
33 | * @author Paul Ferraro | |
34 | */ | |
35 | public abstract class RadioGroup extends AbstractRequirableField | |
36 | { | |
37 | // Cached copy of the value from the selectedBinding | |
38 | private Object _selection; | |
39 | ||
40 | // The value from the HTTP request indicating which | |
41 | // Radio was selected by the user. | |
42 | private int _selectedOption; | |
43 | ||
44 | private boolean _rewinding; | |
45 | ||
46 | private boolean _rendering; | |
47 | ||
48 | private int _nextOptionId; | |
49 | ||
50 | /** | |
51 | * A <code>RadioGroup</code> places itself into the {@link IRequestCycle}as an attribute, so | |
52 | * that its wrapped {@link Radio}components can identify thier state. | |
53 | */ | |
54 | ||
55 | private static final String ATTRIBUTE_NAME = "org.apache.tapestry.active.RadioGroup"; | |
56 | ||
57 | 70 | public static RadioGroup get(IRequestCycle cycle) |
58 | { | |
59 | 70 | return (RadioGroup) cycle.getAttribute(ATTRIBUTE_NAME); |
60 | } | |
61 | ||
62 | 69 | public int getNextOptionId() |
63 | { | |
64 | 69 | if (!_rendering) |
65 | 0 | throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId"); |
66 | ||
67 | 69 | return _nextOptionId++; |
68 | } | |
69 | ||
70 | 69 | public boolean isRewinding() |
71 | { | |
72 | 69 | if (!_rendering) |
73 | 0 | throw Tapestry.createRenderOnlyPropertyException(this, "rewinding"); |
74 | ||
75 | 69 | return _rewinding; |
76 | } | |
77 | ||
78 | /** | |
79 | * Returns true if the value is equal to the current selection for the group. This is invoked by | |
80 | * a {@link Radio}during rendering to determine if it should be marked 'checked'. | |
81 | */ | |
82 | ||
83 | 46 | public boolean isSelection(Object value) |
84 | { | |
85 | 46 | if (!_rendering) |
86 | 0 | throw Tapestry.createRenderOnlyPropertyException(this, "selection"); |
87 | ||
88 | 46 | if (_selection == value) |
89 | 4 | return true; |
90 | ||
91 | 42 | if (_selection == null || value == null) |
92 | 10 | return false; |
93 | ||
94 | 32 | return _selection.equals(value); |
95 | } | |
96 | ||
97 | /** | |
98 | * Invoked by the {@link Radio}which is selected to update the property bound to the selected | |
99 | * parameter. | |
100 | */ | |
101 | ||
102 | 3 | public void updateSelection(Object value) |
103 | { | |
104 | 3 | getBinding("selected").setObject(value); |
105 | } | |
106 | ||
107 | /** | |
108 | * Used by {@link Radio}components when rewinding to see if their value was submitted. | |
109 | */ | |
110 | ||
111 | 20 | public boolean isSelected(int option) |
112 | { | |
113 | 20 | return _selectedOption == option; |
114 | } | |
115 | ||
116 | /** | |
117 | * @see org.apache.tapestry.AbstractComponent#prepareForRender(org.apache.tapestry.IRequestCycle) | |
118 | */ | |
119 | 22 | protected void prepareForRender(IRequestCycle cycle) |
120 | { | |
121 | 22 | if (cycle.getAttribute(ATTRIBUTE_NAME) != null) |
122 | 1 | throw new ApplicationRuntimeException(Tapestry.getMessage("RadioGroup.may-not-nest"), |
123 | this, null, null); | |
124 | ||
125 | 21 | cycle.setAttribute(ATTRIBUTE_NAME, this); |
126 | ||
127 | 21 | _rendering = true; |
128 | 21 | _nextOptionId = 0; |
129 | } | |
130 | ||
131 | /** | |
132 | * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle) | |
133 | */ | |
134 | 22 | protected void cleanupAfterRender(IRequestCycle cycle) |
135 | { | |
136 | 22 | _rendering = false; |
137 | 22 | _selection = null; |
138 | ||
139 | 22 | cycle.removeAttribute(ATTRIBUTE_NAME); |
140 | } | |
141 | ||
142 | /** | |
143 | * @see org.apache.tapestry.AbstractComponent#finishLoad() | |
144 | */ | |
145 | 5 | protected void finishLoad() |
146 | { | |
147 | 5 | setRequiredMessage(ValidationStrings.getMessagePattern(ValidationStrings.REQUIRED_SELECT_FIELD, getPage().getLocale())); |
148 | } | |
149 | ||
150 | /** | |
151 | * @see org.apache.tapestry.form.AbstractRequirableField#bind(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle) | |
152 | */ | |
153 | 6 | public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException |
154 | { | |
155 | 6 | String value = getSubmittedValue(cycle); |
156 | ||
157 | 6 | if (value == null) |
158 | 2 | _selectedOption = -1; |
159 | else | |
160 | 4 | _selectedOption = Integer.parseInt(value); |
161 | ||
162 | 6 | _rewinding = true; |
163 | ||
164 | 6 | renderBody(writer, cycle); |
165 | } | |
166 | ||
167 | /** | |
168 | * @see org.apache.tapestry.form.AbstractRequirableField#renderFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle) | |
169 | */ | |
170 | 13 | protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
171 | { | |
172 | 13 | super.renderFormComponent(writer, cycle); |
173 | ||
174 | 13 | _rewinding = false; |
175 | ||
176 | // For rendering, the Radio components need to know what the current | |
177 | // selection is, so that the correct one can mark itself 'checked'. | |
178 | 13 | _selection = getBinding("selected").getObject(); |
179 | ||
180 | 13 | renderBody(writer, cycle); |
181 | } | |
182 | } |
|