KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RtfTable.java 2776 2007-05-23 20:01:40Z hallm $
3  * $Name$
4  *
5  * Copyright 2001, 2002, 2003, 2004 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.table;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.Iterator JavaDoc;
58
59 import com.lowagie.text.Element;
60 import com.lowagie.text.Row;
61 import com.lowagie.text.Table;
62 import com.lowagie.text.rtf.RtfElement;
63 import com.lowagie.text.rtf.document.RtfDocument;
64 import com.lowagie.text.rtf.text.RtfParagraph;
65
66
67 /**
68  * The RtfTable wraps a Table.
69  * INTERNAL USE ONLY
70  *
71  * @version $Id: RtfTable.java 2776 2007-05-23 20:01:40Z hallm $
72  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
73  * @author Steffen Stundzig
74  * @author Benoit Wiart
75  * @author Thomas Bickel (tmb99@inode.at)
76  */

77 public class RtfTable extends RtfElement {
78
79     /**
80      * The rows of this RtfTable
81      */

82     private ArrayList JavaDoc rows = null;
83     /**
84      * The percentage of the page width that this RtfTable covers
85      */

86     private float tableWidthPercent = 80;
87     /**
88      * An array with the proportional widths of the cells in each row
89      */

90     private float[] proportionalWidths = null;
91     /**
92      * The cell padding
93      */

94     private float cellPadding = 0;
95     /**
96      * The cell spacing
97      */

98     private float cellSpacing = 0;
99     /**
100      * The border style of this RtfTable
101      */

102     private RtfBorderGroup borders = null;
103     /**
104      * The alignment of this RtfTable
105      */

106     private int alignment = Element.ALIGN_CENTER;
107     /**
108      * Whether the cells in this RtfTable must fit in a page
109      */

110     private boolean cellsFitToPage = false;
111     /**
112      * Whether the whole RtfTable must fit in a page
113      */

114     private boolean tableFitToPage = false;
115     /**
116      * The number of header rows in this RtfTable
117      */

118     private int headerRows = 0;
119     
120     /**
121      * Constructs a RtfTable based on a Table for a RtfDocument.
122      *
123      * @param doc The RtfDocument this RtfTable belongs to
124      * @param table The Table that this RtfTable wraps
125      */

126     public RtfTable(RtfDocument doc, Table table) {
127         super(doc);
128         table.complete();
129         importTable(table);
130     }
131     
132     /**
133      * Imports the rows and settings from the Table into this
134      * RtfTable.
135      *
136      * @param table The source Table
137      */

138     private void importTable(Table table) {
139         this.rows = new ArrayList JavaDoc();
140         this.tableWidthPercent = table.getWidth();
141         this.proportionalWidths = table.getProportionalWidths();
142         this.cellPadding = (float) (table.getPadding() * TWIPS_FACTOR);
143         this.cellSpacing = (float) (table.getSpacing() * TWIPS_FACTOR);
144         this.borders = new RtfBorderGroup(this.document, RtfBorder.ROW_BORDER, table.getBorder(), table.getBorderWidth(), table.getBorderColor());
145         this.alignment = table.getAlignment();
146         
147         int i = 0;
148         Iterator JavaDoc rowIterator = table.iterator();
149         while(rowIterator.hasNext()) {
150             this.rows.add(new RtfRow(this.document, this, (Row) rowIterator.next(), i));
151             i++;
152         }
153         for(i = 0; i < this.rows.size(); i++) {
154             ((RtfRow) this.rows.get(i)).handleCellSpanning();
155             ((RtfRow) this.rows.get(i)).cleanRow();
156         }
157         this.headerRows = table.getLastHeaderRow();
158         this.cellsFitToPage = table.isCellsFitPage();
159         this.tableFitToPage = table.isTableFitsPage();
160     }
161     
162     /**
163      * Writes the content of this RtfTable
164      *
165      * @return A byte array with the content of this RtfTable
166      * @deprecated replaced by {@link #writeContent(OutputStream)}
167      */

168     public byte[] write()
169     {
170         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
171         try {
172             writeContent(result);
173         } catch(IOException JavaDoc ioe) {
174             ioe.printStackTrace();
175         }
176         return result.toByteArray();
177     }
178     /**
179      * Writes the content of this RtfTable
180      */

181     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
182     {
183         if(!inHeader) {
184             result.write(RtfParagraph.PARAGRAPH);
185         }
186         
187         for(int i = 0; i < this.rows.size(); i++) {
188             RtfElement re = (RtfElement)this.rows.get(i);
189             //.result.write(re.write());
190
re.writeContent(result);
191         }
192         
193         result.write(RtfParagraph.PARAGRAPH_DEFAULTS);
194     }
195     
196     /**
197      * Gets the alignment of this RtfTable
198      *
199      * @return The alignment of this RtfTable.
200      */

201     protected int getAlignment() {
202         return alignment;
203     }
204     
205     /**
206      * Gets the borders of this RtfTable
207      *
208      * @return The borders of this RtfTable.
209      */

210     protected RtfBorderGroup getBorders() {
211         return this.borders;
212     }
213     
214     /**
215      * Gets the cell padding of this RtfTable
216      *
217      * @return The cell padding of this RtfTable.
218      */

219     protected float getCellPadding() {
220         return cellPadding;
221     }
222     
223     /**
224      * Gets the cell spacing of this RtfTable
225      *
226      * @return The cell spacing of this RtfTable.
227      */

228     protected float getCellSpacing() {
229         return cellSpacing;
230     }
231     
232     /**
233      * Gets the proportional cell widths of this RtfTable
234      *
235      * @return The proportional widths of this RtfTable.
236      */

237     protected float[] getProportionalWidths() {
238         return (float[]) proportionalWidths.clone();
239     }
240     
241     /**
242      * Gets the percentage of the page width this RtfTable covers
243      *
244      * @return The percentage of the page width.
245      */

246     protected float getTableWidthPercent() {
247         return tableWidthPercent;
248     }
249     
250     /**
251      * Gets the rows of this RtfTable
252      *
253      * @return The rows of this RtfTable
254      */

255     protected ArrayList JavaDoc getRows() {
256         return this.rows;
257     }
258     
259     /**
260      * Gets the cellsFitToPage setting of this RtfTable.
261      *
262      * @return The cellsFitToPage setting of this RtfTable.
263      */

264     protected boolean getCellsFitToPage() {
265         return this.cellsFitToPage;
266     }
267     
268     /**
269      * Gets the tableFitToPage setting of this RtfTable.
270      *
271      * @return The tableFitToPage setting of this RtfTable.
272      */

273     protected boolean getTableFitToPage() {
274         return this.tableFitToPage;
275     }
276     
277     /**
278      * Gets the number of header rows of this RtfTable
279      *
280      * @return The number of header rows
281      */

282     protected int getHeaderRows() {
283         return this.headerRows;
284     }
285 }
286
Popular Tags