Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 220   Methods: 9
NCLOC: 119   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
AbstractLinkComponent.java 88.9% 96.3% 88.9% 93.8%
coverage 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.link;
 16   
 
 17   
 import java.util.ArrayList;
 18   
 import java.util.HashMap;
 19   
 import java.util.Iterator;
 20   
 import java.util.List;
 21   
 import java.util.Map;
 22   
 
 23   
 import org.apache.hivemind.ApplicationRuntimeException;
 24   
 import org.apache.tapestry.AbstractComponent;
 25   
 import org.apache.tapestry.IMarkupWriter;
 26   
 import org.apache.tapestry.IRequestCycle;
 27   
 import org.apache.tapestry.Tapestry;
 28   
 import org.apache.tapestry.components.ILinkComponent;
 29   
 import org.apache.tapestry.components.LinkEventType;
 30   
 import org.apache.tapestry.engine.IEngineService;
 31   
 import org.apache.tapestry.engine.ILink;
 32   
 import org.apache.tapestry.html.Body;
 33   
 
 34   
 /**
 35   
  * Base class for implementations of {@link ILinkComponent}. Includes a disabled attribute (that
 36   
  * should be bound to a disabled parameter), an anchor attribute, and a renderer attribute (that
 37   
  * should be bound to a renderer parameter). A default, shared instance of
 38   
  * {@link org.apache.tapestry.link.DefaultLinkRenderer}is used when no specific renderer is
 39   
  * provided.
 40   
  * 
 41   
  * @author Howard Lewis Ship
 42   
  */
 43   
 
 44   
 public abstract class AbstractLinkComponent extends AbstractComponent implements ILinkComponent
 45   
 {
 46   
     private Map _eventHandlers;
 47   
 
 48   
     public abstract boolean isDisabled();
 49   
 
 50   
     /**
 51   
      * Adds an event handler (typically, from a wrapped component such as a
 52   
      * {@link org.apache.tapestry.html.Rollover}).
 53   
      */
 54   
 
 55  6
     public void addEventHandler(LinkEventType eventType, String functionName)
 56   
     {
 57  6
         Object currentValue;
 58   
 
 59  6
         if (_eventHandlers == null)
 60  2
             _eventHandlers = new HashMap();
 61   
 
 62  6
         currentValue = _eventHandlers.get(eventType);
 63   
 
 64   
         // The first value is added as a String
 65   
 
 66  6
         if (currentValue == null)
 67   
         {
 68  2
             _eventHandlers.put(eventType, functionName);
 69  2
             return;
 70   
         }
 71   
 
 72   
         // When adding the second value, convert to a List
 73   
 
 74  4
         if (currentValue instanceof String)
 75   
         {
 76  2
             List list = new ArrayList();
 77  2
             list.add(currentValue);
 78  2
             list.add(functionName);
 79   
 
 80  2
             _eventHandlers.put(eventType, list);
 81  2
             return;
 82   
         }
 83   
 
 84   
         // For the third and up, add the new function to the List
 85   
 
 86  2
         List list = (List) currentValue;
 87  2
         list.add(functionName);
 88   
     }
 89   
 
 90   
     /**
 91   
      * Renders the link by delegating to an instance of {@link ILinkRenderer}.
 92   
      */
 93   
 
 94  137
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 95   
     {
 96  137
         getRenderer().renderLink(writer, cycle, this);
 97   
     }
 98   
 
 99  137
     protected void cleanupAfterRender(IRequestCycle cycle)
 100   
     {
 101  137
         _eventHandlers = null;
 102   
 
 103  137
         super.cleanupAfterRender(cycle);
 104   
     }
 105   
 
 106  134
     protected void writeEventHandlers(IMarkupWriter writer, IRequestCycle cycle)
 107   
     {
 108  134
         String name = null;
 109   
 
 110  134
         if (_eventHandlers == null)
 111  132
             return;
 112   
 
 113  2
         Body body = Body.get(cycle);
 114   
 
 115  2
         if (body == null)
 116  1
             throw new ApplicationRuntimeException(Tapestry
 117   
                     .getMessage("AbstractLinkComponent.events-need-body"), this, null, null);
 118   
 
 119  1
         Iterator i = _eventHandlers.entrySet().iterator();
 120   
 
 121  1
         while (i.hasNext())
 122   
         {
 123  1
             Map.Entry entry = (Map.Entry) i.next();
 124  1
             LinkEventType type = (LinkEventType) entry.getKey();
 125   
 
 126  1
             name = writeEventHandler(writer, body, name, type.getAttributeName(), entry.getValue());
 127   
         }
 128   
 
 129   
     }
 130   
 
 131  1
     protected String writeEventHandler(IMarkupWriter writer, Body body, String name,
 132   
             String attributeName, Object value)
 133   
     {
 134  1
         String wrapperFunctionName;
 135   
 
 136  1
         if (value instanceof String)
 137   
         {
 138  0
             wrapperFunctionName = (String) value;
 139   
         }
 140   
         else
 141   
         {
 142  1
             String finalName = name == null ? body.getUniqueString("Link") : name;
 143   
 
 144  1
             wrapperFunctionName = attributeName + "_" + finalName;
 145   
 
 146  1
             StringBuffer buffer = new StringBuffer();
 147   
 
 148  1
             buffer.append("function ");
 149  1
             buffer.append(wrapperFunctionName);
 150  1
             buffer.append(" ()\n{\n");
 151   
 
 152  1
             Iterator i = ((List) value).iterator();
 153  1
             while (i.hasNext())
 154   
             {
 155  3
                 String functionName = (String) i.next();
 156  3
                 buffer.append("  ");
 157  3
                 buffer.append(functionName);
 158  3
                 buffer.append("();\n");
 159   
             }
 160   
 
 161  1
             buffer.append("}\n\n");
 162   
 
 163  1
             body.addBodyScript(buffer.toString());
 164   
         }
 165   
 
 166  1
         writer.attribute(attributeName, "javascript:" + wrapperFunctionName + "();");
 167   
 
 168  1
         return name;
 169   
     }
 170   
 
 171   
     /** @since 3.0 * */
 172   
 
 173   
     public abstract ILinkRenderer getRenderer();
 174   
 
 175   
     public abstract void setRenderer(ILinkRenderer renderer);
 176   
 
 177  134
     public void renderAdditionalAttributes(IMarkupWriter writer, IRequestCycle cycle)
 178   
     {
 179  134
         writeEventHandlers(writer, cycle);
 180   
 
 181   
         // Generate additional attributes from informal parameters.
 182   
 
 183  133
         renderInformalParameters(writer, cycle);
 184   
     }
 185   
 
 186   
     /**
 187   
      * Utility method for subclasses; Gets the named service from the engine and invokes
 188   
      * {@link IEngineService#getLink(IRequestCycle, org.apache.tapestry.IComponent, Object[])}on
 189   
      * it.
 190   
      * 
 191   
      * @since 3.0
 192   
      * @deprecated To be removed in 3.2; links may now have the necessary engine service injected.
 193   
      */
 194   
 
 195  121
     protected ILink getLink(IRequestCycle cycle, String serviceName, Object parameter)
 196   
     {
 197  121
         IEngineService service = cycle.getEngine().getService(serviceName);
 198   
 
 199  121
         return service.getLink(cycle, parameter);
 200   
     }
 201   
 
 202   
     public abstract String getAnchor();
 203   
 
 204  0
     public ILink getLink(IRequestCycle cycle)
 205   
     {
 206  0
         return null;
 207   
     }
 208   
 
 209   
     /**
 210   
      * Sets the renderer parameter property to its default value
 211   
      * {@link DefaultLinkRenderer#SHARED_INSTANCE}.
 212   
      * 
 213   
      * @since 3.0
 214   
      */
 215  113
     protected void finishLoad()
 216   
     {
 217  113
         setRenderer(DefaultLinkRenderer.SHARED_INSTANCE);
 218   
     }
 219   
 
 220   
 }