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.CommandLink; 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 44 public class CommandLinkRenderer extends WmlRenderer { 45 private static Log log = LogFactory.getLog(CommandLinkRenderer.class); 46 47 48 public CommandLinkRenderer() { 49 super(); 50 log.debug("created object " + this.getClass().getName()); 51 } 52 53 public boolean getRendersChildren(){ 54 return(false); 55 } 56 57 public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException { 58 log.debug("encodeBegin(" + component.getId() + ")"); 59 if (context == null || component == null) { 60 throw new NullPointerException (); 61 } 62 if (!component.isRendered()) return; 63 64 CommandLink comp = (CommandLink)component; 65 ResponseWriter writer = context.getResponseWriter(); 66 67 writer.startElement(Attributes.ANCHOR, component); 68 RendererUtils.writeAttribute(Attributes.TITLE, comp.getTitle(), writer); 69 writer.flush(); 70 } 71 72 public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException { 73 log.debug("encodeChildren(" + component.getId() + ")"); 74 if (context == null || component == null) { 75 throw new NullPointerException (); 76 } 77 if (!component.isRendered()) return; 78 79 RendererUtils.renderChildren(context, component); 80 } 81 82 public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException { 83 log.debug("encodeEnd(" + component.getId() + ")"); 84 if (context == null || component == null) { 85 throw new NullPointerException (); 86 } 87 if (!component.isRendered()) return; 88 89 CommandLink comp = (CommandLink)component; 90 UIForm parentForm = getParentForm(context, component); 91 92 ResponseWriter writer = context.getResponseWriter(); 93 94 String href = RendererUtils.getCurrentUrl(context); 95 96 writer.startElement(Attributes.GO, component); 98 writer.writeAttribute(Attributes.HREF, href, null); 99 writer.writeAttribute(Attributes.METHOD, Attributes.POST, null); 100 101 Set inputTags = RendererUtils.getInputTags(parentForm.getChildren()); 102 Iterator iter = inputTags.iterator(); 103 104 log.debug("Form has " + inputTags.size() + " input tags."); 105 106 while(iter.hasNext()){ 107 UIComponent inputComp = (UIComponent)iter.next(); 108 String name = RendererUtils.getAttribute(inputComp, Attributes.NAME); 109 110 writer.startElement(Attributes.POSTFIELD, component); 112 writer.writeAttribute(Attributes.NAME, name, null); 113 writer.writeAttribute(Attributes.VALUE, "$(" + name + ")", null); 114 writer.endElement(Attributes.POSTFIELD); 115 } 116 117 writer.startElement(Attributes.POSTFIELD, component); 119 writer.writeAttribute(Attributes.NAME, parentForm.getClientId(context) + Attributes.POSTFIX_SUBMITED, null); 120 writer.writeAttribute(Attributes.VALUE, "true", null); 121 writer.endElement(Attributes.POSTFIELD); 122 123 writer.startElement(Attributes.POSTFIELD, component); 125 writer.writeAttribute(Attributes.NAME, component.getClientId(context) + Attributes.POSTFIX_ACTIVATED, null); 126 writer.writeAttribute(Attributes.VALUE, "true", null); 127 writer.endElement(Attributes.POSTFIELD); 128 129 writer.endElement(Attributes.GO); 130 writer.endElement(Attributes.ANCHOR); 131 } 132 133 public void decode(FacesContext context, UIComponent component) { 134 log.debug("decode(" + component.getId() + ")"); 135 if (component == null || context == null) throw new NullPointerException (); 136 if (!component.isRendered()) return; 137 138 Map map = context.getExternalContext().getRequestParameterMap(); 139 140 if (map.containsKey(component.getClientId(context) + Attributes.POSTFIX_ACTIVATED)) 142 component.queueEvent(new ActionEvent(component)); 143 } 144 145 private UIForm getParentForm(FacesContext context, UIComponent component){ 146 UIComponent parent = component.getParent(); 148 while(parent != null && !(parent instanceof UIForm)){ 149 parent = parent.getParent(); 150 } 151 152 String formId; 153 if (parent == null){ throw new FacesException("FacesException - tag commandLink must be nested in a UIForm tag"); 156 } else { 157 return((UIForm)parent); 158 } 159 } 160 161 } 162 163 | Popular Tags |