KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > ImageSubmit


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 JavaDoc;
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 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         String JavaDoc nameOverride = getNameOverride();
43
44         setName((nameOverride == null) ? form.getElementId(this) : form.getElementId(this, nameOverride));
45     }
46
47     protected boolean isClicked(IRequestCycle cycle, String JavaDoc name)
48     {
49         String JavaDoc parameterName = name + ".x";
50
51         return (cycle.getParameter(parameterName) != null);
52     }
53     
54     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
55     {
56         boolean disabled = isDisabled();
57         IAsset disabledImage = getDisabledImage();
58
59         IAsset finalImage = (disabled && disabledImage != null) ? disabledImage : getImage();
60
61         String JavaDoc imageURL = finalImage.buildURL(cycle);
62
63         writer.beginEmpty("input");
64         writer.attribute("type", "image");
65         writer.attribute("name", getName());
66
67         if (disabled)
68             writer.attribute("disabled", "disabled");
69
70         // NN4 places a border unless you tell it otherwise.
71
// IE ignores the border attribute and never shows a border.
72

73         writer.attribute("border", 0);
74
75         writer.attribute("src", imageURL);
76
77         renderIdAttribute(writer, cycle);
78
79         renderInformalParameters(writer, cycle);
80
81         writer.closeTag();
82     }
83
84     void handleClick(IRequestCycle cycle, IForm form)
85     {
86         // The point parameter is not really used, unless the
87
// ImageButton is used for its original purpose (as a kind
88
// of image map). In modern usage, we only care about
89
// whether the user clicked on the image (and thus submitted
90
// the form), not where in the image the user actually clicked.
91

92         if (isParameterBound("point"))
93         {
94             int x = Integer.parseInt(cycle.getParameter(getName() + ".x"));
95             int y = Integer.parseInt(cycle.getParameter(getName() + ".y"));
96
97             setPoint(new Point JavaDoc(x, y));
98         }
99
100         super.handleClick(cycle, form);
101     }
102
103     /** parameter */
104     public abstract IAsset getDisabledImage();
105
106     /** parameter */
107     public abstract IAsset getImage();
108
109     /** parameter */
110     public abstract String JavaDoc getNameOverride();
111
112     /** parameter */
113     public abstract void setPoint(Point JavaDoc point);
114
115     protected void prepareForRender(IRequestCycle cycle)
116     {
117         super.prepareForRender(cycle);
118
119         if (getImage() == null)
120             throw Tapestry.createRequiredParameterException(this, "image");
121     }
122 }
Popular Tags