1 33 34 package com.icesoft.faces.renderkit.dom_html_basic; 35 36 import com.icesoft.faces.context.DOMContext; 37 import com.icesoft.faces.util.DOMUtils; 38 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.Text ; 41 42 import javax.faces.FacesException; 43 import javax.faces.component.NamingContainer; 44 import javax.faces.component.UICommand; 45 import javax.faces.component.UIComponent; 46 import javax.faces.component.UIViewRoot; 47 import javax.faces.context.FacesContext; 48 import javax.faces.event.ActionEvent; 49 50 import java.beans.Beans ; 51 import java.io.IOException ; 52 import java.util.Iterator ; 53 import java.util.Map ; 54 55 public class CommandLinkRenderer extends DomBasicRenderer { 56 57 private static final String HIDDEN_FIELD_NAME = "cl"; 58 59 public void decode(FacesContext facesContext, UIComponent uiComponent) { 60 validateParameters(facesContext, uiComponent, UICommand.class); 61 if (isStatic(uiComponent)) { 62 return; 63 } 64 String commandLinkClientId = uiComponent.getClientId(facesContext); 65 Map requestParameterMap = 66 facesContext.getExternalContext().getRequestParameterMap(); 67 String commonCommandLinkHiddenFieldName = deriveCommonHiddenFieldName( 68 facesContext, (UICommand) uiComponent); 69 String hiddenFieldNameInRequestMap = (String ) requestParameterMap 70 .get(commonCommandLinkHiddenFieldName); 71 if (hiddenFieldNameInRequestMap == null 72 || hiddenFieldNameInRequestMap.equals("") 73 || !commandLinkClientId.equals(hiddenFieldNameInRequestMap)) { 74 return; 76 } 77 ActionEvent actionEvent = new ActionEvent(uiComponent); 79 uiComponent.queueEvent(actionEvent); 80 } 81 82 protected static String deriveCommonHiddenFieldName( 83 FacesContext facesContext, 84 UIComponent uiComponent) { 85 86 if (Beans.isDesignTime()){ 87 return ""; 88 } 89 90 UIComponent parentNamingContainer = findForm(uiComponent); 91 String parentClientId = parentNamingContainer.getClientId(facesContext); 92 String hiddenFieldName = parentClientId 93 + NamingContainer.SEPARATOR_CHAR 94 + UIViewRoot.UNIQUE_ID_PREFIX 95 + HIDDEN_FIELD_NAME; 96 return hiddenFieldName; 97 } 98 99 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 100 throws IOException { 101 102 validateParameters(facesContext, uiComponent, UICommand.class); 103 104 DOMContext domContext = 105 DOMContext.attachDOMContext(facesContext, uiComponent); 106 107 if (!domContext.isInitialized()) { 108 Element root = domContext.createElement("a"); 109 domContext.setRootNode(root); 110 setRootElementId(facesContext, root, uiComponent); 111 root.setAttribute("href", "# "); 112 } 113 Element root = (Element ) domContext.getRootNode(); 114 DOMContext.removeChildren(root); 115 renderLinkText(uiComponent, domContext, root); 116 117 Map parameterMap = getParameterMap(uiComponent); 118 renderOnClick(facesContext, uiComponent, root, parameterMap); 119 120 String styleClass = 121 (String ) uiComponent.getAttributes().get("styleClass"); 122 if (styleClass != null) { 123 root.setAttribute("class", styleClass); 124 } 125 126 PassThruAttributeRenderer.renderAttributes(facesContext, uiComponent, 127 new String []{"onclick"}); 128 129 FormRenderer.addHiddenField(facesContext, 130 deriveCommonHiddenFieldName( 131 facesContext, 132 (UICommand) uiComponent)); 133 Iterator parameterKeys = parameterMap.keySet().iterator(); 134 while (parameterKeys.hasNext()) { 135 String nextKey = (String ) parameterKeys.next(); 136 FormRenderer.addHiddenField(facesContext, nextKey); 137 } 138 domContext.stepInto(uiComponent); 139 } 140 141 142 147 private void renderLinkText(UIComponent uiComponent, DOMContext 148 domContext, Element root) { 149 Object currentValue = ((UICommand) uiComponent).getValue(); 150 String linkText = null; 151 if (currentValue != null) { 152 linkText = DOMUtils.escapeAnsi(currentValue.toString()); 153 } 154 if (linkText != null && linkText.length() != 0) { 156 Text labelNode = (Text ) root.getFirstChild(); 157 if (labelNode == null) { 158 labelNode = domContext.getDocument().createTextNode(linkText); 159 root.appendChild(labelNode); 160 } else { 161 labelNode.setData(linkText); 162 } 163 } 164 } 165 166 172 protected void renderOnClick(FacesContext facesContext, 173 UIComponent uiComponent, 174 Element root, Map parameters) { 175 UIComponent uiForm = findForm(uiComponent); 176 if (uiForm == null) { 177 throw new FacesException("CommandLink must be contained in a form"); 178 } 179 String uiFormClientId = uiForm.getClientId(facesContext); 180 Object onClick = uiComponent.getAttributes().get(HTML.ONCLICK_ATTR); 181 182 if (onClick != null) { 184 onClick = onClick.toString() + ";" + 185 getJavaScriptOnClickString(facesContext, 186 uiComponent, uiFormClientId, 187 parameters); 188 } else { 189 onClick = getJavaScriptOnClickString(facesContext, 190 uiComponent, uiFormClientId, 191 parameters); 192 } 193 root.setAttribute("onclick", 194 onClick.toString()); } 196 197 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 198 throws IOException { 199 validateParameters(facesContext, uiComponent, UICommand.class); 200 DOMContext domContext = 201 DOMContext.getDOMContext(facesContext, uiComponent); 202 domContext.stepOver(); 203 domContext.streamWrite(facesContext, uiComponent); 204 } 205 206 207 private String getJavaScriptOnClickString(FacesContext facesContext, 208 UIComponent uiComponent, 209 String formClientId, 210 Map parameters) { 211 return getJavascriptHiddenFieldSetters(facesContext, 212 (UICommand) uiComponent, 213 formClientId, parameters) 214 + "iceSubmit(" 215 + " document.forms['" + formClientId + "']," 216 + " this,event); " 217 + "return false;"; 218 } 219 220 227 protected static String getJavascriptHiddenFieldSetters( 228 FacesContext facesContext, 229 UICommand uiCommand, String formClientId, Map parameters) { 230 StringBuffer buffer; 231 buffer = new StringBuffer ("document.forms['" + formClientId + "']['"); 232 buffer.append(deriveCommonHiddenFieldName(facesContext, uiCommand)); 233 buffer.append( 234 "'].value='" + uiCommand.getClientId(facesContext) + "';"); 235 Iterator parameterKeys = parameters.keySet().iterator(); 236 while (parameterKeys.hasNext()) { 237 String nextParamName = (String ) parameterKeys.next(); 238 Object nextParamValue = parameters.get(nextParamName); 239 buffer.append("document.forms['" + formClientId + "']['"); 240 buffer.append(nextParamName); 241 buffer.append("'].value='"); 242 buffer.append(nextParamValue); 243 buffer.append("';"); 244 } 245 return buffer.toString(); 246 } 247 248 public boolean getRendersChildren() { 249 return true; 250 } 251 252 public void encodeChildren(FacesContext facesContext, 253 UIComponent uiComponent) 254 throws IOException { 255 validateParameters(facesContext, uiComponent, UICommand.class); 256 Iterator children = uiComponent.getChildren().iterator(); 257 while (children.hasNext()) { 258 UIComponent nextChild = (UIComponent) children.next(); 259 nextChild.encodeBegin(facesContext); 260 if (nextChild.getRendersChildren()) { 261 nextChild.encodeChildren(facesContext); 262 } 263 nextChild.encodeEnd(facesContext); 264 } 265 } 266 } 267 | Popular Tags |