1 /* 2 * $Id: SelectItem.java,v 1.17 2007/04/27 22:00:10 ofung Exp $ 3 */ 4 5 /* 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 7 * 8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. 9 * 10 * The contents of this file are subject to the terms of either the GNU 11 * General Public License Version 2 only ("GPL") or the Common Development 12 * and Distribution License("CDDL") (collectively, the "License"). You 13 * may not use this file except in compliance with the License. You can obtain 14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html 15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific 16 * language governing permissions and limitations under the License. 17 * 18 * When distributing the software, include this License Header Notice in each 19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. 20 * Sun designates this particular file as subject to the "Classpath" exception 21 * as provided by Sun in the GPL Version 2 section of the License file that 22 * accompanied this code. If applicable, add the following below the License 23 * Header, with the fields enclosed by brackets [] replaced by your own 24 * identifying information: "Portions Copyrighted [year] 25 * [name of copyright owner]" 26 * 27 * Contributor(s): 28 * 29 * If you wish your version of this file to be governed by only the CDDL or 30 * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 * elects to include this software in this distribution under the [CDDL or GPL 32 * Version 2] license." If you don't indicate a single choice of license, a 33 * recipient has the option to distribute your version of this file under 34 * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 * its licensees as provided above. However, if you add GPL Version 2 code 36 * and therefore, elected the GPL Version 2 license, then the option applies 37 * only if the new code is made subject to such option by the copyright 38 * holder. 39 */ 40 41 package javax.faces.model; 42 43 44 import java.io.Serializable; 45 import javax.faces.component.UISelectMany; 46 import javax.faces.component.UISelectOne; 47 48 49 /** 50 * <p><strong class="changed_modified_2_0">SelectItem</strong> represents a 51 * single <em>item</em> in the list of supported <em>items</em> 52 * associated with a {@link UISelectMany} or {@link UISelectOne} 53 * component.</p> 54 */ 55 56 public class SelectItem implements Serializable { 57 58 private static final long serialVersionUID = 876782311414654999L; 59 60 61 // ------------------------------------------------------------ Constructors 62 63 64 /** 65 * <p>Construct a <code>SelectItem</code> with no initialized property 66 * values.</p> 67 */ 68 public SelectItem() { 69 70 super(); 71 72 } 73 74 75 /** 76 * <p>Construct a <code>SelectItem</code> with the specified value. The 77 * <code>label</code> property will be set to the value (converted to a 78 * String, if necessary), the <code>description</code> property will be 79 * set to <code>null</code>, the <code>disabled</code> property will be set to 80 * <code>false</code>, and the <code>escape</code> property will be set to 81 ( <code>true</code>.</p> 82 * 83 * @param value Value to be delivered to the model if this 84 * item is selected by the user 85 */ 86 public SelectItem(Object value) { 87 88 this(value, value == null ? null : value.toString(), null, false, true, false); 89 90 } 91 92 93 /** 94 * <p>Construct a <code>SelectItem</code> with the specified value and 95 * label. The <code>description</code> property will be set to 96 * <code>null</code>, the <code>disabled</code> property will be 97 * set to <code>false</code>, and the <code>escape</code> property will 98 * be set to <code>true</code>.</p> 99 * 100 * @param value Value to be delivered to the model if this 101 * item is selected by the user 102 * @param label Label to be rendered for this item in the response 103 */ 104 public SelectItem(Object value, String label) { 105 106 this(value, label, null, false, true, false); 107 108 } 109 110 111 /** 112 * <p>Construct a <code>SelectItem</code> instance with the specified 113 * value, label and description. This <code>disabled</code> property 114 * will be set to <code>false</code>, and the <code>escape</code> 115 * property will be set to <code>true</code>.</p> 116 * 117 * @param value Value to be delivered to the model if this 118 * item is selected by the user 119 * @param label Label to be rendered for this item in the response 120 * @param description Description of this item, for use in tools 121 */ 122 public SelectItem(Object value, String label, String description) { 123 124 this(value, label, description, false, true, false); 125 126 } 127 128 129 /** 130 * <p>Construct a <code>SelectItem</code> instance with the specified 131 * property values. The <code>escape</code> property will be set 132 * to <code>true</code>.</p> 133 * 134 * @param value Value to be delivered to the model if this 135 * item is selected by the user 136 * @param label Label to be rendered for this item in the response 137 * @param description Description of this item, for use in tools 138 * @param disabled Flag indicating that this option is disabled 139 */ 140 public SelectItem(Object value, String label, String description, 141 boolean disabled) { 142 143 this(value, label, description, disabled, true, false); 144 145 } 146 147 /** 148 * <p>Construct a <code>SelectItem</code> instance with the specified 149 * property values.</p> 150 * 151 * @param value Value to be delivered to the model if this 152 * item is selected by the user 153 * @param label Label to be rendered for this item in the response 154 * @param description Description of this item, for use in tools 155 * @param disabled Flag indicating that this option is disabled 156 * @param escape Flag indicating that the text of this option should be 157 * escaped when rendered. 158 * @since 1.2 159 */ 160 public SelectItem(Object value, String label, String description, 161 boolean disabled, boolean escape) { 162 163 this(value, label, description, disabled, escape, false); 164 165 } 166 167 168 /** 169 * <p>Construct a <code>SelectItem</code> instance with the specified 170 * property values.</p> 171 * 172 * @param value Value to be delivered to the model if this 173 * item is selected by the user 174 * @param label Label to be rendered for this item in the response 175 * @param description Description of this item, for use in tools 176 * @param disabled Flag indicating that this option is disabled 177 * @param escape Flag indicating that the text of this option should be 178 * escaped when rendered. 179 * @param noSelectionOption Flag indicating that the current option is a "no selection" option 180 * @since 1.2 181 */ 182 public SelectItem(Object value, String label, String description, 183 boolean disabled, boolean escape, boolean noSelectionOption) { 184 185 super(); 186 setValue(value); 187 setLabel(label); 188 setDescription(description); 189 setDisabled(disabled); 190 setEscape(escape); 191 setNoSelectionOption(noSelectionOption); 192 193 } 194 195 196 197 198 // ------------------------------------------------------ Instance Variables 199 200 201 private String description = null; 202 private boolean disabled = false; 203 private String label = null; 204 @SuppressWarnings({"NonSerializableFieldInSerializableClass"}) 205 private Object value = null; 206 207 208 // -------------------------------------------------------------- Properties 209 210 211 /** 212 * <p>Return a description of this item, for use in development tools. 213 */ 214 public String getDescription() { 215 216 return (this.description); 217 218 } 219 220 221 /** 222 * <p>Set the description of this item, for use in development tools.</p> 223 * 224 * @param description The new description 225 */ 226 public void setDescription(String description) { 227 228 this.description = description; 229 230 } 231 232 233 /** 234 * <p>Return the disabled flag for this item, which should modify the 235 * rendered output to make this item unavailable for selection by the user 236 * if set to <code>true</code>.</p> 237 */ 238 public boolean isDisabled() { 239 240 return (this.disabled); 241 242 } 243 244 245 /** 246 * <p>Set the disabled flag for this item, which should modify the 247 * rendered output to make this item unavailable for selection by the user 248 * if set to <code>true</code>.</p> 249 * 250 * @param disabled The new disabled flag 251 */ 252 public void setDisabled(boolean disabled) { 253 254 this.disabled = disabled; 255 256 } 257 258 259 /** 260 * <p>Return the label of this item, to be rendered visibly for the user. 261 */ 262 public String getLabel() { 263 264 return (this.label); 265 266 } 267 268 269 /** 270 * <p>Set the label of this item, to be rendered visibly for the user. 271 * 272 * @param label The new label 273 */ 274 public void setLabel(String label) { 275 276 this.label = label; 277 278 } 279 280 281 /** 282 * <p>Return the value of this item, to be delivered to the model 283 * if this item is selected by the user. 284 */ 285 public Object getValue() { 286 287 return (this.value); 288 289 } 290 291 292 /** 293 * <p>Set the value of this item, to be delivered to the model 294 * if this item is selected by this user. 295 * 296 * @param value The new value 297 * 298 */ 299 public void setValue(Object value) { 300 301 this.value = value; 302 303 } 304 305 private boolean escape; 306 307 /** 308 * <p class="changed_added_2_0">If and only if this returns 309 * <code>true</code>, the code that renders this select item must 310 * escape the label using escaping syntax appropriate to the content 311 * type being rendered. </p> 312 * 313 * @since 2.0 314 */ 315 public boolean isEscape() { 316 return this.escape; 317 } 318 319 /** 320 * <p class="changed_added_2_0">Set the value of the escape 321 * property. See {@link #isEscape}.</p> 322 * 323 * @since 2.0 324 */ 325 public void setEscape(boolean escape) { 326 this.escape = escape; 327 } 328 329 private boolean noSelectionOption = false; 330 331 /** <p class="changed_added_2_0">Return the value of the 332 * <code>noSelectionOption</code> property. If the value of this 333 * property is <code>true</code>, the system interprets the option 334 * represented by this <code>SelectItem</code> instance as 335 * representing a "no selection" option. See {@link 336 * UISelectOne#validateValue} and {@link UISelectMany#validateValue} 337 * for usage.</p> 338 * 339 * @since 2.0 340 */ 341 342 public boolean isNoSelectionOption() { 343 return noSelectionOption; 344 } 345 346 /** 347 * <p class="changed_added_2_0">Set the value of the 348 * <code>noSelectionOption</code> property.</p> 349 * 350 * @since 2.0 351 */ 352 353 public void setNoSelectionOption(boolean noSelectionOption) { 354 this.noSelectionOption = noSelectionOption; 355 } 356 357 358 359 }