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