KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > tags > RowTag


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.ui.core.tags;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.struts.taglib.logic.IterateTag;
23
24 import java.io.IOException JavaDoc;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.tagext.Tag JavaDoc;
29
30
31 /**
32  * <p>This tag generates table rows (i.e. &lt;tr&gt;....&lt;/tr&gt; elements)
33  * with the background color set differently for alternating odd and even
34  * rows. This tag only operates properly if embedded in an IterateTag.</p>
35  *
36  * <p>The following parameters can be specified for this Tag:</p>
37  * <ul>
38  * <li><code>oddColor </code> - The color for Odd numbered rows
39  * <li><code>evenColor</code> - The color for Even numbered rows
40  * <li><code>oddStyleClass</code> - The style class for Odd numbered rows
41  * <li><code>evenStyleClass</code> - The style class for Even numbered rows
42  * <li><code>align</code> - The alignment for the table row
43  * <li><code>valign</code> - The vertical alignment for the table row
44  * </ul>
45  *
46  * <p>Additionally this tag inherits the Event Handler and Style attributes
47  * from the BaseHandlerTag which can also be specified</p>
48  *
49  * TODO Make RowTag work with STL's forEach tag too.
50  *
51  * @jsp.tag name="row"
52  *
53  * @author Amarda Business Systems Ltd
54  * @version 1.0
55  */

56 public class RowTag extends org.apache.struts.taglib.html.BaseHandlerTag
57 {
58     // ----------------------------------------------------- Instance Variables
59
private static Log mLogger = LogFactory.getFactory()
60                                            .getInstance(RowTag.class);
61     protected final static String JavaDoc QUOTE = "\"";
62
63     /**
64      * Color of Odd rows in a table
65      */

66     protected String JavaDoc oddColor = null;
67
68     /**
69      * Color of Even rows in a table
70      */

71     protected String JavaDoc evenColor = null;
72
73     /**
74      * StyleClass of Odd rows in a table
75      */

76     protected String JavaDoc oddStyleClass = null;
77
78     /**
79      * Style Class of Even rows in a table
80      */

81     protected String JavaDoc evenStyleClass = null;
82
83     /**
84      * Alignment of the table row
85      */

86     protected String JavaDoc align = null;
87
88     /**
89      * Vertical Alignment of the table row
90      */

91     protected String JavaDoc valign = null;
92
93     /**
94      * Return the color of Odd rows
95      * @jsp.attribute
96      */

97     public String JavaDoc getOddColor()
98     {
99         return (this.oddColor);
100     }
101
102     /**
103      * Set the color of Odd rows
104      *
105      * @param color HTML bgcolor value for Odd rows
106      */

107     public void setOddColor(String JavaDoc color)
108     {
109         this.oddColor = color;
110     }
111
112     /**
113      * Return the color of Even rows
114      * @jsp.attribute
115      */

116     public String JavaDoc getEvenColor()
117     {
118         return (this.evenColor);
119     }
120
121     /**
122      * Set the color of Even rows
123      *
124      * @param color HTML bgcolor value for Even rows
125      */

126     public void setEvenColor(String JavaDoc color)
127     {
128         this.evenColor = color;
129     }
130
131     /**
132      * Return the Style Class of Odd rows
133      * @jsp.attribute
134      */

135     public String JavaDoc getOddStyleClass()
136     {
137         return (this.oddStyleClass);
138     }
139
140     /**
141      * Set the Style Class of Odd rows
142      *
143      * @param styleClass HTML Style Class value for Odd rows
144      */

145     public void setOddStyleClass(String JavaDoc styleClass)
146     {
147         this.oddStyleClass = styleClass;
148     }
149
150     /**
151      * Return the Style Class of Even rows
152      * @jsp.attribute
153      */

154     public String JavaDoc getEvenStyleClass()
155     {
156         return (this.evenStyleClass);
157     }
158
159     /**
160      * Set the styleClass of Even rows
161      *
162      * @param styleClass HTML Style Class value for Even rows
163      */

164     public void setEvenStyleClass(String JavaDoc styleClass)
165     {
166         this.evenStyleClass = styleClass;
167     }
168
169     /**
170      * Return the Alignment
171      * @jsp.attribute
172      */

173     public String JavaDoc getAlign()
174     {
175         return (this.align);
176     }
177
178     /**
179      * Set the Alignment
180      *
181      * @param Value for Alignment
182      */

183     public void setAlign(String JavaDoc align)
184     {
185         this.align = align;
186     }
187
188     /**
189      * Return the Vertical Alignment
190      * @jsp.attribute
191      */

192     public String JavaDoc getValign()
193     {
194         return (this.valign);
195     }
196
197     /**
198      * Set the Vertical Alignment
199      *
200      * @param Value for Vertical Alignment
201      */

202     public void setValign(String JavaDoc valign)
203     {
204         this.valign = valign;
205     }
206
207     // ----------------------------------------------------- Public Methods
208

209     /**
210      * Start of Tag processing
211      *
212      * @exception JspException if a JSP exception occurs
213      */

214     public int doStartTag() throws JspException JavaDoc
215     {
216         // Continue processing this page
217
return EVAL_BODY_BUFFERED;
218     }
219
220     /**
221      * End of Tag Processing
222      *
223      * @exception JspException if a JSP exception occurs
224      */

225     public int doEndTag() throws JspException JavaDoc
226     {
227         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
228
229
230         // Create a <tr> element based on the parameters
231
buffer.append("<tr");
232
233
234         // Prepare this HTML elements attributes
235
prepareAttributes(buffer);
236
237         buffer.append(">");
238
239         // Add Body Content
240
if (bodyContent != null)
241         {
242             buffer.append(bodyContent.getString().trim());
243         }
244
245         buffer.append("</tr>");
246
247         // Render this element to our writer
248
JspWriter JavaDoc writer = pageContext.getOut();
249
250         try
251         {
252             writer.print(buffer.toString());
253         }
254         catch (IOException JavaDoc e)
255         {
256             mLogger.error("ERROR in tag", e);
257             throw new JspException JavaDoc("Exception in RowTag doEndTag():" +
258                                    e.toString());
259         }
260
261         return EVAL_PAGE;
262     }
263
264     /**
265      * Prepare the attributes of the HTML element
266      */

267     protected void prepareAttributes(StringBuffer JavaDoc buffer)
268     {
269         // Determine if it is an "Odd" or "Even" row
270
boolean evenNumber = ((getRowNumber() % 2) == 0)
271                              ? true : false;
272
273
274         // Append bgcolor parameter
275
buffer.append(prepareBgcolor(evenNumber));
276
277
278         // Append CSS class parameter
279
buffer.append(prepareClass(evenNumber));
280
281
282         // Append "align" parameter
283
buffer.append(prepareAttribute("align", align));
284
285
286         // Append "valign" parameter
287
buffer.append(prepareAttribute("valign", valign));
288
289
290         // Append Event Handler details
291
buffer.append(prepareEventHandlers());
292
293         try
294         {
295             // Append Style details
296
buffer.append(prepareStyles());
297         }
298         catch (Exception JavaDoc e)
299         {
300             mLogger.error("Unexpected exception", e);
301         }
302     }
303
304     /**
305      * Format attribute="value" from the specified attribute & value
306      */

307     protected String JavaDoc prepareAttribute(String JavaDoc attribute, String JavaDoc value)
308     {
309         return (value == null)
310                ? "" : " " + attribute + "=" + QUOTE + value + QUOTE;
311     }
312
313     /**
314      * Format the bgcolor attribute depending on whether
315      * the row is odd or even.
316      *
317      * @param evenNumber Boolean set to true if an even numbered row
318      *
319      */

320     protected String JavaDoc prepareBgcolor(boolean evenNumber)
321     {
322         if (evenNumber)
323         {
324             return prepareAttribute("bgcolor", evenColor);
325         }
326         else
327         {
328             return prepareAttribute("bgcolor", oddColor);
329         }
330     }
331
332     /**
333      * Format the Style sheet class attribute depending on whether
334      * the row is odd or even.
335      *
336      * @param evenNumber Boolean set to true if an even numbered row
337      *
338      */

339     protected String JavaDoc prepareClass(boolean evenNumber)
340     {
341         if (evenNumber)
342         {
343             return prepareAttribute("class", evenStyleClass);
344         }
345         else
346         {
347             return prepareAttribute("class", oddStyleClass);
348         }
349     }
350
351     /**
352      * Determine the Row Number - from the IterateTag
353      */

354     protected int getRowNumber()
355     {
356         // Determine if embedded in an IterateTag
357
Tag tag = findAncestorWithClass(this, IterateTag.class);
358
359         if (tag == null)
360         {
361             return 1;
362         }
363
364         // Determine the current row number
365
IterateTag iterator = (IterateTag) tag;
366
367         // return iterator.getLengthCount() + 1;
368
return iterator.getIndex() + 1;
369     }
370
371     /**
372      * Release resources after Tag processing has finished.
373      */

374     public void release()
375     {
376         super.release();
377
378         oddColor = null;
379         evenColor = null;
380         oddStyleClass = null;
381         evenStyleClass = null;
382         align = null;
383         valign = null;
384     }
385 }
Popular Tags