Clover coverage report - Code Coverage for tapestry-contrib release 4.0-beta-10
Coverage timestamp: Sat Oct 8 2005 19:13:41 EDT
file stats: LOC: 99   Methods: 3
NCLOC: 45   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ButtonLinkRenderer.java 0% 0% 0% 0%
coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.contrib.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    import org.apache.tapestry.link.ILinkRenderer;
 24   
 25    /**
 26    * An {@link ILinkRenderer} implementation that generates an HTML button.
 27    * This is particularly useful for implementing cancel buttons.
 28    *
 29    * @author Paul Ferraro
 30    * @since 4.0
 31    */
 32    public class ButtonLinkRenderer implements ILinkRenderer
 33    {
 34    public static final ILinkRenderer SHARED_INSTANCE = new ButtonLinkRenderer();
 35   
 36    /**
 37    * @see org.apache.tapestry.link.ILinkRenderer#renderLink(org.apache.tapestry.IMarkupWriter,
 38    * org.apache.tapestry.IRequestCycle, org.apache.tapestry.components.ILinkComponent)
 39    */
 40  0 public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent component)
 41    {
 42  0 if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null)
 43    {
 44  0 String message = Tapestry.getMessage("AbstractLinkComponent.no-nesting");
 45  0 throw new ApplicationRuntimeException(message, component, null, null);
 46    }
 47   
 48  0 cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, component);
 49   
 50  0 ILink link = component.getLink(cycle);
 51   
 52  0 writer.begin("button");
 53  0 writer.attribute("type", "button");
 54   
 55  0 if (component.isDisabled())
 56    {
 57  0 writer.attribute("disabled", "disabled");
 58    }
 59   
 60  0 String url = link.getURL(component.getAnchor(), true);
 61  0 String target = component.getTarget();
 62  0 String onclick = (target == null) ? getScript(url) : getScript(url, target);
 63   
 64  0 writer.attribute("onclick", onclick);
 65   
 66  0 component.renderAdditionalAttributes(writer, cycle);
 67   
 68  0 IMarkupWriter wrappedWriter = writer.getNestedWriter();
 69   
 70  0 component.renderBody(wrappedWriter, cycle);
 71   
 72  0 wrappedWriter.close();
 73   
 74  0 writer.end();
 75   
 76  0 cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
 77    }
 78   
 79    /**
 80    * Generates the onclick event handler that opens the specified url in the current window.
 81    * @param url the url generated by this link
 82    * @return a JavaScript onclick event handler
 83    */
 84  0 protected String getScript(String url)
 85    {
 86  0 return "window.location='" + url + "'";
 87    }
 88   
 89    /**
 90    * Generates the onclick event handler that opens the specified url in the specified window or frame.
 91    * @param url the url generated by this link
 92    * @param target the name of the target window or frame
 93    * @return a JavaScript onclick event handler
 94    */
 95  0 protected String getScript(String url, String target)
 96    {
 97  0 return "window.open('" + url + "','" + target + "')";
 98    }
 99    }