Coverage Report - org.apache.tapestry.link.AbstractLinkComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractLinkComponent
0%
0/58
0%
0/16
1.917
 
 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.ILink;
 31  
 
 32  
 /**
 33  
  * Base class for implementations of {@link ILinkComponent}. Includes a disabled attribute (that
 34  
  * should be bound to a disabled parameter), an anchor attribute, and a renderer attribute (that
 35  
  * should be bound to a renderer parameter). A default, shared instance of
 36  
  * {@link org.apache.tapestry.link.DefaultLinkRenderer} is used when no specific renderer is
 37  
  * provided.
 38  
  * 
 39  
  * @author Howard Lewis Ship
 40  
  */
 41  
 
 42  0
 public abstract class AbstractLinkComponent extends AbstractComponent implements ILinkComponent
 43  
 {
 44  
     private Map _eventHandlers;
 45  
     
 46  
     public abstract boolean isDisabled();
 47  
     
 48  
     /**
 49  
      * Adds an event handler (typically, from a wrapped component such as a
 50  
      * {@link org.apache.tapestry.html.Rollover}).
 51  
      */
 52  
 
 53  
     public void addEventHandler(LinkEventType eventType, String functionName)
 54  
     {
 55  
         Object currentValue;
 56  
 
 57  0
         if (_eventHandlers == null)
 58  0
             _eventHandlers = new HashMap();
 59  
 
 60  0
         currentValue = _eventHandlers.get(eventType);
 61  
 
 62  
         // The first value is added as a String
 63  
 
 64  0
         if (currentValue == null)
 65  
         {
 66  0
             _eventHandlers.put(eventType, functionName);
 67  0
             return;
 68  
         }
 69  
 
 70  
         // When adding the second value, convert to a List
 71  
 
 72  0
         if (currentValue instanceof String)
 73  
         {
 74  0
             List list = new ArrayList();
 75  0
             list.add(currentValue);
 76  0
             list.add(functionName);
 77  
 
 78  0
             _eventHandlers.put(eventType, list);
 79  0
             return;
 80  
         }
 81  
 
 82  
         // For the third and up, add the new function to the List
 83  
 
 84  0
         List list = (List) currentValue;
 85  0
         list.add(functionName);
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Renders the link by delegating to an instance of {@link ILinkRenderer}.
 90  
      */
 91  
 
 92  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 93  
     {
 94  0
         getRenderer().renderLink(writer, cycle, this);
 95  0
     }
 96  
 
 97  
     protected void cleanupAfterRender(IRequestCycle cycle)
 98  
     {
 99  0
         super.cleanupAfterRender(cycle);
 100  
         
 101  0
         _eventHandlers = null;
 102  0
     }
 103  
 
 104  
     protected void writeEventHandlers(IMarkupWriter writer, IRequestCycle cycle)
 105  
     {
 106  0
         String name = null;
 107  
 
 108  0
         if (_eventHandlers == null)
 109  0
             return;
 110  
         
 111  0
         PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
 112  
         
 113  0
         Iterator i = _eventHandlers.entrySet().iterator();
 114  
         
 115  0
         while (i.hasNext())
 116  
         {
 117  0
             Map.Entry entry = (Map.Entry) i.next();
 118  0
             LinkEventType type = (LinkEventType) entry.getKey();
 119  
 
 120  0
             name = writeEventHandler(
 121  
                     writer,
 122  
                     pageRenderSupport,
 123  
                     name,
 124  
                     type.getAttributeName(),
 125  
                     entry.getValue());
 126  0
         }
 127  
 
 128  0
     }
 129  
 
 130  
     protected String writeEventHandler(IMarkupWriter writer, PageRenderSupport pageRenderSupport,
 131  
             String name, String attributeName, Object value)
 132  
     {
 133  
         String wrapperFunctionName;
 134  
 
 135  0
         if (value instanceof String)
 136  
         {
 137  0
             wrapperFunctionName = (String) value;
 138  
         }
 139  
         else
 140  
         {
 141  0
             String finalName = name == null ? pageRenderSupport.getUniqueString("Link") : name;
 142  
 
 143  0
             wrapperFunctionName = attributeName + "_" + finalName;
 144  
 
 145  0
             StringBuffer buffer = new StringBuffer();
 146  
             
 147  0
             buffer.append("function ");
 148  0
             buffer.append(wrapperFunctionName);
 149  0
             buffer.append(" ()\n{\n");
 150  
 
 151  0
             Iterator i = ((List) value).iterator();
 152  0
             while (i.hasNext())
 153  
             {
 154  0
                 String functionName = (String) i.next();
 155  0
                 buffer.append("  ");
 156  0
                 buffer.append(functionName);
 157  0
                 buffer.append("();\n");
 158  0
             }
 159  
 
 160  0
             buffer.append("}\n\n");
 161  
 
 162  0
             pageRenderSupport.addBodyScript(this, buffer.toString());
 163  
         }
 164  
 
 165  0
         writer.attribute(attributeName, "javascript:" + wrapperFunctionName + "();");
 166  
 
 167  0
         return name;
 168  
     }
 169  
 
 170  
     /** @since 3.0 * */
 171  
 
 172  
     public abstract ILinkRenderer getRenderer();
 173  
 
 174  
     public abstract void setRenderer(ILinkRenderer renderer);
 175  
 
 176  
     public void renderAdditionalAttributes(IMarkupWriter writer, IRequestCycle cycle)
 177  
     {
 178  0
         renderIdAttribute(writer, cycle);
 179  
         
 180  0
         writeEventHandlers(writer, cycle);
 181  
         
 182  
         // Generate additional attributes from informal parameters.
 183  
         
 184  0
         renderInformalParameters(writer, cycle);
 185  0
     }
 186  
     
 187  
     public abstract String getAnchor();
 188  
 
 189  
     public ILink getLink(IRequestCycle cycle)
 190  
     {
 191  0
         return null;
 192  
     }
 193  
 
 194  
     /**
 195  
      * Sets the renderer parameter property to its default value
 196  
      * {@link DefaultLinkRenderer#SHARED_INSTANCE}.
 197  
      * 
 198  
      * @since 3.0
 199  
      */
 200  
     protected void finishLoad()
 201  
     {
 202  0
         setRenderer(DefaultLinkRenderer.SHARED_INSTANCE);
 203  0
     }
 204  
 
 205  
 }