KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > html > Table


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.html;
66
67 /**
68  * Table.java
69  *
70  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
71  */

72
73 import java.io.PrintWriter JavaDoc;
74 import java.util.Enumeration JavaDoc;
75 import java.util.StringTokenizer JavaDoc;
76
77
78 /**
79  * @author Michael Nash
80  * @version $Revision: 1.9 $ $Date: 2004/11/17 20:48:18 $
81  */

82 public class Table
83         extends HtmlElement {
84     private String JavaDoc thisClass = (this.getClass().getName() + ".");
85     private String JavaDoc alignment = "center";
86     private int cellspacing = 0;
87     private int cellpadding = 0;
88     private int border = 0;
89     private String JavaDoc titleString = ("");
90     private String JavaDoc verticalAlignment = null;
91     private String JavaDoc caption = null;
92     private String JavaDoc width = null;
93
94     /**
95      * Constructor
96      */

97     public Table()
98             throws HtmlException {
99         super();
100     } /* Table() */
101
102     /**
103      * Constructor
104      *
105      * @param newName name of the table
106      */

107     public Table(String JavaDoc newName)
108             throws HtmlException {
109         super(newName);
110     } /* Table(String) */
111
112     /**
113      * Add a new element to this table.
114      *
115      * @param newElement The new row to be added to the table
116      * @throws HtmlException If the new element is invalid or not a Row
117      */

118     public synchronized void add(HtmlElement newElement)
119             throws HtmlException {
120         String JavaDoc myName = (thisClass + "add(HtmlElement)");
121
122         if (newElement instanceof Row) {
123             super.add(newElement);
124         } else {
125             throw new HtmlException(myName +
126                     ":Can't add anything except Rows " +
127                     "to Tables. Attempted to add " +
128                     newElement.getName() + " to " + getName());
129         }
130     } /* add(HtmlElement) */
131
132
133     /**
134      * A shortcut method to quickly add a heading row to a table
135      *
136      * @param heading A pipe-delimited string giving the labels for the
137      * headings
138      */

139     public synchronized void addHeading(String JavaDoc heading)
140             throws HtmlException {
141         setTitle(heading);
142     } /* addHeading(String) */
143
144
145     /**
146      * Display the table
147      *
148      * @param out output stream
149      * @param depth the number of tabs to indent
150      */

151     protected synchronized void display(PrintWriter JavaDoc out, int depth)
152             throws HtmlException {
153
154         /*
155
156         if (contents.size() == 0) {
157
158             throw new HtmlException(myName + ":Table " + getName()
159
160                 + " has no contents");
161
162         }
163
164 */

165         out.println();
166         this.padWithTabs(out, depth);
167         out.print("<table");
168
169         if (cSSClass != null) {
170             out.print(" class=\"" + cSSClass + "\"");
171         }
172         if (cSSID != null) {
173             out.print(" id=\"" + cSSID + "\"");
174         }
175
176         out.print(" border=\"" + border + "\" cellspacing=\"" + cellspacing +
177                 "\"");
178
179         if (alignment != null) {
180             out.print(" align=\"" + alignment + "\"");
181         }
182         if (width != null) {
183             out.print(" width=\"" + width + "\"");
184         }
185         if (verticalAlignment == null) {
186             out.println(">");
187         } else {
188             out.println(" valign=\"" + verticalAlignment + "\">");
189         }
190         if (caption != null) {
191             this.padWithTabs(out, depth);
192             out.println("<caption class=\"jc-caption\" align=\"top\">" +
193                     caption + "</caption>");
194         }
195         if (!titleString.equals("")) {
196             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(titleString, "|");
197             this.padWithTabs(out, depth);
198             out.println("<tr class=\"jc-tabletitlerow\">");
199
200             while (stk.hasMoreTokens()) {
201                 this.padWithTabs(out, depth);
202                 out.print("<th class=\"jc-tabletitle\">");
203                 out.print(stk.nextToken());
204                 out.println("</th>");
205             }
206
207             out.println("</tr>");
208         } /* if there is a title */
209
210
211         HtmlElement oneElement = null;
212
213         for (Enumeration JavaDoc e = contents.elements(); e.hasMoreElements();) {
214             oneElement = (HtmlElement) e.nextElement();
215             oneElement.display(out, depth + 1);
216         }
217
218         this.padWithTabs(out, depth);
219         out.println("</table>");
220         setDisplayed();
221     } /* display(PrintWriter) */
222
223
224     /**
225      * @param newAlignment left|right|center
226      */

227     public synchronized void setAlignment(String JavaDoc newAlignment)
228             throws HtmlException {
229         String JavaDoc myName = (thisClass + "setAlignment(String)");
230
231         if (newAlignment == null) {
232             alignment = null;
233
234             return;
235         }
236         if (newAlignment.equalsIgnoreCase("left")) {
237             alignment = newAlignment;
238         } else if (newAlignment.equalsIgnoreCase("right")) {
239             alignment = newAlignment;
240         } else if (newAlignment.equalsIgnoreCase("center")) {
241             alignment = newAlignment;
242         } else {
243             throw new HtmlException(myName +
244                     ":Alignment must be left, right, or center for " +
245                     getName());
246         }
247     } /* setAlignment(String) */
248
249
250     /**
251      * @param newBorder border size
252      */

253     public synchronized void setBorder(int newBorder)
254             throws HtmlException {
255         border = newBorder;
256     } /* setBorder(int) */
257
258
259     /**
260      * Set a caption for the entire table
261      *
262      * @param newCaption The caption to be set
263      */

264     public synchronized void setCaption(String JavaDoc newCaption) {
265         caption = newCaption;
266     } /* setCaption(String) */
267
268     /**
269      * @param newPadding padding size
270      */

271     public synchronized void setCellPadding(int newPadding)
272             throws HtmlException {
273         cellpadding = newPadding;
274     } /* setCellSpacing(int) */
275
276
277     /**
278      * @param newSpacing cell spacing size
279      */

280     public synchronized void setCellSpacing(int newSpacing)
281             throws HtmlException {
282         cellspacing = newSpacing;
283     } /* setCellSpacing(int) */
284
285
286     /**
287      * Set a special title string
288      *
289      * @param newTitleString A "|"-delimited list of column headings
290      * @throws HtmlException If the title cannot be used
291      */

292     public synchronized void setTitle(String JavaDoc newTitleString)
293             throws HtmlException {
294         String JavaDoc myName = (thisClass + "setTitle(String)");
295
296         if (newTitleString == null) {
297             throw new HtmlException(myName + ":Title string cannot be null");
298         }
299
300         titleString = newTitleString;
301     } /* setTitle(String) */
302
303
304     /**
305      * Set the veritical aligment of this table
306      *
307      * @param newAlignment New alignment value: Top, bottom or center
308      * @throws HtmlException If the alignment is invalid
309      */

310     public synchronized void setVerticalAlignment(String JavaDoc newAlignment)
311             throws HtmlException {
312         String JavaDoc myName = (thisClass + "setVerticalAlignment(String)");
313
314         if (newAlignment.equalsIgnoreCase("top")) {
315             verticalAlignment = newAlignment;
316         } else if (newAlignment.equalsIgnoreCase("bottom")) {
317             verticalAlignment = newAlignment;
318         } else if (newAlignment.equalsIgnoreCase("center")) {
319             verticalAlignment = newAlignment;
320         } else {
321             throw new HtmlException(myName +
322                     ":Alignment must be top, bottom," +
323                     " or center for " + getName());
324         }
325     } /* setVerticalAlignment(String) */
326
327
328     /**
329      * @param newWidth width of the table
330      */

331     public void setWidth(String JavaDoc newWidth) {
332         width = newWidth;
333     } /* setWidth(String) */
334
335 } /* Table */
336
Popular Tags