1 33 34 package com.icesoft.faces.renderkit.dom_html_basic; 35 36 import com.icesoft.faces.context.DOMContext; 37 import com.icesoft.faces.context.effects.JavascriptContext; 38 import org.w3c.dom.Element ; 39 40 import javax.faces.component.UICommand; 41 import javax.faces.component.UIComponent; 42 import javax.faces.context.FacesContext; 43 import javax.faces.event.ActionEvent; 44 import java.io.IOException ; 45 import java.util.Map ; 46 47 public class ButtonRenderer extends DomBasicRenderer { 48 49 public void decode(FacesContext facesContext, UIComponent uiComponent) { 50 validateParameters(facesContext, uiComponent, UICommand.class); 51 if (isStatic(uiComponent)) { 52 return; 53 } 54 boolean thisButtonInvokedSubmit = 55 didThisButtonInvokeSubmit(facesContext, uiComponent); 56 if (!thisButtonInvokedSubmit) { 57 return; 58 } 59 String type = ((String ) uiComponent.getAttributes().get("type")) 60 .toLowerCase(); 61 if ((type != null) && (type.equals("reset"))) { 62 return; 63 } 64 ActionEvent actionEvent = new ActionEvent(uiComponent); 65 uiComponent.queueEvent(actionEvent); 66 } 67 68 73 private boolean didThisButtonInvokeSubmit( 74 FacesContext facesContext, UIComponent uiComponent) { 75 boolean thisButtonInvokedSubmit = false; 76 if (!isTextFieldInvokedSubmit(facesContext)) { 79 Map requestParameterMap = 80 facesContext.getExternalContext().getRequestParameterMap(); 81 String componentClientId = uiComponent.getClientId(facesContext); 82 String clientIdInRequestMap = 83 (String ) requestParameterMap.get(componentClientId); 84 thisButtonInvokedSubmit = clientIdInRequestMap != null; 85 if (!thisButtonInvokedSubmit) { 86 String clientIdXCoordinateInRequestMap = 89 componentClientId + ".x"; 90 String clientIdYCoordinateInRequestMap = 91 componentClientId + ".y"; 92 if (requestParameterMap.get(componentClientId + ".x") != null 93 || requestParameterMap.get(componentClientId + ".y") != 94 null) { 95 thisButtonInvokedSubmit = true; 96 } 97 } 98 } 99 return thisButtonInvokedSubmit; 100 } 101 102 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 103 throws IOException { 104 validateParameters(facesContext, uiComponent, UICommand.class); 105 106 DOMContext domContext = 107 DOMContext.attachDOMContext(facesContext, uiComponent); 108 String clientId = uiComponent.getClientId(facesContext); 109 110 if (!domContext.isInitialized()) { 111 Element root = domContext.createElement("input"); 112 domContext.setRootNode(root); 113 root.setAttribute("name", clientId); 114 } 115 Element root = (Element ) domContext.getRootNode(); 116 117 setRootElementId(facesContext, root, uiComponent); 118 119 String typeAttribute = 126 ((String ) uiComponent.getAttributes().get("type")) 127 .toLowerCase(); 128 if (typeAttribute == null || (!typeAttribute.equals("reset") && 129 (!typeAttribute.equals("button")))) { 130 typeAttribute = "submit"; 131 } 132 uiComponent.getAttributes().put("type", typeAttribute); 133 134 String imageAttribute = 138 (String ) uiComponent.getAttributes().get("image"); 139 if (imageAttribute != null) { 140 typeAttribute = "image"; 141 root.setAttribute("type", typeAttribute); 142 143 String pathToImage = getResourceURL(facesContext, imageAttribute); 146 147 root.setAttribute("src", pathToImage); 148 root.removeAttribute("value"); 149 } else { 150 root.setAttribute("type", typeAttribute); 151 String label = ""; 152 Object componentValue = ((UICommand) uiComponent).getValue(); 153 if (componentValue != null) { 154 label = componentValue.toString(); 155 } else { 156 label = ""; 157 } 158 root.setAttribute("value", label); 159 root.removeAttribute("src"); 160 } 161 162 String styleClass = 163 (String ) uiComponent.getAttributes().get("styleClass"); 164 if (styleClass != null) { 165 root.setAttribute("class", styleClass); 166 } 167 168 JavascriptContext.fireEffect(uiComponent, facesContext); 169 170 PassThruAttributeRenderer 171 .renderAttributes(facesContext, uiComponent, null); 172 if (typeAttribute.equals("submit") || typeAttribute.equals("image")) { 174 renderOnClick(uiComponent, root); 175 } 176 177 domContext.stepOver(); 178 domContext.streamWrite(facesContext, uiComponent); 179 } 180 181 185 protected void renderOnClick(UIComponent uiComponent, Element root) { 186 String onclick = (String ) uiComponent.getAttributes().get("onclick"); 187 String submitCode = this.ICESUBMIT + "return false;"; 188 if (onclick == null) { 189 onclick = submitCode; 190 } else { 191 onclick += submitCode; 192 } 193 root.setAttribute("onclick", onclick); 194 } 195 196 public void encodeChildren(FacesContext facesContext, 197 UIComponent uiComponent) 198 throws IOException { 199 validateParameters(facesContext, uiComponent, UICommand.class); 200 } 201 202 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 203 throws IOException { 204 validateParameters(facesContext, uiComponent, UICommand.class); 205 } 206 207 private boolean isTextFieldInvokedSubmit(FacesContext facesContext) { 208 Object textFieldfocus = 209 facesContext.getExternalContext().getRequestParameterMap() 210 .get(FormRenderer.FOCUS_HIDDEN_FIELD); 211 if (textFieldfocus == null) { 212 return false; 213 } 214 215 if (textFieldfocus.toString().length() > 0) { 216 return true; 217 } else { 218 return false; 219 } 220 } 221 } 222 | Popular Tags |