KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > SimpleTable


1 /*
2  * $Id: SimpleTable.java 2748 2007-05-12 15:11:48Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999-2005 by 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 package com.lowagie.text;
51
52 import java.util.ArrayList JavaDoc;
53 import java.util.Iterator JavaDoc;
54
55 import com.lowagie.text.pdf.PdfContentByte;
56 import com.lowagie.text.pdf.PdfPTable;
57 import com.lowagie.text.pdf.PdfPTableEvent;
58
59 /**
60  * Rectangle that can be used for Cells.
61  * This Rectangle is padded and knows how to draw itself in a PdfPTable or PdfPcellEvent.
62  */

63 public class SimpleTable extends Rectangle implements PdfPTableEvent, TextElementArray {
64
65     /** the content of a Table. */
66     private ArrayList JavaDoc content = new ArrayList JavaDoc();
67     /** the width of the Table. */
68     private float width = 0f;
69     /** the widthpercentage of the Table. */
70     private float widthpercentage = 0f;
71     /** the spacing of the Cells. */
72     private float cellspacing;
73     /** the padding of the Cells. */
74     private float cellpadding;
75     /** the alignment of the table. */
76     private int alignment;
77     
78     /**
79      * A RectangleCell is always constructed without any dimensions.
80      * Dimensions are defined after creation.
81      */

82     public SimpleTable() {
83         super(0f, 0f, 0f, 0f);
84         setBorder(BOX);
85         setBorderWidth(2f);
86     }
87     
88     /**
89      * Adds content to this object.
90      * @param element
91      * @throws BadElementException
92      */

93     public void addElement(SimpleCell element) throws BadElementException {
94         if(!element.isCellgroup()) {
95             throw new BadElementException("You can't add cells to a table directly, add them to a row first.");
96         }
97         content.add(element);
98     }
99     
100     /**
101      * Creates a Table object based on this TableAttributes object.
102      * @return a com.lowagie.text.Table object
103      * @throws BadElementException
104      */

105     public Table createTable() throws BadElementException {
106         if (content.isEmpty()) throw new BadElementException("Trying to create a table without rows.");
107         SimpleCell row = (SimpleCell)content.get(0);
108         SimpleCell cell;
109         int columns = 0;
110         for (Iterator JavaDoc i = row.getContent().iterator(); i.hasNext(); ) {
111             cell = (SimpleCell)i.next();
112             columns += cell.getColspan();
113         }
114         float[] widths = new float[columns];
115         float[] widthpercentages = new float[columns];
116         Table table = new Table(columns);
117         table.setAlignment(alignment);
118         table.setSpacing(cellspacing);
119         table.setPadding(cellpadding);
120         table.cloneNonPositionParameters(this);
121         int pos;
122         for (Iterator JavaDoc rows = content.iterator(); rows.hasNext(); ) {
123             row = (SimpleCell)rows.next();
124             pos = 0;
125             for (Iterator JavaDoc cells = row.getContent().iterator(); cells.hasNext(); ) {
126                 cell = (SimpleCell)cells.next();
127                 table.addCell(cell.createCell(row));
128                 if (cell.getColspan() == 1) {
129                     if (cell.getWidth() > 0) widths[pos] = cell.getWidth();
130                     if (cell.getWidthpercentage() > 0) widthpercentages[pos] = cell.getWidthpercentage();
131                 }
132                 pos += cell.getColspan();
133             }
134         }
135         float sumWidths = 0f;
136         for(int i = 0; i < columns; i++) {
137             if (widths[i] == 0) {
138                 sumWidths = 0;
139                 break;
140             }
141             sumWidths += widths[i];
142         }
143         if (sumWidths > 0) {
144             table.setWidth(sumWidths);
145             table.setLocked(true);
146             table.setWidths(widths);
147         }
148         else {
149             for(int i = 0; i < columns; i++) {
150                 if (widthpercentages[i] == 0) {
151                     sumWidths = 0;
152                     break;
153                 }
154                 sumWidths += widthpercentages[i];
155             }
156             if (sumWidths > 0) {
157                 table.setWidths(widthpercentages);
158             }
159         }
160         if (width > 0) {
161             table.setWidth(width);
162             table.setLocked(true);
163         }
164         else if (widthpercentage > 0) {
165             table.setWidth(widthpercentage);
166         }
167         return table;
168     }
169     
170     /**
171      * Creates a PdfPTable object based on this TableAttributes object.
172      * @return a com.lowagie.text.pdf.PdfPTable object
173      * @throws DocumentException
174      */

175     public PdfPTable createPdfPTable() throws DocumentException {
176         if (content.isEmpty()) throw new BadElementException("Trying to create a table without rows.");
177         SimpleCell row = (SimpleCell)content.get(0);
178         SimpleCell cell;
179         int columns = 0;
180         for (Iterator JavaDoc i = row.getContent().iterator(); i.hasNext(); ) {
181             cell = (SimpleCell)i.next();
182             columns += cell.getColspan();
183         }
184         float[] widths = new float[columns];
185         float[] widthpercentages = new float[columns];
186         PdfPTable table = new PdfPTable(columns);
187         table.setTableEvent(this);
188         table.setHorizontalAlignment(alignment);
189         int pos;
190         for (Iterator JavaDoc rows = content.iterator(); rows.hasNext(); ) {
191             row = (SimpleCell)rows.next();
192             pos = 0;
193             for (Iterator JavaDoc cells = row.getContent().iterator(); cells.hasNext(); ) {
194                 cell = (SimpleCell)cells.next();
195                 if (Float.isNaN(cell.getSpacing_left())) {
196                     cell.setSpacing_left(cellspacing / 2f);
197                 }
198                 if (Float.isNaN(cell.getSpacing_right())) {
199                     cell.setSpacing_right(cellspacing / 2f);
200                 }
201                 if (Float.isNaN(cell.getSpacing_top())) {
202                     cell.setSpacing_top(cellspacing / 2f);
203                 }
204                 if (Float.isNaN(cell.getSpacing_bottom())) {
205                     cell.setSpacing_bottom(cellspacing / 2f);
206                 }
207                 cell.setPadding(cellpadding);
208                 table.addCell(cell.createPdfPCell(row));
209                 if (cell.getColspan() == 1) {
210                     if (cell.getWidth() > 0) widths[pos] = cell.getWidth();
211                     if (cell.getWidthpercentage() > 0) widthpercentages[pos] = cell.getWidthpercentage();
212                 }
213                 pos += cell.getColspan();
214             }
215         }
216         float sumWidths = 0f;
217         for(int i = 0; i < columns; i++) {
218             if (widths[i] == 0) {
219                 sumWidths = 0;
220                 break;
221             }
222             sumWidths += widths[i];
223         }
224         if (sumWidths > 0) {
225             table.setTotalWidth(sumWidths);
226             table.setWidths(widths);
227         }
228         else {
229             for(int i = 0; i < columns; i++) {
230                 if (widthpercentages[i] == 0) {
231                     sumWidths = 0;
232                     break;
233                 }
234                 sumWidths += widthpercentages[i];
235             }
236             if (sumWidths > 0) {
237                 table.setWidths(widthpercentages);
238             }
239         }
240         if (width > 0) {
241             table.setTotalWidth(width);
242         }
243         if (widthpercentage > 0) {
244             table.setWidthPercentage(widthpercentage);
245         }
246         return table;
247     }
248     
249     /**
250      * @param rectangle
251      * @param spacing
252      * @return a rectangle
253      */

254     public static SimpleTable getDimensionlessInstance(Rectangle rectangle, float spacing) {
255         SimpleTable event = new SimpleTable();
256         event.cloneNonPositionParameters(rectangle);
257         event.setCellspacing(spacing);
258         return event;
259     }
260     
261     /**
262      * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable, float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
263      */

264     public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
265         float[] width = widths[0];
266         Rectangle rect = new Rectangle(width[0], heights[heights.length - 1], width[width.length - 1], heights[0]);
267         rect.cloneNonPositionParameters(this);
268         int bd = rect.getBorder();
269         rect.setBorder(Rectangle.NO_BORDER);
270         canvases[PdfPTable.BACKGROUNDCANVAS].rectangle(rect);
271         rect.setBorder(bd);
272         rect.setBackgroundColor(null);
273         canvases[PdfPTable.LINECANVAS].rectangle(rect);
274     }
275     
276     /**
277      * @return Returns the cellpadding.
278      */

279     public float getCellpadding() {
280         return cellpadding;
281     }
282     /**
283      * @param cellpadding The cellpadding to set.
284      */

285     public void setCellpadding(float cellpadding) {
286         this.cellpadding = cellpadding;
287     }
288     /**
289      * @return Returns the cellspacing.
290      */

291     public float getCellspacing() {
292         return cellspacing;
293     }
294     /**
295      * @param cellspacing The cellspacing to set.
296      */

297     public void setCellspacing(float cellspacing) {
298         this.cellspacing = cellspacing;
299     }
300     
301     /**
302      * @return Returns the alignment.
303      */

304     public int getAlignment() {
305         return alignment;
306     }
307     /**
308      * @param alignment The alignment to set.
309      */

310     public void setAlignment(int alignment) {
311         this.alignment = alignment;
312     }
313     /**
314      * @return Returns the width.
315      */

316     public float getWidth() {
317         return width;
318     }
319     /**
320      * @param width The width to set.
321      */

322     public void setWidth(float width) {
323         this.width = width;
324     }
325     /**
326      * @return Returns the widthpercentage.
327      */

328     public float getWidthpercentage() {
329         return widthpercentage;
330     }
331     /**
332      * @param widthpercentage The widthpercentage to set.
333      */

334     public void setWidthpercentage(float widthpercentage) {
335         this.widthpercentage = widthpercentage;
336     }
337     /**
338      * @see com.lowagie.text.Element#type()
339      */

340     public int type() {
341         return Element.TABLE;
342     }
343
344     /**
345      * @see com.lowagie.text.TextElementArray#add(java.lang.Object)
346      */

347     public boolean add(Object JavaDoc o) {
348         try {
349             addElement((SimpleCell)o);
350             return true;
351         }
352         catch(ClassCastException JavaDoc e) {
353             return false;
354         }
355         catch(BadElementException e) {
356             throw new ExceptionConverter(e);
357         }
358     }
359 }
Popular Tags