Coverage Report - org.apache.tapestry.contrib.link.ButtonLinkRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
ButtonLinkRenderer
0%
0/25
0%
0/8
2.667
 
 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  0
 public class ButtonLinkRenderer implements ILinkRenderer
 33  
 {
 34  0
     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  
     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
         if (!cycle.isRewinding()) {
 61  0
             String url = link.getURL(component.getAnchor(), true);
 62  0
             String target = component.getTarget();
 63  0
             String onclick = (target == null) ? getScript(url) : getScript(url, target);
 64  
 
 65  0
             writer.attribute("onclick", onclick);
 66  
         }
 67  
         
 68  0
         component.renderAdditionalAttributes(writer, cycle);
 69  
 
 70  0
         IMarkupWriter wrappedWriter = writer.getNestedWriter();
 71  
 
 72  0
         component.renderBody(wrappedWriter, cycle);
 73  
 
 74  0
         wrappedWriter.close();
 75  
 
 76  0
         writer.end();
 77  
 
 78  0
         cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
 79  0
     }
 80  
 
 81  
     /**
 82  
      * Generates the onclick event handler that opens the specified url in the current window.
 83  
      * @param url the url generated by this link
 84  
      * @return a JavaScript onclick event handler
 85  
      */
 86  
     protected String getScript(String url)
 87  
     {
 88  0
         return "window.location='" + url + "'";
 89  
     }
 90  
 
 91  
     /**
 92  
      * Generates the onclick event handler that opens the specified url in the specified window or frame.
 93  
      * @param url the url generated by this link
 94  
      * @param target the name of the target window or frame
 95  
      * @return a JavaScript onclick event handler
 96  
      */
 97  
     protected String getScript(String url, String target)
 98  
     {
 99  0
         return "window.open('" + url + "','" + target + "')";
 100  
     }
 101  
 }