1 16 package org.apache.myfaces.renderkit.html; 17 18 import org.apache.myfaces.config.MyfacesConfig; 19 import org.apache.myfaces.renderkit.JSFAttr; 20 import org.apache.myfaces.renderkit.RendererUtils; 21 import org.apache.myfaces.renderkit.html.util.DummyFormResponseWriter; 22 import org.apache.myfaces.renderkit.html.util.DummyFormUtils; 23 import org.apache.myfaces.renderkit.html.util.JavascriptUtils; 24 25 import javax.faces.component.UICommand; 26 import javax.faces.component.UIComponent; 27 import javax.faces.component.UIForm; 28 import javax.faces.component.ValueHolder; 29 import javax.faces.component.html.HtmlCommandButton; 30 import javax.faces.context.FacesContext; 31 import javax.faces.context.ResponseWriter; 32 import javax.faces.event.ActionEvent; 33 import java.io.IOException ; 34 import java.util.Map ; 35 36 37 72 public class HtmlButtonRendererBase 73 extends HtmlRenderer 74 { 75 private static final String IMAGE_BUTTON_SUFFIX_X = ".x"; 76 private static final String IMAGE_BUTTON_SUFFIX_Y = ".y"; 77 78 public void decode(FacesContext facesContext, UIComponent uiComponent) 79 { 80 RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class); 81 82 if (!isReset(uiComponent) && isSubmitted(facesContext, uiComponent)) 84 { 85 uiComponent.queueEvent(new ActionEvent(uiComponent)); 86 } 87 } 88 89 private static boolean isReset(UIComponent uiComponent) 90 { 91 return "reset".equalsIgnoreCase((String ) uiComponent.getAttributes().get(HTML.TYPE_ATTR)); 92 } 93 94 private static boolean isSubmitted(FacesContext facesContext, UIComponent uiComponent) 95 { 96 String clientId = uiComponent.getClientId(facesContext); 97 Map paramMap = facesContext.getExternalContext().getRequestParameterMap(); 98 return paramMap.containsKey(clientId) || paramMap.containsKey(clientId + IMAGE_BUTTON_SUFFIX_X) || paramMap.containsKey(clientId + IMAGE_BUTTON_SUFFIX_Y); 99 } 100 101 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 102 throws IOException 103 { 104 RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class); 105 106 String clientId = uiComponent.getClientId(facesContext); 107 108 ResponseWriter writer = facesContext.getResponseWriter(); 109 110 writer.startElement(HTML.INPUT_ELEM, uiComponent); 111 112 writer.writeAttribute(HTML.ID_ATTR, clientId, null); 113 writer.writeAttribute(HTML.NAME_ATTR, clientId, null); 114 115 String image = getImage(uiComponent); 116 117 if (image != null) 118 { 119 writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_IMAGE, null); 120 writer.writeAttribute(HTML.SRC_ATTR, image, JSFAttr.IMAGE_ATTR); 121 } 122 else 123 { 124 String type = getType(uiComponent); 125 126 if (type == null) 127 { 128 type = HTML.INPUT_TYPE_SUBMIT; 129 } 130 writer.writeAttribute(HTML.TYPE_ATTR, type, JSFAttr.TYPE_ATTR); 131 Object value = getValue(uiComponent); 132 if (value != null) 133 { 134 writer.writeAttribute(HTML.VALUE_ATTR, value, null); 135 } 136 } 137 if (JavascriptUtils.isJavascriptAllowed(facesContext.getExternalContext())) 138 { 139 StringBuffer onClick = buildOnClick(uiComponent, facesContext, writer); 140 writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null); 141 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 142 HTML.BUTTON_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_ONCLICK); 143 } 144 else 145 { 146 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 147 HTML.BUTTON_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED); 148 } 149 150 if (isDisabled(facesContext, uiComponent)) 151 { 152 writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null); 153 } 154 155 writer.endElement(HTML.INPUT_ELEM); 156 } 157 158 159 protected StringBuffer buildOnClick(UIComponent uiComponent, FacesContext facesContext, ResponseWriter writer) 160 throws IOException 161 { 162 UIComponent parent = uiComponent.getParent(); 164 while (parent != null && !(parent instanceof UIForm)) 165 { 166 parent = parent.getParent(); 167 } 168 169 UIForm nestingForm = null; 170 String formName; 171 DummyFormResponseWriter dummyFormResponseWriter; 172 if (parent != null) 173 { 174 nestingForm = (UIForm)parent; 176 formName = nestingForm.getClientId(facesContext); 177 dummyFormResponseWriter = null; 178 } 179 else 180 { 181 formName = DummyFormUtils.DUMMY_FORM_NAME; 183 dummyFormResponseWriter = DummyFormUtils.getDummyFormResponseWriter(facesContext); 184 dummyFormResponseWriter.setWriteDummyForm(true); 185 } 186 StringBuffer onClick = new StringBuffer (); 187 String commandOnClick = (String )uiComponent.getAttributes().get(HTML.ONCLICK_ATTR); 188 189 if (commandOnClick != null) 190 { 191 onClick.append(commandOnClick); 192 onClick.append(';'); 193 } 194 195 onClick.append(HtmlRendererUtils.getClearHiddenCommandFormParamsFunctionName(formName)).append("();"); 197 198 if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isAutoScroll()) 199 { 200 JavascriptUtils.appendAutoScrollAssignment(onClick, formName); 201 } 202 203 String hiddenFieldName = HtmlRendererUtils.getHiddenCommandLinkFieldName(formName); 205 if (nestingForm != null) 206 { 207 HtmlFormRendererBase.addHiddenCommandParameter(nestingForm, hiddenFieldName); 208 } 209 else 210 { 211 dummyFormResponseWriter.addDummyFormParameter(hiddenFieldName); 212 } 213 214 return onClick; 215 } 216 217 218 219 protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent) 220 { 221 if (uiComponent instanceof HtmlCommandButton) 223 { 224 return ((HtmlCommandButton)uiComponent).isDisabled(); 225 } 226 else 227 { 228 return RendererUtils.getBooleanAttribute(uiComponent, HTML.DISABLED_ATTR, false); 229 } 230 } 231 232 233 private String getImage(UIComponent uiComponent) 234 { 235 if (uiComponent instanceof HtmlCommandButton) 236 { 237 return ((HtmlCommandButton)uiComponent).getImage(); 238 } 239 return (String )uiComponent.getAttributes().get(JSFAttr.IMAGE_ATTR); 240 } 241 242 private String getType(UIComponent uiComponent) 243 { 244 if (uiComponent instanceof HtmlCommandButton) 245 { 246 return ((HtmlCommandButton)uiComponent).getType(); 247 } 248 return (String )uiComponent.getAttributes().get(JSFAttr.TYPE_ATTR); 249 } 250 251 private Object getValue(UIComponent uiComponent) 252 { 253 if (uiComponent instanceof ValueHolder) 254 { 255 return ((ValueHolder)uiComponent).getValue(); 256 } 257 return uiComponent.getAttributes().get(JSFAttr.VALUE_ATTR); 258 } 259 } 260 | Popular Tags |