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