KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > ImageButton


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.tags.IHtmlAccessable;
21 import org.apache.beehive.netui.tags.internal.PageFlowTagUtils;
22 import org.apache.beehive.netui.tags.javascript.CoreScriptFeature;
23 import org.apache.beehive.netui.tags.javascript.ScriptRequestState;
24 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
25 import org.apache.beehive.netui.tags.rendering.InputImageTag;
26 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
27 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
28 import org.apache.beehive.netui.util.Bundle;
29 import org.apache.struts.Globals;
30 import org.apache.struts.config.ModuleConfig;
31
32 import javax.servlet.ServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.jsp.JspException JavaDoc;
36 import java.net.URISyntaxException JavaDoc;
37
38 /**
39  * Generates an <code>&lt;input type="image"></code> tag with the specified attributes. ImageButton
40  * ignores its body content.
41  * @jsptagref.tagdescription Renders an <code>&lt;input type="image"></code> tag with the specified attributes.
42  *
43  * <p><b>JavaScript</b></p>
44  * <p>If the &lt;netui:imageButton> specifies
45  * a <code>rolloverImage</code> attribute, the following JavaScript will be written to the page:</p>
46  * <pre> function swapImage(control, image)
47  * {
48  * control.src = image;
49  * }</pre>
50  * @example In this sample, an &lt;netui:imageButton> tag displays the image "house.jpg".
51  * On mouseover, the image "house_highlight.jpg" is displayed.
52  * When clicked, the &lt;netui:imageButton> will invoke the action specified by its parent
53  * &lt;netui:form> tag.
54  * <pre> &lt;netui:form action="formSubmit">
55  * &lt;netui:imageButton rolloverImage="house_highlight.jpg" SRC="house.jpg" />
56  * &lt;/netui:form></pre>
57  * @netui:tag name="imageButton" description="Combines the functionality of the netui:image and netui:button tags."
58  */

59 public class ImageButton
60         extends HtmlFocusBaseTag
61         implements IHtmlAccessable
62 {
63     private InputImageTag.State _state = new InputImageTag.State();
64
65     private String JavaDoc _page; // The module-relative URI of the image.
66
private String JavaDoc _rolloverImage; // The roll-over image of the ImageButton.
67

68     /**
69      * Return the name of the Tag.
70      */

71     public String JavaDoc getTagName()
72     {
73         return "ImageButton";
74     }
75
76     /**
77      * This method will return the state associated with the tag. This is used by this
78      * base class to access the individual state objects created by the tags.
79      * @return a subclass of the <code>AbstractHtmlState</code> class.
80      */

81     protected AbstractHtmlState getState()
82     {
83         return _state;
84     }
85
86     /**
87      * Base support for the attribute tag. This is overridden to prevent setting the <code>src</code>
88      * and <code>value</code> attributes
89      * @param name The name of the attribute. This value may not be null or the empty string.
90      * @param value The value of the attribute. This may contain an expression.
91      * @param facet The name of a facet to which the attribute will be applied. This is optional.
92      * @throws JspException A JspException may be thrown if there is an error setting the attribute.
93      */

94     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
95             throws JspException JavaDoc
96     {
97         if (name != null) {
98             if (name.equals(SRC) || name.equals(VALUE)) {
99                 String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
100                 registerTagError(s, null);
101             }
102             else {
103                 if (name.equals(DISABLED)) {
104                     _state.disabled = new Boolean JavaDoc(value).booleanValue();
105                     return;
106                 }
107             }
108         }
109         super.setAttribute(name, value, facet);
110     }
111
112     /**
113      * Sets the property to specify where to align the image.
114      * @param align the image alignment.
115      * @jsptagref.attributedescription The alignment of the image.
116      * @jsptagref.databindable false
117      * @jsptagref.attributesyntaxvalue <i>string_align</i>
118      * @netui:attribute required="false" rtexprvalue="true"
119      * description="Sets the property to specify where to align the image."
120      */

121     public void setAlign(String JavaDoc align)
122     {
123         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALIGN, align);
124     }
125
126     /**
127      * Sets the accessKey attribute value. This should key value of the
128      * keyboard navigation key. It is recommended not to use the following
129      * values because there are often used by browsers <code>A, C, E, F, G,
130      * H, V, left arrow, and right arrow</code>.
131      * @param accessKey the accessKey value.
132      * @jsptagref.attributedescription The keyboard navigation key for the element.
133      * The following values are not recommended because they
134      * are often used by browsers: <code>A, C, E, F, G,
135      * H, V, left arrow, and right arrow</code>
136      * @jsptagref.databindable false
137      * @jsptagref.attributesyntaxvalue <i>string_accessKey</i>
138      * @netui:attribute required="false" rtexprvalue="true" type="char"
139      * description="The keyboard navigation key for the element.
140      * The following values are not recommended because they
141      * are often used by browsers: A, C, E, F, G,
142      * H, V, left arrow, and right arrow"
143      */

144     public void setAccessKey(char accessKey)
145     {
146         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ACCESSKEY, Character.toString(accessKey));
147     }
148
149     /**
150      * Sets the tabIndex of the rendered html tag.
151      * @param tabindex the tab index.
152      * @jsptagref.attributedescription The tabIndex of the rendered HTML tag. This attribute determines the position of the
153      * rendered HTML tag in the sequence of tags that the user may advance through by pressing the TAB key.
154      * @jsptagref.databindable false
155      * @jsptagref.attributesyntaxvalue <i>string_tabIndex</i>
156      * @netui:attribute required="false" rtexprvalue="true" type="int"
157      * description="The tabIndex of the rendered HTML tag. This attribute determines the position of the
158      * rendered HTML tag in the sequence of tags that the user may advance through by pressing the TAB key."
159      */

160     public void setTabindex(int tabindex)
161     {
162         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, TABINDEX, Integer.toString(tabindex));
163     }
164
165     /**
166      * Sets the usemap of for the map.
167      * @param usemap the tab index.
168      * @jsptagref.attributedescription Sets the usemap of for the map.
169      * @jsptagref.databindable false
170      * @jsptagref.attributesyntaxvalue <i>string_usemap</i>
171      * @netui:attribute required="false" rtexprvalue="true"
172      * description="Sets the usemap of for the map."
173      */

174     public void setUsemap(String JavaDoc usemap)
175     {
176         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, USEMAP, usemap);
177     }
178
179     /**
180      * Sets the ismap of for the map.
181      * @param ismap the tab index.
182      * @jsptagref.attributedescription Sets the ismap of for the map.
183      * @jsptagref.databindable false
184      * @jsptagref.attributesyntaxvalue <i>string_ismap</i>
185      * @netui:attribute required="false" rtexprvalue="true"
186      * description="Sets the ismap of for the map."
187      */

188     public void setIsmap(String JavaDoc ismap)
189     {
190         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ISMAP, ismap);
191     }
192
193     /**
194      * Sets the property to specify the alt text of the image.
195      * @param alt the image alt text.
196      * @jsptagref.attributedescription The alternative text of the image
197      * @jsptagref.databindable Read Only
198      * @jsptagref.attributesyntaxvalue <i>string_alt</i>
199      * @netui:attribute required="false" rtexprvalue="true"
200      * description="The alternative text of the image"
201      */

202     public void setAlt(String JavaDoc alt)
203     {
204         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALT, alt);
205     }
206
207     /**
208      * Sets the roll-over image of the ImageAnchor.
209      * @param rolloverImage the rollover image.
210      * @jsptagref.attributedescription The URI of the rollover image.
211      * @jsptagref.databindable false
212      * @jsptagref.attributesyntaxvalue <i>string_rolloverImage</i>
213      * @netui:attribute required="false" rtexprvalue="true"
214      * description="The URI of the rollover image."
215      */

216     public void setRolloverImage(String JavaDoc rolloverImage)
217     {
218         _rolloverImage = rolloverImage;
219     }
220
221     /**
222      * Sets the image source URI.
223      * @param src the source URI.
224      * @jsptagref.attributedescription The image source URI
225      * @jsptagref.databindable Read Only
226      * @jsptagref.attributesyntaxvalue <i>string_src</i>
227      * @netui:attribute required="false" rtexprvalue="true"
228      * description="The image source URI"
229      * reftype="img-url"
230      */

231     public void setSrc(String JavaDoc src)
232             throws JspException JavaDoc
233     {
234         _state.src = src;
235     }
236
237     /**
238      * Set the value of the ImageButton.
239      * @param value the value of the ImageButton.
240      * @jsptagref.attributedescription The value of the image button.
241      * @jsptagref.databindable false
242      * @jsptagref.attributesyntaxvalue <i>string_value</i>
243      * @netui:attribute required="false" rtexprvalue="true"
244      * description="The value of the image button."
245      */

246     public void setValue(String JavaDoc value)
247     {
248         _state.value = value;
249     }
250
251     /**
252      * Process the start of this tag.
253      * @throws JspException if a JSP exception has occurred
254      */

255     public int doStartTag() throws JspException JavaDoc
256     {
257
258         if (_rolloverImage != null && getJavaScriptAttribute(ONMOUSEOVER) == null) {
259             // cause the roll over script to be inserted
260
ScriptRequestState srs = ScriptRequestState.getScriptRequestState((HttpServletRequest JavaDoc) pageContext.getRequest());
261             WriteRenderAppender writer = new WriteRenderAppender(pageContext);
262             srs.writeFeature(getScriptReporter(), writer, CoreScriptFeature.ROLLOVER, true, false, null);
263         }
264
265         return EVAL_BODY_BUFFERED;
266     }
267
268     /**
269      * Save the body content of the ImageButton.
270      * @throws JspException if a JSP exception has occurred
271      */

272     public int doAfterBody() throws JspException JavaDoc
273     {
274
275         if (bodyContent != null) {
276             bodyContent.clearBody();
277         }
278         return SKIP_BODY;
279     }
280
281     /**
282      * Process the end of this tag.
283      * @throws JspException if a JSP exception has occurred
284      */

285     public int doEndTag() throws JspException JavaDoc
286     {
287         ServletRequest JavaDoc req = pageContext.getRequest();
288         String JavaDoc idScript = null;
289         String JavaDoc tmp = null;
290
291         // we assume that tagId will over have override id if both
292
// are defined.
293
if (getTagId() != null) {
294             idScript = renderNameAndId((HttpServletRequest JavaDoc) req, _state, null);
295         }
296
297         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
298         tmp = src();
299         if (tmp != null) {
300             try {
301                 String JavaDoc uri = PageFlowTagUtils.rewriteResourceURL(pageContext, tmp, null, null);
302                 _state.src = response.encodeURL(uri);
303             }
304             catch (URISyntaxException JavaDoc e) {
305                 // report the error...
306
String JavaDoc s = Bundle.getString("Tags_Image_URLException",
307                         new Object JavaDoc[]{_state.src, e.getMessage()});
308                 registerTagError(s, e);
309             }
310         }
311
312         _state.disabled = isDisabled();
313
314         if (_rolloverImage != null) {
315
316             if (hasErrors()) {
317                 reportErrors();
318                 localRelease();
319                 return EVAL_PAGE;
320             }
321
322             try {
323                 String JavaDoc uri = PageFlowTagUtils.rewriteResourceURL(pageContext, _rolloverImage, null, null);
324                 _rolloverImage = response.encodeURL(qualifyUrlToContext(uri));
325             }
326             catch (URISyntaxException JavaDoc e) {
327                 // report the error...
328
String JavaDoc s = Bundle.getString("Tags_Rollover_Image_URLException",
329                         new Object JavaDoc[]{_rolloverImage, e.getMessage()});
330                 registerTagError(s, e);
331             }
332         }
333
334         if ((getJavaScriptAttribute(ONMOUSEOUT) == null) && (_rolloverImage != null)) {
335             setOnMouseOut("swapImage(this,'" + _state.src + "')");
336         }
337         if ((getJavaScriptAttribute(ONMOUSEOVER) == null) && (_rolloverImage != null)) {
338             setOnMouseOver("swapImage(this,'" + _rolloverImage + "')");
339         }
340
341         WriteRenderAppender writer = new WriteRenderAppender(pageContext);
342         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.INPUT_IMAGE_TAG, req);
343         br.doStartTag(writer, _state);
344         br.doEndTag(writer);
345
346         if (idScript != null)
347             write(idScript);
348
349         // Evaluate the remainder of this page
350
localRelease();
351         return EVAL_PAGE;
352     }
353
354     /**
355      * Release any acquired resources.
356      */

357     protected void localRelease()
358     {
359         super.localRelease();
360
361         _state.clear();
362
363         _page = null;
364         _rolloverImage = null;
365     }
366
367     /**
368      * Return the base source URL that will be rendered in the <code>src</code>
369      * property for this generated element, or <code>null</code> if there is
370      * no such URL.
371      */

372     private String JavaDoc src()
373     {
374
375         // Deal with a direct context-relative page that has been specified
376
if (_page != null) {
377             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
378             ModuleConfig config = (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
379             if (config == null)
380                 return (request.getContextPath() + _page);
381             return (request.getContextPath() + config.getPrefix() + _page);
382         }
383
384         // Deal with an absolute source that has been specified
385
if (_state.src != null)
386             return _state.src;
387         return null;
388     }
389 }
390
Popular Tags