1 16 package org.apache.myfaces.wap.renderkit.wml; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import javax.faces.FacesException; 23 import javax.faces.component.UIComponent; 24 import javax.faces.component.UIForm; 25 import javax.faces.context.FacesContext; 26 import javax.faces.context.ResponseWriter; 27 import javax.faces.event.ActionEvent; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.myfaces.wap.component.CommandButton; 32 import org.apache.myfaces.wap.renderkit.Attributes; 33 import org.apache.myfaces.wap.renderkit.RendererUtils; 34 import org.apache.myfaces.wap.renderkit.WmlRenderer; 35 36 37 45 public class CommandButtonRenderer extends WmlRenderer { 46 private static Log log = LogFactory.getLog(CommandButtonRenderer.class); 47 48 49 public CommandButtonRenderer() { 50 super(); 51 log.debug("created object " + this.getClass().getName()); 52 } 53 54 public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException { 55 log.debug("encodeBegin(" + component.getId() + ")"); 56 if (context == null || component == null) { 57 throw new NullPointerException (); 58 } 59 if (!component.isRendered()) return; 60 61 } 62 63 public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException { 64 log.debug("encodeChildren(" + component.getId() + ")"); 65 if (context == null || component == null) { 66 throw new NullPointerException (); 67 } 68 } 69 70 public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException { 71 log.debug("encodeEnd(" + component.getId() + ")"); 72 if (context == null || component == null) { 73 throw new NullPointerException (); 74 } 75 if (!component.isRendered()) return; 76 77 ResponseWriter writer = context.getResponseWriter(); 78 CommandButton comp = (CommandButton)component; 79 80 writer.startElement(Attributes.DO, component); 82 RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer); 83 RendererUtils.writeAttribute(Attributes.LABEL, comp.getValue(), writer); 87 RendererUtils.writeAttribute(Attributes.TYPE, comp.getType(), writer); 88 89 90 if (comp.isOptional()) RendererUtils.writeAttribute(Attributes.OPTIONAL, "true", writer); 91 92 if (isReset(comp)) 93 renderReset(context, comp); 94 else 95 renderAction(context, comp); 96 97 writer.endElement(Attributes.DO); 98 } 99 100 101 public void decode(FacesContext context, UIComponent component) { 102 log.debug("decode(" + component.getId() + ")"); 103 if (component == null ) throw new NullPointerException (); 104 if (!component.isRendered()) return; 105 106 Map map = context.getExternalContext().getRequestParameterMap(); 107 108 if (map.containsKey(component.getClientId(context) + Attributes.POSTFIX_ACTIVATED) && !isReset((CommandButton)component)){ 110 log.debug("action queued"); 111 component.queueEvent(new ActionEvent(component)); 112 } 113 } 114 115 private void renderAction(FacesContext context, CommandButton comp) throws java.io.IOException { 116 ResponseWriter writer = context.getResponseWriter(); 117 118 UIForm parentForm = getParentForm(context, comp); 120 String href = RendererUtils.getCurrentUrl(context); 121 122 writer.startElement(Attributes.GO, comp); 124 writer.writeAttribute(Attributes.HREF, href, null); 125 writer.writeAttribute(Attributes.METHOD, Attributes.POST, null); 126 127 Set inputTags = RendererUtils.getInputTags(parentForm.getChildren()); 128 Iterator iter = inputTags.iterator(); 129 130 log.debug("Form has " + inputTags.size() + " input tags."); 131 132 while(iter.hasNext()){ 133 UIComponent component = (UIComponent)iter.next(); 134 String name = RendererUtils.getAttribute(component, Attributes.NAME); 135 136 writer.startElement(Attributes.POSTFIELD, component); 138 writer.writeAttribute(Attributes.NAME, name, null); 139 writer.writeAttribute(Attributes.VALUE, "$(" + name + ")", null); 140 writer.endElement(Attributes.POSTFIELD); 141 } 142 143 writer.startElement(Attributes.POSTFIELD, comp); 145 writer.writeAttribute(Attributes.NAME, parentForm.getClientId(context) + Attributes.POSTFIX_SUBMITED, null); 146 writer.writeAttribute(Attributes.VALUE, "true", null); 147 writer.endElement(Attributes.POSTFIELD); 148 149 writer.startElement(Attributes.POSTFIELD, comp); 151 writer.writeAttribute(Attributes.NAME, comp.getClientId(context) + Attributes.POSTFIX_ACTIVATED, null); 152 writer.writeAttribute(Attributes.VALUE, "true", null); 153 writer.endElement(Attributes.POSTFIELD); 154 155 writer.endElement(Attributes.GO); 156 157 } 158 159 private void renderReset(FacesContext context, CommandButton comp) throws java.io.IOException { 160 ResponseWriter writer = context.getResponseWriter(); 161 162 writer.startElement(Attributes.REFRESH, comp); 163 164 UIForm parentForm = getParentForm(context, comp); 165 Set inputTags = RendererUtils.getInputTags(parentForm.getChildren()); 166 Iterator iter = inputTags.iterator(); 167 168 while(iter.hasNext()){ 169 UIComponent component = (UIComponent)iter.next(); 170 171 writer.startElement(Attributes.SETVAR, comp); 173 String name = RendererUtils.getAttribute(component, Attributes.NAME); 174 writer.writeAttribute(Attributes.NAME, name, null); 175 176 String value = RendererUtils.getAttribute(component, Attributes.VALUE); 177 writer.writeAttribute(Attributes.VALUE, value, null); 178 writer.endElement(Attributes.SETVAR); 179 } 180 181 writer.endElement(Attributes.REFRESH); 182 } 183 184 private boolean isReset(CommandButton comp){ 185 return ("reset".compareToIgnoreCase(comp.getType()) == 0); 186 } 187 188 189 private UIForm getParentForm(FacesContext context, UIComponent component){ 190 UIComponent parent = component.getParent(); 192 while(parent != null && !(parent instanceof UIForm)){ 193 parent = parent.getParent(); 194 } 195 196 String formId; 197 if (parent == null){ throw new FacesException("FacesException - tag commandButton must be nested in a UIForm tag"); 200 } else { 201 return((UIForm)parent); 202 } 203 } 204 } 205 206 | Popular Tags |