001 package org.apache.turbine.util.template; 002 003 004 /* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024 import org.apache.ecs.html.Option; 025 import org.apache.ecs.html.Select; 026 027 /** 028 * This class is for generating a SelectorBox. It is good when used 029 * with WM because you can stuff it into the context and then just 030 * call it to generate the HTML. It can be used in other cases as 031 * well, but WM is the best case for it right now. 032 * 033 * <p>For example code showing the usage for this module, please see 034 * the toString() method below to see how it would be refered to from 035 * WM. 036 * 037 * <pre> 038 * // get the roles for a user 039 * RoleSet userRoles = new DefaultAccessControl().getRoles(loginid, null); 040 * if ( userRoles != null ) 041 * { 042 * context.put("hasRoleSet", Boolean.TRUE); 043 * 044 * // get an array of the users roles 045 * Role[] usersRoles = userRoles.getRolesArray(); 046 * // get an array of all the roles in the system 047 * Role[] allRoles = ((RoleSet)RolePeer.retrieveSet()).getRolesArray(); 048 * 049 * Object[] names = new Object[allRoles.length]; 050 * Object[] values = new Object[allRoles.length]; 051 * for ( int i=0;i<allRoles.length; i++ ) 052 * { 053 * names[i] = new Integer(allRoles[i].getPrimaryKey()).toString(); 054 * values[i] = allRoles[i].getName(); 055 * } 056 * 057 * SelectorBox sb = new SelectorBox("roleSetBox", names, values); 058 * sb.buildBooleans(usersRoles, allRoles); 059 * context.put("roleSetBox", sb); 060 * } 061 * else 062 * { 063 * context.put("hasRoleSet", Boolean.FALSE); 064 * } 065 * </pre> 066 * 067 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 068 * @version $Id: SelectorBox.java 615328 2008-01-25 20:25:05Z tv $ 069 */ 070 public class SelectorBox 071 { 072 /** This is the Select ECS element. */ 073 private Select sel = null; 074 075 /** This is the size of the Select statement. */ 076 private int size = 1; 077 078 /** This is the name= value. */ 079 private String name = null; 080 081 /** This is the value= portion of the option element. */ 082 private Object[] names = null; 083 084 /** This is the data after the option element. */ 085 private Object[] values = null; 086 087 /** This is an array of which items are selected. */ 088 private boolean[] selected = null; 089 090 /** 091 * Generic constructor, builds a select box with a default size of 092 * 1 and no selected items. 093 * 094 * @param name A String with the name for the select box. 095 * @param names An Object[] with the names. 096 * @param values An Object[] with the values. 097 */ 098 public SelectorBox(String name, Object[] names, Object[] values) 099 { 100 this(name, names, values, 1, null); 101 } 102 103 /** 104 * Generic constructor builds a select box. 105 * 106 * @param name A String with the name for the select box. 107 * @param names An Object[] with the names. 108 * @param values An Object[] with the values. 109 * @param size An int specifying the size. 110 */ 111 public SelectorBox(String name, Object[] names, Object[] values, int size) 112 { 113 this(name, names, values, size, null); 114 } 115 116 /** 117 * Generic constructor builds a select box. 118 * 119 * @param name A String with the name for the select box. 120 * @param names An Object[] with the names. 121 * @param values An Object[] with the values. 122 * @param selected A boolean[] with the selected items. 123 */ 124 public SelectorBox(String name, Object[] names, Object[] values, 125 boolean[] selected) 126 { 127 this(name, names, values, 1, selected); 128 } 129 130 /** 131 * Primary constructor for everything. 132 * 133 * @param name A String with the name for the select box. 134 * @param names An Object[] with the names. 135 * @param values An Object[] with the values. 136 * @param size An int specifying the size. 137 * @param selected A boolean[] with the selected items. 138 */ 139 public SelectorBox(String name, Object[] names, Object[] values, int size, 140 boolean[] selected) 141 { 142 this.name = name; 143 this.names = names; 144 this.values = values; 145 this.size = size; 146 this.selected = selected; 147 148 sel = new Select(name, size); 149 sel.setName(name); 150 sel.setSize(size); 151 } 152 153 /** 154 * Pass in an array of selected items and the entire set of items 155 * and it will determine which items in the selected set are also 156 * in the entireset and then build a boolean[] up that is the same 157 * size as the entireSet with markings to tell whether or not the 158 * items are marked or not. It uses toString().equalsIgnoreCase() 159 * on the Object in the Object[] to determine if the items are 160 * equal. 161 * 162 * @param selectedSet An Object[]. 163 * @param entireSet An Object[]. 164 */ 165 public void buildBooleans(Object[] selectedSet, Object[] entireSet) 166 { 167 selected = new boolean[entireSet.length]; 168 for (int j = 0; j < entireSet.length; j++) 169 { 170 Object r2 = entireSet[j]; 171 for (int i = 0; i < selectedSet.length; i++) 172 { 173 Object r1 = selectedSet[i]; 174 if (r1 != null && r2 != null && 175 r1.toString().equalsIgnoreCase(r2.toString())) 176 { 177 selected[j] = true; 178 } 179 } 180 } 181 } 182 183 /** 184 * This builds out the select box at a certain size. To use this 185 * element in WM, you simply build this object in your java code, 186 * put it into the context and then call $selectBox.toString(5). 187 * 188 * @param size An int with the size. 189 * @return A String with the HTML code. 190 */ 191 public String toString(int size) 192 { 193 sel.setSize(size); 194 sel.setName(name); 195 for (int f = 0; f < values.length; f++) 196 { 197 Option opt = new Option((String) values[f]); 198 opt.addElement((String) names[f]); 199 if (selected != null && selected[f] == true) 200 { 201 opt.setSelected(true); 202 } 203 sel.addElement(opt); 204 } 205 String output = sel.toString(); 206 reset(); 207 return output; 208 } 209 210 /** 211 * Resets the internal state of the SelectorBox. 212 */ 213 public void reset() 214 { 215 sel = new Select(name, size); 216 } 217 218 /** 219 * This builds out the select box at a certain size. To use this 220 * element in WM, you simply build this object in your java code, 221 * put it into the context and then call $selectBox and it will 222 * build it with the default size of 1. 223 * 224 * @return A String with the HTML code. 225 */ 226 public String toString() 227 { 228 return this.toString(size); 229 } 230 231 /** 232 * This allows you to set the multiple attribute to the select 233 * element. Example usage from within WM is like this: 234 * 235 * <p> 236 * $selectBox.setMultiple(true).toString(4) 237 * 238 * @param val True if multiple selection should be allowed. 239 * @return A SelectorBox (self). 240 */ 241 public SelectorBox setMultiple(boolean val) 242 { 243 sel.setMultiple(val); 244 return this; 245 } 246 247 /** 248 * This allows one to set the name= attribute to the select 249 * element. 250 * 251 * @param name A String with the name. 252 * @return A SelectorBox (self). 253 */ 254 public SelectorBox setName(String name) 255 { 256 this.name = name; 257 sel.setName(name); 258 return this; 259 } 260 261 /** 262 * This allows one to set the size of the select element. 263 * 264 * @param size An int with the size. 265 * @return A SelectorBox (self). 266 */ 267 public SelectorBox setSize(int size) 268 { 269 this.size = size; 270 sel.setSize(size); 271 return this; 272 } 273 274 /** 275 * This allows one to set an onChange attribute on the select tag 276 * 277 * @param script A string with the script to put in onChange 278 * @return A SelectorBox (self). 279 */ 280 public SelectorBox setOnChange(String script) 281 { 282 sel.setOnChange(script); 283 return this; 284 } 285 286 /** 287 * This allows one to set the array of selected booleans. 288 * 289 * @param an array of booleans 290 * @return A SelectorBox (self). 291 */ 292 public SelectorBox setSelected(boolean[] bools) 293 { 294 this.selected = bools; 295 return this; 296 } 297 298 /** 299 * This will set all elements as unselected, except for the 300 * element(s) with the given name. 301 * 302 * @param name The name to appear as selected. 303 * @return A SelectorBox (self). 304 */ 305 public SelectorBox setSelected(Object name) 306 { 307 if (name != null) 308 { 309 selected = new boolean[names.length]; 310 for (int i = 0; i < names.length; i++) 311 { 312 Object o = names[i]; 313 if (o != null && o.toString().equalsIgnoreCase(name.toString())) 314 { 315 selected[i] = true; 316 } 317 } 318 } 319 return this; 320 } 321 }