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.ValidationStrings; |
22 |
| import org.apache.tapestry.valid.ValidatorException; |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public abstract class RadioGroup extends AbstractRequirableField |
36 |
| { |
37 |
| |
38 |
| private Object _selection; |
39 |
| |
40 |
| |
41 |
| |
42 |
| private int _selectedOption; |
43 |
| |
44 |
| private boolean _rewinding; |
45 |
| |
46 |
| private boolean _rendering; |
47 |
| |
48 |
| private int _nextOptionId; |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
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 |
| |
80 |
| |
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 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
3
| public void updateSelection(Object value)
|
103 |
| { |
104 |
3
| getBinding("selected").setObject(value);
|
105 |
| } |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
20
| public boolean isSelected(int option)
|
112 |
| { |
113 |
20
| return _selectedOption == option;
|
114 |
| } |
115 |
| |
116 |
| |
117 |
| |
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 |
| |
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 |
| |
144 |
| |
145 |
5
| protected void finishLoad()
|
146 |
| { |
147 |
5
| setRequiredMessage(ValidationStrings.getMessagePattern(ValidationStrings.REQUIRED_SELECT_FIELD, getPage().getLocale()));
|
148 |
| } |
149 |
| |
150 |
| |
151 |
| |
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 |
| |
169 |
| |
170 |
13
| protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
171 |
| { |
172 |
13
| super.renderFormComponent(writer, cycle);
|
173 |
| |
174 |
13
| _rewinding = false;
|
175 |
| |
176 |
| |
177 |
| |
178 |
13
| _selection = getBinding("selected").getObject();
|
179 |
| |
180 |
13
| renderBody(writer, cycle);
|
181 |
| } |
182 |
| } |