KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > sqltable > EvenOddTag


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.sqltable;
14
15 import java.io.IOException JavaDoc;
16
17 import javax.servlet.jsp.JspException JavaDoc;
18 import javax.servlet.jsp.jstl.core.LoopTag;
19 import javax.servlet.jsp.jstl.core.LoopTagStatus;
20 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
21
22 import org.apache.log4j.Logger;
23
24 /**
25  * appends "even" or "odd" depending on the index of the sorrounding
26  * loop tag.
27  * @author av
28  */

29 public class EvenOddTag extends TagSupport JavaDoc {
30   String JavaDoc clazz;
31   String JavaDoc even;
32   String JavaDoc odd;
33
34   private static final Logger logger = Logger.getLogger(EvenOddTag.class);
35
36   public int doStartTag() throws JspException JavaDoc {
37     LoopTag tag = (LoopTag) super.findAncestorWithClass(this, LoopTag.class);
38     if (tag == null)
39       throw new JspException JavaDoc("must be nested in a loop tag");
40       
41     LoopTagStatus status = tag.getLoopStatus();
42     String JavaDoc s;
43     if (status.getCount() % 2 == 0)
44       s = (clazz == null) ? even : clazz + "-even";
45     else
46       s = (clazz == null) ? odd : clazz + "-odd";
47     try {
48       pageContext.getOut().print(s);
49     } catch (IOException JavaDoc e) {
50       logger.error(null, e);
51     }
52
53     return super.doStartTag();
54   }
55
56   public void setClazz(String JavaDoc clazz) {
57     this.clazz = clazz;
58   }
59
60   public void setEven(String JavaDoc even) {
61     this.even = even;
62   }
63   public void setOdd(String JavaDoc odd) {
64     this.odd = odd;
65   }
66 }
67
Popular Tags