Coverage Report - org.apache.tapestry.form.ImageSubmit
 
Classes in this File Line Coverage Branch Coverage Complexity
ImageSubmit
87% 
83% 
1.444
 
 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.form;
 16  
 
 17  
 import java.awt.Point;
 18  
 
 19  
 import org.apache.tapestry.IAsset;
 20  
 import org.apache.tapestry.IForm;
 21  
 import org.apache.tapestry.IMarkupWriter;
 22  
 import org.apache.tapestry.IRequestCycle;
 23  
 import org.apache.tapestry.Tapestry;
 24  
 
 25  
 /**
 26  
  * Used to create an image button inside a {@link Form}. Although it is occasionally useful to know
 27  
  * the {@link Point}on the image that was clicked (i.e., use the image as a kind of image map,
 28  
  * which was the original intent of the HTML element), it is more commonly used to provide a graphic
 29  
  * image for the user to click, rather than the rather plain &lt;input type=submit&gt;. [ <a
 30  
  * href="../../../../../ComponentReference/ImageSubmit.html">Component Reference </a>]
 31  
  * 
 32  
  * @author Howard Lewis Ship
 33  
  */
 34  
 
 35  9
 public abstract class ImageSubmit extends Submit
 36  
 {
 37  
     /**
 38  
      * @see org.apache.tapestry.form.AbstractFormComponent#setName(org.apache.tapestry.IForm)
 39  
      */
 40  
     protected void setName(IForm form)
 41  
     {
 42  8
         String nameOverride = getNameOverride();
 43  
 
 44  8
         setName((nameOverride == null) ? form.getElementId(this) : form.getElementId(this, nameOverride));
 45  8
     }
 46  
 
 47  
     protected boolean isClicked(IRequestCycle cycle, String name)
 48  
     {
 49  3
         String parameterName = name + ".x";
 50  
 
 51  3
         return (cycle.getParameter(parameterName) != null);
 52  
     }
 53  
     
 54  
     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 55  
     {
 56  4
         boolean disabled = isDisabled();
 57  4
         IAsset disabledImage = getDisabledImage();
 58  
 
 59  4
         IAsset finalImage = (disabled && disabledImage != null) ? disabledImage : getImage();
 60  
 
 61  4
         String imageURL = finalImage.buildURL();
 62  
 
 63  4
         writer.beginEmpty("input");
 64  4
         writer.attribute("type", "image");
 65  4
         writer.attribute("name", getName());
 66  
         
 67  4
         if (disabled)
 68  2
             writer.attribute("disabled", "disabled");        
 69  
         
 70  4
         writer.attribute("src", imageURL);
 71  
 
 72  4
         renderIdAttribute(writer, cycle);
 73  
 
 74  4
         renderInformalParameters(writer, cycle);
 75  
         
 76  4
         renderSubmitBindings(writer, cycle);
 77  
         
 78  4
         writer.closeTag();
 79  4
     }
 80  
 
 81  
     void handleClick(IRequestCycle cycle, IForm form)
 82  
     {
 83  
         // The point parameter is not really used, unless the
 84  
         // ImageButton is used for its original purpose (as a kind
 85  
         // of image map). In modern usage, we only care about
 86  
         // whether the user clicked on the image (and thus submitted
 87  
         // the form), not where in the image the user actually clicked.
 88  
 
 89  2
         if (isParameterBound("point"))
 90  
         {
 91  1
             int x = Integer.parseInt(cycle.getParameter(getName() + ".x"));
 92  1
             int y = Integer.parseInt(cycle.getParameter(getName() + ".y"));
 93  
 
 94  1
             setPoint(new Point(x, y));
 95  
         }
 96  
 
 97  2
         super.handleClick(cycle, form);
 98  2
     }
 99  
 
 100  
     /** parameter. */
 101  
     public abstract IAsset getDisabledImage();
 102  
 
 103  
     /** parameter. */
 104  
     public abstract IAsset getImage();
 105  
 
 106  
     /** parameter. */
 107  
     public abstract String getNameOverride();
 108  
 
 109  
     /** parameter. */
 110  
     public abstract void setPoint(Point point);
 111  
 
 112  
     protected void prepareForRender(IRequestCycle cycle)
 113  
     {
 114  0
         super.prepareForRender(cycle);
 115  
 
 116  0
         if (getImage() == null)
 117  0
             throw Tapestry.createRequiredParameterException(this, "image");
 118  0
     }
 119  
 }