1 17 package org.alfresco.web.ui.common.renderer; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 import java.util.Map ; 22 23 import javax.faces.component.UIComponent; 24 import javax.faces.context.FacesContext; 25 import javax.faces.event.ActionEvent; 26 27 import org.alfresco.web.ui.common.Utils; 28 import org.alfresco.web.ui.common.component.UIActionLink; 29 import org.alfresco.web.ui.common.component.UIMenu; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 36 public class ActionLinkRenderer extends BaseRenderer 37 { 38 private static Log logger = LogFactory.getLog(ActionLinkRenderer.class); 39 40 43 46 public void decode(FacesContext context, UIComponent component) 47 { 48 Map requestMap = context.getExternalContext().getRequestParameterMap(); 49 String fieldId = Utils.getActionHiddenFieldName(context, component); 50 String value = (String )requestMap.get(fieldId); 51 if (value != null && value.equals(component.getClientId(context))) 53 { 54 UIActionLink link = (UIActionLink)component; 57 Map <String , String > destParams = link.getParameterMap(); 58 destParams.clear(); 59 Map <String , String > actionParams = getParameterMap(link); 60 if (actionParams != null) 61 { 62 for (String name : actionParams.keySet()) 63 { 64 String paramValue = (String )requestMap.get(name); 65 destParams.put(name, paramValue); 66 } 67 } 68 69 ActionEvent event = new ActionEvent(component); 70 component.queueEvent(event); 71 } 72 } 73 74 77 public void encodeEnd(FacesContext context, UIComponent component) throws IOException 78 { 79 if (component.isRendered() == true) 81 { 82 Writer out = context.getResponseWriter(); 83 84 UIActionLink link = (UIActionLink)component; 85 86 if (isInMenu(link) == true) 87 { 88 out.write( renderMenuAction(context, link) ); 90 } 91 else 92 { 93 out.write( renderActionLink(context, link) ); 95 } 96 } 97 } 98 99 107 private String renderActionLink(FacesContext context, UIActionLink link) 108 { 109 Map attrs = link.getAttributes(); 110 StringBuilder linkBuf = new StringBuilder (256); 111 112 if (link.getHref() == null) 113 { 114 linkBuf.append("<a HREF='#' onclick=\""); 115 116 if (link.getOnclick() != null) 118 { 119 linkBuf.append(link.getOnclick()); 120 } 121 else 122 { 123 linkBuf.append(Utils.generateFormSubmit(context, link, Utils.getActionHiddenFieldName(context, link), link.getClientId(context), getParameterMap(link))); 126 } 127 128 linkBuf.append('"'); 129 } 130 else 131 { 132 String href = link.getHref(); 133 if (href.startsWith("http") == false && href.startsWith("file") == false) 134 { 135 href = context.getExternalContext().getRequestContextPath() + href; 136 } 137 linkBuf.append("<a HREF=\"") 138 .append(href) 139 .append('"'); 140 141 if (link.getTarget() != null) 143 { 144 linkBuf.append(" target=\"") 145 .append(link.getTarget()) 146 .append("\""); 147 } 148 } 149 150 if (attrs.get("style") != null) 151 { 152 linkBuf.append(" style=\"") 153 .append(attrs.get("style")) 154 .append('"'); 155 } 156 if (attrs.get("styleClass") != null) 157 { 158 linkBuf.append(" class=") 159 .append(attrs.get("styleClass")); 160 } 161 if (link.getTooltip() != null) 162 { 163 linkBuf.append(" title=\"") 164 .append(Utils.encode(link.getTooltip())) 165 .append('"'); 166 } 167 linkBuf.append('>'); 168 169 StringBuilder buf = new StringBuilder (350); 170 if (link.getImage() != null) 171 { 172 int padding = link.getPadding(); 173 if (padding != 0) 174 { 175 buf.append("<table cellspacing=0 cellpadding=0><tr><td width=16>"); 177 } 178 179 if (link.getShowLink() == false) 180 { 181 buf.append(linkBuf.toString()); 182 } 183 184 buf.append(Utils.buildImageTag(context, link.getImage(), (String )link.getValue(), "absmiddle")); 186 187 if (link.getShowLink() == false) 188 { 189 buf.append("</a>"); 190 } 191 else 192 { 193 if (padding != 0) 194 { 195 buf.append("</td><td style=\"padding:") 196 .append(padding) 197 .append("px\">"); 198 } 199 else 200 { 201 buf.append("<span style='padding-left:2px"); 203 204 if (attrs.get("verticalAlign") != null) 206 { 207 buf.append(";vertical-align:") 208 .append(attrs.get("verticalAlign")); 209 } 210 211 buf.append("'>"); 212 } 213 214 buf.append(linkBuf.toString()); 215 buf.append(Utils.encode(link.getValue().toString())); 216 buf.append("</a>"); 217 218 if (padding == 0) 219 { 220 buf.append("</span>"); 221 } 222 } 223 224 if (padding != 0) 225 { 226 buf.append("</td></tr></table>"); 227 } 228 } 229 else 230 { 231 buf.append(linkBuf.toString()); 232 buf.append(Utils.encode(link.getValue().toString())); 233 buf.append("</a>"); 234 } 235 236 return buf.toString(); 237 } 238 239 247 private String renderMenuAction(FacesContext context, UIActionLink link) 248 { 249 StringBuilder buf = new StringBuilder (256); 250 251 buf.append("<tr><td>"); 252 253 if (link.getImage() != null) 255 { 256 buf.append(Utils.buildImageTag(context, link.getImage(), (String )link.getValue())); 257 } 258 259 buf.append("</td><td"); 260 int padding = link.getPadding(); 261 if (padding != 0) 262 { 263 buf.append(" style=\"padding:") 264 .append(padding) 265 .append("px\""); 266 } 267 buf.append(">"); 268 269 if (link.getHref() == null) 271 { 272 buf.append("<a HREF='#' onclick=\""); 273 buf.append(Utils.generateFormSubmit(context, link, Utils.getActionHiddenFieldName(context, link), link.getClientId(context), getParameterMap(link))); 274 buf.append('"'); 275 } 276 else 277 { 278 String href = link.getHref(); 279 if (href.startsWith("http") == false) 280 { 281 href = context.getExternalContext().getRequestContextPath() + href; 282 } 283 buf.append("<a HREF=\"") 284 .append(href) 285 .append('"'); 286 287 if (link.getTarget() != null) 289 { 290 buf.append(" target=\"") 291 .append(link.getTarget()) 292 .append("\""); 293 } 294 } 295 296 Map attrs = link.getAttributes(); 297 if (attrs.get("style") != null) 298 { 299 buf.append(" style=\"") 300 .append(attrs.get("style")) 301 .append('"'); 302 } 303 if (attrs.get("styleClass") != null) 304 { 305 buf.append(" class=") 306 .append(attrs.get("styleClass")); 307 } 308 buf.append('>'); 309 buf.append(Utils.encode(link.getValue().toString())); 310 buf.append("</a>"); 311 312 buf.append("</td></tr>"); 313 314 return buf.toString(); 315 } 316 317 318 321 328 private static boolean isInMenu(UIActionLink link) 329 { 330 UIComponent parent = link.getParent(); 331 while (parent != null) 332 { 333 if (parent instanceof UIMenu) 334 { 335 break; 336 } 337 parent = parent.getParent(); 338 } 339 return (parent != null); 340 } 341 } 342 | Popular Tags |