KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > TableTag


1 /*
2  * Copyright 2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16
17
18 package org.apache.webapp.admin;
19
20
21 import java.io.IOException JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspWriter JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
28
29
30 /**
31  * <p>JSP custom tag that renders an "instant table" control. To the user,
32  * it appears as an HTML &lt;table&gt; element
33  * This tag has the following user-settable attributes:</p>
34  * <ul>
35  * <li><strong>columns</strong> - (Integer) number of columns in the table.
36  * If not specified, one two columns will be created.</li>
37  * <li><strong>table-class</strong> - The CSS style class to be applied to the
38  * entire rendered output of the entire table, if any.</li>
39  * <li><strong>header-row-class</strong> - The CSS style class to be applied to the
40  * entire rendered output of the table header-row, if any.</li>
41  *
42  * </ul>
43  *
44  * <strong>FIXME</strong> - Internationalize the exception messages!
45  *
46  * @author Manveen Kaur
47  * @version $Revision: 1.4 $ $Date: 2004/02/27 14:59:01 $
48  */

49
50 public class TableTag extends BodyTagSupport JavaDoc {
51     
52     
53     // ----------------------------------------------------- Manifest Constants
54

55     
56     /**
57      * Attribute name used to indicate that we have generated the JavaScript
58      * function already on the current page. The data stored for this
59      * attribute is arbitrary - only its existence is relevant.
60      */

61     // protected static final String FUNCTION_TAG =
62
// "org.apache.webapp.admin.TableTag.FUNCTION_TAG";
63

64     
65     // ----------------------------------------------------- Instance Variables
66

67     
68     /**
69      * The set of labels for the rows displayed by this control.
70      */

71     protected ArrayList JavaDoc labels = new ArrayList JavaDoc();
72     
73     
74     /**
75      * The set of datas for the rows displayed by this control.
76      */

77     protected ArrayList JavaDoc datas = new ArrayList JavaDoc();
78     
79     
80     /**
81      * The set of labelStyles for the rows displayed by this control.
82      */

83     protected ArrayList JavaDoc labelStyles = new ArrayList JavaDoc();
84     
85     
86     /**
87      * The set of dataStyles for the rows displayed by this control.
88      */

89     protected ArrayList JavaDoc dataStyles = new ArrayList JavaDoc();
90     
91     /**
92      * The set of "headers" flags for rows displayed by this control.
93      */

94     protected ArrayList JavaDoc headers = new ArrayList JavaDoc();
95     
96     /**
97      * The set of styleIds for the rows displayed by this control.
98      */

99     protected ArrayList JavaDoc styleIds = new ArrayList JavaDoc();
100         
101     
102     // ------------------------------------------------------------- Properties
103

104     
105     /**
106      * The number of elements that will be displayed to the user.
107      */

108     protected int columns = 2;
109     
110     public int getColumns() {
111         return (this.columns);
112     }
113     
114     public void setColumns(int columns) {
115         this.columns = columns;
116     }
117     
118     
119     /**
120      * The CSS style class to be applied to the entire rendered output
121      * of this "instant table" object.
122      */

123     protected String JavaDoc tableStyle = null;
124     
125     public String JavaDoc getTableStyle() {
126         return (this.tableStyle);
127     }
128     
129     public void setTableStyle(String JavaDoc tableStyle) {
130         this.tableStyle = tableStyle;
131     }
132     
133     /**
134      * The CSS Style for the lines between table rows.
135      */

136     protected String JavaDoc lineStyle = null;
137     
138     public String JavaDoc getLineStyle() {
139         return (this.lineStyle);
140     }
141     
142     public void setLineStyle(String JavaDoc lineStyle) {
143         this.lineStyle = lineStyle;
144     }
145     
146     // --------------------------------------------------------- Public Methods
147

148
149     public int doStartTag() throws JspException JavaDoc {
150  
151         this.headers.clear();
152         this.labels.clear();
153         this.datas.clear();
154         this.labelStyles.clear();
155         this.dataStyles.clear();
156         this.styleIds.clear();
157         
158         return (EVAL_BODY_TAG);
159  
160      }
161
162     
163     /**
164      * Render this instant actions control.
165      *
166      * @exception JspException if a processing error occurs
167      */

168     public int doEndTag() throws JspException JavaDoc {
169         
170         JspWriter JavaDoc out = pageContext.getOut();
171         
172         try {
173             
174             // Render the beginning of this element
175
out.println();
176             out.print("<table ");
177             if (columns > 2) {
178                 out.print(" columns=\"");
179                 out.print(columns);
180                 out.print("\"");
181             }
182             if (tableStyle != null) {
183                 out.print(" class=\"");
184                 out.print(tableStyle);
185                 out.print("\"");
186                 out.print(" border=\"1\" cellspacing=\"0\" ");
187                 out.print(" cellpadding=\"0\" width=\"100%\" ");
188             }
189             out.println(">");
190             
191             
192             // Render each defined row
193
int n = labels.size();
194             for (int i = 0; i < n; i++) {
195                 String JavaDoc label = (String JavaDoc) labels.get(i);
196                 boolean header = ((Boolean JavaDoc) headers.get(i)).booleanValue();
197                 String JavaDoc data = (String JavaDoc) datas.get(i);
198                 String JavaDoc labelStyle = (String JavaDoc) labelStyles.get(i);
199                 String JavaDoc dataStyle = (String JavaDoc) dataStyles.get(i);
200                 String JavaDoc styleId = (String JavaDoc) styleIds.get(i);
201                 
202                 if (header) {
203                     out.println("<tr class=\"header-row\" >");
204                     out.println(" <th scope=\"col\" width=\"27%\"> ");
205                 
206                     out.print(" <div align=\"left\"");
207                     if (labelStyle != null)
208                         out.print( " class=\"" + labelStyle +"\"");
209                     out.print(">");
210                     if (styleId != null) {
211                         out.print("<label for=\"" + styleId + "\">");
212                     }
213                     out.print(label);
214                     if (styleId != null) {
215                         out.print("</label>");
216                     }
217                     out.println(" </div>");
218                     out.println(" </th>");
219                 
220                     out.println(" <th scope=\"col\" width=\"73%\"> ");
221                     out.print(" <div align=\"left\"" );
222                     if (dataStyle != null)
223                         out.print(" class=\"" + dataStyle + "\"");
224                     out.print(">");
225                     out.print(data);
226                     out.println(" </div>");
227                     out.print(" </th>");
228                     out.println("</tr>");
229                 } else {
230                     out.println("<tr>");
231                 
232                     out.println(" <td scope=\"row\" width=\"27%\"> ");
233                 
234                     out.print(" <div align=\"left\"");
235                     if (labelStyle != null)
236                         out.print( " class=\"" + labelStyle +"\"");
237                     out.print(">");
238                     if (styleId != null) {
239                         out.print("<label for=\"" + styleId + "\">");
240                     }
241                     out.print(label);
242                     if (styleId != null) {
243                         out.print("</label>");
244                     }
245                     out.println(" </div>");
246                     out.println(" </td>");
247                 
248                     out.println(" <td width=\"73%\"> ");
249                     out.print(" <div align=\"left\"" );
250                     if (dataStyle != null)
251                         out.print(" class=\"" + dataStyle + "\"");
252                     out.print(">");
253                     out.print(data);
254                     out.println(" </div>");
255                     out.print(" </td>");
256                     out.println("</tr>");
257                 }
258                 
259                 /*
260                 if (!header) {
261                     out.println("<tr height=\"1\">");
262                     out.println(" <td class=\""+ lineStyle + "\" colspan=\"2\">");
263                     out.println(" <img SRC=\"\" alt=\"\" width=\"1\" height=\"1\" border=\"0\">");
264                     out.println(" </td>");
265                     out.println("</tr>");
266                 }
267                  */

268             }
269             
270             // Render the end of this element
271
out.println("</table>");
272             out.println();
273             
274         } catch (IOException JavaDoc e) {
275             throw new JspException JavaDoc(e);
276         }
277         
278         return (EVAL_PAGE);
279         
280     }
281     
282     
283     /**
284      * Release all state information set by this tag.
285      */

286     public void release() {
287         
288         this.headers.clear();
289         this.labels.clear();
290         this.datas.clear();
291         this.labelStyles.clear();
292         this.dataStyles.clear();
293         this.columns = 2;
294         this.tableStyle = null;
295         this.lineStyle = null;
296         this.styleIds.clear();
297         
298     }
299     
300     
301     // -------------------------------------------------------- Package Methods
302

303     
304     /**
305      * Add a new Action to the set that will be rendered by this control.
306      *
307      * @param label Localized label visible to the user
308      * @param selected Initial selected state of this option
309      * @param url URL to which control should be transferred if selected
310      */

311     
312     void addRow(boolean header, String JavaDoc label, String JavaDoc data,
313     String JavaDoc labelStyle, String JavaDoc dataStyle, String JavaDoc styleId) {
314         
315         headers.add(new Boolean JavaDoc(header));
316         labels.add(label);
317         datas.add(data);
318         labelStyles.add(labelStyle);
319         dataStyles.add(dataStyle);
320         styleIds.add(styleId);
321         
322     }
323     
324     // ------------------------------------------------------ Protected Methods
325

326     
327 }
328
Popular Tags