Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SelectorBox |
|
| 1.8571428571428572;1,857 |
1 | package org.apache.turbine.util.template; | |
2 | ||
3 | import org.apache.ecs.html.Option; | |
4 | import org.apache.ecs.html.Select; | |
5 | ||
6 | ||
7 | /* | |
8 | * Licensed to the Apache Software Foundation (ASF) under one | |
9 | * or more contributor license agreements. See the NOTICE file | |
10 | * distributed with this work for additional information | |
11 | * regarding copyright ownership. The ASF licenses this file | |
12 | * to you under the Apache License, Version 2.0 (the | |
13 | * "License"); you may not use this file except in compliance | |
14 | * with the License. You may obtain a copy of the License at | |
15 | * | |
16 | * http://www.apache.org/licenses/LICENSE-2.0 | |
17 | * | |
18 | * Unless required by applicable law or agreed to in writing, | |
19 | * software distributed under the License is distributed on an | |
20 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
21 | * KIND, either express or implied. See the License for the | |
22 | * specific language governing permissions and limitations | |
23 | * under the License. | |
24 | */ | |
25 | ||
26 | /** | |
27 | * This class is for generating a SelectorBox. It is good when used | |
28 | * with WM because you can stuff it into the context and then just | |
29 | * call it to generate the HTML. It can be used in other cases as | |
30 | * well, but WM is the best case for it right now. | |
31 | * | |
32 | * <p>For example code showing the usage for this module, please see | |
33 | * the toString() method below to see how it would be referred to from | |
34 | * WM. | |
35 | * | |
36 | * <pre> | |
37 | * // get the roles for a user | |
38 | * RoleSet userRoles = new DefaultAccessControl().getRoles(loginid, null); | |
39 | * if ( userRoles != null ) | |
40 | * { | |
41 | * context.put("hasRoleSet", Boolean.TRUE); | |
42 | * | |
43 | * // get an array of the users roles | |
44 | * Role[] usersRoles = userRoles.getRolesArray(); | |
45 | * // get an array of all the roles in the system | |
46 | * Role[] allRoles = ((RoleSet)RolePeer.retrieveSet()).getRolesArray(); | |
47 | * | |
48 | * Object[] names = new Object[allRoles.length]; | |
49 | * Object[] values = new Object[allRoles.length]; | |
50 | * for ( int i=0;i<allRoles.length; i++ ) | |
51 | * { | |
52 | * names[i] = Integer.valueOf(allRoles[i].getPrimaryKey()).toString(); | |
53 | * values[i] = allRoles[i].getName(); | |
54 | * } | |
55 | * | |
56 | * SelectorBox sb = new SelectorBox("roleSetBox", names, values); | |
57 | * sb.buildBooleans(usersRoles, allRoles); | |
58 | * context.put("roleSetBox", sb); | |
59 | * } | |
60 | * else | |
61 | * { | |
62 | * context.put("hasRoleSet", Boolean.FALSE); | |
63 | * } | |
64 | * </pre> | |
65 | * | |
66 | * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> | |
67 | * @version $Id: SelectorBox.java 1726423 2016-01-23 17:55:09Z tv $ | |
68 | */ | |
69 | public class SelectorBox | |
70 | { | |
71 | /** This is the Select ECS element. */ | |
72 | 0 | private Select sel = null; |
73 | ||
74 | /** This is the size of the Select statement. */ | |
75 | 0 | private int size = 1; |
76 | ||
77 | /** This is the name= value. */ | |
78 | 0 | private String name = null; |
79 | ||
80 | /** This is the value= portion of the option element. */ | |
81 | 0 | private Object[] names = null; |
82 | ||
83 | /** This is the data after the option element. */ | |
84 | 0 | private Object[] values = null; |
85 | ||
86 | /** This is an array of which items are selected. */ | |
87 | 0 | private boolean[] selected = null; |
88 | ||
89 | /** | |
90 | * Generic constructor, builds a select box with a default size of | |
91 | * 1 and no selected items. | |
92 | * | |
93 | * @param name A String with the name for the select box. | |
94 | * @param names An Object[] with the names. | |
95 | * @param values An Object[] with the values. | |
96 | */ | |
97 | public SelectorBox(String name, Object[] names, Object[] values) | |
98 | { | |
99 | 0 | this(name, names, values, 1, null); |
100 | 0 | } |
101 | ||
102 | /** | |
103 | * Generic constructor builds a select box. | |
104 | * | |
105 | * @param name A String with the name for the select box. | |
106 | * @param names An Object[] with the names. | |
107 | * @param values An Object[] with the values. | |
108 | * @param size An int specifying the size. | |
109 | */ | |
110 | public SelectorBox(String name, Object[] names, Object[] values, int size) | |
111 | { | |
112 | 0 | this(name, names, values, size, null); |
113 | 0 | } |
114 | ||
115 | /** | |
116 | * Generic constructor builds a select box. | |
117 | * | |
118 | * @param name A String with the name for the select box. | |
119 | * @param names An Object[] with the names. | |
120 | * @param values An Object[] with the values. | |
121 | * @param selected A boolean[] with the selected items. | |
122 | */ | |
123 | public SelectorBox(String name, Object[] names, Object[] values, | |
124 | boolean[] selected) | |
125 | { | |
126 | 0 | this(name, names, values, 1, selected); |
127 | 0 | } |
128 | ||
129 | /** | |
130 | * Primary constructor for everything. | |
131 | * | |
132 | * @param name A String with the name for the select box. | |
133 | * @param names An Object[] with the names. | |
134 | * @param values An Object[] with the values. | |
135 | * @param size An int specifying the size. | |
136 | * @param selected A boolean[] with the selected items. | |
137 | */ | |
138 | public SelectorBox(String name, Object[] names, Object[] values, int size, | |
139 | boolean[] selected) | |
140 | 0 | { |
141 | 0 | this.name = name; |
142 | 0 | this.names = names; |
143 | 0 | this.values = values; |
144 | 0 | this.size = size; |
145 | 0 | this.selected = selected; |
146 | ||
147 | 0 | sel = new Select(name, size); |
148 | 0 | sel.setName(name); |
149 | 0 | sel.setSize(size); |
150 | 0 | } |
151 | ||
152 | /** | |
153 | * Pass in an array of selected items and the entire set of items | |
154 | * and it will determine which items in the selected set are also | |
155 | * in the entire set and then build a boolean[] up that is the same | |
156 | * size as the entireSet with markings to tell whether or not the | |
157 | * items are marked or not. It uses toString().equalsIgnoreCase() | |
158 | * on the Object in the Object[] to determine if the items are | |
159 | * equal. | |
160 | * | |
161 | * @param selectedSet An Object[]. | |
162 | * @param entireSet An Object[]. | |
163 | */ | |
164 | public void buildBooleans(Object[] selectedSet, Object[] entireSet) | |
165 | { | |
166 | 0 | selected = new boolean[entireSet.length]; |
167 | 0 | for (int j = 0; j < entireSet.length; j++) |
168 | { | |
169 | 0 | Object r2 = entireSet[j]; |
170 | 0 | for (int i = 0; i < selectedSet.length; i++) |
171 | { | |
172 | 0 | Object r1 = selectedSet[i]; |
173 | 0 | if (r1 != null && r2 != null && |
174 | r1.toString().equalsIgnoreCase(r2.toString())) | |
175 | { | |
176 | 0 | selected[j] = true; |
177 | } | |
178 | } | |
179 | } | |
180 | 0 | } |
181 | ||
182 | /** | |
183 | * This builds out the select box at a certain size. To use this | |
184 | * element in WM, you simply build this object in your java code, | |
185 | * put it into the context and then call $selectBox.toString(5). | |
186 | * | |
187 | * @param size An int with the size. | |
188 | * @return A String with the HTML code. | |
189 | */ | |
190 | public String toString(int size) | |
191 | { | |
192 | 0 | sel.setSize(size); |
193 | 0 | sel.setName(name); |
194 | 0 | for (int f = 0; f < values.length; f++) |
195 | { | |
196 | 0 | Option opt = new Option((String) values[f]); |
197 | 0 | opt.addElement((String) names[f]); |
198 | 0 | if (selected != null && selected[f] == true) |
199 | { | |
200 | 0 | opt.setSelected(true); |
201 | } | |
202 | 0 | sel.addElement(opt); |
203 | } | |
204 | 0 | String output = sel.toString(); |
205 | 0 | reset(); |
206 | 0 | return output; |
207 | } | |
208 | ||
209 | /** | |
210 | * Resets the internal state of the SelectorBox. | |
211 | */ | |
212 | public void reset() | |
213 | { | |
214 | 0 | sel = new Select(name, size); |
215 | 0 | } |
216 | ||
217 | /** | |
218 | * This builds out the select box at a certain size. To use this | |
219 | * element in WM, you simply build this object in your java code, | |
220 | * put it into the context and then call $selectBox and it will | |
221 | * build it with the default size of 1. | |
222 | * | |
223 | * @return A String with the HTML code. | |
224 | */ | |
225 | @Override | |
226 | public String toString() | |
227 | { | |
228 | 0 | return this.toString(size); |
229 | } | |
230 | ||
231 | /** | |
232 | * This allows you to set the multiple attribute to the select | |
233 | * element. Example usage from within WM is like this: | |
234 | * | |
235 | * <p> | |
236 | * $selectBox.setMultiple(true).toString(4) | |
237 | * | |
238 | * @param val True if multiple selection should be allowed. | |
239 | * @return A SelectorBox (self). | |
240 | */ | |
241 | public SelectorBox setMultiple(boolean val) | |
242 | { | |
243 | 0 | sel.setMultiple(val); |
244 | 0 | return this; |
245 | } | |
246 | ||
247 | /** | |
248 | * This allows one to set the name= attribute to the select | |
249 | * element. | |
250 | * | |
251 | * @param name A String with the name. | |
252 | * @return A SelectorBox (self). | |
253 | */ | |
254 | public SelectorBox setName(String name) | |
255 | { | |
256 | 0 | this.name = name; |
257 | 0 | sel.setName(name); |
258 | 0 | return this; |
259 | } | |
260 | ||
261 | /** | |
262 | * This allows one to set the size of the select element. | |
263 | * | |
264 | * @param size An int with the size. | |
265 | * @return A SelectorBox (self). | |
266 | */ | |
267 | public SelectorBox setSize(int size) | |
268 | { | |
269 | 0 | this.size = size; |
270 | 0 | sel.setSize(size); |
271 | 0 | return this; |
272 | } | |
273 | ||
274 | /** | |
275 | * This allows one to set an onChange attribute on the select tag | |
276 | * | |
277 | * @param script A string with the script to put in onChange | |
278 | * @return A SelectorBox (self). | |
279 | */ | |
280 | public SelectorBox setOnChange(String script) | |
281 | { | |
282 | 0 | sel.setOnChange(script); |
283 | 0 | return this; |
284 | } | |
285 | ||
286 | /** | |
287 | * This allows one to set the array of selected booleans. | |
288 | * | |
289 | * @param bools an array of booleans | |
290 | * @return A SelectorBox (self). | |
291 | */ | |
292 | public SelectorBox setSelected(boolean[] bools) | |
293 | { | |
294 | 0 | this.selected = bools; |
295 | 0 | return this; |
296 | } | |
297 | ||
298 | /** | |
299 | * This will set all elements as unselected, except for the | |
300 | * element(s) with the given name. | |
301 | * | |
302 | * @param name The name to appear as selected. | |
303 | * @return A SelectorBox (self). | |
304 | */ | |
305 | public SelectorBox setSelected(Object name) | |
306 | { | |
307 | 0 | if (name != null) |
308 | { | |
309 | 0 | selected = new boolean[names.length]; |
310 | 0 | for (int i = 0; i < names.length; i++) |
311 | { | |
312 | 0 | Object o = names[i]; |
313 | 0 | if (o != null && o.toString().equalsIgnoreCase(name.toString())) |
314 | { | |
315 | 0 | selected[i] = true; |
316 | } | |
317 | } | |
318 | } | |
319 | 0 | return this; |
320 | } | |
321 | } |