1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.form; |
16 | |
|
17 | |
import org.apache.hivemind.ApplicationRuntimeException; |
18 | |
import org.apache.tapestry.*; |
19 | |
import org.apache.tapestry.valid.ValidatorException; |
20 | |
|
21 | |
import java.util.Map; |
22 | |
import java.util.HashMap; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public abstract class RadioGroup extends AbstractFormComponent implements ValidatableField |
36 | |
{ |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
static final String ATTRIBUTE_NAME = "org.apache.tapestry.active.RadioGroup"; |
43 | |
|
44 | |
|
45 | |
Object _selection; |
46 | |
|
47 | |
|
48 | |
|
49 | |
int _selectedOption; |
50 | |
|
51 | |
boolean _rewinding; |
52 | |
|
53 | |
boolean _rendering; |
54 | |
|
55 | |
private int _nextOptionId; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public abstract IScript getScript(); |
61 | |
|
62 | |
public static RadioGroup get(IRequestCycle cycle) |
63 | |
{ |
64 | 0 | return (RadioGroup) cycle.getAttribute(ATTRIBUTE_NAME); |
65 | |
} |
66 | |
|
67 | |
public int getNextOptionId() |
68 | |
{ |
69 | 0 | if (!_rendering) |
70 | 0 | throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId"); |
71 | |
|
72 | 0 | return _nextOptionId++; |
73 | |
} |
74 | |
|
75 | |
public boolean isRewinding() |
76 | |
{ |
77 | 0 | if (!_rendering) |
78 | 0 | throw Tapestry.createRenderOnlyPropertyException(this, "rewinding"); |
79 | |
|
80 | 0 | return _rewinding; |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public boolean isSelection(Object value) |
89 | |
{ |
90 | 0 | if (!_rendering) |
91 | 0 | throw Tapestry.createRenderOnlyPropertyException(this, "selection"); |
92 | |
|
93 | 0 | if (_selection == value) |
94 | 0 | return true; |
95 | |
|
96 | 0 | if (_selection == null || value == null) |
97 | 0 | return false; |
98 | |
|
99 | 0 | return _selection.equals(value); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void updateSelection(Object value) |
108 | |
{ |
109 | 0 | getBinding("selected").setObject(value); |
110 | |
|
111 | 0 | _selection = value; |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public boolean isSelected(int option) |
119 | |
{ |
120 | 0 | return _selectedOption == option; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
protected void prepareForRender(IRequestCycle cycle) |
127 | |
{ |
128 | 0 | super.prepareForRender(cycle); |
129 | |
|
130 | 0 | if (cycle.getAttribute(ATTRIBUTE_NAME) != null) |
131 | 0 | throw new ApplicationRuntimeException(Tapestry.getMessage("RadioGroup.may-not-nest"), |
132 | |
this, null, null); |
133 | |
|
134 | 0 | cycle.setAttribute(ATTRIBUTE_NAME, this); |
135 | |
|
136 | 0 | _rendering = true; |
137 | 0 | _nextOptionId = 0; |
138 | 0 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
protected void cleanupAfterRender(IRequestCycle cycle) |
144 | |
{ |
145 | 0 | super.cleanupAfterRender(cycle); |
146 | |
|
147 | 0 | _rendering = false; |
148 | 0 | _selection = null; |
149 | |
|
150 | 0 | cycle.removeAttribute(ATTRIBUTE_NAME); |
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
158 | |
{ |
159 | 0 | _rewinding = false; |
160 | |
|
161 | |
|
162 | 0 | PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this); |
163 | 0 | Map symbols = new HashMap(); |
164 | 0 | symbols.put( "id", getClientId() ); |
165 | 0 | getScript().execute(this, cycle, pageRenderSupport, symbols); |
166 | |
|
167 | |
|
168 | |
|
169 | 0 | _selection = getBinding("selected").getObject(); |
170 | |
|
171 | 0 | renderDelegatePrefix(writer, cycle); |
172 | |
|
173 | 0 | writer.begin(getTemplateTagName()); |
174 | |
|
175 | 0 | renderInformalParameters(writer, cycle); |
176 | |
|
177 | 0 | if (getId() != null && !isParameterBound("id")) |
178 | 0 | renderIdAttribute(writer, cycle); |
179 | |
|
180 | 0 | renderDelegateAttributes(writer, cycle); |
181 | |
|
182 | 0 | renderBody(writer, cycle); |
183 | |
|
184 | 0 | writer.end(); |
185 | |
|
186 | 0 | renderDelegateSuffix(writer, cycle); |
187 | |
|
188 | 0 | getValidatableFieldSupport().renderContributions(this, writer, cycle); |
189 | 0 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
196 | |
{ |
197 | 0 | String value = cycle.getParameter(getName()); |
198 | |
|
199 | 0 | if (value == null) |
200 | 0 | _selectedOption = -1; |
201 | |
else |
202 | 0 | _selectedOption = Integer.parseInt(value); |
203 | |
|
204 | 0 | _rewinding = true; |
205 | |
|
206 | 0 | renderBody(writer, cycle); |
207 | |
|
208 | |
try |
209 | |
{ |
210 | 0 | getValidatableFieldSupport().validate(this, writer, cycle, _selection); |
211 | |
} |
212 | 0 | catch (ValidatorException e) |
213 | |
{ |
214 | 0 | getForm().getDelegate().record(e); |
215 | 0 | } |
216 | 0 | } |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
public abstract ValidatableFieldSupport getValidatableFieldSupport(); |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
public boolean isRequired() |
227 | |
{ |
228 | 0 | return getValidatableFieldSupport().isRequired(this); |
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
protected boolean getCanTakeFocus() |
235 | |
{ |
236 | 0 | return false; |
237 | |
} |
238 | |
} |