KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfTable


1 /*
2  * $Id: PdfTable.java 2752 2007-05-15 14:58:33Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
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.pdf;
52
53 import java.util.ArrayList JavaDoc;
54 import java.util.Iterator JavaDoc;
55
56 import com.lowagie.text.Cell;
57 import com.lowagie.text.Element;
58 import com.lowagie.text.Rectangle;
59 import com.lowagie.text.Row;
60 import com.lowagie.text.Table;
61
62 /**
63  * <CODE>PdfTable</CODE> is an object that contains the graphics and text of a table.
64  *
65  * @see com.lowagie.text.Table
66  * @see com.lowagie.text.Row
67  * @see com.lowagie.text.Cell
68  * @see PdfCell
69  */

70
71 public class PdfTable extends Rectangle {
72     
73     // membervariables
74

75     /** this is the number of columns in the table. */
76     private int columns;
77     
78     /** this is the ArrayList with all the cell of the table header. */
79     private ArrayList JavaDoc headercells;
80     
81     /** this is the ArrayList with all the cells in the table. */
82     private ArrayList JavaDoc cells;
83     
84     /** Original table used to build this object*/
85     protected Table table;
86     
87     /** Cached column widths. */
88     protected float[] positions;
89
90     // constructors
91

92     /**
93      * Constructs a <CODE>PdfTable</CODE>-object.
94      *
95      * @param table a <CODE>Table</CODE>
96      * @param left the left border on the page
97      * @param right the right border on the page
98      * @param top the start position of the top of the table
99      * @param supportUpdateRowAdditions
100      * if true, table rows will be deleted after building the PdfTable table,
101      * in order to preserve memory and detect future row additions
102      */

103     
104     PdfTable(Table table, float left, float right, float top, boolean supportUpdateRowAdditions) {
105         // constructs a Rectangle (the bottomvalue will be changed afterwards)
106
super(left, top, right, top);
107         this.table = table;
108         table.complete();
109
110         // copying the attributes from class Table
111
cloneNonPositionParameters(table);
112
113         this.columns = table.getColumns();
114         positions = table.getWidths(left, right - left);
115         
116         // initialisation of some parameters
117
setLeft(positions[0]);
118         setRight(positions[positions.length - 1]);
119         
120         headercells = new ArrayList JavaDoc();
121         cells = new ArrayList JavaDoc();
122
123         updateRowAdditionsInternal();
124         if (supportUpdateRowAdditions) {
125             table.deleteAllRows();
126         }
127     }
128
129     // methods
130

131     /**
132      * Updates the table row additions in the underlying table object and deletes all table rows,
133      * in order to preserve memory and detect future row additions.
134      * <p><b>Pre-requisite</b>: the object must have been built with the parameter <code>supportUpdateRowAdditions</code> equals to true.
135      */

136     
137     void updateRowAdditions() {
138         table.complete();
139         updateRowAdditionsInternal();
140         table.deleteAllRows();
141     }
142     
143     /**
144      * Updates the table row additions in the underlying table object
145      */

146     
147     private void updateRowAdditionsInternal() {
148         // correct table : fill empty cells/ parse table in table
149
Row row;
150         int prevRows = rows();
151         int rowNumber = 0;
152         int groupNumber = 0;
153         boolean groupChange;
154         int firstDataRow = table.getLastHeaderRow() + 1;
155         Cell cell;
156         PdfCell currentCell;
157         ArrayList JavaDoc newCells = new ArrayList JavaDoc();
158         int rows = table.size() + 1;
159         float[] offsets = new float[rows];
160         for (int i = 0; i < rows; i++) {
161             offsets[i] = getBottom();
162         }
163         
164         // loop over all the rows
165
for (Iterator JavaDoc rowIterator = table.iterator(); rowIterator.hasNext(); ) {
166             groupChange = false;
167             row = (Row) rowIterator.next();
168             if (row.isEmpty()) {
169                 if (rowNumber < rows - 1 && offsets[rowNumber + 1] > offsets[rowNumber]) offsets[rowNumber + 1] = offsets[rowNumber];
170             }
171             else {
172                 for(int i = 0; i < row.getColumns(); i++) {
173                     cell = (Cell) row.getCell(i);
174                     if (cell != null) {
175                         currentCell = new PdfCell(cell, rowNumber+prevRows, positions[i], positions[i + cell.getColspan()], offsets[rowNumber], cellspacing(), cellpadding());
176                         try {
177                      if (offsets[rowNumber] - currentCell.getHeight() - cellpadding() < offsets[rowNumber + currentCell.rowspan()]) {
178                         offsets[rowNumber + currentCell.rowspan()] = offsets[rowNumber] - currentCell.getHeight() - cellpadding();
179                             }
180                         }
181                         catch(ArrayIndexOutOfBoundsException JavaDoc aioobe) {
182                             if (offsets[rowNumber] - currentCell.getHeight() < offsets[rows - 1]) {
183                                 offsets[rows - 1] = offsets[rowNumber] - currentCell.getHeight();
184                             }
185                         }
186                         if (rowNumber < firstDataRow) {
187                             currentCell.setHeader();
188                             headercells.add(currentCell);
189                         }
190                         currentCell.setGroupNumber(groupNumber);
191                         groupChange |= cell.getGroupChange();
192                         newCells.add(currentCell);
193                     }
194                 }
195             }
196             rowNumber++;
197             if( groupChange ) groupNumber++;
198         }
199         
200         // loop over all the cells
201
int n = newCells.size();
202         for (int i = 0; i < n; i++) {
203             currentCell = (PdfCell) newCells.get(i);
204             try {
205                 currentCell.setBottom(offsets[currentCell.rownumber()-prevRows + currentCell.rowspan()]);
206             }
207             catch(ArrayIndexOutOfBoundsException JavaDoc aioobe) {
208                 currentCell.setBottom(offsets[rows - 1]);
209             }
210         }
211         cells.addAll(newCells);
212         setBottom(offsets[rows - 1]);
213     }
214
215     /**
216      * Get the number of rows
217      */

218     
219     int rows() {
220         return cells.isEmpty() ? 0 : ((PdfCell)cells.get(cells.size()-1)).rownumber()+1;
221     }
222
223     /** @see com.lowagie.text.Element#type() */
224     public int type() {
225         return Element.TABLE;
226     }
227     
228     /**
229      * Returns the arraylist with the cells of the table header.
230      *
231      * @return an <CODE>ArrayList</CODE>
232      */

233     
234     ArrayList JavaDoc getHeaderCells() {
235         return headercells;
236     }
237     
238     /**
239      * Checks if there is a table header.
240      *
241      * @return an <CODE>ArrayList</CODE>
242      */

243     
244     boolean hasHeader() {
245         return !headercells.isEmpty();
246     }
247     
248     /**
249      * Returns the arraylist with the cells of the table.
250      *
251      * @return an <CODE>ArrayList</CODE>
252      */

253     
254     ArrayList JavaDoc getCells() {
255         return cells;
256     }
257     
258     /**
259      * Returns the number of columns of the table.
260      *
261      * @return the number of columns
262      */

263     
264     int columns() {
265         return columns;
266     }
267     
268     /**
269      * Returns the cellpadding of the table.
270      *
271      * @return the cellpadding
272      */

273     
274     final float cellpadding() {
275         return table.getPadding();
276     }
277     
278     /**
279      * Returns the cellspacing of the table.
280      *
281      * @return the cellspacing
282      */

283     
284     final float cellspacing() {
285         return table.getSpacing();
286     }
287     
288     /**
289      * Checks if this <CODE>Table</CODE> has to fit a page.
290      *
291      * @return true if the table may not be split
292      */

293
294     public final boolean hasToFitPageTable() {
295         return table.isTableFitsPage();
296     }
297
298     /**
299      * Checks if the cells of this <CODE>Table</CODE> have to fit a page.
300      *
301      * @return true if the cells may not be split
302      */

303     
304     public final boolean hasToFitPageCells() {
305         return table.isCellsFitPage();
306     }
307
308     /**
309      * Gets the offset of this table.
310      *
311      * @return the space between this table and the previous element.
312      */

313     public float getOffset() {
314         return table.getOffset();
315     }
316 }
317
Popular Tags