KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > template > SelectorBox


1 package org.apache.turbine.util.template;
2
3 /*
4  * Copyright 2001-2004 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&lt;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,v 1.5.2.2 2004/05/20 03:27:24 seade Exp $
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 JavaDoc name = null;
75
76     /** This is the value= portion of the option element. */
77     private Object JavaDoc[] names = null;
78
79     /** This is the data after the option element. */
80     private Object JavaDoc[] 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 JavaDoc name, Object JavaDoc[] names, Object JavaDoc[] 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 JavaDoc name, Object JavaDoc[] names, Object JavaDoc[] 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 JavaDoc name, Object JavaDoc[] names, Object JavaDoc[] 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 JavaDoc name, Object JavaDoc[] names, Object JavaDoc[] 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 JavaDoc[] selectedSet, Object JavaDoc[] entireSet)
161     {
162         selected = new boolean[entireSet.length];
163         for (int j = 0; j < entireSet.length; j++)
164         {
165             Object JavaDoc r2 = entireSet[j];
166             for (int i = 0; i < selectedSet.length; i++)
167             {
168                 Object JavaDoc 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 JavaDoc 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 JavaDoc) values[f]);
193             opt.addElement((String JavaDoc) names[f]);
194             if (selected != null && selected[f] == true)
195             {
196                 opt.setSelected(true);
197             }
198             sel.addElement(opt);
199         }
200         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 }
317
Popular Tags