Clover coverage report - Code Coverage for tapestry release 4.0-rc-2
Coverage timestamp: Sat Dec 17 2005 09:39:46 PST
file stats: LOC: 220   Methods: 9
NCLOC: 120   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractLinkComponent.java 6.2% 21.2% 66.7% 23.4%
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  0 public void addEventHandler(LinkEventType eventType, String functionName)
 55    {
 56  0 Object currentValue;
 57   
 58  0 if (_eventHandlers == null)
 59  0 _eventHandlers = new HashMap();
 60   
 61  0 currentValue = _eventHandlers.get(eventType);
 62   
 63    // The first value is added as a String
 64   
 65  0 if (currentValue == null)
 66    {
 67  0 _eventHandlers.put(eventType, functionName);
 68  0 return;
 69    }
 70   
 71    // When adding the second value, convert to a List
 72   
 73  0 if (currentValue instanceof String)
 74    {
 75  0 List list = new ArrayList();
 76  0 list.add(currentValue);
 77  0 list.add(functionName);
 78   
 79  0 _eventHandlers.put(eventType, list);
 80  0 return;
 81    }
 82   
 83    // For the third and up, add the new function to the List
 84   
 85  0 List list = (List) currentValue;
 86  0 list.add(functionName);
 87    }
 88   
 89    /**
 90    * Renders the link by delegating to an instance of {@link ILinkRenderer}.
 91    */
 92   
 93  70 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 94    {
 95  70 getRenderer().renderLink(writer, cycle, this);
 96    }
 97   
 98  70 protected void cleanupAfterRender(IRequestCycle cycle)
 99    {
 100  70 _eventHandlers = null;
 101   
 102  70 super.cleanupAfterRender(cycle);
 103    }
 104   
 105  70 protected void writeEventHandlers(IMarkupWriter writer, IRequestCycle cycle)
 106    {
 107  70 String name = null;
 108   
 109  70 if (_eventHandlers == null)
 110  70 return;
 111   
 112  0 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
 113   
 114  0 Iterator i = _eventHandlers.entrySet().iterator();
 115   
 116  0 while (i.hasNext())
 117    {
 118  0 Map.Entry entry = (Map.Entry) i.next();
 119  0 LinkEventType type = (LinkEventType) entry.getKey();
 120   
 121  0 name = writeEventHandler(
 122    writer,
 123    pageRenderSupport,
 124    name,
 125    type.getAttributeName(),
 126    entry.getValue());
 127    }
 128   
 129    }
 130   
 131  0 protected String writeEventHandler(IMarkupWriter writer, PageRenderSupport pageRenderSupport,
 132    String name, String attributeName, Object value)
 133    {
 134  0 String wrapperFunctionName;
 135   
 136  0 if (value instanceof String)
 137    {
 138  0 wrapperFunctionName = (String) value;
 139    }
 140    else
 141    {
 142  0 String finalName = name == null ? pageRenderSupport.getUniqueString("Link") : name;
 143   
 144  0 wrapperFunctionName = attributeName + "_" + finalName;
 145   
 146  0 StringBuffer buffer = new StringBuffer();
 147   
 148  0 buffer.append("function ");
 149  0 buffer.append(wrapperFunctionName);
 150  0 buffer.append(" ()\n{\n");
 151   
 152  0 Iterator i = ((List) value).iterator();
 153  0 while (i.hasNext())
 154    {
 155  0 String functionName = (String) i.next();
 156  0 buffer.append(" ");
 157  0 buffer.append(functionName);
 158  0 buffer.append("();\n");
 159    }
 160   
 161  0 buffer.append("}\n\n");
 162   
 163  0 pageRenderSupport.addBodyScript(buffer.toString());
 164    }
 165   
 166  0 writer.attribute(attributeName, "javascript:" + wrapperFunctionName + "();");
 167   
 168  0 return name;
 169    }
 170   
 171    /** @since 3.0 * */
 172   
 173    public abstract ILinkRenderer getRenderer();
 174   
 175    public abstract void setRenderer(ILinkRenderer renderer);
 176   
 177  70 public void renderAdditionalAttributes(IMarkupWriter writer, IRequestCycle cycle)
 178    {
 179  70 writeEventHandlers(writer, cycle);
 180   
 181    // Generate additional attributes from informal parameters.
 182   
 183  70 renderInformalParameters(writer, cycle);
 184    }
 185   
 186    /**
 187    * Utility method for subclasses; Gets the named service from the engine and invokes
 188    * {@link IEngineService#getLink(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  57 protected ILink getLink(IRequestCycle cycle, String serviceName, Object parameter)
 196    {
 197  57 IEngineService service = cycle.getEngine().getService(serviceName);
 198   
 199  57 return service.getLink(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  61 protected void finishLoad()
 216    {
 217  61 setRenderer(DefaultLinkRenderer.SHARED_INSTANCE);
 218    }
 219   
 220    }