Source code: com/amarda/framework/taglib/RowTag.java
1 package com.amarda.framework.taglib;
2
3
4 import java.io.IOException;
5 import javax.servlet.jsp.JspWriter;
6 import javax.servlet.jsp.JspException;
7 import javax.servlet.jsp.tagext.TagSupport;
8 import javax.servlet.jsp.tagext.Tag;
9
10 import org.apache.struts.taglib.logic.IterateTag;
11 /**
12 * <p>This tag generates table rows (i.e. <tr>....</tr> elements) with the
13 * background color set differently for alternating odd and even rows. This tag only operates
14 * properly if embedded in an IterateTag.</p>
15 *
16 * <p>The following parameters can be specified for this Tag:</p>
17 * <ul>
18 * <li><code>oddColor </code> - The color for Odd numbered rows
19 * <li><code>evenColor</code> - The color for Even numbered rows
20 * <li><code>oddStyleClass</code> - The style class for Odd numbered rows
21 * <li><code>evenStyleClass</code> - The style class for Even numbered rows
22 * <li><code>align</code> - The alignment for the table row
23 * <li><code>valign</code> - The vertical alignment for the table row
24 * </ul>
25 *
26 * <p>Additionally this tag inherits the Event Handler and Style attributes
27 * from the BaseHandlerTag which can also be specified</p>
28 *
29 * @author Amarda Business Systems Ltd
30 * @version 1.0
31 */
32
33 public class RowTag extends org.apache.struts.taglib.html.BaseHandlerTag {
34
35 // ----------------------------------------------------- Instance Variables
36
37 protected final static String QUOTE = "\"";
38
39 /**
40 * Color of Odd rows in a table
41 */
42 protected String oddColor = null;
43
44 /**
45 * Return the color of Odd rows
46 */
47 public String getOddColor() {
48 return (this.oddColor);
49 }
50
51 /**
52 * Set the color of Odd rows
53 *
54 * @param color HTML bgcolor value for Odd rows
55 */
56 public void setOddColor(String color) {
57 this.oddColor = color;
58 }
59
60 /**
61 * Color of Even rows in a table
62 */
63 protected String evenColor = null;
64
65 /**
66 * Return the color of Even rows
67 */
68 public String getEvenColor() {
69 return (this.evenColor);
70 }
71
72 /**
73 * Set the color of Even rows
74 *
75 * @param color HTML bgcolor value for Even rows
76 */
77 public void setEvenColor(String color) {
78 this.evenColor = color;
79 }
80
81 /**
82 * StyleClass of Odd rows in a table
83 */
84 protected String oddStyleClass = null;
85
86 /**
87 * Return the Style Class of Odd rows
88 */
89 public String getOddStyleClass() {
90 return (this.oddStyleClass);
91 }
92
93 /**
94 * Set the Style Class of Odd rows
95 *
96 * @param styleClass HTML Style Class value for Odd rows
97 */
98 public void setOddStyleClass(String styleClass) {
99 this.oddStyleClass = styleClass;
100 }
101
102 /**
103 * Style Class of Even rows in a table
104 */
105 protected String evenStyleClass = null;
106
107 /**
108 * Return the Style Class of Even rows
109 */
110 public String getEvenStyleClass() {
111 return (this.evenStyleClass);
112 }
113
114 /**
115 * Set the styleClass of Even rows
116 *
117 * @param styleClass HTML Style Class value for Even rows
118 */
119 public void setEvenStyleClass(String styleClass) {
120 this.evenStyleClass = styleClass;
121 }
122
123 /**
124 * Alignment of the table row
125 */
126 protected String align = null;
127
128 /**
129 * Return the Alignment
130 */
131 public String getAlign() {
132 return (this.align);
133 }
134
135 /**
136 * Set the Alignment
137 *
138 * @param Value for Alignment
139 */
140
141 public void setAlign(String align) {
142 this.align = align;
143 }
144 /**
145 * Vertical Alignment of the table row
146 */
147 protected String valign = null;
148
149 /**
150 * Return the Vertical Alignment
151 */
152 public String getValign() {
153 return (this.valign);
154 }
155
156 /**
157 * Set the Vertical Alignment
158 *
159 * @param Value for Vertical Alignment
160 */
161
162 public void setValign(String valign) {
163 this.valign = valign;
164 }
165
166
167 // ----------------------------------------------------- Public Methods
168
169 /**
170 * Start of Tag processing
171 *
172 * @exception JspException if a JSP exception occurs
173 */
174 public int doStartTag() throws JspException {
175
176 // Continue processing this page
177 return (EVAL_BODY_TAG);
178
179 }
180 /**
181 * End of Tag Processing
182 *
183 * @exception JspException if a JSP exception occurs
184 */
185 public int doEndTag() throws JspException {
186
187 StringBuffer buffer = new StringBuffer();
188
189 // Create a <tr> element based on the parameters
190 buffer.append("<tr");
191
192 // Prepare this HTML elements attributes
193 prepareAttributes(buffer);
194
195 buffer.append(">");
196
197 // Add Body Content
198 if (bodyContent != null)
199 buffer.append(bodyContent.getString().trim());
200
201 buffer.append("</tr>");
202
203 // Render this element to our writer
204 JspWriter writer = pageContext.getOut();
205 try {
206 writer.print(buffer.toString());
207 }
208 catch (IOException e) {
209 throw new JspException("Exception in RowTag doEndTag():"+e.toString());
210 }
211
212 return EVAL_PAGE;
213 }
214 /**
215 * Prepare the attributes of the HTML element
216 */
217 protected void prepareAttributes(StringBuffer buffer) {
218
219 // Determine if it is an "Odd" or "Even" row
220 boolean evenNumber = (getRowNumber() % 2) == 0 ? true : false;
221
222 // Append bgcolor parameter
223 buffer.append(prepareBgcolor(evenNumber));
224
225 // Append CSS class parameter
226 buffer.append(prepareClass(evenNumber));
227
228 // Append "align" parameter
229 buffer.append(prepareAttribute("align", align));
230
231 // Append "valign" parameter
232 buffer.append(prepareAttribute("valign", valign));
233
234 // Append Event Handler details
235 buffer.append(prepareEventHandlers());
236
237 // Append Style details
238 buffer.append(prepareStyles());
239
240 }
241
242 /**
243 * Format attribute="value" from the specified attribute & value
244 */
245 protected String prepareAttribute(String attribute, String value) {
246
247 return value == null ? "" : " " + attribute + "=" + QUOTE + value + QUOTE;
248
249 }
250
251 /**
252 * Format the bgcolor attribute depending on whether
253 * the row is odd or even.
254 *
255 * @param evenNumber Boolean set to true if an even numbered row
256 *
257 */
258 protected String prepareBgcolor(boolean evenNumber) {
259
260 if (evenNumber)
261 return prepareAttribute("bgcolor", evenColor);
262 else
263 return prepareAttribute("bgcolor", oddColor);
264
265 }
266
267 /**
268 * Format the Style sheet class attribute depending on whether
269 * the row is odd or even.
270 *
271 * @param evenNumber Boolean set to true if an even numbered row
272 *
273 */
274 protected String prepareClass(boolean evenNumber) {
275
276 if (evenNumber)
277 return prepareAttribute("class", evenStyleClass);
278 else
279 return prepareAttribute("class", oddStyleClass);
280
281
282 }
283
284 /**
285 * Determine the Row Number - from the IterateTag
286 */
287 protected int getRowNumber() {
288
289 // Determine if embedded in an IterateTag
290 Tag tag = findAncestorWithClass(this, IterateTag.class);
291 if (tag == null)
292 return 1;
293
294 // Determine the current row number
295 IterateTag iterator = (IterateTag)tag;
296 // return iterator.getLengthCount() + 1;
297 return iterator.getIndex() + 1;
298 }
299
300 /**
301 * Release resources after Tag processing has finished.
302 */
303 public void release() {
304
305 super.release();
306
307 oddColor = null;
308 evenColor = null;
309 oddStyleClass = null;
310 evenStyleClass = null;
311 align = null;
312 valign = null;
313
314 }
315 }