KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > RtfTable


1 /**
2  * $Id: RtfTable.java 2748 2007-05-12 15:11:48Z blowagie $
3  * $Name$
4  *
5  * Copyright 2001, 2002 by Mark Hall
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.rtf;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.util.ArrayList JavaDoc;
56 import java.util.Iterator JavaDoc;
57
58 import com.lowagie.text.DocumentException;
59 import com.lowagie.text.Element;
60 import com.lowagie.text.Row;
61 import com.lowagie.text.Table;
62
63 /**
64  * A Helper Class for the <CODE>RtfWriter</CODE>.
65  * <P>
66  * Do not use it directly, except if you want to write a <CODE>DocumentListener</CODE> for Rtf
67  *
68  * ONLY FOR USE WITH THE RtfWriter NOT with the RtfWriter2.
69  *
70  * Parts of this Class were contributed by Steffen Stundzig. Many thanks for the
71  * improvements.
72  * Updates Benoit Wiart
73  * @deprecated Please move to the RtfWriter2 and associated classes.
74  */

75 public class RtfTable {
76     /** Stores the different rows. */
77     private ArrayList JavaDoc rowsList = new ArrayList JavaDoc();
78     /** Stores the RtfWriter, which created this RtfTable. */
79     private RtfWriter writer = null;
80     /** Stores the Table, which this RtfTable is based on. */
81     private Table origTable = null;
82
83
84
85     /**
86      * Create a new <code>RtfTable</code>.
87      *
88      * @param writer The <code>RtfWriter</code> that created this Table
89      */

90     public RtfTable(RtfWriter writer) {
91         super();
92         this.writer = writer;
93     }
94
95     /**
96      * Import a <CODE>Table</CODE> into the <CODE>RtfTable</CODE>.
97      * <P>
98      * @param table A <code>Table</code> specifying the <code>Table</code> to be imported
99      * @param pageWidth An <code>int</code> specifying the page width
100      * @return true if importing the table succeeded
101      */

102     public boolean importTable(Table table, int pageWidth) {
103         origTable = table;
104         // All Cells are pregenerated first, so that cell and rowspanning work
105
Iterator JavaDoc rows = table.iterator();
106         Row row = null;
107
108         int tableWidth = (int) table.getWidth();
109         int cellpadding = (int) (table.getPadding() * RtfWriter.TWIPSFACTOR);
110         int cellspacing = (int) (table.getSpacing() * RtfWriter.TWIPSFACTOR);
111         float[] propWidths = table.getProportionalWidths();
112
113         int borders = table.getBorder();
114         java.awt.Color JavaDoc borderColor = table.getBorderColor();
115         float borderWidth = table.getBorderWidth();
116
117         for (int i = 0; i < table.size(); i++) {
118             RtfRow rtfRow = new RtfRow(writer, this);
119             rtfRow.pregenerateRows(table.getColumns());
120             rowsList.add(rtfRow);
121         }
122         int i = 0;
123         while (rows.hasNext()) {
124             row = (Row) rows.next();
125             row.setHorizontalAlignment(table.getAlignment());
126             RtfRow rtfRow = (RtfRow) rowsList.get(i);
127             rtfRow.importRow(row, propWidths, tableWidth, pageWidth, cellpadding, cellspacing, borders, borderColor, borderWidth, i);
128             i++;
129         }
130         return true;
131     }
132
133     /**
134      * Output the content of the <CODE>RtfTable</CODE> to an OutputStream.
135      *
136      * @param os The <code>OutputStream</code> that the content of the <code>RtfTable</code> is to be written to
137      * @return true if writing the table succeeded
138      * @throws DocumentException
139      * @throws IOException
140      */

141     public boolean writeTable(ByteArrayOutputStream JavaDoc os) throws DocumentException, IOException JavaDoc {
142         
143         if(!this.writer.writingHeaderFooter()) {
144             // Added by Benoit Wiart (b.wiart@proxiad.com)
145
// Add a new line before each table
146
os.write(RtfWriter.escape);
147             os.write(RtfWriter.paragraph);
148         }
149             
150         int size = rowsList.size();
151         for (int i = 0; i < size; i++) {
152             RtfRow row = (RtfRow) rowsList.get(i);
153             row.writeRow(os, i, origTable);
154             os.write((byte) '\n');
155         }
156         if (!writer.writingHeaderFooter()) {
157             os.write(RtfWriter.escape);
158             os.write(RtfWriter.paragraphDefaults);
159             os.write(RtfWriter.escape);
160             os.write(RtfWriter.paragraph);
161             switch (origTable.getAlignment()) {
162                 case Element.ALIGN_LEFT:
163                     os.write(RtfWriter.escape);
164                     os.write(RtfWriter.alignLeft);
165                     break;
166                 case Element.ALIGN_RIGHT:
167                     os.write(RtfWriter.escape);
168                     os.write(RtfWriter.alignRight);
169                     break;
170                 case Element.ALIGN_CENTER:
171                     os.write(RtfWriter.escape);
172                     os.write(RtfWriter.alignCenter);
173                     break;
174                 case Element.ALIGN_JUSTIFIED:
175                 case Element.ALIGN_JUSTIFIED_ALL:
176                     os.write(RtfWriter.escape);
177                     os.write(RtfWriter.alignJustify);
178                     break;
179             }
180         }
181         return true;
182     }
183
184     /**
185      * <code>RtfCell</code>s call this method to specify that a certain other cell is to be merged with it.
186      *
187      * @param x The column position of the cell to be merged
188      * @param y The row position of the cell to be merged
189      * @param mergeType The merge type specifies the kind of merge to be applied (MERGE_HORIZ_PREV, MERGE_VERT_PREV, MERGE_BOTH_PREV)
190      * @param mergeCell The <code>RtfCell</code> that the cell at x and y is to be merged with
191      */

192     public void setMerge(int x, int y, int mergeType, RtfCell mergeCell) {
193         RtfRow row = (RtfRow) rowsList.get(y);
194         row.setMerge(x, mergeType, mergeCell);
195     }
196
197     /**
198      * This method allows access to the original Table that led to this RtfTable.
199      *
200      * @return The Table object that is the basis of this RtfTable.
201      */

202     protected Table getOriginalTable() {
203         return origTable;
204     }
205 }
206
Popular Tags