1 /* 2 * $Id: UISelectOne.java,v 1.56 2007/07/27 19:59:08 rlubke 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.component; 42 43 44 import javax.faces.application.FacesMessage; 45 import javax.faces.context.FacesContext; 46 47 48 /** 49 * <p><strong class="changed_modified_2_0">UISelectOne</strong> is a 50 * {@link UIComponent} that represents the user's choice of zero or one 51 * items from among a discrete set of available options. The user can 52 * modify the selected value. Optionally, the component can be 53 * preconfigured with a currently selected item, by storing it as the 54 * <code>value</code> property of the component.</p> 55 * 56 * <p>This component is generally rendered as a select box or a group of 57 * radio buttons.</p> 58 * 59 * <p>By default, the <code>rendererType</code> property is set to 60 * "<code>javax.faces.Menu</code>". This value can be changed by 61 * calling the <code>setRendererType()</code> method.</p> 62 */ 63 64 public class UISelectOne extends UIInput { 65 66 67 // ------------------------------------------------------ Manifest Constants 68 69 70 /** 71 * <p>The standard component type for this component.</p> 72 */ 73 public static final String COMPONENT_TYPE = "javax.faces.SelectOne"; 74 75 76 /** 77 * <p>The standard component family for this component.</p> 78 */ 79 public static final String COMPONENT_FAMILY = "javax.faces.SelectOne"; 80 81 82 /** 83 * <p>The message identifier of the 84 * {@link javax.faces.application.FacesMessage} to be created if 85 * a value not matching the available options is specified. 86 */ 87 public static final String INVALID_MESSAGE_ID = 88 "javax.faces.component.UISelectOne.INVALID"; 89 90 91 // ------------------------------------------------------------ Constructors 92 93 94 /** 95 * <p>Create a new {@link UISelectOne} instance with default property 96 * values.</p> 97 */ 98 public UISelectOne() { 99 100 super(); 101 setRendererType("javax.faces.Menu"); 102 103 } 104 105 106 // -------------------------------------------------------------- Properties 107 108 109 public String getFamily() { 110 111 return (COMPONENT_FAMILY); 112 113 } 114 115 116 // ------------------------------------------------------ Validation Methods 117 118 119 /** 120 * <p><span class="changed_modified_2_0">In</span> addition to the 121 * standard validation behavior inherited from {@link UIInput}, 122 * ensure that any specified value is equal to one of the available 123 * options. Before comparing each option, coerce the option value 124 * type to the type of this component's value following the 125 * Expression Language coercion rules. If the specified value is 126 * not equal to any of the options, enqueue an error message and set 127 * the <code>valid</code> property to <code>false</code>.</p> 128 * 129 * <p class="changed_added_2_0">If {@link #isRequired} returns 130 * <code>true</code>, and the current value is equal to the value of 131 * an inner {@link UISelectItem} whose {@link 132 * UISelectItem#isNoSelectionOption} method returns 133 * <code>true</code>, enqueue an error message and set the 134 * <code>valid</code> property to <code>false</code>.</p> 135 * 136 * @param context The {@link FacesContext} for the current request 137 * 138 * @param value The converted value to test for membership. 139 * 140 * @throws NullPointerException if <code>context</code> 141 * is <code>null</code> 142 */ 143 protected void validateValue(FacesContext context, Object value) { 144 145 // Skip validation if it is not necessary 146 super.validateValue(context, value); 147 148 if (!isValid() || (value == null)) { 149 return; 150 } 151 152 // Ensure that the value matches one of the available options 153 boolean found = SelectUtils.matchValue(getFacesContext(), 154 this, 155 value, 156 new SelectItemsIterator(context, this), 157 getConverter()); 158 159 boolean isNoSelection = SelectUtils.valueIsNoSelectionOption(getFacesContext(), 160 this, 161 value, 162 new SelectItemsIterator(context, this), 163 getConverter()); 164 165 // Enqueue an error message if an invalid value was specified 166 if ((!found) || 167 (isRequired() && isNoSelection)) { 168 FacesMessage message = 169 MessageFactory.getMessage(context, INVALID_MESSAGE_ID, 170 MessageFactory.getLabel(context, this)); 171 context.addMessage(getClientId(context), message); 172 setValid(false); 173 } 174 175 } 176 177 }