KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > tags > RowTag


1 package org.roller.presentation.tags;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.apache.struts.taglib.logic.IterateTag;
6
7 import java.io.IOException JavaDoc;
8
9 import javax.servlet.jsp.JspException JavaDoc;
10 import javax.servlet.jsp.JspWriter JavaDoc;
11 import javax.servlet.jsp.tagext.Tag JavaDoc;
12
13
14 /**
15  * <p>This tag generates table rows (i.e. &lt;tr&gt;....&lt;/tr&gt; elements)
16  * with the background color set differently for alternating odd and even
17  * rows. This tag only operates properly if embedded in an IterateTag.</p>
18  *
19  * <p>The following parameters can be specified for this Tag:</p>
20  * <ul>
21  * <li><code>oddColor </code> - The color for Odd numbered rows
22  * <li><code>evenColor</code> - The color for Even numbered rows
23  * <li><code>oddStyleClass</code> - The style class for Odd numbered rows
24  * <li><code>evenStyleClass</code> - The style class for Even numbered rows
25  * <li><code>align</code> - The alignment for the table row
26  * <li><code>valign</code> - The vertical alignment for the table row
27  * </ul>
28  *
29  * <p>Additionally this tag inherits the Event Handler and Style attributes
30  * from the BaseHandlerTag which can also be specified</p>
31  *
32  * TODO Make RowTag work with STL's forEach tag too.
33  *
34  * @jsp.tag name="row"
35  *
36  * @author Amarda Business Systems Ltd
37  * @version 1.0
38  */

39 public class RowTag extends org.apache.struts.taglib.html.BaseHandlerTag
40 {
41     // ----------------------------------------------------- Instance Variables
42
private static Log mLogger = LogFactory.getFactory()
43                                            .getInstance(RowTag.class);
44     protected final static String JavaDoc QUOTE = "\"";
45
46     /**
47      * Color of Odd rows in a table
48      */

49     protected String JavaDoc oddColor = null;
50
51     /**
52      * Color of Even rows in a table
53      */

54     protected String JavaDoc evenColor = null;
55
56     /**
57      * StyleClass of Odd rows in a table
58      */

59     protected String JavaDoc oddStyleClass = null;
60
61     /**
62      * Style Class of Even rows in a table
63      */

64     protected String JavaDoc evenStyleClass = null;
65
66     /**
67      * Alignment of the table row
68      */

69     protected String JavaDoc align = null;
70
71     /**
72      * Vertical Alignment of the table row
73      */

74     protected String JavaDoc valign = null;
75
76     /**
77      * Return the color of Odd rows
78      * @jsp.attribute
79      */

80     public String JavaDoc getOddColor()
81     {
82         return (this.oddColor);
83     }
84
85     /**
86      * Set the color of Odd rows
87      *
88      * @param color HTML bgcolor value for Odd rows
89      */

90     public void setOddColor(String JavaDoc color)
91     {
92         this.oddColor = color;
93     }
94
95     /**
96      * Return the color of Even rows
97      * @jsp.attribute
98      */

99     public String JavaDoc getEvenColor()
100     {
101         return (this.evenColor);
102     }
103
104     /**
105      * Set the color of Even rows
106      *
107      * @param color HTML bgcolor value for Even rows
108      */

109     public void setEvenColor(String JavaDoc color)
110     {
111         this.evenColor = color;
112     }
113
114     /**
115      * Return the Style Class of Odd rows
116      * @jsp.attribute
117      */

118     public String JavaDoc getOddStyleClass()
119     {
120         return (this.oddStyleClass);
121     }
122
123     /**
124      * Set the Style Class of Odd rows
125      *
126      * @param styleClass HTML Style Class value for Odd rows
127      */

128     public void setOddStyleClass(String JavaDoc styleClass)
129     {
130         this.oddStyleClass = styleClass;
131     }
132
133     /**
134      * Return the Style Class of Even rows
135      * @jsp.attribute
136      */

137     public String JavaDoc getEvenStyleClass()
138     {
139         return (this.evenStyleClass);
140     }
141
142     /**
143      * Set the styleClass of Even rows
144      *
145      * @param styleClass HTML Style Class value for Even rows
146      */

147     public void setEvenStyleClass(String JavaDoc styleClass)
148     {
149         this.evenStyleClass = styleClass;
150     }
151
152     /**
153      * Return the Alignment
154      * @jsp.attribute
155      */

156     public String JavaDoc getAlign()
157     {
158         return (this.align);
159     }
160
161     /**
162      * Set the Alignment
163      *
164      * @param Value for Alignment
165      */

166     public void setAlign(String JavaDoc align)
167     {
168         this.align = align;
169     }
170
171     /**
172      * Return the Vertical Alignment
173      * @jsp.attribute
174      */

175     public String JavaDoc getValign()
176     {
177         return (this.valign);
178     }
179
180     /**
181      * Set the Vertical Alignment
182      *
183      * @param Value for Vertical Alignment
184      */

185     public void setValign(String JavaDoc valign)
186     {
187         this.valign = valign;
188     }
189
190     // ----------------------------------------------------- Public Methods
191

192     /**
193      * Start of Tag processing
194      *
195      * @exception JspException if a JSP exception occurs
196      */

197     public int doStartTag() throws JspException JavaDoc
198     {
199         // Continue processing this page
200
return EVAL_BODY_BUFFERED;
201     }
202
203     /**
204      * End of Tag Processing
205      *
206      * @exception JspException if a JSP exception occurs
207      */

208     public int doEndTag() throws JspException JavaDoc
209     {
210         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
211
212
213         // Create a <tr> element based on the parameters
214
buffer.append("<tr");
215
216
217         // Prepare this HTML elements attributes
218
prepareAttributes(buffer);
219
220         buffer.append(">");
221
222         // Add Body Content
223
if (bodyContent != null)
224         {
225             buffer.append(bodyContent.getString().trim());
226         }
227
228         buffer.append("</tr>");
229
230         // Render this element to our writer
231
JspWriter JavaDoc writer = pageContext.getOut();
232
233         try
234         {
235             writer.print(buffer.toString());
236         }
237         catch (IOException JavaDoc e)
238         {
239             mLogger.error("ERROR in tag", e);
240             throw new JspException JavaDoc("Exception in RowTag doEndTag():" +
241                                    e.toString());
242         }
243
244         return EVAL_PAGE;
245     }
246
247     /**
248      * Prepare the attributes of the HTML element
249      */

250     protected void prepareAttributes(StringBuffer JavaDoc buffer)
251     {
252         // Determine if it is an "Odd" or "Even" row
253
boolean evenNumber = ((getRowNumber() % 2) == 0)
254                              ? true : false;
255
256
257         // Append bgcolor parameter
258
buffer.append(prepareBgcolor(evenNumber));
259
260
261         // Append CSS class parameter
262
buffer.append(prepareClass(evenNumber));
263
264
265         // Append "align" parameter
266
buffer.append(prepareAttribute("align", align));
267
268
269         // Append "valign" parameter
270
buffer.append(prepareAttribute("valign", valign));
271
272
273         // Append Event Handler details
274
buffer.append(prepareEventHandlers());
275
276         try
277         {
278             // Append Style details
279
buffer.append(prepareStyles());
280         }
281         catch (Exception JavaDoc e)
282         {
283             mLogger.error("Unexpected exception", e);
284         }
285     }
286
287     /**
288      * Format attribute="value" from the specified attribute & value
289      */

290     protected String JavaDoc prepareAttribute(String JavaDoc attribute, String JavaDoc value)
291     {
292         return (value == null)
293                ? "" : " " + attribute + "=" + QUOTE + value + QUOTE;
294     }
295
296     /**
297      * Format the bgcolor attribute depending on whether
298      * the row is odd or even.
299      *
300      * @param evenNumber Boolean set to true if an even numbered row
301      *
302      */

303     protected String JavaDoc prepareBgcolor(boolean evenNumber)
304     {
305         if (evenNumber)
306         {
307             return prepareAttribute("bgcolor", evenColor);
308         }
309         else
310         {
311             return prepareAttribute("bgcolor", oddColor);
312         }
313     }
314
315     /**
316      * Format the Style sheet class attribute depending on whether
317      * the row is odd or even.
318      *
319      * @param evenNumber Boolean set to true if an even numbered row
320      *
321      */

322     protected String JavaDoc prepareClass(boolean evenNumber)
323     {
324         if (evenNumber)
325         {
326             return prepareAttribute("class", evenStyleClass);
327         }
328         else
329         {
330             return prepareAttribute("class", oddStyleClass);
331         }
332     }
333
334     /**
335      * Determine the Row Number - from the IterateTag
336      */

337     protected int getRowNumber()
338     {
339         // Determine if embedded in an IterateTag
340
Tag tag = findAncestorWithClass(this, IterateTag.class);
341
342         if (tag == null)
343         {
344             return 1;
345         }
346
347         // Determine the current row number
348
IterateTag iterator = (IterateTag) tag;
349
350         // return iterator.getLengthCount() + 1;
351
return iterator.getIndex() + 1;
352     }
353
354     /**
355      * Release resources after Tag processing has finished.
356      */

357     public void release()
358     {
359         super.release();
360
361         oddColor = null;
362         evenColor = null;
363         oddStyleClass = null;
364         evenStyleClass = null;
365         align = null;
366         valign = null;
367     }
368 }
Popular Tags