1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.form; |
16 |
|
|
17 |
|
import java.util.HashSet; |
18 |
|
import java.util.Set; |
19 |
|
|
20 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
21 |
|
import org.apache.tapestry.IMarkupWriter; |
22 |
|
import org.apache.tapestry.IRequestCycle; |
23 |
|
import org.apache.tapestry.Tapestry; |
24 |
|
import org.apache.tapestry.valid.ValidatorException; |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
0 |
public abstract class Select extends AbstractFormComponent implements ValidatableField |
40 |
|
{ |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
private static final String ATTRIBUTE_NAME = "org.apache.tapestry.active.Select"; |
48 |
|
|
49 |
|
private boolean _rewinding; |
50 |
|
|
51 |
|
private boolean _rendering; |
52 |
|
|
53 |
|
private Set _selections; |
54 |
|
|
55 |
|
private int _nextOptionId; |
56 |
|
|
57 |
|
public static Select get(IRequestCycle cycle) |
58 |
|
{ |
59 |
0 |
return (Select) cycle.getAttribute(ATTRIBUTE_NAME); |
60 |
|
} |
61 |
|
|
62 |
|
public abstract boolean isMultiple(); |
63 |
|
|
64 |
|
public boolean isRewinding() |
65 |
|
{ |
66 |
0 |
if (!_rendering) |
67 |
0 |
throw Tapestry.createRenderOnlyPropertyException(this, "rewinding"); |
68 |
|
|
69 |
0 |
return _rewinding; |
70 |
|
} |
71 |
|
|
72 |
|
public String getNextOptionId() |
73 |
|
{ |
74 |
0 |
if (!_rendering) |
75 |
0 |
throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId"); |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
0 |
return Integer.toString(_nextOptionId++); |
80 |
|
} |
81 |
|
|
82 |
|
public boolean isSelected(String value) |
83 |
|
{ |
84 |
0 |
if (_selections == null) |
85 |
0 |
return false; |
86 |
|
|
87 |
0 |
return _selections.contains(value); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
protected void prepareForRender(IRequestCycle cycle) |
94 |
|
{ |
95 |
0 |
if (cycle.getAttribute(ATTRIBUTE_NAME) != null) |
96 |
0 |
throw new ApplicationRuntimeException(Tapestry.getMessage("Select.may-not-nest"), this, |
97 |
|
null, null); |
98 |
|
|
99 |
0 |
cycle.setAttribute(ATTRIBUTE_NAME, this); |
100 |
|
|
101 |
0 |
_rendering = true; |
102 |
0 |
_nextOptionId = 0; |
103 |
0 |
} |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
protected void cleanupAfterRender(IRequestCycle cycle) |
109 |
|
{ |
110 |
0 |
_rendering = false; |
111 |
0 |
_selections = null; |
112 |
|
|
113 |
0 |
cycle.removeAttribute(ATTRIBUTE_NAME); |
114 |
0 |
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
120 |
|
{ |
121 |
0 |
_rewinding = false; |
122 |
|
|
123 |
0 |
renderDelegatePrefix(writer, cycle); |
124 |
|
|
125 |
0 |
writer.begin("select"); |
126 |
|
|
127 |
0 |
writer.attribute("name", getName()); |
128 |
|
|
129 |
0 |
if (isMultiple()) |
130 |
0 |
writer.attribute("multiple", "multiple"); |
131 |
|
|
132 |
0 |
if (isDisabled()) |
133 |
0 |
writer.attribute("disabled", "disabled"); |
134 |
|
|
135 |
0 |
renderIdAttribute(writer, cycle); |
136 |
|
|
137 |
0 |
renderDelegateAttributes(writer, cycle); |
138 |
|
|
139 |
0 |
getValidatableFieldSupport().renderContributions(this, writer, cycle); |
140 |
|
|
141 |
0 |
renderInformalParameters(writer, cycle); |
142 |
|
|
143 |
0 |
renderBody(writer, cycle); |
144 |
|
|
145 |
0 |
writer.end(); |
146 |
|
|
147 |
0 |
renderDelegateSuffix(writer, cycle); |
148 |
0 |
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
154 |
|
{ |
155 |
0 |
_selections = null; |
156 |
0 |
_rewinding = true; |
157 |
|
|
158 |
0 |
String[] parameters = cycle.getParameters(getName()); |
159 |
|
|
160 |
|
try |
161 |
|
{ |
162 |
0 |
if (parameters != null) |
163 |
|
{ |
164 |
0 |
int length = parameters.length; |
165 |
|
|
166 |
0 |
_selections = new HashSet((length > 30) ? 101 : 7); |
167 |
|
|
168 |
0 |
for (int i = 0; i < length; i++) |
169 |
0 |
_selections.add(parameters[i]); |
170 |
|
} |
171 |
|
|
172 |
0 |
renderBody(writer, cycle); |
173 |
|
|
174 |
|
|
175 |
0 |
getValidatableFieldSupport().validate(this, writer, cycle, parameters); |
176 |
|
} |
177 |
0 |
catch (ValidatorException e) |
178 |
|
{ |
179 |
0 |
getForm().getDelegate().record(e); |
180 |
0 |
} |
181 |
0 |
} |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
public abstract ValidatableFieldSupport getValidatableFieldSupport(); |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
public boolean isRequired() |
192 |
|
{ |
193 |
0 |
return getValidatableFieldSupport().isRequired(this); |
194 |
|
} |
195 |
|
} |