KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.PageFlowTagUtils;
21 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
22 import org.apache.beehive.netui.tags.rendering.ImageTag;
23 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
24 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
25 import org.apache.beehive.netui.util.Bundle;
26 import org.apache.beehive.netui.util.ParamHelper;
27
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.servlet.jsp.JspException JavaDoc;
32 import java.net.URISyntaxException JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * Generates an image with the specified attributes. Image ignores its
38  * body content.
39  * @jsptagref.tagdescription Renders an HTML <img> tag with specified attributes.
40  * @example In this sample, an Image shows "friends.jpg" at 150 x 175 pixels, with the id "Friends".
41  * <pre>&lt;netui:image SRC="friends.jpg" tagId="Friends" height="150" width="175" /></pre>
42  * @netui:tag name="image" description="Places an image file type on your page."
43  */

44 public class Image extends HtmlBaseTag
45         implements IUrlParams
46 {
47     private ImageTag.State _state = new ImageTag.State();
48
49     private String JavaDoc _location = null; // The location hash to append to the url.
50
private Map JavaDoc _params;
51
52     /**
53      * Return the name of the Tag.
54      */

55     public String JavaDoc getTagName()
56     {
57         return "Image";
58     }
59
60     /**
61      * This method will return the state associated with the tag. This is used by this
62      * base class to access the individual state objects created by the tags.
63      * @return a subclass of the <code>AbstractHtmlState</code> class.
64      */

65     protected AbstractHtmlState getState()
66     {
67         return _state;
68     }
69
70     /**
71      * Base support for the attribute tag. This is overridden to prevent setting the <code>src</code>
72      * attribute.
73      * @param name The name of the attribute. This value may not be null or the empty string.
74      * @param value The value of the attribute. This may contain an expression.
75      * @param facet The name of a facet to which the attribute will be applied. This is optional.
76      * @throws JspException A JspException may be thrown if there is an error setting the attribute.
77      */

78     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
79             throws JspException JavaDoc
80     {
81         if (name != null && name.equals(SRC)) {
82             String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
83             registerTagError(s, null);
84         }
85         super.setAttribute(name, value, facet);
86     }
87
88     /**
89      * Sets the property to specify where to align the image.
90      * @param align the image alignment.
91      * @jsptagref.attributedescription The alignment of the image.
92      * @jsptagref.databindable false
93      * @jsptagref.attributesyntaxvalue <i>string_align</i>
94      * @netui:attribute required="false" rtexprvalue="true"
95      * description="The alignment of the image."
96      */

97     public void setAlign(String JavaDoc align)
98     {
99         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALIGN, align);
100     }
101
102     /**
103      * Sets the property to specify the alt text of the image.
104      * @param alt the image alignment.
105      * @jsptagref.attributedescription The alternative text of the image
106      * @jsptagref.databindable Read Only
107      * @jsptagref.attributesyntaxvalue <i>string_alt</i>
108      * @netui:attribute required="false" rtexprvalue="true"
109      * description="The alternative text of the image."
110      */

111     public void setAlt(String JavaDoc alt)
112     {
113         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALT, alt, true);
114     }
115
116     /**
117      * Sets the property to specify a link to the the long description to supplement
118      * the short description in the <code>alt</code> attribute.
119      * @param longdesc the longdesc.
120      * @jsptagref.attributedescription Specifies a link to the the long description.
121      * @jsptagref.databindable false
122      * @jsptagref.attributesyntaxvalue <i>string_longdesc</i>
123      * @netui:attribute required="false" rtexprvalue="true"
124      * description="Specifies a link to the the long description."
125      */

126     public void setLongdesc(String JavaDoc longdesc)
127     {
128         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, LONGDESC, longdesc);
129     }
130
131     /**
132      * Sets the border size around the image.
133      * @param border the border size.
134      * @jsptagref.attributedescription The border size around the image
135      * @jsptagref.databindable false
136      * @jsptagref.attributesyntaxvalue <i>integer_pixelBorder</i>
137      * @netui:attribute required="false" rtexprvalue="true"
138      * description="The border size around the image."
139      */

140     public void setBorder(String JavaDoc border)
141     {
142         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, BORDER, border);
143     }
144
145     /**
146      * Sets the image height.
147      * @param height the height.
148      * @jsptagref.attributedescription The image height
149      * @jsptagref.databindable Read Only
150      * @jsptagref.attributesyntaxvalue <i>integer_height</i>
151      * @netui:attribute required="false" rtexprvalue="true"
152      * description="The image height."
153      */

154     public void setHeight(String JavaDoc height)
155     {
156         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HEIGHT, height);
157     }
158
159     /**
160      * Sets the the horizontal spacing around the image.
161      * @param hspace the horizontal spacing.
162      * @jsptagref.attributedescription The horizontal spacing around the image.
163      * @jsptagref.databindable Read Only
164      * @jsptagref.attributesyntaxvalue <i>integer_hspace</i>
165      * @netui:attribute required="false" rtexprvalue="true"
166      * description="The horizontal spacing around the image."
167      */

168     public void setHspace(String JavaDoc hspace)
169     {
170         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HSPACE, hspace);
171     }
172
173     /**
174      * Sets the server-side image map declaration.
175      * @param ismap the image map declaration.
176      * @jsptagref.attributedescription The server-side map declaration.
177      * @jsptagref.databindable false
178      * @jsptagref.attributesyntaxvalue <i>string_isMap</i>
179      * @netui:attribute required="false" rtexprvalue="true"
180      * description="The server-side map declaration."
181      */

182     public void setIsmap(String JavaDoc ismap)
183     {
184         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ISMAP, ismap);
185     }
186
187     /**
188      * Sets the location hash to append to the url.
189      * @param location the location hash.
190      * @jsptagref.attributedescription The location hash to append to the URL.
191      * @jsptagref.databindable false
192      * @jsptagref.attributesyntaxvalue <i>string_location</i>
193      * @netui:attribute required="false" rtexprvalue="true"
194      * description="The location hash to append to the URL."
195      */

196     public void setLocation(String JavaDoc location)
197     {
198         _location = location;
199     }
200
201     /**
202      * Sets the image source URI.
203      * @param src the source URI.
204      * @jsptagref.attributedescription The image source URI
205      * @jsptagref.databindable Read Only
206      * @jsptagref.attributesyntaxvalue <i>string_src</i>
207      * @netui:attribute required="false" rtexprvalue="true"
208      * description="The image source URI"
209      */

210     public void setSrc(String JavaDoc src)
211             throws JspException JavaDoc
212     {
213         _state.src = src;
214     }
215
216     /**
217      * Sets the client-side image map declaration.
218      * @param usemap the map declaration.
219      * @jsptagref.attributedescription The client-side image map declaration
220      * @jsptagref.databindable false
221      * @jsptagref.attributesyntaxvalue <i>string_useMap</i>
222      * @netui:attribute required="false" rtexprvalue="true"
223      * description="The client-side image map declaration"
224      */

225     public void setUsemap(String JavaDoc usemap)
226     {
227         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, USEMAP, usemap);
228     }
229
230     /**
231      * Sets the vertical spacing around the image.
232      * @param vspace the vertical spacing.
233      * @jsptagref.attributedescription The vertical spacing around the image.
234      * @jsptagref.databindable Read Only
235      * @jsptagref.attributesyntaxvalue <i>string_vspace</i>
236      * @netui:attribute required="false" rtexprvalue="true"
237      * description="The vertical spacing around the image."
238      */

239     public void setVspace(String JavaDoc vspace)
240     {
241         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, VSPACE, vspace);
242     }
243
244     /**
245      * Sets the image width.
246      * @param width the image width.
247      * @jsptagref.attributedescription The image width.
248      * @jsptagref.databindable Read Only
249      * @jsptagref.attributesyntaxvalue <i>integer_pixelWidth</i>
250      * @netui:attribute required="false" rtexprvalue="true"
251      * description="The image width."
252      */

253     public void setWidth(String JavaDoc width)
254     {
255         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, WIDTH, width);
256     }
257
258     /**
259      * Adds a URL parameter to the generated hyperlink.
260      * @param name the name of the parameter to be added.
261      * @param value the value of the parameter to be added (a String or String[]).
262      * @param facet
263      */

264     public void addParameter(String JavaDoc name, Object JavaDoc value, String JavaDoc facet) throws JspException JavaDoc
265     {
266         assert(name != null) : "Parameter 'name' must not be null";
267
268         if (_params == null) {
269             _params = new HashMap JavaDoc();
270         }
271         ParamHelper.addParam(_params, name, value);
272     }
273
274     /**
275      * Render the beginning of the IMG tag.
276      * @throws JspException if a JSP exception has occurred
277      */

278     public int doStartTag() throws JspException JavaDoc
279     {
280         // Evaluate the body of this tag
281
return EVAL_BODY_BUFFERED;
282     }
283
284     /**
285      * Render the end of the IMG tag.
286      * @throws JspException if a JSP exception has occurred
287      */

288     public int doEndTag() throws JspException JavaDoc
289     {
290         ServletRequest JavaDoc req = pageContext.getRequest();
291         String JavaDoc scriptId = null;
292
293         // Generate the name definition or image element
294

295         String JavaDoc uri = null;
296         if (_state.src != null) {
297             try {
298                 uri = PageFlowTagUtils.rewriteResourceURL(pageContext, _state.src, _params, _location);
299             }
300             catch (URISyntaxException JavaDoc e) {
301                 // report the error...
302
String JavaDoc s = Bundle.getString("Tags_Image_URLException",
303                         new Object JavaDoc[]{_state.src, e.getMessage()});
304                 registerTagError(s, e);
305             }
306         }
307
308         if (uri != null) {
309             _state.src = ((HttpServletResponse JavaDoc) pageContext.getResponse()).encodeURL(uri);
310         }
311
312         // we assume that tagId will over have override id if both
313
// are defined.
314
if (_state.id != null) {
315             scriptId = renderNameAndId((HttpServletRequest JavaDoc) req, _state, null);
316         }
317
318         WriteRenderAppender writer = new WriteRenderAppender(pageContext);
319         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.IMAGE_TAG, req);
320         br.doStartTag(writer, _state);
321         br.doEndTag(writer);
322
323
324         if (scriptId != null)
325             write(scriptId);
326
327         // Evaluate the remainder of this page
328
localRelease();
329         return EVAL_PAGE;
330     }
331
332     /**
333      * Release any acquired resources.
334      */

335     protected void localRelease()
336     {
337         super.localRelease();
338
339         _state.clear();
340
341         _location = null;
342         _params = null;
343     }
344 }
345
Popular Tags