KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.turbine.util;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import org.apache.ecs.Element;
58 import org.apache.ecs.GenericElement;
59 import org.apache.ecs.html.Option;
60 import org.apache.ecs.html.Select;
61
62 /**
63  * This class is for generating a SelectorBox. It is good when used
64  * with WM because you can stuff it into the context and then just
65  * call it to generate the HTML. It can be used in other cases as
66  * well, but WM is the best case for it right now.
67  *
68  * <p>For example code showing the usage for this module, please see
69  * the toString() method below to see how it would be refered to from
70  * WM.
71  *
72  * <pre>
73  * // get the roles for a user
74  * RoleSet userRoles = new DefaultAccessControl().getRoles(loginid, null);
75  * if ( userRoles != null )
76  * {
77  * context.put("hasRoleSet", Boolean.TRUE);
78  *
79  * // get an array of the users roles
80  * Role[] usersRoles = userRoles.getRolesArray();
81  * // get an array of all the roles in the system
82  * Role[] allRoles = ((RoleSet)RolePeer.retrieveSet()).getRolesArray();
83  *
84  * Object[] names = new Object[allRoles.length];
85  * Object[] values = new Object[allRoles.length];
86  * for ( int i=0;i<allRoles.length; i++ )
87  * {
88  * names[i] = new Integer(allRoles[i].getPrimaryKey()).toString();
89  * values[i] = allRoles[i].getName();
90  * }
91  *
92  * SelectorBox sb = new SelectorBox("roleSetBox", names, values);
93  * sb.buildBooleans(usersRoles, allRoles);
94  * context.put("roleSetBox", sb);
95  * }
96  * else
97  * {
98  * context.put("hasRoleSet", Boolean.FALSE);
99  * }
100  * </pre>
101  *
102  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
103  * @version $Id: SelectorBox.java,v 1.1.1.1 2001/08/16 04:41:49 jvanzyl Exp $
104  */

105 public class SelectorBox
106 {
107     /** This is the Select ECS element. */
108     private Select sel = null;
109
110     /** This is the size of the Select statement. */
111     private int size = 1;
112
113     /** This is the name= value. */
114     private String JavaDoc name = null;
115
116     /** This is the value= portion of the option element. */
117     private Object JavaDoc[] names = null;
118
119     /** This is the data after the option element. */
120     private Object JavaDoc[] values = null;
121
122     /** This is an array of which items are selected. */
123     private boolean[] selected = null;
124
125
126     /**
127      * Generic constructor, builds a select box with a default size of
128      * 1 and no selected items.
129      *
130      * @param name A String with the name for the select box.
131      * @param names An Object[] with the names.
132      * @param values An Object[] with the values.
133      */

134     public SelectorBox ( String JavaDoc name,
135                          Object JavaDoc[] names,
136                          Object JavaDoc[] values )
137     {
138         this(name, names,values, 1, null);
139     }
140
141     /**
142      * Generic constructor builds a select box.
143      *
144      * @param name A String with the name for the select box.
145      * @param names An Object[] with the names.
146      * @param values An Object[] with the values.
147      * @param size An int specifying the size.
148      */

149     public SelectorBox ( String JavaDoc name,
150                          Object JavaDoc[] names,
151                          Object JavaDoc[] values,
152                          int size )
153     {
154         this(name, names,values, size, null);
155     }
156
157     /**
158      * Generic constructor builds a select box.
159      *
160      * @param name A String with the name for the select box.
161      * @param names An Object[] with the names.
162      * @param values An Object[] with the values.
163      * @param selected A boolean[] with the selected items.
164      */

165     public SelectorBox ( String JavaDoc name,
166                          Object JavaDoc[] names,
167                          Object JavaDoc[] values,
168                          boolean[] selected )
169     {
170         this(name, names,values, 1, selected);
171     }
172
173     /**
174      * Primary constructor for everything.
175      *
176      * @param name A String with the name for the select box.
177      * @param names An Object[] with the names.
178      * @param values An Object[] with the values.
179      * @param size An int specifying the size.
180      * @param selected A boolean[] with the selected items.
181      */

182     public SelectorBox ( String JavaDoc name,
183                          Object JavaDoc[] names,
184                          Object JavaDoc[] values,
185                          int size,
186                          boolean[] selected )
187     {
188         this.name = name;
189         this.names = names;
190         this.values = values;
191         this.size = size;
192         this.selected = selected;
193
194         sel = new Select(name, size);
195         sel.setName(name);
196         sel.setSize(size);
197     }
198
199     /**
200      * Pass in an array of selected items and the entire set of items
201      * and it will determine which items in the selected set are also
202      * in the entireset and then build a boolean[] up that is the same
203      * size as the entireSet with markings to tell whether or not the
204      * items are marked or not. It uses toString().equalsIgnoreCase()
205      * on the Object in the Object[] to determine if the items are
206      * equal.
207      *
208      * @param selectedSet An Object[].
209      * @param entireSet An Object[].
210      */

211     public void buildBooleans(Object JavaDoc[] selectedSet,
212                               Object JavaDoc[] entireSet)
213     {
214         selected = new boolean[entireSet.length];
215         for ( int j=0; j<entireSet.length; j++ )
216         {
217             Object JavaDoc r2 = entireSet[j];
218             for ( int i=0; i<selectedSet.length; i++ )
219             {
220                 Object JavaDoc r1 = selectedSet[i];
221                 if ( r1 != null && r2 != null &&
222                     r1.toString().equalsIgnoreCase(r2.toString()) )
223                 {
224                     selected[j] = true;
225                 }
226             }
227         }
228     }
229
230     /**
231      * This builds out the select box at a certain size. To use this
232      * element in WM, you simply build this object in your java code,
233      * put it into the context and then call $selectBox.toString(5).
234      *
235      * @param size An int with the size.
236      * @return A String with the HTML code.
237      */

238     public String JavaDoc toString(int size)
239     {
240         sel.setSize(size);
241         sel.setName(name);
242         for (int f=0;f<values.length;f++)
243         {
244             Option opt = new Option((String JavaDoc)values[f]);
245             opt.addElement((String JavaDoc)names[f]);
246             if ( selected != null && selected[f] == true )
247             {
248                 opt.setSelected(true);
249             }
250             sel.addElement(opt);
251         }
252         String JavaDoc output = sel.toString();
253         reset();
254         return output;
255     }
256
257     /**
258      * Resets the internal state of the SelectorBox.
259      */

260     public void reset()
261     {
262         sel = new Select(name, size);
263     }
264
265     /**
266      * This builds out the select box at a certain size. To use this
267      * element in WM, you simply build this object in your java code,
268      * put it into the context and then call $selectBox and it will
269      * build it with the default size of 1.
270      *
271      * @return A String with the HTML code.
272      */

273     public String JavaDoc toString()
274     {
275         return this.toString(size);
276     }
277
278     /**
279      * This allows you to set the multiple attribute to the select
280      * element. Example usage from within WM is like this:
281      *
282      * <p>
283      * $selectBox.setMultiple(true).toString(4)
284      *
285      * @param val True if multiple selection should be allowed.
286      * @return A SelectorBox (self).
287      */

288     public SelectorBox setMultiple ( boolean val )
289     {
290         sel.setMultiple(val);
291         return this;
292     }
293
294     /**
295      * This allows one to set the name= attribute to the select
296      * element.
297      *
298      * @param name A String with the name.
299      * @return A SelectorBox (self).
300      */

301     public SelectorBox setName ( String JavaDoc name )
302     {
303         this.name = name;
304         sel.setName(name);
305         return this;
306     }
307
308     /**
309      * This allows one to set the size of the select element.
310      *
311      * @param size An int with the size.
312      * @return A SelectorBox (self).
313      */

314     public SelectorBox setSize(int size)
315     {
316         this.size = size;
317         sel.setSize(size);
318         return this;
319     }
320
321     /**
322      * This allows one to set an onChange attribute on the select tag
323      *
324      * @param script A string with the script to put in onChange
325      * @return A SelectorBox (self).
326      */

327     public SelectorBox setOnChange(String JavaDoc script)
328     {
329         sel.setOnChange(script);
330         return this;
331     }
332
333     /**
334      * This allows one to set the array of selected booleans.
335      *
336      * @param an array of booleans
337      * @return A SelectorBox (self).
338      */

339     public SelectorBox setSelected(boolean[] bools)
340     {
341         this.selected = bools;
342         return this;
343     }
344
345     /**
346      * This will set all elements as unselected, except for the
347      * element(s) with the given name.
348      *
349      * @param name The name to appear as selected.
350      * @return A SelectorBox (self).
351      */

352     public SelectorBox setSelected(Object JavaDoc name)
353     {
354         if (name != null)
355         {
356             selected = new boolean[names.length];
357             for ( int i=0; i<names.length; i++ )
358             {
359                 Object JavaDoc o = names[i];
360                 if ( o != null &&
361                      o.toString().equalsIgnoreCase(name.toString()) )
362                 {
363                     selected[i] = true;
364                 }
365             }
366         }
367         return this;
368     }
369 }
370
Popular Tags