Clover coverage report - Code Coverage for tapestry release 4.0-beta-10
Coverage timestamp: Sat Oct 8 2005 19:08:05 EDT
file stats: LOC: 220   Methods: 9
NCLOC: 120   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractLinkComponent.java 87.5% 96.2% 88.9% 93.5%
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.tapestry.AbstractComponent;
 24    import org.apache.tapestry.IMarkupWriter;
 25    import org.apache.tapestry.IRequestCycle;
 26    import org.apache.tapestry.PageRenderSupport;
 27    import org.apache.tapestry.TapestryUtils;
 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   
 33    /**
 34    * Base class for implementations of {@link ILinkComponent}. Includes a disabled attribute (that
 35    * should be bound to a disabled parameter), an anchor attribute, and a renderer attribute (that
 36    * should be bound to a renderer parameter). A default, shared instance of
 37    * {@link org.apache.tapestry.link.DefaultLinkRenderer}is used when no specific renderer is
 38    * provided.
 39    *
 40    * @author Howard Lewis Ship
 41    */
 42   
 43    public abstract class AbstractLinkComponent extends AbstractComponent implements ILinkComponent
 44    {
 45    private Map _eventHandlers;
 46   
 47    public abstract boolean isDisabled();
 48   
 49    /**
 50    * Adds an event handler (typically, from a wrapped component such as a
 51    * {@link org.apache.tapestry.html.Rollover}).
 52    */
 53   
 54  6 public void addEventHandler(LinkEventType eventType, String functionName)
 55    {
 56  6 Object currentValue;
 57   
 58  6 if (_eventHandlers == null)
 59  2 _eventHandlers = new HashMap();
 60   
 61  6 currentValue = _eventHandlers.get(eventType);
 62   
 63    // The first value is added as a String
 64   
 65  6 if (currentValue == null)
 66    {
 67  2 _eventHandlers.put(eventType, functionName);
 68  2 return;
 69    }
 70   
 71    // When adding the second value, convert to a List
 72   
 73  4 if (currentValue instanceof String)
 74    {
 75  2 List list = new ArrayList();
 76  2 list.add(currentValue);
 77  2 list.add(functionName);
 78   
 79  2 _eventHandlers.put(eventType, list);
 80  2 return;
 81    }
 82   
 83    // For the third and up, add the new function to the List
 84   
 85  2 List list = (List) currentValue;
 86  2 list.add(functionName);
 87    }
 88   
 89    /**
 90    * Renders the link by delegating to an instance of {@link ILinkRenderer}.
 91    */
 92   
 93  83 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 94    {
 95  83 getRenderer().renderLink(writer, cycle, this);
 96    }
 97   
 98  83 protected void cleanupAfterRender(IRequestCycle cycle)
 99    {
 100  83 _eventHandlers = null;
 101   
 102  83 super.cleanupAfterRender(cycle);
 103    }
 104   
 105  80 protected void writeEventHandlers(IMarkupWriter writer, IRequestCycle cycle)
 106    {
 107  80 String name = null;
 108   
 109  80 if (_eventHandlers == null)
 110  78 return;
 111   
 112  2 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
 113   
 114  1 Iterator i = _eventHandlers.entrySet().iterator();
 115   
 116  1 while (i.hasNext())
 117    {
 118  1 Map.Entry entry = (Map.Entry) i.next();
 119  1 LinkEventType type = (LinkEventType) entry.getKey();
 120   
 121  1 name = writeEventHandler(
 122    writer,
 123    pageRenderSupport,
 124    name,
 125    type.getAttributeName(),
 126    entry.getValue());
 127    }
 128   
 129    }
 130   
 131  1 protected String writeEventHandler(IMarkupWriter writer, PageRenderSupport pageRenderSupport,
 132    String name, 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 ? pageRenderSupport.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 pageRenderSupport.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  80 public void renderAdditionalAttributes(IMarkupWriter writer, IRequestCycle cycle)
 178    {
 179  80 writeEventHandlers(writer, cycle);
 180   
 181    // Generate additional attributes from informal parameters.
 182   
 183  79 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 4.1; links may now have the necessary engine service injected.
 193    */
 194   
 195  68 protected ILink getLink(IRequestCycle cycle, String serviceName, Object parameter)
 196    {
 197  68 IEngineService service = cycle.getEngine().getService(serviceName);
 198   
 199  68 return service.getLink(cycle, false, 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  74 protected void finishLoad()
 216    {
 217  74 setRenderer(DefaultLinkRenderer.SHARED_INSTANCE);
 218    }
 219   
 220    }