KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > TableTag


1 package fr.improve.struts.taglib.layout;
2
3 import java.util.Iterator JavaDoc;
4
5 import javax.servlet.jsp.JspException JavaDoc;
6 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
7
8 import org.apache.struts.taglib.html.Constants;
9
10 import fr.improve.struts.taglib.layout.util.LayoutUtils;
11 import fr.improve.struts.taglib.layout.util.TagUtils;
12
13 /**
14  * Iterate over the specified collection and render the body of the tag in different cells. <br>
15  * The number of cell per lines can be fixed by the user. <br>
16  * @author: Jean-Noël Ribette
17  */

18 public class TableTag extends BodyTagSupport JavaDoc {
19     protected String JavaDoc name = Constants.BEAN_KEY;
20     protected String JavaDoc property;
21     protected String JavaDoc id;
22     protected String JavaDoc indexId;
23     
24     protected String JavaDoc width;
25     protected String JavaDoc align;
26     protected int col = 2;
27
28     protected Iterator JavaDoc iterator = null;
29     protected int index = 0;
30     public int doAfterBody() throws JspException JavaDoc {
31
32         // Render the output from this iteration to the output stream
33
if (bodyContent != null) {
34             TagUtils.writePrevious(pageContext, "<td>");
35             TagUtils.writePrevious(pageContext, bodyContent.getString());
36             TagUtils.writePrevious(pageContext, "</td>");
37             if (index % col == 0) TagUtils.writePrevious(pageContext, "</tr><tr>");
38             bodyContent.clearBody();
39         }
40
41         // Decide whether to iterate or quit
42
if (iterator.hasNext()) {
43             Object JavaDoc element = iterator.next();
44             if (element == null)
45                 pageContext.removeAttribute(id);
46             else
47                 pageContext.setAttribute(id, element);
48             index++;
49             if (indexId != null)
50                 pageContext.setAttribute(indexId, new Integer JavaDoc(index));
51             return (EVAL_BODY_TAG);
52         } else
53             return (SKIP_BODY);
54
55     }
56     public int doEndTag() throws JspException JavaDoc {
57         TagUtils.write(pageContext,"</tr></table>");
58         return EVAL_PAGE;
59     }
60     public int doStartTag() throws JspException JavaDoc {
61         Object JavaDoc collection = LayoutUtils.getBeanFromPageContext(pageContext, name, property);
62         iterator = LayoutUtils.getIterator(collection);
63
64         if (iterator.hasNext()) {
65             Object JavaDoc element = iterator.next();
66             if (element == null)
67                 pageContext.removeAttribute(id);
68             else
69                 pageContext.setAttribute(id, element);
70             index++;
71             if (indexId != null)
72                 pageContext.setAttribute(indexId, new Integer JavaDoc(index));
73
74             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
75             sb.append("<table border=\"0\"");
76             if (width!=null) {
77                 sb.append(" width=\"");
78                 sb.append(width);
79                 sb.append("\"");
80             }
81             if (align!=null) {
82                 sb.append(" align=\"");
83                 sb.append(align);
84                 sb.append("\"");
85             }
86             sb.append("><tr>");
87             TagUtils.write(pageContext,sb.toString());
88             return EVAL_BODY_TAG;
89         }
90         return SKIP_BODY;
91     }
92 /**
93  * Creation date: (03/07/01 15:15:15)
94  * @return java.lang.String
95  */

96 public java.lang.String JavaDoc getAlign() {
97     return align;
98 }
99 /**
100  * Creation date: (03/07/01 15:15:15)
101  * @return java.lang.String
102  */

103 public java.lang.String JavaDoc getId() {
104     return id;
105 }
106 /**
107  * Creation date: (03/07/01 15:15:15)
108  * @return java.lang.String
109  */

110 public java.lang.String JavaDoc getIndexId() {
111     return indexId;
112 }
113 /**
114  * Creation date: (03/07/01 15:15:15)
115  * @return java.lang.String
116  */

117 public java.lang.String JavaDoc getName() {
118     return name;
119 }
120 /**
121  * Creation date: (03/07/01 15:15:15)
122  * @return java.lang.String
123  */

124 public java.lang.String JavaDoc getProperty() {
125     return property;
126 }
127 /**
128  * Creation date: (03/07/01 15:15:15)
129  * @return java.lang.String
130  */

131 public java.lang.String JavaDoc getWidth() {
132     return width;
133 }
134     public void release() {
135         name = Constants.BEAN_KEY;
136         property = null;
137         id = null;
138         indexId = null;
139
140         width = null;
141         align = null;
142         col = 2;
143
144         iterator = null;
145         index = 0;
146     }
147 /**
148  * Creation date: (03/07/01 15:15:15)
149  * @param newAlign java.lang.String
150  */

151 public void setAlign(java.lang.String JavaDoc newAlign) {
152     align = newAlign;
153 }
154     public void setCol(String JavaDoc col) throws JspException JavaDoc {
155         try {
156             this.col = Integer.parseInt(col);
157             if (this.col<1) throw new Exception JavaDoc();
158         } catch (Exception JavaDoc e) {
159             throw new JspException JavaDoc("Invalid attribute for TableTag: row most be a positive integer");
160         }
161             
162     }
163 /**
164  * Creation date: (03/07/01 15:15:15)
165  * @param newId java.lang.String
166  */

167 public void setId(java.lang.String JavaDoc newId) {
168     id = newId;
169 }
170 /**
171  * Creation date: (03/07/01 15:15:15)
172  * @param newIndexId java.lang.String
173  */

174 public void setIndexId(java.lang.String JavaDoc newIndexId) {
175     indexId = newIndexId;
176 }
177 /**
178  * Creation date: (03/07/01 15:15:15)
179  * @param newName java.lang.String
180  */

181 public void setName(java.lang.String JavaDoc newName) {
182     name = newName;
183 }
184 /**
185  * Creation date: (03/07/01 15:15:15)
186  * @param newProperty java.lang.String
187  */

188 public void setProperty(java.lang.String JavaDoc newProperty) {
189     property = newProperty;
190 }
191 /**
192  * Creation date: (03/07/01 15:15:15)
193  * @param newWidth java.lang.String
194  */

195 public void setWidth(java.lang.String JavaDoc newWidth) {
196     width = newWidth;
197 }
198 }
199
Popular Tags