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: 196   Methods: 4
NCLOC: 115   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
Rollover.java 0% 0% 0% 0%
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.html;
 16   
 
 17   
 import java.util.HashMap;
 18   
 import java.util.Map;
 19   
 
 20   
 import org.apache.hivemind.ApplicationRuntimeException;
 21   
 import org.apache.hivemind.Resource;
 22   
 import org.apache.tapestry.AbstractComponent;
 23   
 import org.apache.tapestry.IAsset;
 24   
 import org.apache.tapestry.IEngine;
 25   
 import org.apache.tapestry.IMarkupWriter;
 26   
 import org.apache.tapestry.IRequestCycle;
 27   
 import org.apache.tapestry.IScript;
 28   
 import org.apache.tapestry.Tapestry;
 29   
 import org.apache.tapestry.components.ILinkComponent;
 30   
 import org.apache.tapestry.components.LinkEventType;
 31   
 import org.apache.tapestry.engine.IScriptSource;
 32   
 
 33   
 /**
 34   
  *  Combines a link component (such as {@link org.apache.tapestry.link.DirectLink}) 
 35   
  *  with an <img> and JavaScript code
 36   
  *  to create a rollover effect that works with both Netscape Navigator and 
 37   
  *  Internet Explorer.
 38   
  *
 39   
  *  [<a href="../../../../../ComponentReference/Rollover.html">Component Reference</a>]
 40   
  *
 41   
  *
 42   
  *  @author Howard Lewis Ship
 43   
  * 
 44   
  **/
 45   
 
 46   
 public abstract class Rollover extends AbstractComponent
 47   
 {
 48   
     private IScript _parsedScript;
 49   
 
 50   
     /**
 51   
      *  Converts an {@link IAsset} binding into a usable URL.  Returns null
 52   
      *  if the binding does not exist or the binding's value is null.
 53   
      *
 54   
      **/
 55   
 
 56  0
     protected String getAssetURL(IAsset asset, IRequestCycle cycle)
 57   
     {
 58  0
         if (asset == null)
 59  0
             return null;
 60   
 
 61  0
         return asset.buildURL(cycle);
 62   
     }
 63   
 
 64  0
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 65   
     {
 66   
         // No body, so we skip it all if not rewinding (assumes no side effects on
 67   
         // accessors).
 68   
 
 69  0
         if (cycle.isRewinding())
 70  0
             return;
 71   
 
 72  0
         String imageURL = null;
 73  0
         String focusURL = null;
 74  0
         String blurURL = null;
 75  0
         boolean dynamic = false;
 76  0
         String imageName = null;
 77   
 
 78  0
         Body body = Body.get(cycle);
 79  0
         if (body == null)
 80  0
             throw new ApplicationRuntimeException(
 81   
                 Tapestry.getMessage("Rollover.must-be-contained-by-body"),
 82   
                 this,
 83   
                 null,
 84   
                 null);
 85   
 
 86  0
         ILinkComponent serviceLink =
 87   
             (ILinkComponent) cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
 88   
 
 89  0
         if (serviceLink == null)
 90  0
             throw new ApplicationRuntimeException(
 91   
                 Tapestry.getMessage("Rollover.must-be-contained-by-link"),
 92   
                 this,
 93   
                 null,
 94   
                 null);
 95   
 
 96  0
         boolean linkDisabled = serviceLink.isDisabled();
 97   
 
 98  0
         if (linkDisabled)
 99   
         {
 100  0
             imageURL = getAssetURL(getDisabled(), cycle);
 101   
 
 102  0
             if (imageURL == null)
 103  0
                 imageURL = getAssetURL(getImage(), cycle);
 104   
         }
 105   
         else
 106   
         {
 107  0
             imageURL = getAssetURL(getImage(), cycle);
 108  0
             focusURL = getAssetURL(getFocus(), cycle);
 109  0
             blurURL = getAssetURL(getBlur(), cycle);
 110   
 
 111  0
             dynamic = (focusURL != null) || (blurURL != null);
 112   
         }
 113   
 
 114  0
         if (imageURL == null)
 115  0
             throw Tapestry.createRequiredParameterException(this, "image");
 116   
 
 117  0
         writer.beginEmpty("img");
 118   
 
 119  0
         writer.attribute("src", imageURL);
 120   
 
 121  0
         writer.attribute("border", 0);
 122   
 
 123  0
         if (dynamic)
 124   
         {
 125  0
             if (focusURL == null)
 126  0
                 focusURL = imageURL;
 127   
 
 128  0
             if (blurURL == null)
 129  0
                 blurURL = imageURL;
 130   
 
 131  0
             imageName = writeScript(cycle, body, serviceLink, focusURL, blurURL);
 132   
 
 133  0
             writer.attribute("name", imageName);
 134   
         }
 135   
 
 136  0
         renderInformalParameters(writer, cycle);
 137   
 
 138  0
         writer.closeTag();
 139   
 
 140   
     }
 141   
 
 142  0
     private IScript getParsedScript()
 143   
     {
 144  0
         if (_parsedScript == null)
 145   
         {
 146  0
             IEngine engine = getPage().getEngine();
 147  0
             IScriptSource source = engine.getScriptSource();
 148   
 
 149  0
             Resource scriptLocation =
 150   
                 getSpecification().getSpecificationLocation().getRelativeResource(
 151   
                     "Rollover.script");
 152   
 
 153  0
             _parsedScript = source.getScript(scriptLocation);
 154   
         }
 155   
 
 156  0
         return _parsedScript;
 157   
     }
 158   
 
 159  0
     private String writeScript(
 160   
         IRequestCycle cycle,
 161   
         Body body,
 162   
         ILinkComponent link,
 163   
         String focusURL,
 164   
         String blurURL)
 165   
     {
 166  0
         String imageName = body.getUniqueString(getId());
 167  0
         String focusImageURL = body.getPreloadedImageReference(focusURL);
 168  0
         String blurImageURL = body.getPreloadedImageReference(blurURL);
 169   
 
 170  0
         Map symbols = new HashMap();
 171   
 
 172  0
         symbols.put("imageName", imageName);
 173  0
         symbols.put("focusImageURL", focusImageURL);
 174  0
         symbols.put("blurImageURL", blurImageURL);
 175   
 
 176  0
         getParsedScript().execute(cycle, body, symbols);
 177   
 
 178   
         // Add attributes to the link to control mouse over/out.
 179   
         // Because the script is written before the <body> tag,
 180   
         // there won't be any timing issues (such as cause
 181   
         // bug #113893).
 182   
 
 183  0
         link.addEventHandler(LinkEventType.MOUSE_OVER, (String) symbols.get("onMouseOverName"));
 184  0
         link.addEventHandler(LinkEventType.MOUSE_OUT, (String) symbols.get("onMouseOutName"));
 185   
 
 186  0
         return imageName;
 187   
     }
 188   
 
 189   
     public abstract IAsset getBlur();
 190   
 
 191   
     public abstract IAsset getDisabled();
 192   
 
 193   
     public abstract IAsset getFocus();
 194   
 
 195   
     public abstract IAsset getImage();
 196   
 }