View Javadoc
1 package org.apache.turbine.util.template; 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.html.Option; 58 import org.apache.ecs.html.Select; 59 60 /*** 61 * This class is for generating a SelectorBox. It is good when used 62 * with WM because you can stuff it into the context and then just 63 * call it to generate the HTML. It can be used in other cases as 64 * well, but WM is the best case for it right now. 65 * 66 * <p>For example code showing the usage for this module, please see 67 * the toString() method below to see how it would be refered to from 68 * WM. 69 * 70 * <pre> 71 * // get the roles for a user 72 * RoleSet userRoles = new DefaultAccessControl().getRoles(loginid, null); 73 * if ( userRoles != null ) 74 * { 75 * context.put("hasRoleSet", Boolean.TRUE); 76 * 77 * // get an array of the users roles 78 * Role[] usersRoles = userRoles.getRolesArray(); 79 * // get an array of all the roles in the system 80 * Role[] allRoles = ((RoleSet)RolePeer.retrieveSet()).getRolesArray(); 81 * 82 * Object[] names = new Object[allRoles.length]; 83 * Object[] values = new Object[allRoles.length]; 84 * for ( int i=0;i<allRoles.length; i++ ) 85 * { 86 * names[i] = new Integer(allRoles[i].getPrimaryKey()).toString(); 87 * values[i] = allRoles[i].getName(); 88 * } 89 * 90 * SelectorBox sb = new SelectorBox("roleSetBox", names, values); 91 * sb.buildBooleans(usersRoles, allRoles); 92 * context.put("roleSetBox", sb); 93 * } 94 * else 95 * { 96 * context.put("hasRoleSet", Boolean.FALSE); 97 * } 98 * </pre> 99 * 100 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 101 * @version $Id: SelectorBox.java,v 1.2 2002/07/11 16:53:20 mpoeschl Exp $ 102 */ 103 public class SelectorBox 104 { 105 /*** This is the Select ECS element. */ 106 private Select sel = null; 107 108 /*** This is the size of the Select statement. */ 109 private int size = 1; 110 111 /*** This is the name= value. */ 112 private String name = null; 113 114 /*** This is the value= portion of the option element. */ 115 private Object[] names = null; 116 117 /*** This is the data after the option element. */ 118 private Object[] values = null; 119 120 /*** This is an array of which items are selected. */ 121 private boolean[] selected = null; 122 123 124 /*** 125 * Generic constructor, builds a select box with a default size of 126 * 1 and no selected items. 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 */ 132 public SelectorBox(String name, Object[] names, Object[] values) 133 { 134 this(name, names, values, 1, null); 135 } 136 137 /*** 138 * Generic constructor builds a select box. 139 * 140 * @param name A String with the name for the select box. 141 * @param names An Object[] with the names. 142 * @param values An Object[] with the values. 143 * @param size An int specifying the size. 144 */ 145 public SelectorBox(String name, Object[] names, Object[] values, int size) 146 { 147 this(name, names, values, size, null); 148 } 149 150 /*** 151 * Generic constructor builds a select box. 152 * 153 * @param name A String with the name for the select box. 154 * @param names An Object[] with the names. 155 * @param values An Object[] with the values. 156 * @param selected A boolean[] with the selected items. 157 */ 158 public SelectorBox(String name, Object[] names, Object[] values, 159 boolean[] selected) 160 { 161 this(name, names, values, 1, selected); 162 } 163 164 /*** 165 * Primary constructor for everything. 166 * 167 * @param name A String with the name for the select box. 168 * @param names An Object[] with the names. 169 * @param values An Object[] with the values. 170 * @param size An int specifying the size. 171 * @param selected A boolean[] with the selected items. 172 */ 173 public SelectorBox(String name, Object[] names, Object[] values, int size, 174 boolean[] selected ) 175 { 176 this.name = name; 177 this.names = names; 178 this.values = values; 179 this.size = size; 180 this.selected = selected; 181 182 sel = new Select(name, size); 183 sel.setName(name); 184 sel.setSize(size); 185 } 186 187 /*** 188 * Pass in an array of selected items and the entire set of items 189 * and it will determine which items in the selected set are also 190 * in the entireset and then build a boolean[] up that is the same 191 * size as the entireSet with markings to tell whether or not the 192 * items are marked or not. It uses toString().equalsIgnoreCase() 193 * on the Object in the Object[] to determine if the items are 194 * equal. 195 * 196 * @param selectedSet An Object[]. 197 * @param entireSet An Object[]. 198 */ 199 public void buildBooleans(Object[] selectedSet, Object[] entireSet) 200 { 201 selected = new boolean[entireSet.length]; 202 for (int j = 0; j < entireSet.length; j++) 203 { 204 Object r2 = entireSet[j]; 205 for (int i = 0; i < selectedSet.length; i++) 206 { 207 Object r1 = selectedSet[i]; 208 if (r1 != null && r2 != null && 209 r1.toString().equalsIgnoreCase(r2.toString())) 210 { 211 selected[j] = true; 212 } 213 } 214 } 215 } 216 217 /*** 218 * This builds out the select box at a certain size. To use this 219 * element in WM, you simply build this object in your java code, 220 * put it into the context and then call $selectBox.toString(5). 221 * 222 * @param size An int with the size. 223 * @return A String with the HTML code. 224 */ 225 public String toString(int size) 226 { 227 sel.setSize(size); 228 sel.setName(name); 229 for (int f = 0; f < values.length; f++) 230 { 231 Option opt = new Option((String) values[f]); 232 opt.addElement((String) names[f]); 233 if (selected != null && selected[f] == true) 234 { 235 opt.setSelected(true); 236 } 237 sel.addElement(opt); 238 } 239 String output = sel.toString(); 240 reset(); 241 return output; 242 } 243 244 /*** 245 * Resets the internal state of the SelectorBox. 246 */ 247 public void reset() 248 { 249 sel = new Select(name, size); 250 } 251 252 /*** 253 * This builds out the select box at a certain size. To use this 254 * element in WM, you simply build this object in your java code, 255 * put it into the context and then call $selectBox and it will 256 * build it with the default size of 1. 257 * 258 * @return A String with the HTML code. 259 */ 260 public String toString() 261 { 262 return this.toString(size); 263 } 264 265 /*** 266 * This allows you to set the multiple attribute to the select 267 * element. Example usage from within WM is like this: 268 * 269 * <p> 270 * $selectBox.setMultiple(true).toString(4) 271 * 272 * @param val True if multiple selection should be allowed. 273 * @return A SelectorBox (self). 274 */ 275 public SelectorBox setMultiple(boolean val) 276 { 277 sel.setMultiple(val); 278 return this; 279 } 280 281 /*** 282 * This allows one to set the name= attribute to the select 283 * element. 284 * 285 * @param name A String with the name. 286 * @return A SelectorBox (self). 287 */ 288 public SelectorBox setName(String name) 289 { 290 this.name = name; 291 sel.setName(name); 292 return this; 293 } 294 295 /*** 296 * This allows one to set the size of the select element. 297 * 298 * @param size An int with the size. 299 * @return A SelectorBox (self). 300 */ 301 public SelectorBox setSize(int size) 302 { 303 this.size = size; 304 sel.setSize(size); 305 return this; 306 } 307 308 /*** 309 * This allows one to set an onChange attribute on the select tag 310 * 311 * @param script A string with the script to put in onChange 312 * @return A SelectorBox (self). 313 */ 314 public SelectorBox setOnChange(String script) 315 { 316 sel.setOnChange(script); 317 return this; 318 } 319 320 /*** 321 * This allows one to set the array of selected booleans. 322 * 323 * @param an array of booleans 324 * @return A SelectorBox (self). 325 */ 326 public SelectorBox setSelected(boolean[] bools) 327 { 328 this.selected = bools; 329 return this; 330 } 331 332 /*** 333 * This will set all elements as unselected, except for the 334 * element(s) with the given name. 335 * 336 * @param name The name to appear as selected. 337 * @return A SelectorBox (self). 338 */ 339 public SelectorBox setSelected(Object name) 340 { 341 if (name != null) 342 { 343 selected = new boolean[names.length]; 344 for (int i = 0; i < names.length; i++) 345 { 346 Object o = names[i]; 347 if (o != null && o.toString().equalsIgnoreCase(name.toString())) 348 { 349 selected[i] = true; 350 } 351 } 352 } 353 return this; 354 } 355 }

This page was automatically generated by Maven