KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.jcorporate.expresso.core.misc.StringUtil;
68
69 import java.io.PrintWriter JavaDoc;
70 import java.util.Enumeration JavaDoc;
71
72
73 /**
74  * Cell - The Cell of an HTML table
75  *
76  * @author Michael P. Nash
77  * @version $Revision: 1.8 $ $Date: 2004/11/17 20:48:18 $
78  */

79 public class Cell
80         extends HtmlElement {
81     private String JavaDoc thisClass = (this.getClass().getName() + ".");
82     private String JavaDoc alignment = null;
83     private int cellspacing = 0;
84     private int border = 0;
85     private String JavaDoc verticalAlignment = null;
86     private boolean noWrap = true;
87     private int colSpan = 0;
88     private int rowSpan = 0;
89     private String JavaDoc width = null;
90     private boolean isHeaderCell = false;
91     private String JavaDoc bgColor = null;
92     private String JavaDoc fgColor = null;
93
94     /**
95      * Constructor
96      *
97      * @throws HtmlException If the super constructor fails
98      */

99     public Cell()
100             throws HtmlException {
101         super();
102     } /* Cell() */
103
104     /**
105      * Constructor
106      * Create a cell with the given element as it's contents
107      *
108      * @param newElement Another HTML element to appear in the cell
109      * @throws HtmlException If the parameter is invalid
110      */

111     public Cell(HtmlElement newElement)
112             throws HtmlException {
113         super(newElement);
114     } /* Cell(HtmlElement) */
115
116     /**
117      * Constructor with text contents for the new cell
118      * Create a new cell with the given text as its contents
119      *
120      * @param contentText The text string which becomes the contents
121      * of this new cell
122      * @throws HtmlException If the parameter is invalid
123      */

124     public Cell(String JavaDoc contentText)
125             throws HtmlException {
126         super(contentText);
127     } /* Cell(String) */
128
129     /**
130      * Display the Cell
131      *
132      * @param out PrintWriter to the client
133      * @param depth the number of tabs to indent
134      * @throws HtmlException If an error occurs displaying the cell or it's
135      * contents
136      */

137     protected synchronized void display(PrintWriter JavaDoc out, int depth)
138             throws HtmlException {
139         String JavaDoc myName = (thisClass + "display(PrintWriter)");
140
141         if (contents.size() == 0) {
142             throw new HtmlException(myName + ":Cell '" + getName() +
143                     "' has no contents");
144         }
145
146         this.padWithTabs(out, depth);
147
148         if (isHeaderCell) {
149             out.print("<th");
150         } else {
151             out.print("<td");
152         }
153         if (cSSClass != null) {
154             out.print(" class=\"" + cSSClass + "\"");
155         }
156         if (cSSID != null) {
157             out.print(" ID=\"" + cSSID + "\"");
158         }
159         if (!StringUtil.notNull(fgColor).equals("")) {
160             out.print(" color=\"" + fgColor + "\"");
161         }
162         if (!StringUtil.notNull(bgColor).equals("")) {
163             out.print(" bgcolor=\"" + bgColor + "\"");
164         }
165         if (alignment != null) {
166             out.print(" align=\"" + alignment + "\"");
167         }
168         if (verticalAlignment != null) {
169             out.print(" valign=\"" + verticalAlignment + "\"");
170         }
171         if (colSpan > 0) {
172             out.print(" colspan=\"" + colSpan + "\"");
173         }
174         if (rowSpan > 0) {
175             out.print(" rowspan=\"" + rowSpan + "\"");
176         }
177         if (width != null) {
178             out.print(" width=\"" + width + "\"");
179         }
180         if (noWrap) {
181             out.print(" nowrap");
182         }
183
184         out.println(">");
185
186         HtmlElement oneElement = null;
187
188         for (Enumeration JavaDoc e = contents.elements(); e.hasMoreElements();) {
189             oneElement = (HtmlElement) e.nextElement();
190             oneElement.display(out, depth + 1);
191         }
192         if (isHeaderCell) {
193             out.println();
194             this.padWithTabs(out, depth);
195             out.println("</th>");
196         } else {
197             out.println();
198             this.padWithTabs(out, depth);
199             out.println("</td>");
200         }
201
202         setDisplayed();
203     } /* display(PrintWriter) */
204
205
206     /**
207      * Set the horizontal alignment of the contents of this cell
208      *
209      * @param newAlignment New alignment as a string. Must be "left",
210      * "right" or "center"
211      * @throws HtmlException If the parameter is invalid
212      */

213     public synchronized void setAlignment(String JavaDoc newAlignment)
214             throws HtmlException {
215         String JavaDoc myName = (thisClass + "setAlignment(String)");
216
217         if (newAlignment.equalsIgnoreCase("left")) {
218             alignment = newAlignment;
219         } else if (newAlignment.equalsIgnoreCase("right")) {
220             alignment = newAlignment;
221         } else if (newAlignment.equalsIgnoreCase("center")) {
222             alignment = newAlignment;
223         } else {
224             throw new HtmlException(myName +
225                     ":Alignment must be left, right, " +
226                     "or center for '" + getName() + "'");
227         }
228     } /* setAlignment(String) */
229
230
231     /**
232      * Set the background color of the cell
233      *
234      * @param newColor Color to set the background of this cell
235      * @throws HtmlException If the parameter is invalid
236      */

237     public void setBGColor(String JavaDoc newColor)
238             throws HtmlException {
239         bgColor = newColor;
240     } /* setBGColor(String) */
241
242
243     /**
244      * Set the border for this cell
245      *
246      * @param newBorder New Border value for this Cell
247      * @throws HtmlException If the parameter is invalid
248      */

249     public synchronized void setBorder(int newBorder)
250             throws HtmlException {
251         border = newBorder;
252     } /* setBorder(int) */
253
254
255     /**
256      * Set the cell spacing of this individual cell
257      *
258      * @param newSpacing The new spacing for this cell
259      * @throws HtmlException If the parameter is invalid
260      */

261     public synchronized void setCellSpacing(int newSpacing)
262             throws HtmlException {
263         cellspacing = newSpacing;
264     } /* setCellSpacing(int) */
265
266
267     /**
268      * Set how many columns in the table this cell spans. Default is 0
269      *
270      * @param newSpan Number of columns to span
271      * @throws HtmlException If the parameter is invalid
272      */

273     public void setColSpan(int newSpan)
274             throws HtmlException {
275         colSpan = newSpan;
276     } /* setColSpan(int) */
277
278
279     /**
280      * Set the foreground color of the cell
281      *
282      * @param newColor Color to set the foreground of this cell
283      * @throws HtmlException If the parameter is invalid
284      */

285     public void setFGColor(String JavaDoc newColor)
286             throws HtmlException {
287         fgColor = newColor;
288     } /* setFGColor(String) */
289
290
291     /**
292      * Set the cell to be a header cell
293      *
294      * @param state set to true if header
295      */

296     public void setHeaderCell(boolean state) {
297         isHeaderCell = state;
298     } /* setHeaderCell(boolean) */
299
300     /**
301      * Tell this cell whether or not to wrap it's contents
302      *
303      * @param newWrap True to wrap contents, else false
304      * @throws HtmlException If the parameter is invalid
305      */

306     public void setNoWrap(boolean newWrap)
307             throws HtmlException {
308         noWrap = newWrap;
309     } /* setNoWrap(boolean) */
310
311
312     /**
313      * Set how many rows this Cell spans. Default 0
314      *
315      * @param newSpan Number of rows to span
316      * @throws HtmlException If the parameter is invalid
317      */

318     public void setRowSpan(int newSpan)
319             throws HtmlException {
320         rowSpan = newSpan;
321     } /* setRowSpan(int) */
322
323
324     /**
325      * Set the vertical alignment of the contents of the cell
326      *
327      * @param newAlignment Vertical alignment value
328      * @throws HtmlException If the parameter is invalid
329      */

330     public synchronized void setVerticalAlignment(String JavaDoc newAlignment)
331             throws HtmlException {
332         String JavaDoc myName = (thisClass + "setVerticalAlignment(String)");
333
334         if (newAlignment.equalsIgnoreCase("top")) {
335             verticalAlignment = newAlignment;
336         } else if (newAlignment.equalsIgnoreCase("bottom")) {
337             verticalAlignment = newAlignment;
338         } else if (newAlignment.equalsIgnoreCase("center")) {
339             verticalAlignment = newAlignment;
340         } else {
341             throw new HtmlException(myName +
342                     ":Verical alignment must be top, " +
343                     "bottom, or center for " + getName());
344         }
345     } /* setVerticalAlignment(String) */
346
347
348     /**
349      * @param newWidth The width of the cell
350      */

351     public void setWidth(String JavaDoc newWidth)
352             throws HtmlException {
353         width = newWidth;
354     } /* setWidth(String) */
355
356
357 } /* Cell */
358
359 /* Cell */
Popular Tags