Coverage Report - org.apache.tapestry.html.Rollover
 
Classes in this File Line Coverage Branch Coverage Complexity
Rollover
0%
0/52
0%
0/26
3
 
 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.html;
 16  
 
 17  
 import org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.tapestry.*;
 19  
 import org.apache.tapestry.components.ILinkComponent;
 20  
 import org.apache.tapestry.form.IFormComponent;
 21  
 import org.apache.tapestry.form.LinkSubmit;
 22  
 
 23  
 import java.util.HashMap;
 24  
 import java.util.Map;
 25  
 
 26  
 /**
 27  
  * Combines a link component (such as
 28  
  * {@link org.apache.tapestry.link.DirectLink}) with an <img> and
 29  
  * JavaScript code to create a rollover effect that works with both Netscape
 30  
  * Navigator and Internet Explorer. [ <a
 31  
  * href="../../../../../components/link/rollover.html">Component Reference
 32  
  * </a>]
 33  
  *
 34  
  * @author Howard Lewis Ship
 35  
  */
 36  
 
 37  0
 public abstract class Rollover extends AbstractComponent
 38  
 {
 39  
 
 40  
     /**
 41  
      * Converts an {@link IAsset}binding into a usable URL. Returns null if the
 42  
      * binding does not exist or the binding's value is null.
 43  
      *
 44  
      * @param asset
 45  
      *          The asset to generate a url for.
 46  
      * @return The url to the asset resource, or null if it couldn't be generated.
 47  
      */
 48  
 
 49  
     protected String getAssetURL(IAsset asset)
 50  
     {
 51  0
         if (asset == null)
 52  0
             return null;
 53  
 
 54  0
         return asset.buildURL();
 55  
     }
 56  
 
 57  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 58  
     {
 59  
         // No body, so we skip it all if not rewinding
 60  
         // (assumes no side effects on accessors).
 61  
 
 62  0
         if (cycle.isRewinding())
 63  0
             return;
 64  
 
 65  0
         String imageURL = null;
 66  0
         String mouseOverURL = null;
 67  0
         String mouseOutURL = null;
 68  0
         boolean dynamic = false;
 69  
         String imageId;
 70  0
         boolean linkDisabled = false;
 71  
 
 72  0
         PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
 73  
 
 74  0
         Object serviceLink = cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
 75  0
         if (serviceLink == null)
 76  
         {
 77  0
             serviceLink = cycle.getAttribute(LinkSubmit.ATTRIBUTE_NAME);
 78  
 
 79  0
             if (serviceLink != null)
 80  0
                 linkDisabled = ((IFormComponent) serviceLink).isDisabled();
 81  
         } else
 82  
         {
 83  0
             linkDisabled = ((ILinkComponent) serviceLink).isDisabled();
 84  
         }
 85  
 
 86  0
         if (serviceLink == null)
 87  0
             throw new ApplicationRuntimeException(Tapestry.getMessage("Rollover.must-be-contained-by-link"), this, null, null);
 88  
 
 89  0
         if (linkDisabled)
 90  
         {
 91  0
             imageURL = getAssetURL(getDisabled());
 92  
 
 93  0
             if (imageURL == null)
 94  0
                 imageURL = getAssetURL(getImage());
 95  
         }
 96  
         else
 97  
         {
 98  0
             imageURL = getAssetURL(getImage());
 99  0
             mouseOverURL = getAssetURL(getMouseOver());
 100  0
             mouseOutURL = getAssetURL(getMouseOut());
 101  
 
 102  0
             dynamic = (mouseOverURL != null) || (mouseOutURL != null);
 103  
         }
 104  
 
 105  0
         if (imageURL == null)
 106  0
             throw Tapestry.createRequiredParameterException(this, "image");
 107  
 
 108  0
         writer.beginEmpty("img");
 109  
 
 110  0
         writer.attribute("src", imageURL);
 111  
 
 112  0
         if (dynamic)
 113  
         {
 114  0
             if (mouseOverURL == null)
 115  0
                 mouseOverURL = imageURL;
 116  
 
 117  0
             if (mouseOutURL == null)
 118  0
                 mouseOutURL = imageURL;
 119  
 
 120  0
             imageId = writeScript(cycle, pageRenderSupport, serviceLink, mouseOverURL, mouseOutURL);
 121  
 
 122  0
             writer.attribute("id", imageId);
 123  
         }
 124  
 
 125  0
         renderInformalParameters(writer, cycle);
 126  
 
 127  0
         writer.closeTag();
 128  
 
 129  0
     }
 130  
 
 131  
     // Injected
 132  
 
 133  
     public abstract IScript getScript();
 134  
 
 135  
     private String writeScript(IRequestCycle cycle, PageRenderSupport pageRenderSupport,
 136  
                                Object link, String mouseOverImageURL, String mouseOutImageURL)
 137  
     {
 138  0
         String imageId = pageRenderSupport.getUniqueString(getId());
 139  
 
 140  0
         String preloadedMouseOverImageURL = pageRenderSupport.getPreloadedImageReference(this, mouseOverImageURL);
 141  0
         String preloadedMouseOutImageURL = pageRenderSupport.getPreloadedImageReference(this, mouseOutImageURL);
 142  
 
 143  0
         Map symbols = new HashMap();
 144  
 
 145  0
         symbols.put("link", link);
 146  0
         symbols.put("imageId", imageId);
 147  0
         symbols.put("mouseOverImageURL", preloadedMouseOverImageURL);
 148  0
         symbols.put("mouseOutImageURL", preloadedMouseOutImageURL);
 149  
 
 150  0
         getScript().execute(this, cycle, pageRenderSupport, symbols);
 151  
 
 152  0
         return imageId;
 153  
     }
 154  
 
 155  
     public abstract IAsset getMouseOut();
 156  
 
 157  
     public abstract IAsset getDisabled();
 158  
 
 159  
     public abstract IAsset getMouseOver();
 160  
 
 161  
     public abstract IAsset getImage();
 162  
 }