1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package javax.faces.component; 20 21 import javax.faces.context.FacesContext; 22 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent; 23 24 /** 25 * Component for choosing one option out of a set of possibilities. 26 * <p> 27 * This component is expected to have children of type UISelectItem or UISelectItems; these define 28 * the set of possible options that the user can choose from. 29 * </p> 30 * <p> 31 * See the javadoc for this class in the 32 * <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a> 33 * for further details. 34 * </p> 35 */ 36 @JSFComponent(defaultRendererType = "javax.faces.Menu") 37 public class UISelectOne extends UIInput 38 { 39 public static final String COMPONENT_TYPE = "javax.faces.SelectOne"; 40 public static final String COMPONENT_FAMILY = "javax.faces.SelectOne"; 41 42 public static final String INVALID_MESSAGE_ID = "javax.faces.component.UISelectOne.INVALID"; 43 44 public UISelectOne() 45 { 46 setRendererType("javax.faces.Menu"); 47 } 48 49 @Override 50 public String getFamily() 51 { 52 return COMPONENT_FAMILY; 53 } 54 55 /** 56 * Verify that the result of converting the newly submitted value is <i>equal</i> to the value 57 * property of one of the child SelectItem objects. If this is not true, a validation error is 58 * reported. 59 * 60 * @see javax.faces.component.UIInput#validateValue(javax.faces.context.FacesContext,java.lang.Object) 61 */ 62 protected void validateValue(FacesContext context, Object value) 63 { 64 super.validateValue(context, value); 65 66 if (!isValid() || value == null) 67 { 68 return; 69 } 70 71 _SelectItemsUtil._ValueConverter converter = new _SelectItemsUtil._ValueConverter() 72 { 73 public Object getConvertedValue(FacesContext context, String value) 74 { 75 return UISelectOne.this.getConvertedValue(context, value); 76 } 77 }; 78 79 // selected value must match to one of the available options 80 if (!_SelectItemsUtil.matchValue(context, value, new _SelectItemsIterator(this), converter)) 81 { 82 _MessageUtils.addErrorMessage(context, this, INVALID_MESSAGE_ID, new Object[] 83 { _MessageUtils.getLabel(context, this) }); 84 setValid(false); 85 } 86 } 87 }