1 4 package org.apache.myfaces.custom.jslistener; 5 6 import org.apache.myfaces.renderkit.RendererUtils; 7 import org.apache.myfaces.renderkit.html.HTML; 8 import org.apache.myfaces.renderkit.html.HtmlRenderer; 9 import org.apache.myfaces.custom.jsvalueset.HtmlJsValueSet; 10 import org.apache.myfaces.component.html.util.AddResource; 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 import javax.faces.application.Application; 15 import javax.faces.component.UIComponent; 16 import javax.faces.component.UINamingContainer; 17 import javax.faces.context.FacesContext; 18 import javax.faces.context.ResponseWriter; 19 import java.io.IOException ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 36 public class JsValueChangeListenerRenderer 37 extends HtmlRenderer 38 { 39 private static Log log = LogFactory.getLog(JsValueChangeListenerRenderer.class); 40 41 public void encodeEnd(FacesContext facesContext, UIComponent component) 42 throws IOException 43 { 44 RendererUtils.checkParamValidity(facesContext, component, JsValueChangeListener.class); 45 46 UIComponent parent = component.getParent(); 47 48 JsValueChangeListener jsValueChangeListener = (JsValueChangeListener) component; 49 50 51 String aFor = jsValueChangeListener.getFor(); 52 String expressionValue = jsValueChangeListener.getExpressionValue(); 53 String property = jsValueChangeListener.getProperty(); 54 55 AddResource.addJavaScriptToHeader( 56 JsValueChangeListenerTag.class, "JSListener.js", false, getFacesContext()); 57 58 if(aFor!=null) 59 { 60 UIComponent forComponent = component.findComponent(aFor); 61 62 String forComponentId = null; 63 64 if (forComponent == null) 65 { 66 if (log.isInfoEnabled()) 67 { 68 log.info("Unable to find component '" + aFor + "' (calling findComponent on component '" + component.getClientId(getFacesContext()) + "') - will try to render component id based on the parent-id (on same level)"); 69 } 70 if (aFor.length() > 0 && aFor.charAt(0) == UINamingContainer.SEPARATOR_CHAR) 71 { 72 forComponentId = aFor.substring(1); 74 } 75 else 76 { 77 String labelClientId = component.getClientId(getFacesContext()); 79 int colon = labelClientId.lastIndexOf(UINamingContainer.SEPARATOR_CHAR); 80 if (colon == -1) 81 { 82 forComponentId = aFor; 83 } 84 else 85 { 86 forComponentId = labelClientId.substring(0, colon + 1) + aFor; 87 } 88 } 89 } 90 else 91 { 92 forComponentId = forComponent.getClientId(getFacesContext()); 93 } 94 95 expressionValue = expressionValue.replaceAll("\\'","\\\\'"); 96 expressionValue = expressionValue.replaceAll("\"","\\\""); 97 98 99 String methodCall = "orgApacheMyfacesJsListenerSetExpressionProperty('"+ 100 parent.getClientId(getFacesContext())+"','"+ 101 forComponentId+"',"+ 102 (property==null?"null":"'"+property+"'")+ 103 ",'"+expressionValue+"');"; 104 105 106 callMethod(parent, "onchange",methodCall); 107 108 } 109 } 110 111 private void callMethod(UIComponent uiComponent, String propName, String value) 112 { 113 Object oldValue = uiComponent.getAttributes().get(propName); 114 115 if(oldValue != null) 116 { 117 String oldValueStr = oldValue.toString().trim(); 118 119 if(oldValueStr.indexOf(value)!=-1) 121 return; 122 123 if(oldValueStr.length()>0 && !oldValueStr.endsWith(";")) 124 oldValueStr +=";"; 125 126 value = oldValueStr + value; 127 128 } 129 130 uiComponent.getAttributes().put(propName, value); 131 } 132 133 134 protected Application getApplication() 135 { 136 return getFacesContext().getApplication(); 137 } 138 139 protected FacesContext getFacesContext() 140 { 141 return FacesContext.getCurrentInstance(); 142 } 143 144 } 145 | Popular Tags |