View Javadoc

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