KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > ImageTag


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

18
19
20 package org.apache.struts.taglib.html;
21
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26
27 import org.apache.struts.Globals;
28 import org.apache.struts.config.ModuleConfig;
29 import org.apache.struts.taglib.TagUtils;
30
31
32 /**
33  * Tag for input fields of type "image".
34  *
35  * @version $Rev: 164927 $ $Date: 2005-04-27 01:41:05 +0100 (Wed, 27 Apr 2005) $
36  */

37
38 public class ImageTag extends SubmitTag {
39
40
41     // ------------------------------------------------------------- Properties
42

43
44     /**
45      * The alignment for this image.
46      */

47     protected String JavaDoc align = null;
48
49     /**
50      * @deprecated Align attribute is deprecated in HTML 4.x.
51      */

52     public String JavaDoc getAlign() {
53         return (this.align);
54     }
55
56     /**
57      * @deprecated Align attribute is deprecated in HTML 4.x.
58      */

59     public void setAlign(String JavaDoc align) {
60         this.align = align;
61     }
62
63
64     /**
65      * The border size around the image.
66      */

67     protected String JavaDoc border = null;
68
69     public String JavaDoc getBorder() {
70         return (this.border);
71     }
72
73     public void setBorder(String JavaDoc border) {
74         this.border = border;
75     }
76
77
78     /**
79      * The module-relative URI of the image.
80      */

81     protected String JavaDoc page = null;
82
83     public String JavaDoc getPage() {
84         return (this.page);
85     }
86
87     public void setPage(String JavaDoc page) {
88         this.page = page;
89     }
90
91
92     /**
93      * The message resources key of the module-relative URI of the image.
94      */

95     protected String JavaDoc pageKey = null;
96
97     public String JavaDoc getPageKey() {
98         return (this.pageKey);
99     }
100
101     public void setPageKey(String JavaDoc pageKey) {
102         this.pageKey = pageKey;
103     }
104
105     /**
106      * The URL of this image.
107      */

108     protected String JavaDoc src = null;
109
110     public String JavaDoc getSrc() {
111         return (this.src);
112     }
113
114     public void setSrc(String JavaDoc src) {
115         this.src = src;
116     }
117
118
119     /**
120      * The message resources key for the URL of this image.
121      */

122     protected String JavaDoc srcKey = null;
123
124     public String JavaDoc getSrcKey() {
125         return (this.srcKey);
126     }
127
128     public void setSrcKey(String JavaDoc srcKey) {
129         this.srcKey = srcKey;
130     }
131
132
133     // --------------------------------------------------------- Constructor
134

135     public ImageTag() {
136         super();
137         property = "";
138     }
139
140     // --------------------------------------------------------- Protected Methods
141

142     /**
143      * Render the opening element.
144      *
145      * @return The opening part of the element.
146      */

147     protected String JavaDoc getElementOpen() {
148         return "<input type=\"image\"";
149     }
150
151     /**
152      * Render the button attributes
153      * @param results The StringBuffer that output will be appended to.
154      */

155     protected void prepareButtonAttributes(StringBuffer JavaDoc results)
156                       throws JspException JavaDoc {
157         String JavaDoc tmp = src();
158         if (tmp != null) {
159             HttpServletResponse JavaDoc response =
160                 (HttpServletResponse JavaDoc) pageContext.getResponse();
161             prepareAttribute(results, "src", response.encodeURL(tmp));
162         }
163         prepareAttribute(results, "align", getAlign());
164         prepareAttribute(results, "border", getBorder());
165         prepareAttribute(results, "value", getValue());
166         prepareAttribute(results, "accesskey", getAccesskey());
167         prepareAttribute(results, "tabindex", getTabindex());
168     }
169
170
171     /**
172      * Release any acquired resources.
173      */

174     public void release() {
175
176         super.release();
177         page = null;
178         pageKey = null;
179         property = "";
180         src = null;
181         srcKey = null;
182
183     }
184
185
186     // ------------------------------------------------------ Protected Methods
187

188
189     /**
190      * Return the base source URL that will be rendered in the <code>src</code>
191      * property for this generated element, or <code>null</code> if there is
192      * no such URL.
193      *
194      * @exception JspException if an error occurs
195      */

196     protected String JavaDoc src() throws JspException JavaDoc {
197
198         // Deal with a direct context-relative page that has been specified
199
if (this.page != null) {
200             if ((this.src != null) || (this.srcKey != null) ||
201                 (this.pageKey != null)) {
202                 JspException JavaDoc e = new JspException JavaDoc
203                     (messages.getMessage("imgTag.src"));
204                 TagUtils.getInstance().saveException(pageContext, e);
205                 throw e;
206             }
207             
208             ModuleConfig config = (ModuleConfig)
209                 pageContext.getRequest().getAttribute(Globals.MODULE_KEY);
210             
211             HttpServletRequest JavaDoc request =
212                 (HttpServletRequest JavaDoc) pageContext.getRequest();
213             
214             String JavaDoc pageValue = this.page;
215             if (config != null) {
216                 pageValue = TagUtils.getInstance().pageURL(request,
217                                                            this.page,
218                                                            config);
219             }
220             return (request.getContextPath() + pageValue);
221         }
222
223         // Deal with an indirect context-relative page that has been specified
224
if (this.pageKey != null) {
225             if ((this.src != null) || (this.srcKey != null)) {
226                 JspException JavaDoc e = new JspException JavaDoc
227                     (messages.getMessage("imgTag.src"));
228                 TagUtils.getInstance().saveException(pageContext, e);
229                 throw e;
230             }
231             
232             ModuleConfig config = (ModuleConfig)
233                 pageContext.getRequest().getAttribute(Globals.MODULE_KEY);
234             
235             HttpServletRequest JavaDoc request =
236                 (HttpServletRequest JavaDoc) pageContext.getRequest();
237             
238             String JavaDoc pageValue = TagUtils.getInstance().message(
239                                           pageContext,
240                                           getBundle(),
241                                           getLocale(),
242                                           this.pageKey);
243             if (config != null) {
244                 pageValue = TagUtils.getInstance().pageURL(request,
245                                                            pageValue,
246                                                            config);
247             }
248             return (request.getContextPath() + pageValue);
249         }
250
251         // Deal with an absolute source that has been specified
252
if (this.src != null) {
253             if (this.srcKey != null) {
254                 JspException JavaDoc e = new JspException JavaDoc
255                     (messages.getMessage("imgTag.src"));
256                 TagUtils.getInstance().saveException(pageContext, e);
257                 throw e;
258             }
259             return (this.src);
260         }
261
262         // Deal with an indirect source that has been specified
263
if (this.srcKey == null) {
264             JspException JavaDoc e = new JspException JavaDoc
265                 (messages.getMessage("imgTag.src"));
266             TagUtils.getInstance().saveException(pageContext, e);
267             throw e;
268         }
269         
270         return TagUtils.getInstance().message(
271             pageContext,
272             getBundle(),
273             getLocale(),
274             this.srcKey);
275
276     }
277
278
279 }
280
Popular Tags