001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.services.html;
066:
067: /**
068: * Table.java
069: *
070: * Copyright 1999, 2000, 2001 Jcorporate Ltd.
071: */
072:
073: import java.io.PrintWriter;
074: import java.util.Enumeration;
075: import java.util.StringTokenizer;
076:
077: /**
078: * @author Michael Nash
079: * @version $Revision: 1.9 $ $Date: 2004/11/17 20:48:18 $
080: */
081: public class Table extends HtmlElement {
082: private String this Class = (this .getClass().getName() + ".");
083: private String alignment = "center";
084: private int cellspacing = 0;
085: private int cellpadding = 0;
086: private int border = 0;
087: private String titleString = ("");
088: private String verticalAlignment = null;
089: private String caption = null;
090: private String width = null;
091:
092: /**
093: * Constructor
094: */
095: public Table() throws HtmlException {
096: super ();
097: } /* Table() */
098:
099: /**
100: * Constructor
101: *
102: * @param newName name of the table
103: */
104: public Table(String newName) throws HtmlException {
105: super (newName);
106: } /* Table(String) */
107:
108: /**
109: * Add a new element to this table.
110: *
111: * @param newElement The new row to be added to the table
112: * @throws HtmlException If the new element is invalid or not a Row
113: */
114: public synchronized void add(HtmlElement newElement)
115: throws HtmlException {
116: String myName = (this Class + "add(HtmlElement)");
117:
118: if (newElement instanceof Row) {
119: super .add(newElement);
120: } else {
121: throw new HtmlException(myName
122: + ":Can't add anything except Rows "
123: + "to Tables. Attempted to add "
124: + newElement.getName() + " to " + getName());
125: }
126: } /* add(HtmlElement) */
127:
128: /**
129: * A shortcut method to quickly add a heading row to a table
130: *
131: * @param heading A pipe-delimited string giving the labels for the
132: * headings
133: */
134: public synchronized void addHeading(String heading)
135: throws HtmlException {
136: setTitle(heading);
137: } /* addHeading(String) */
138:
139: /**
140: * Display the table
141: *
142: * @param out output stream
143: * @param depth the number of tabs to indent
144: */
145: protected synchronized void display(PrintWriter out, int depth)
146: throws HtmlException {
147:
148: /*
149:
150: if (contents.size() == 0) {
151:
152: throw new HtmlException(myName + ":Table " + getName()
153:
154: + " has no contents");
155:
156: }
157:
158: */
159: out.println();
160: this .padWithTabs(out, depth);
161: out.print("<table");
162:
163: if (cSSClass != null) {
164: out.print(" class=\"" + cSSClass + "\"");
165: }
166: if (cSSID != null) {
167: out.print(" id=\"" + cSSID + "\"");
168: }
169:
170: out.print(" border=\"" + border + "\" cellspacing=\""
171: + cellspacing + "\"");
172:
173: if (alignment != null) {
174: out.print(" align=\"" + alignment + "\"");
175: }
176: if (width != null) {
177: out.print(" width=\"" + width + "\"");
178: }
179: if (verticalAlignment == null) {
180: out.println(">");
181: } else {
182: out.println(" valign=\"" + verticalAlignment + "\">");
183: }
184: if (caption != null) {
185: this .padWithTabs(out, depth);
186: out.println("<caption class=\"jc-caption\" align=\"top\">"
187: + caption + "</caption>");
188: }
189: if (!titleString.equals("")) {
190: StringTokenizer stk = new StringTokenizer(titleString, "|");
191: this .padWithTabs(out, depth);
192: out.println("<tr class=\"jc-tabletitlerow\">");
193:
194: while (stk.hasMoreTokens()) {
195: this .padWithTabs(out, depth);
196: out.print("<th class=\"jc-tabletitle\">");
197: out.print(stk.nextToken());
198: out.println("</th>");
199: }
200:
201: out.println("</tr>");
202: } /* if there is a title */
203:
204: HtmlElement oneElement = null;
205:
206: for (Enumeration e = contents.elements(); e.hasMoreElements();) {
207: oneElement = (HtmlElement) e.nextElement();
208: oneElement.display(out, depth + 1);
209: }
210:
211: this .padWithTabs(out, depth);
212: out.println("</table>");
213: setDisplayed();
214: } /* display(PrintWriter) */
215:
216: /**
217: * @param newAlignment left|right|center
218: */
219: public synchronized void setAlignment(String newAlignment)
220: throws HtmlException {
221: String myName = (this Class + "setAlignment(String)");
222:
223: if (newAlignment == null) {
224: alignment = null;
225:
226: return;
227: }
228: if (newAlignment.equalsIgnoreCase("left")) {
229: alignment = newAlignment;
230: } else if (newAlignment.equalsIgnoreCase("right")) {
231: alignment = newAlignment;
232: } else if (newAlignment.equalsIgnoreCase("center")) {
233: alignment = newAlignment;
234: } else {
235: throw new HtmlException(myName
236: + ":Alignment must be left, right, or center for "
237: + getName());
238: }
239: } /* setAlignment(String) */
240:
241: /**
242: * @param newBorder border size
243: */
244: public synchronized void setBorder(int newBorder)
245: throws HtmlException {
246: border = newBorder;
247: } /* setBorder(int) */
248:
249: /**
250: * Set a caption for the entire table
251: *
252: * @param newCaption The caption to be set
253: */
254: public synchronized void setCaption(String newCaption) {
255: caption = newCaption;
256: } /* setCaption(String) */
257:
258: /**
259: * @param newPadding padding size
260: */
261: public synchronized void setCellPadding(int newPadding)
262: throws HtmlException {
263: cellpadding = newPadding;
264: } /* setCellSpacing(int) */
265:
266: /**
267: * @param newSpacing cell spacing size
268: */
269: public synchronized void setCellSpacing(int newSpacing)
270: throws HtmlException {
271: cellspacing = newSpacing;
272: } /* setCellSpacing(int) */
273:
274: /**
275: * Set a special title string
276: *
277: * @param newTitleString A "|"-delimited list of column headings
278: * @throws HtmlException If the title cannot be used
279: */
280: public synchronized void setTitle(String newTitleString)
281: throws HtmlException {
282: String myName = (this Class + "setTitle(String)");
283:
284: if (newTitleString == null) {
285: throw new HtmlException(myName
286: + ":Title string cannot be null");
287: }
288:
289: titleString = newTitleString;
290: } /* setTitle(String) */
291:
292: /**
293: * Set the veritical aligment of this table
294: *
295: * @param newAlignment New alignment value: Top, bottom or center
296: * @throws HtmlException If the alignment is invalid
297: */
298: public synchronized void setVerticalAlignment(String newAlignment)
299: throws HtmlException {
300: String myName = (this Class + "setVerticalAlignment(String)");
301:
302: if (newAlignment.equalsIgnoreCase("top")) {
303: verticalAlignment = newAlignment;
304: } else if (newAlignment.equalsIgnoreCase("bottom")) {
305: verticalAlignment = newAlignment;
306: } else if (newAlignment.equalsIgnoreCase("center")) {
307: verticalAlignment = newAlignment;
308: } else {
309: throw new HtmlException(myName
310: + ":Alignment must be top, bottom,"
311: + " or center for " + getName());
312: }
313: } /* setVerticalAlignment(String) */
314:
315: /**
316: * @param newWidth width of the table
317: */
318: public void setWidth(String newWidth) {
319: width = newWidth;
320: } /* setWidth(String) */
321:
322: } /* Table */
|