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 org.apache.myfaces.custom.ajaxchildcombobox; 20 21 import java.io.IOException; 22 23 import javax.faces.context.FacesContext; 24 import javax.faces.el.MethodBinding; 25 import javax.faces.render.Renderer; 26 27 import org.apache.myfaces.component.html.ext.HtmlSelectOneMenu; 28 import org.apache.myfaces.custom.ajax.api.AjaxComponent; 29 import org.apache.myfaces.custom.ajax.api.AjaxRenderer; 30 31 /** 32 * Refreshes contents through an ajax call when the parent combo box's value is changed. 33 * 34 * This component is to be used in conjunction with a regular combo box or list box. 35 * When the selected value of the latter changes, it executes an ajax call to the 36 * specified method to refresh its contents based on the new selected value. 37 * 38 * @JSFComponent 39 * name = "s:ajaxChildComboBox" 40 * class = "org.apache.myfaces.custom.ajaxchildcombobox.AjaxChildComboBox" 41 * tagClass = "org.apache.myfaces.custom.ajaxchildcombobox.AjaxChildComboBoxTag" 42 * tagHandler = "org.apache.myfaces.custom.ajaxchildcombobox.AjaxChildComboBoxTagHandler" 43 * 44 * @author Sharath Reddy 45 */ 46 public abstract class AbstractAjaxChildComboBox extends HtmlSelectOneMenu implements AjaxComponent 47 { 48 public static final String COMPONENT_TYPE = "org.apache.myfaces.AjaxChildComboBox"; 49 public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.AjaxChildComboBox"; 50 51 public AbstractAjaxChildComboBox() 52 { 53 super(); 54 setRendererType(AbstractAjaxChildComboBox.DEFAULT_RENDERER_TYPE); 55 } 56 57 public void encodeAjax(FacesContext context) 58 throws IOException 59 { 60 61 if (context == null) throw new NullPointerException("context"); 62 if (!isRendered()) return; 63 Renderer renderer = getRenderer(context); 64 if (renderer != null && renderer instanceof AjaxRenderer) 65 { 66 ((AjaxRenderer) renderer).encodeAjax(context, this); 67 } 68 } 69 70 public void decodeAjax(FacesContext context) 71 { 72 //Do Nothing 73 } 74 75 /** 76 * Method to call via ajax to reload the combo box 77 * 78 * @JSFProperty 79 * methodSignature = "java.lang.String" 80 * returnSignature = "javax.faces.model.SelectItem []" 81 * stateHolder = "true" 82 */ 83 public abstract MethodBinding getAjaxSelectItemsMethod(); 84 85 // 86 87 /** 88 * id of the parent combo box 89 * 90 * This is not a 'Parent' in terms of the component heirarchy; 91 * This is the component whose 'onchange' event triggers a refresh. 92 * 93 * @JSFProperty 94 * literalOnly="true" 95 */ 96 public abstract String getParentComboBox(); 97 98 99 /** 100 * We cannot verify that the result of converting the newly submitted value 101 * is <i>equal</i> to the value property of one of the child SelectItem 102 * objects. This is because the contents of the child combo box could have 103 * been reloaded by a change in the parent combo box. 104 * 105 * @see javax.faces.component.UIInput#validateValue(javax.faces.context.FacesContext, java.lang.Object) 106 */ 107 protected void validateValue(FacesContext context, Object value) 108 { 109 return; 110 // selected value must match to one of the available options 111 /* if (!_SelectItemsUtil.matchValue(context, value, new _SelectItemsIterator(this), converter)) 112 { 113 _MessageUtils.addErrorMessage(context, this, INVALID_MESSAGE_ID, 114 new Object[] {getId()}); 115 setValid(false); 116 }*/ 117 } 118 }