KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByRef;
21 import org.apache.beehive.netui.tags.IHtmlAccessable;
22 import org.apache.beehive.netui.tags.internal.PageFlowTagUtils;
23 import org.apache.beehive.netui.tags.javascript.CoreScriptFeature;
24 import org.apache.beehive.netui.tags.javascript.ScriptRequestState;
25 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
26 import org.apache.beehive.netui.tags.rendering.ImageTag;
27 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
28 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
29 import org.apache.beehive.netui.util.Bundle;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.jsp.JspException JavaDoc;
34 import java.net.URISyntaxException JavaDoc;
35
36 /**
37  * Generates a URL-encoded hyperlink to a specified URI with an image
38  * enclosed as the body. ImageAnchor provides support for image rollovers.
39  *
40  * An imageAnchor must have one of five attributes to correctly create the hyperlink:
41  * <ul>
42  * <li>action - an action invoked by clicking the hyperlink.</li>
43  * <li>href - a URL to go to</li>
44  * <li>linkName - an internal place in the page to move to</li>
45  * <li>clientAction - the action to run on the client</li>
46  * <li>tagId - the ID of the tag</li>
47  * <li>formSubmit - indicates whether or not the enclosing Form should be submitted</li>
48  * </ul>
49  * @jsptagref.tagdescription <p>
50  * Generates a hyperlink with a clickable image. Provides support for image rollovers.
51  * <p>
52  * The &lt;netui:imageAnchor> tag must have one of five attributes to correctly create the hyperlink:
53  * <blockquote>
54  * <ul>
55  * <li><code>action</code> - an action method invoked by clicking the hyperlink</li>
56  * <li><code>href</code> - an URL to go to</li>
57  * <li><code>linkName</code> - an internal place in the page to move to</li>
58  * <li><code>clientAction</code> - the action to run on the client</li>
59  * <li><code>tagId</code> - the ID of the tag</li>
60  * <li><code>formSubmit</code> - indicates whether or not the enclosing Form should be submitted</li>
61  * </ul>
62  * </blockquote>
63  *
64  * <p><b>JavaScript</b></p>
65  * <p>If the <code>formSubmit</code> attribute is set to <code>true</code> and no
66  * <code>onClick</code> attribute is set, the following JavaScript function will be written to the HTML page.
67  * This JavaScript function will be referenced by the <code>onclick</code> attribute of the generated image
68  * anchor tag.</p>
69  * <pre>
70  * function anchor_submit_form(netuiName, newAction)
71  * {
72  * for (var i=0; i&lt;document.forms.length; i++) {
73  * if (document.forms[i].id == netuiName) {
74  * document.forms[i].method = "POST";
75  * document.forms[i].action = newAction;
76  * document.forms[i].submit();
77  * }
78  * }
79  * }</pre>
80  * <p>It is possible to write a custom <code>onClick</code> JavaScript event handler that would
81  * do additional work, for example form validation, and still submit the form correctly. To
82  * accomplish this, reference a JavaScript function in the <code>onClick</code>
83  * attribute:</p>
84  * <pre> &lt;netui:imageAnchor formSubmit="true" <b>onClick="SubmitFromAnchor();return false;"</b>&gt;
85  * View Results
86  * &lt;/netui:imageAnchor&gt;</pre>
87  *
88  * <p>And add the referenced JavaScript function to the page:</p>
89  * <pre> function SubmitFromAnchor()
90  * {
91  * // implement custom logic here
92  *
93  * for(var i=0; i&lt;document.forms.length; i++)
94  * {
95  * // submit to the action /aWebapp/formPost.do
96  * if (document.forms[i].action == "/aWebapp/formPost.do")
97  * {
98  * document.forms[i].method="POST";
99  * document.forms[i].action="/aWebapp/formPost.do";
100  * document.forms[i].submit();
101  * }
102  * }
103  * }</pre>
104  *
105  * <p>This will cause the JavaScript function to be executed before posting the form.</p>
106  * @example In this sample, an &lt;netui:imageAnchor> shows "top.jpg" at 25 x 25 pixels and navigates to
107  * index.jsp when clicked.
108  * <pre> &lt;netui:imageAnchor HREF="index.jsp" SRC="top.jpg" height="25" width="25" /&gt;</pre>
109  * </p>
110  * @netui:tag name="imageAnchor" description="Combines the functionality of the netui:image and netui:anchor tags."
111  */

112 public class ImageAnchor
113         extends Anchor
114         implements IHtmlAccessable
115 {
116     private ImageTag.State _imgState = new ImageTag.State();
117     private String JavaDoc _rolloverImage = null; // The roll-over image of the ImageAnchor.
118

119     /**
120      * Return the name of the Tag.
121      */

122     public String JavaDoc getTagName()
123     {
124         return "ImageAnchor";
125     }
126
127     /**
128      * Base support for the attribute tag. This is overridden to prevent setting the <code>src</code>
129      * attribute. ImageAnchor supports two facets, <code>image</code> and <code>anchor<code>. The default
130      * facet is anchor, meaning if the facet is not specified, the attribute will be applied to the
131      * &lt;a> element. To apply an attribute to the &lt;img> element you must specify the
132      * <code>image</code> facet.
133      * @param name The name of the attribute. This value may not be null or the empty string.
134      * @param value The value of the attribute. This may contain an expression.
135      * @param facet The name of a facet to which the attribute will be applied. This is optional.
136      * @throws JspException A JspException may be thrown if there is an error setting the attribute.
137      */

138     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
139             throws JspException JavaDoc
140     {
141         if (name != null && name.equals(SRC)) {
142             String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
143             registerTagError(s, null);
144         }
145         if (facet != null) {
146             if (facet.equals("image")) {
147                 setStateAttribute(name, value, _imgState);
148                 return;
149             }
150         }
151         super.setAttribute(name, value, facet);
152     }
153
154     /**
155      * Sets the property to specify where to align the image.
156      * @param align the image alignment.
157      * @jsptagref.attributedescription The alignment of the image.
158      * @jsptagref.databindable false
159      * @jsptagref.attributesyntaxvalue <i>string_align</i>
160      * @netui:attribute required="false" rtexprvalue="true"
161      * description="The alignment of the image."
162      */

163     public void setAlign(String JavaDoc align)
164     {
165         _imgState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALIGN, align);
166     }
167
168     /**
169      * Sets the property to specify the alt text of the image.
170      * @param alt the image alignment.
171      * @jsptagref.attributedescription The alternative text of the image
172      * @jsptagref.databindable Read Only
173      * @jsptagref.attributesyntaxvalue <i>string_alt</i>
174      * @netui:attribute required="false" rtexprvalue="true"
175      * description="The alternative text of the image."
176      */

177     public void setAlt(String JavaDoc alt)
178     {
179         _imgState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALT, alt);
180     }
181
182     /**
183      * Sets the property to specify a link to the the long description to supplement
184      * the short description in the <code>alt</code> attribute.
185      * @param longdesc the longdesc.
186      * @jsptagref.attributedescription Specifies a link to the the long description.
187      * @jsptagref.databindable false
188      * @jsptagref.attributesyntaxvalue <i>string_longdesc</i>
189      * @netui:attribute required="false" rtexprvalue="true"
190      * description="Specifies a link to the the long description."
191      */

192     public void setLongdesc(String JavaDoc longdesc)
193     {
194         _imgState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, LONGDESC, longdesc);
195     }
196
197     /**
198      * Sets the border size around the image.
199      * @param border the border size.
200      * @jsptagref.attributedescription The border size around the image
201      * @jsptagref.databindable false
202      * @jsptagref.attributesyntaxvalue <i>integer_pixelBorder</i>
203      * @netui:attribute required="false" rtexprvalue="true"
204      * description="The border size around the image."
205      */

206     public void setBorder(String JavaDoc border)
207     {
208         _imgState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, BORDER, border);
209     }
210
211     /**
212      * Sets the image height.
213      * @param height the height.
214      * @jsptagref.attributedescription The image height
215      * @jsptagref.databindable Read Only
216      * @jsptagref.attributesyntaxvalue <i>integer_height</i>
217      * @netui:attribute required="false" rtexprvalue="true"
218      * description="The image height."
219      */

220     public void setHeight(String JavaDoc height)
221     {
222         _imgState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HEIGHT, height);
223     }
224
225     /**
226      * Sets the the horizontal spacing around the image.
227      * @param hspace the horizontal spacing.
228      * @jsptagref.attributedescription The horizontal spacing around the image.
229      * @jsptagref.databindable Read Only
230      * @jsptagref.attributesyntaxvalue <i>integer_hspace</i>
231      * @netui:attribute required="false" rtexprvalue="true"
232      * description="The horizontal spacing around the image."
233      */

234     public void setHspace(String JavaDoc hspace)
235     {
236         _imgState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HSPACE, hspace);
237     }
238
239     /**
240      * Sets the server-side image map declaration.
241      * @param ismap the image map declaration.
242      * @jsptagref.attributedescription The server-side map declaration.
243      * @jsptagref.databindable false
244      * @jsptagref.attributesyntaxvalue <i>string_isMap</i>
245      * @netui:attribute required="false" rtexprvalue="true"
246      * description="The server-side map declaration."
247      */

248     public void setIsmap(String JavaDoc ismap)
249     {
250         _imgState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ISMAP, ismap);
251     }
252
253     /**
254      * Sets the roll-over image of the ImageAnchor.
255      * @param rolloverImage the rollover image.
256      * @jsptagref.attributedescription The URI of the rollover image.
257      * @jsptagref.databindable false
258      * @jsptagref.attributesyntaxvalue <i>string_rolloverImage</i>
259      * @netui:attribute required="false" rtexprvalue="true"
260      * description="The URI of the rollover image."
261      */

262     public void setRolloverImage(String JavaDoc rolloverImage)
263     {
264         _rolloverImage = rolloverImage;
265     }
266
267     /**
268      * Sets the image source URI.
269      * @param src the image source URI.
270      * @jsptagref.attributedescription The image source URI
271      * @jsptagref.databindable Read Only
272      * @jsptagref.attributesyntaxvalue <i>string_src</i>
273      * @netui:attribute required="false" rtexprvalue="true"
274      * description="The image source URI"
275      * reftype="img-url"
276      */

277     public void setSrc(String JavaDoc src)
278             throws JspException JavaDoc
279     {
280         _imgState.src = src;
281     }
282
283     /**
284      * Sets the client-side image map declaration.
285      * @param usemap the map declaration.
286      * @jsptagref.attributedescription The client-side image map declaration
287      * @jsptagref.databindable false
288      * @jsptagref.attributesyntaxvalue <i>string_useMap</i>
289      * @netui:attribute required="false" rtexprvalue="true"
290      * description="The client-side image map declaration"
291      */

292     public void setUsemap(String JavaDoc usemap)
293     {
294         _imgState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, USEMAP, usemap);
295     }
296
297     /**
298      * Sets the vertical spacing around the image.
299      * @param vspace the vertical spacing.
300      * @jsptagref.attributedescription The vertical spacing around the image.
301      * @jsptagref.databindable Read Only
302      * @jsptagref.attributesyntaxvalue <i>string_vspace</i>
303      * @netui:attribute required="false" rtexprvalue="true"
304      * description="The vertical spacing around the image."
305      */

306     public void setVspace(String JavaDoc vspace)
307     {
308         _imgState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, VSPACE, vspace);
309     }
310
311     /**
312      * Set the &lt;img> style for the contained image. When the tag library is
313      * running in legacy mode, this will override the <code>style</code> attribute if that is
314      * set. If this is not set, and <code>style</code> is set, then it will be applied to
315      * the image.
316      * @param imageStyle the label style
317      * @jsptagref.attributedescription For legacy documents. Specifies style information for the
318      * contained image. When the tag library is running in legacy mode, this will override the
319      * <code>style</code> attribute. If this is not set, and <code>style</code> is set,
320      * then it will be applied to the image.
321      * @jsptagref.databindable false
322      * @jsptagref.attributesyntaxvalue <i>string_imagestyle</i>
323      * @netui:attribute required="false" rtexprvalue="true"
324      * description="Set the style for the contained image."
325      */

326     public void setImageStyle(String JavaDoc imageStyle)
327     {
328         _imgState.style = imageStyle;
329     }
330
331     /**
332      * Set the label style class for each contained Image. When the tag library is
333      * running in legacy mode, this will override the <code>styleClass</code> attribute if that is
334      * set. If this is not set, and <code>styleClass</code> is set, then it will be applied to
335      * the image.
336      * @param imageClass the image class
337      * @jsptagref.attributedescription For legacy documents. The style class (a style sheet selector).
338      * When the tag library is running in legacy mode, this will override the <code>styleClass</code>
339      * attribute. If this is not set, and <code>styleClass</code> is set, then it will be applied to
340      * the image.
341      * @jsptagref.databindable false
342      * @jsptagref.attributesyntaxvalue <i>string_imageclass</i>
343      * @netui:attribute required="false" rtexprvalue="true"
344      * description="Set the label style class for each contained image."
345      */

346     public void setImageStyleClass(String JavaDoc imageClass)
347     {
348         _imgState.styleClass = imageClass;
349     }
350
351     /**
352      * Sets the image width.
353      * @param width the image width.
354      * @jsptagref.attributedescription The image width.
355      * @jsptagref.databindable Read Only
356      * @jsptagref.attributesyntaxvalue <i>integer_pixelWidth</i>
357      * @netui:attribute required="false" rtexprvalue="true"
358      * description="The image width."
359      */

360     public void setWidth(String JavaDoc width)
361     {
362         _imgState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, WIDTH, width);
363     }
364
365     /**
366      * Insert rollover javascript.
367      * <p>
368      * Support for indexed property since Struts 1.1
369      * @throws JspException if a JSP exception has occurred
370      */

371     public int doStartTag() throws JspException JavaDoc
372     {
373         if (_rolloverImage != null && getJavaScriptAttribute(ONMOUSEOVER) == null) {
374             // cause the roll over script to be inserted
375
WriteRenderAppender writer = new WriteRenderAppender(pageContext);
376             ScriptRequestState srs = ScriptRequestState.getScriptRequestState((HttpServletRequest JavaDoc) pageContext.getRequest());
377             srs.writeFeature(getScriptReporter(), writer, CoreScriptFeature.ROLLOVER, true, false, null);
378         }
379
380         return EVAL_BODY_BUFFERED;
381     }
382
383     /**
384      * Render the image and hyperlink.
385      * @throws JspException if a JSP exception has occurred
386      */

387     public int doEndTag() throws JspException JavaDoc
388     {
389         // report errors that may have occurred when the required attributes are being set
390
if (hasErrors())
391             return reportAndExit(EVAL_PAGE);
392
393         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
394
395         // build the anchor into the results
396
// render the anchor tag
397
WriteRenderAppender writer = new WriteRenderAppender(pageContext);
398         TagRenderingBase trb = TagRenderingBase.Factory.getRendering(TagRenderingBase.ANCHOR_TAG, req);
399         ByRef script = new ByRef();
400         if (!createAnchorBeginTag(req, script, trb, writer, REQUIRED_ATTR)) {
401             reportErrors();
402             if (!script.isNull())
403                 write(script.getRef().toString());
404             localRelease();
405             return EVAL_PAGE;
406         }
407
408         // set the source and lowsrc attributes
409
// the lowsrc is deprecated and should be removed.
410
HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
411         if (_imgState.src != null) {
412             try {
413                 String JavaDoc uri = PageFlowTagUtils.rewriteResourceURL(pageContext, _imgState.src, null, null);
414                 _imgState.src = response.encodeURL(uri);
415             }
416             catch (URISyntaxException JavaDoc e) {
417                 // report the error...
418
String JavaDoc s = Bundle.getString("Tags_Image_URLException",
419                         new Object JavaDoc[]{_imgState.src, e.getMessage()});
420                 registerTagError(s, e);
421             }
422         }
423
424         // set the rollover image
425
if (_rolloverImage != null) {
426             try {
427                 String JavaDoc uri = PageFlowTagUtils.rewriteResourceURL(pageContext, _rolloverImage, null, null);
428                 _rolloverImage = response.encodeURL(qualifyUrlToContext(uri));
429             }
430             catch (URISyntaxException JavaDoc e) {
431                 // report the error...
432
String JavaDoc s = Bundle.getString("Tags_Rollover_Image_URLException",
433                         new Object JavaDoc[]{_rolloverImage, e.getMessage()});
434                 registerTagError(s, e);
435             }
436
437             if (getJavaScriptAttribute(ONMOUSEOUT) == null) {
438                 String JavaDoc s = "swapImage(this,'" + response.encodeURL(_imgState.src) + "')";
439                 _imgState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONMOUSEOUT, s);
440             }
441             if (getJavaScriptAttribute(ONMOUSEOVER) == null) {
442                 String JavaDoc s = "swapImage(this,'" + response.encodeURL(_rolloverImage) + "')";
443                 _imgState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONMOUSEOVER, s);
444             }
445         }
446
447         // render the image tag.
448
TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.IMAGE_TAG, req);
449         br.doStartTag(writer, _imgState);
450         br.doEndTag(writer);
451
452         // write the end tag
453
trb.doEndTag(writer);
454         if (!script.isNull())
455             write(script.getRef().toString());
456
457         localRelease();
458         return EVAL_PAGE;
459     }
460
461     /**
462      * Release any acquired resources.
463      */

464     protected void localRelease()
465     {
466         super.localRelease();
467
468         _imgState.clear();
469         _rolloverImage = null;
470     }
471 }
472
Popular Tags