1 16 17 package org.apache.struts.faces.renderer; 18 19 20 import java.io.IOException ; 21 import java.util.Iterator ; 22 23 import javax.faces.component.NamingContainer; 24 import javax.faces.component.UICommand; 25 import javax.faces.component.UIComponent; 26 import javax.faces.component.UIForm; 27 import javax.faces.component.UIParameter; 28 import javax.faces.context.FacesContext; 29 import javax.faces.context.ResponseWriter; 30 import javax.faces.event.ActionEvent; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.struts.Globals; 35 import org.apache.struts.config.ActionConfig; 36 import org.apache.struts.config.ModuleConfig; 37 38 39 45 46 public class CommandLinkRenderer extends AbstractRenderer { 47 48 49 51 52 55 private static final String TOKEN = 56 "org_apache_struts_faces_renderer_CommandLinkRenderer"; 57 58 59 62 private static Log log = LogFactory.getLog(CommandLinkRenderer.class); 63 64 65 67 68 78 public void decode(FacesContext context, UIComponent component) { 79 80 if ((context == null) || (component == null)) { 82 throw new NullPointerException (); 83 } 84 85 if (!component.isRendered() || isDisabled(component) || 87 isReadOnly(component)) { 88 return; 89 } 90 91 UIForm form = null; 93 UIComponent parent = component.getParent(); 94 while (parent != null) { 95 if (parent instanceof UIForm) { 96 form = (UIForm) parent; 97 break; 98 } 99 parent = parent.getParent(); 100 } 101 if (form == null) { 102 log.warn("CommandLinkComponent not nested inside UIForm, ignored"); 103 return; 104 } 105 106 String paramId = TOKEN; 108 String value = (String ) 109 context.getExternalContext().getRequestParameterMap().get(paramId); 110 if ((value == null) || !value.equals(component.getClientId(context))) { 111 if (log.isTraceEnabled()) { 112 log.trace("decode(" + component.getId() + ") --> not active"); 113 } 114 return; 115 } 116 117 if (log.isTraceEnabled()) { 119 log.trace("decode(" + component.getId() + ") --> queueEvent()"); 120 } 121 component.queueEvent(new ActionEvent(component)); 122 123 } 124 125 126 private static String passThrough[] = 127 { "accesskey", "charset", "dir", "hreflang", "lang", "onblur", 128 "ondblclick", "onfocus", "onkeydown", 129 "onkeypress", "onkeyup", "onmousedown", "onmousemove", 130 "onmouseout", "onmouseover", "onmouseup", "rel", "rev", 131 "style", "tabindex", "target", "title", "type" }; 132 133 134 145 public void renderStart(FacesContext context, UIComponent component, 146 ResponseWriter writer) 147 throws IOException { 148 149 if (!component.isRendered() || isDisabled(component) || 151 isReadOnly(component)) { 152 return; 153 } 154 155 UIForm form = null; 157 UIComponent parent = component.getParent(); 158 while (parent != null) { 159 if (parent instanceof UIForm) { 160 form = (UIForm) parent; 161 break; 162 } 163 parent = parent.getParent(); 164 } 165 if (form == null) { 166 log.warn("CommandLinkComponent not nested inside UIForm, ignored"); 167 return; 168 } 169 String formClientId = form.getClientId(context); 170 171 String key = formClientId + NamingContainer.SEPARATOR_CHAR + TOKEN; 174 if (context.getExternalContext().getRequestMap().get(key) == null) { 175 writer.startElement("input", null); 176 writer.writeAttribute("name", TOKEN, null); 177 writer.writeAttribute("type", "hidden", null); 178 writer.writeAttribute("value", "", null); 179 writer.endElement("input"); 180 context.getExternalContext().getRequestMap().put 181 (key, Boolean.TRUE); 182 } 183 184 185 writer.startElement("a", component); 187 188 } 189 190 191 202 public void renderAttributes(FacesContext context, UIComponent component, 203 ResponseWriter writer) 204 throws IOException { 205 206 if (!component.isRendered() || isDisabled(component) || 208 isReadOnly(component)) { 209 return; 210 } 211 212 UIForm form = null; 214 UIComponent parent = component.getParent(); 215 while (parent != null) { 216 if (parent instanceof UIForm) { 217 form = (UIForm) parent; 218 break; 219 } 220 parent = parent.getParent(); 221 } 222 if (form == null) { 223 log.warn("CommandLinkComponent not nested inside UIForm, ignored"); 224 return; 225 } 226 String formClientId = form.getClientId(context); 227 228 if (component.getId() != null) { 230 writer.writeAttribute("id", component.getClientId(context), "id"); 231 } 232 writer.writeAttribute("href", "#", null); 233 String styleClass = (String ) 234 component.getAttributes().get("styleClass"); 235 if (styleClass != null) { 236 writer.writeAttribute("class", styleClass, "styleClass"); 237 } 238 renderPassThrough(context, component, writer, passThrough); 239 240 StringBuffer sb = new StringBuffer (); 242 sb.append("document.forms['"); 243 sb.append(formClientId); 244 sb.append("']['"); 245 sb.append(TOKEN); 246 sb.append("'].value='"); 247 sb.append(component.getClientId(context)); 248 sb.append("';"); 249 Iterator kids = component.getChildren().iterator(); 250 while (kids.hasNext()) { 251 UIComponent kid = (UIComponent) kids.next(); 252 if (!(kid instanceof UIParameter)) { 253 continue; 254 } 255 sb.append("document.forms['"); 256 sb.append(formClientId); 257 sb.append("']['"); 258 sb.append((String ) kid.getAttributes().get("name")); 259 sb.append("'].value='"); 260 sb.append((String ) kid.getAttributes().get("value")); 261 sb.append("';"); 262 } 263 sb.append("document.forms['"); 264 sb.append(formClientId); 265 sb.append("'].submit(); return false;"); 266 writer.writeAttribute("onclick", sb.toString(), null); 267 268 Object value = component.getAttributes().get("value"); 270 if (value != null) { 271 if (value instanceof String ) { 272 writer.write((String ) value); 273 } else { 274 writer.write(value.toString()); 275 } 276 } 277 278 } 279 280 281 292 public void renderEnd(FacesContext context, UIComponent component, 293 ResponseWriter writer) 294 throws IOException { 295 296 if (!component.isRendered() || isDisabled(component) || 298 isReadOnly(component)) { 299 return; 300 } 301 302 writer.endElement("a"); 304 305 } 306 307 308 } 309 | Popular Tags |