KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > tags > CaptionTag


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.tags;
13
14 import java.util.List JavaDoc;
15
16 import javax.servlet.jsp.JspException JavaDoc;
17 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
18
19 import org.displaytag.exception.TagStructureException;
20 import org.displaytag.properties.MediaTypeEnum;
21 import org.displaytag.util.HtmlAttributeMap;
22 import org.displaytag.util.MediaUtil;
23 import org.displaytag.util.MultipleHtmlAttribute;
24 import org.displaytag.util.TagConstants;
25
26
27 /**
28  * Simple caption tag which mimics a standard html caption.
29  * @author Fabrizio Giustina
30  * @version $Revision: 907 $ ($Author: fgiust $)
31  */

32 public class CaptionTag extends BodyTagSupport JavaDoc implements MediaUtil.SupportsMedia
33 {
34
35     /**
36      * D1597A17A6.
37      */

38     private static final long serialVersionUID = 899149338534L;
39
40     /**
41      * Map containing all the standard html attributes.
42      */

43     private HtmlAttributeMap attributeMap = new HtmlAttributeMap();
44
45     /**
46      * is this the first iteration?
47      */

48     private boolean firstIteration = true;
49
50     /**
51      * The media supported attribute.
52      */

53     private List JavaDoc supportedMedia;
54
55     /**
56      * setter for the "style" html attribute.
57      * @param value attribute value
58      */

59     public void setStyle(String JavaDoc value)
60     {
61         this.attributeMap.put(TagConstants.ATTRIBUTE_STYLE, value);
62     }
63
64     /**
65      * setter for the "class" html attribute.
66      * @param value attribute value
67      */

68     public void setClass(String JavaDoc value)
69     {
70         this.attributeMap.put(TagConstants.ATTRIBUTE_CLASS, new MultipleHtmlAttribute(value));
71     }
72
73     /**
74      * setter for the "id" html attribute.
75      * @param value attribute value
76      */

77     public void setId(String JavaDoc value)
78     {
79         this.attributeMap.put(TagConstants.ATTRIBUTE_ID, value);
80     }
81
82     /**
83      * setter for the "title" html attribute.
84      * @param value attribute value
85      */

86     public void setTitle(String JavaDoc value)
87     {
88         this.attributeMap.put(TagConstants.ATTRIBUTE_TITLE, value);
89     }
90
91     /**
92      * setter for the "lang" html attribute.
93      * @param value attribute value
94      */

95     public void setLang(String JavaDoc value)
96     {
97         this.attributeMap.put(TagConstants.ATTRIBUTE_LANG, value);
98     }
99
100     /**
101      * setter for the "dir" html attribute.
102      * @param value attribute value
103      */

104     public void setDir(String JavaDoc value)
105     {
106         this.attributeMap.put(TagConstants.ATTRIBUTE_DIR, value);
107     }
108
109     /**
110      * create the open tag containing all the attributes.
111      * @return open tag string
112      */

113     public String JavaDoc getOpenTag()
114     {
115
116         if (this.attributeMap.size() == 0)
117         {
118             return TagConstants.TAG_OPEN + TagConstants.TAGNAME_CAPTION + TagConstants.TAG_CLOSE;
119         }
120
121         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
122
123         buffer.append(TagConstants.TAG_OPEN).append(TagConstants.TAGNAME_CAPTION);
124
125         buffer.append(this.attributeMap);
126
127         buffer.append(TagConstants.TAG_CLOSE);
128
129         return buffer.toString();
130     }
131
132     /**
133      * create the closing tag.
134      * @return <code>&lt;/caption&gt;</code>
135      */

136     public String JavaDoc getCloseTag()
137     {
138         return TagConstants.TAG_OPENCLOSING + TagConstants.TAGNAME_CAPTION + TagConstants.TAG_CLOSE;
139     }
140
141     /**
142      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
143      */

144     public int doStartTag() throws JspException JavaDoc
145     {
146         TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class);
147
148         if (tableTag == null)
149         {
150             throw new TagStructureException(getClass(), "caption", "table"); //$NON-NLS-1$ //$NON-NLS-2$
151
}
152
153         MediaTypeEnum currentMediaType = (MediaTypeEnum) this.pageContext.findAttribute(TableTag.PAGE_ATTRIBUTE_MEDIA);
154         if (!MediaUtil.availableForMedia(this, currentMediaType))
155         {
156             return SKIP_BODY;
157         }
158
159         // add caption only once
160
if (tableTag.isFirstIteration())
161         {
162             this.firstIteration = true;
163             // using int to avoid deprecation error in compilation using j2ee 1.3 (EVAL_BODY_TAG)
164
return 2;
165         }
166
167         this.firstIteration = false;
168         return SKIP_BODY;
169     }
170
171     /**
172      * @see org.displaytag.util.MediaUtil.SupportsMedia#setSupportedMedia(java.util.List)
173      */

174     public void setSupportedMedia(List JavaDoc media)
175     {
176         this.supportedMedia = media;
177     }
178
179     /**
180      * @see org.displaytag.util.MediaUtil.SupportsMedia#getSupportedMedia()
181      */

182     public List JavaDoc getSupportedMedia()
183     {
184         return this.supportedMedia;
185     }
186
187     /**
188      * Tag setter.
189      * @param media the space delimited list of supported types
190      */

191     public void setMedia(String JavaDoc media)
192     {
193         MediaUtil.setMedia(this, media);
194     }
195
196     /**
197      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
198      */

199     public int doEndTag() throws JspException JavaDoc
200     {
201         if (this.firstIteration)
202         {
203             TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class);
204
205             if (tableTag == null)
206             {
207                 throw new TagStructureException(getClass(), "caption", "table"); //$NON-NLS-1$ //$NON-NLS-2$
208
}
209
210             MediaTypeEnum currentMediaType = (MediaTypeEnum) this.pageContext
211                 .findAttribute(TableTag.PAGE_ATTRIBUTE_MEDIA);
212             if (currentMediaType != null && !MediaUtil.availableForMedia(this, currentMediaType))
213             {
214                 return SKIP_BODY;
215             }
216
217             if (getBodyContent() != null)
218             {
219                 // set the caption format-agnostic content so it can be written in various formats.
220
tableTag.setCaption(getBodyContent().getString());
221                 // set the nested caption tag to write the caption in html format. See HtmlTableWriter.writeCaption
222
tableTag.setCaptionTag(this);
223             }
224
225             this.firstIteration = false;
226
227         }
228
229         return EVAL_PAGE;
230     }
231
232     /**
233      * @see javax.servlet.jsp.tagext.Tag#release()
234      */

235     public void release()
236     {
237         super.release();
238         this.attributeMap.clear();
239         this.supportedMedia = null;
240     }
241
242 }
Popular Tags