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