1 15 package org.apache.tapestry.form; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.tapestry.IForm; 19 import org.apache.tapestry.IMarkupWriter; 20 import org.apache.tapestry.IRequestCycle; 21 import org.apache.tapestry.PageRenderSupport; 22 import org.apache.tapestry.Tapestry; 23 import org.apache.tapestry.TapestryUtils; 24 25 31 32 public abstract class LinkSubmit extends AbstractSubmit 33 { 34 38 39 public static final String ATTRIBUTE_NAME = "org.apache.tapestry.form.LinkSubmit"; 40 41 46 public static final String ATTRIBUTE_FUNCTION_NAME = "org.apache.tapestry.form.LinkSubmit_function"; 47 48 protected boolean isClicked(IRequestCycle cycle, String name) 49 { 50 String value = cycle.getParameter("_linkSubmit"); 53 54 return value != null && value.equals(name); 57 } 58 59 62 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) 63 { 64 boolean disabled = isDisabled(); 65 66 IMarkupWriter wrappedWriter; 67 68 if (!disabled) 69 { 70 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport( 71 cycle, 72 this); 73 74 if (cycle.getAttribute(ATTRIBUTE_FUNCTION_NAME) == null) 76 { 77 pageRenderSupport 78 .addBodyScript("function submitLink(form, elementId) { form._linkSubmit.value = elementId; if (form.onsubmit == null || form.onsubmit()) form.submit(); }"); 79 cycle.setAttribute(ATTRIBUTE_FUNCTION_NAME, this); 80 } 81 82 IForm form = getForm(); 83 String formName = form.getName(); 84 85 String formHiddenFieldAttributeName = ATTRIBUTE_FUNCTION_NAME + formName; 87 if (cycle.getAttribute(formHiddenFieldAttributeName) == null) 88 { 89 writer.beginEmpty("input"); 90 writer.attribute("type", "hidden"); 91 writer.attribute("name", "_linkSubmit"); 92 cycle.setAttribute(formHiddenFieldAttributeName, this); 93 } 94 95 writer.begin("a"); 96 writer.attribute("href", "javascript:submitLink(document." + formName + ",\"" + getName() 97 + "\");"); 98 99 wrappedWriter = writer.getNestedWriter(); 103 } 104 else 105 wrappedWriter = writer; 106 107 renderBody(wrappedWriter, cycle); 108 109 if (!disabled) 110 { 111 renderInformalParameters(writer, cycle); 113 114 wrappedWriter.close(); 116 117 writer.end(); 119 } 120 } 121 122 125 protected void prepareForRender(IRequestCycle cycle) 126 { 127 if (cycle.getAttribute(ATTRIBUTE_NAME) != null) 128 throw new ApplicationRuntimeException(Tapestry.getMessage("LinkSubmit.may-not-nest"), 129 this, null, null); 130 131 cycle.setAttribute(ATTRIBUTE_NAME, this); 132 } 133 134 137 protected void cleanupAfterRender(IRequestCycle cycle) 138 { 139 cycle.removeAttribute(ATTRIBUTE_NAME); 140 } 141 } | Popular Tags |