1 15 package org.apache.tapestry.link; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.tapestry.IMarkupWriter; 19 import org.apache.tapestry.IRequestCycle; 20 import org.apache.tapestry.Tapestry; 21 import org.apache.tapestry.components.ILinkComponent; 22 import org.apache.tapestry.engine.ILink; 23 24 31 public class ButtonLinkRenderer implements ILinkRenderer 32 { 33 public static final ILinkRenderer SHARED_INSTANCE = new ButtonLinkRenderer(); 34 35 39 public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent component) 40 { 41 if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null) 42 { 43 String message = Tapestry.getMessage("AbstractLinkComponent.no-nesting"); 44 throw new ApplicationRuntimeException(message, component, null, null); 45 } 46 47 cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, component); 48 49 ILink link = component.getLink(cycle); 50 51 writer.begin("button"); 52 writer.attribute("type", "button"); 53 54 if (component.isDisabled()) 55 { 56 writer.attribute("disabled", "disabled"); 57 } 58 59 String url = link.getURL(component.getAnchor(), true); 60 String target = component.getTarget(); 61 String onclick = (target == null) ? getScript(url) : getScript(url, target); 62 63 writer.attribute("onclick", onclick); 64 65 component.renderAdditionalAttributes(writer, cycle); 66 67 IMarkupWriter wrappedWriter = writer.getNestedWriter(); 68 69 component.renderBody(wrappedWriter, cycle); 70 71 wrappedWriter.close(); 72 73 writer.end(); 74 75 cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME); 76 } 77 78 83 protected String getScript(String url) 84 { 85 return "window.location='" + url + "'"; 86 } 87 88 94 protected String getScript(String url, String target) 95 { 96 return "window.open('" + url + "','" + target + "')"; 97 } 98 } | Popular Tags |