1 20 package org.enhydra.barracuda.core.comp.renderer.html; 21 22 import java.util.*; 23 24 import org.apache.log4j.*; 25 import org.w3c.dom.*; 26 import org.w3c.dom.html.*; 27 28 import org.enhydra.barracuda.core.comp.*; 29 import org.enhydra.barracuda.core.comp.helper.*; 30 import org.enhydra.barracuda.core.comp.renderer.*; 31 import org.enhydra.barracuda.core.comp.scripting.*; 32 import org.enhydra.barracuda.core.helper.servlet.*; 33 import org.enhydra.barracuda.core.util.http.URLRewriter; 34 import org.enhydra.barracuda.core.view.*; 35 import org.enhydra.barracuda.plankton.http.LightweightURL; 36 37 40 public class HTMLActionRenderer extends HTMLComponentRenderer { 41 42 protected static final Logger logger = Logger.getLogger(HTMLActionRenderer.class.getName()); 43 44 protected EnabledHelper eh = new EnabledHelper(); 45 46 60 public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { Node templateNode = vc.getTemplateNode(); 63 Node defaultNode = null; 64 if ((templateNode instanceof HTMLAnchorElement) 65 || (templateNode instanceof HTMLFormElement) 66 || (templateNode instanceof HTMLInputElement) 67 || (templateNode instanceof HTMLButtonElement) 68 || (templateNode instanceof HTMLSelectElement)) { 69 defaultNode = templateNode.cloneNode(true); 70 } else { 71 return super.createDefaultNode(doc, comp, vc); 72 } 73 if (logger.isInfoEnabled()) logger.info("Creating default node: "+defaultNode); 74 return defaultNode; 75 } 76 77 87 public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException { 88 if (!(comp instanceof BAction)) { 90 String errmsg = "This renderer can only render BAction components: "+comp; 91 logger.warn(errmsg); 92 throw new NoSuitableRendererException(errmsg); 93 } 94 BAction actionComp = (BAction) comp; 95 HTMLElement el = (HTMLElement)view.getNode(); 96 97 if (logger.isDebugEnabled()) showNodeInterfaces(view, logger); 99 100 super.renderComponent(actionComp, view, vc); 102 103 manipulateActionElement(el, actionComp, vc); 104 105 eh.setEnabled(el, actionComp.isEnabled()); 107 } 108 109 120 protected static void manipulateActionElement(Element el, BAction comp, ViewContext vc) throws RenderException { 121 if (el instanceof HTMLAnchorElement) { 122 manipulateActionElement((HTMLAnchorElement)el, comp, vc); 123 } else if (el instanceof HTMLFormElement) { 124 manipulateActionElement((HTMLFormElement)el, comp, vc); 125 } else if (el instanceof HTMLInputElement) { 126 manipulateActionElement((HTMLInputElement)el, comp, vc); 127 } else if (el instanceof HTMLButtonElement) { 128 manipulateActionElement((HTMLButtonElement)el, comp, vc); 129 } else if (el instanceof HTMLSelectElement) { 130 manipulateActionElement((HTMLSelectElement)el, comp, vc); 131 } else { 135 String errmsg = "Element does not implement one of HTMLAnchorElement, HTMLFormElement, HTMLInputElement, HTMLButtonElement, or HTMLSelectElement and cannot be rendered: "+el; 136 logger.warn(errmsg); 137 throw new NoSuitableRendererException(errmsg); 138 } 139 } 140 141 146 protected static void manipulateActionElement(HTMLAnchorElement el, BAction comp, ViewContext vc) throws RenderException { 147 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLAnchorElement interface..."); 148 149 if (comp.hasAction()) el.setHref(comp.getAction(vc)); 155 if (comp.getDisableBackButton() && clientScriptingAllowed(comp, vc)) { 158 el.setAttribute("onclick", "location.replace(this.href);return false;"); 159 } 160 } 161 162 167 protected static void manipulateActionElement(HTMLFormElement el, BAction comp, ViewContext vc) throws RenderException { 168 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLFormElement interface..."); 169 170 if (clientScriptingAllowed(comp, vc)) { 172 StringBuffer sb = new StringBuffer (200); 173 StringBuffer sbAction = new StringBuffer (200); 174 boolean disable = comp.getDisableBackButton(); 175 176 if (disable) sbAction.append(ParamGateway.PARAM_TARGET); 177 LightweightURL lu = new LightweightURL(comp.getAction(vc, true)); 178 sbAction.append(lu.getBaseStr()); 179 if (disable) sbAction.append(ParamGateway.PARAM_EXT); 180 sbAction.append(lu.getParamStr()); 181 182 el.setAction(URLRewriter.encodeURL(vc, sbAction.toString())); 184 185 addSubmitScript(comp, sb, "this"); 186 187 el.setAttribute("onsubmit", sb.toString()); 189 190 includeScriptLibrary(el, comp, BScriptResource.JS_FORM_CONTROL); 192 if (disable) includeScriptLibrary(el, comp, BScriptResource.JS_CLIENT_SERVER_HTTP_LIB); 193 } else { el.setAction(comp.getAction(vc)); 195 } 196 } 197 198 203 protected static void manipulateActionElement(HTMLInputElement el, BAction comp, ViewContext vc) throws RenderException { 204 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLInputElement interface..."); 205 HTMLFormElement fel = el.getForm(); 206 208 if (clientScriptingAllowed(comp, vc)) { 210 StringBuffer sb = new StringBuffer (200); 212 StringBuffer sbAction = new StringBuffer (100); 213 boolean disable = comp.getDisableBackButton(); 214 215 if (disable) sbAction.append(ParamGateway.PARAM_TARGET); 217 LightweightURL lu = new LightweightURL(comp.getAction(vc, true)); 218 sbAction.append(lu.getBaseStr()); 219 if (disable) sbAction.append(ParamGateway.PARAM_EXT); 220 sb.append("this.form.action='").append(URLRewriter.encodeURL(vc, sbAction.toString())).append(lu.getParamStr()).append("';"); 221 222 addSubmitScript(comp, sb, "this.form"); 223 224 el.setAttribute("onclick", sb.toString()); 226 227 if ("submit".equalsIgnoreCase(el.getType())) { 234 fel.setAttribute("onsubmit", new StringBuffer (50).append("this.").append(NamingHelper.getName(el)).append(".click();return false;").toString()); 235 } 236 237 includeScriptLibrary(el, comp, BScriptResource.JS_FORM_CONTROL); 239 if (disable) includeScriptLibrary(el, comp, BScriptResource.JS_CLIENT_SERVER_HTTP_LIB); 240 } else { if (!"submit".equalsIgnoreCase(el.getType())) { 243 if ("button".equalsIgnoreCase(el.getType())) { 244 el.setAttribute("type", "submit"); 247 } else { 248 if (comp.getRenderStrategy() != RenderStrategy.CUSTOM_SCRIPT) { 249 String errmsg = "Cannot render Input action listener; input type is not 'submit': "+el; 254 logger.warn(errmsg); 255 throw new NoSuitableRendererException(errmsg); 256 } 257 } 258 } 259 260 el.setName(FormGateway.FORM_TARGET+comp.getAction(vc, true)); 262 263 fel.setAction(URLRewriter.encodeURL(vc, NamingHelper.getName(fel)+FormGateway.FORM_EXT)); 265 } 266 } 267 268 273 protected static void manipulateActionElement(HTMLButtonElement el, BAction comp, ViewContext vc) throws RenderException { 274 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLButtonElement interface..."); 275 HTMLFormElement fel = el.getForm(); 276 278 if (clientScriptingAllowed(comp, vc)) { 280 StringBuffer sb = new StringBuffer (200); 282 StringBuffer sbAction = new StringBuffer (100); 283 boolean disable = comp.getDisableBackButton(); 284 285 if (disable) sbAction.append(ParamGateway.PARAM_TARGET); 287 LightweightURL lu = new LightweightURL(comp.getAction(vc, true)); 288 sbAction.append(lu.getBaseStr()); 289 if (disable) sbAction.append(ParamGateway.PARAM_EXT); 290 sb.append("this.form.action='").append(URLRewriter.encodeURL(vc, sbAction.toString())).append(lu.getParamStr()).append("';"); 291 292 addSubmitScript(comp, sb, "this.form"); 293 294 el.setAttribute("onclick", sb.toString()); 296 297 if ("submit".equalsIgnoreCase(el.getType())) { 304 fel.setAttribute("onsubmit", new StringBuffer (50).append("this.").append(NamingHelper.getName(el)).append(".click();return false;").toString()); 305 } 306 307 includeScriptLibrary(el, comp, BScriptResource.JS_FORM_CONTROL); 309 if (disable) includeScriptLibrary(el, comp, BScriptResource.JS_CLIENT_SERVER_HTTP_LIB); 310 } else { if (!"submit".equalsIgnoreCase(el.getType())) { 313 if ("button".equalsIgnoreCase(el.getType())) { 314 el.setAttribute("type", "submit"); 317 } else { 318 if (comp.getRenderStrategy() != RenderStrategy.CUSTOM_SCRIPT) { 319 String errmsg = "Cannot render Button action listener; button type is not 'submit': "+el; 323 logger.warn(errmsg); 324 throw new NoSuitableRendererException(errmsg); 325 } 326 } 327 } 328 329 el.setName(FormGateway.FORM_TARGET+comp.getAction(vc, true)); 331 332 fel.setAction(URLRewriter.encodeURL(vc, NamingHelper.getName(fel)+FormGateway.FORM_EXT)); 334 } 335 } 336 337 342 protected static void manipulateActionElement(HTMLSelectElement el, BAction comp, ViewContext vc) throws RenderException { 343 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLSelectElement interface..."); 344 345 HTMLFormElement fel = el.getForm(); 346 348 if (clientScriptingAllowed(comp, vc)) { 350 StringBuffer sb = new StringBuffer (200); 352 StringBuffer sbAction = new StringBuffer (100); 353 boolean disable = comp.getDisableBackButton(); 354 355 if (disable) sbAction.append(ParamGateway.PARAM_TARGET); 357 LightweightURL lu = new LightweightURL(comp.getAction(vc, true)); 358 sbAction.append(lu.getBaseStr()); 359 if (disable) sbAction.append(ParamGateway.PARAM_EXT); 360 sb.append("this.form.action='").append(URLRewriter.encodeURL(vc, sbAction.toString())).append(lu.getParamStr()).append("';"); 361 362 addSubmitScript(comp, sb, "this.form"); 363 364 el.setAttribute("onchange", sb.toString()); 366 367 includeScriptLibrary(el, comp, BScriptResource.JS_FORM_CONTROL); 369 if (disable) includeScriptLibrary(el, comp, BScriptResource.JS_CLIENT_SERVER_HTTP_LIB); 370 } else { 371 if (comp.getRenderStrategy() != RenderStrategy.CUSTOM_SCRIPT) { 372 String errmsg = "Cannot render Select action listener; client does not support Javascript: "+el; 373 logger.warn(errmsg); 374 throw new NoSuitableRendererException(errmsg); 375 } 376 377 el.setName(FormGateway.FORM_TARGET+comp.getAction(vc, true)); 379 380 fel.setAction(URLRewriter.encodeURL(vc, NamingHelper.getName(fel)+FormGateway.FORM_EXT)); 382 } 383 } 384 385 387 397 private static boolean clientScriptingAllowed(BAction comp, ViewContext vc) { 398 return vc.getViewCapabilities().getScriptingType() instanceof ScriptingType.JavaScript1x 399 && comp.getRenderStrategy() != RenderStrategy.NEVER_SCRIPT 400 && comp.getRenderStrategy() != RenderStrategy.CUSTOM_SCRIPT; 401 } 402 403 415 private static void includeScriptLibrary(HTMLElement el, BAction comp, String script) { 416 BScriptResource bsr = new BScriptResource(ResourceGateway.EXT_RESOURCE_ID+script); 417 bsr.setView(new DefaultView(el)); 418 comp.addStepChild(bsr, true); 419 } 420 421 433 private static void addSubmitScript(BAction comp, StringBuffer sb, String form) { 434 sb.append("return "); 437 if (comp.getDisableFormLocking()) sb.append("doSubmitAndNoLock("); 438 else sb.append("doSubmitAndLock("); 439 sb.append(form); 440 Collection scriptFunctions = comp.getScriptFunctions(); 442 if (scriptFunctions != null) { 443 Iterator iter = scriptFunctions.iterator(); 444 while (iter.hasNext()) { 445 sb.append(",").append((String )iter.next()); 446 } 447 } 448 if (comp.getDisableBackButton()) sb.append(",cshl_SubmitAndReplace"); 449 sb.append(");"); 450 } 451 } 452 | Popular Tags |