1 package org.apache.turbine.util.template;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 private Select sel = null;
69
70 /*** This is the size of the Select statement. */
71 private int size = 1;
72
73 /*** This is the name= value. */
74 private String name = null;
75
76 /*** This is the value= portion of the option element. */
77 private Object[] names = null;
78
79 /*** This is the data after the option element. */
80 private Object[] values = null;
81
82 /*** This is an array of which items are selected. */
83 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 this(name, names, values, 1, null);
96 }
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 this(name, names, values, size, null);
109 }
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 this(name, names, values, 1, selected);
123 }
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 {
137 this.name = name;
138 this.names = names;
139 this.values = values;
140 this.size = size;
141 this.selected = selected;
142
143 sel = new Select(name, size);
144 sel.setName(name);
145 sel.setSize(size);
146 }
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 selected = new boolean[entireSet.length];
163 for (int j = 0; j < entireSet.length; j++)
164 {
165 Object r2 = entireSet[j];
166 for (int i = 0; i < selectedSet.length; i++)
167 {
168 Object r1 = selectedSet[i];
169 if (r1 != null && r2 != null &&
170 r1.toString().equalsIgnoreCase(r2.toString()))
171 {
172 selected[j] = true;
173 }
174 }
175 }
176 }
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 sel.setSize(size);
189 sel.setName(name);
190 for (int f = 0; f < values.length; f++)
191 {
192 Option opt = new Option((String) values[f]);
193 opt.addElement((String) names[f]);
194 if (selected != null && selected[f] == true)
195 {
196 opt.setSelected(true);
197 }
198 sel.addElement(opt);
199 }
200 String output = sel.toString();
201 reset();
202 return output;
203 }
204
205 /***
206 * Resets the internal state of the SelectorBox.
207 */
208 public void reset()
209 {
210 sel = new Select(name, size);
211 }
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 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 sel.setMultiple(val);
239 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 this.name = name;
252 sel.setName(name);
253 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 this.size = size;
265 sel.setSize(size);
266 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 sel.setOnChange(script);
278 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 this.selected = bools;
290 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 if (name != null)
303 {
304 selected = new boolean[names.length];
305 for (int i = 0; i < names.length; i++)
306 {
307 Object o = names[i];
308 if (o != null && o.toString().equalsIgnoreCase(name.toString()))
309 {
310 selected[i] = true;
311 }
312 }
313 }
314 return this;
315 }
316 }