001: /*
002: * $Id: SimpleTable.java 2748 2007-05-12 15:11:48Z blowagie $
003: * $Name$
004: *
005: * Copyright 1999-2005 by Bruno Lowagie.
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU LIBRARY GENERAL PUBLIC LICENSE for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050: package com.lowagie.text;
051:
052: import java.util.ArrayList;
053: import java.util.Iterator;
054:
055: import com.lowagie.text.pdf.PdfContentByte;
056: import com.lowagie.text.pdf.PdfPTable;
057: import com.lowagie.text.pdf.PdfPTableEvent;
058:
059: /**
060: * Rectangle that can be used for Cells.
061: * This Rectangle is padded and knows how to draw itself in a PdfPTable or PdfPcellEvent.
062: */
063: public class SimpleTable extends Rectangle implements PdfPTableEvent,
064: TextElementArray {
065:
066: /** the content of a Table. */
067: private ArrayList content = new ArrayList();
068: /** the width of the Table. */
069: private float width = 0f;
070: /** the widthpercentage of the Table. */
071: private float widthpercentage = 0f;
072: /** the spacing of the Cells. */
073: private float cellspacing;
074: /** the padding of the Cells. */
075: private float cellpadding;
076: /** the alignment of the table. */
077: private int alignment;
078:
079: /**
080: * A RectangleCell is always constructed without any dimensions.
081: * Dimensions are defined after creation.
082: */
083: public SimpleTable() {
084: super (0f, 0f, 0f, 0f);
085: setBorder(BOX);
086: setBorderWidth(2f);
087: }
088:
089: /**
090: * Adds content to this object.
091: * @param element
092: * @throws BadElementException
093: */
094: public void addElement(SimpleCell element)
095: throws BadElementException {
096: if (!element.isCellgroup()) {
097: throw new BadElementException(
098: "You can't add cells to a table directly, add them to a row first.");
099: }
100: content.add(element);
101: }
102:
103: /**
104: * Creates a Table object based on this TableAttributes object.
105: * @return a com.lowagie.text.Table object
106: * @throws BadElementException
107: */
108: public Table createTable() throws BadElementException {
109: if (content.isEmpty())
110: throw new BadElementException(
111: "Trying to create a table without rows.");
112: SimpleCell row = (SimpleCell) content.get(0);
113: SimpleCell cell;
114: int columns = 0;
115: for (Iterator i = row.getContent().iterator(); i.hasNext();) {
116: cell = (SimpleCell) i.next();
117: columns += cell.getColspan();
118: }
119: float[] widths = new float[columns];
120: float[] widthpercentages = new float[columns];
121: Table table = new Table(columns);
122: table.setAlignment(alignment);
123: table.setSpacing(cellspacing);
124: table.setPadding(cellpadding);
125: table.cloneNonPositionParameters(this );
126: int pos;
127: for (Iterator rows = content.iterator(); rows.hasNext();) {
128: row = (SimpleCell) rows.next();
129: pos = 0;
130: for (Iterator cells = row.getContent().iterator(); cells
131: .hasNext();) {
132: cell = (SimpleCell) cells.next();
133: table.addCell(cell.createCell(row));
134: if (cell.getColspan() == 1) {
135: if (cell.getWidth() > 0)
136: widths[pos] = cell.getWidth();
137: if (cell.getWidthpercentage() > 0)
138: widthpercentages[pos] = cell
139: .getWidthpercentage();
140: }
141: pos += cell.getColspan();
142: }
143: }
144: float sumWidths = 0f;
145: for (int i = 0; i < columns; i++) {
146: if (widths[i] == 0) {
147: sumWidths = 0;
148: break;
149: }
150: sumWidths += widths[i];
151: }
152: if (sumWidths > 0) {
153: table.setWidth(sumWidths);
154: table.setLocked(true);
155: table.setWidths(widths);
156: } else {
157: for (int i = 0; i < columns; i++) {
158: if (widthpercentages[i] == 0) {
159: sumWidths = 0;
160: break;
161: }
162: sumWidths += widthpercentages[i];
163: }
164: if (sumWidths > 0) {
165: table.setWidths(widthpercentages);
166: }
167: }
168: if (width > 0) {
169: table.setWidth(width);
170: table.setLocked(true);
171: } else if (widthpercentage > 0) {
172: table.setWidth(widthpercentage);
173: }
174: return table;
175: }
176:
177: /**
178: * Creates a PdfPTable object based on this TableAttributes object.
179: * @return a com.lowagie.text.pdf.PdfPTable object
180: * @throws DocumentException
181: */
182: public PdfPTable createPdfPTable() throws DocumentException {
183: if (content.isEmpty())
184: throw new BadElementException(
185: "Trying to create a table without rows.");
186: SimpleCell row = (SimpleCell) content.get(0);
187: SimpleCell cell;
188: int columns = 0;
189: for (Iterator i = row.getContent().iterator(); i.hasNext();) {
190: cell = (SimpleCell) i.next();
191: columns += cell.getColspan();
192: }
193: float[] widths = new float[columns];
194: float[] widthpercentages = new float[columns];
195: PdfPTable table = new PdfPTable(columns);
196: table.setTableEvent(this );
197: table.setHorizontalAlignment(alignment);
198: int pos;
199: for (Iterator rows = content.iterator(); rows.hasNext();) {
200: row = (SimpleCell) rows.next();
201: pos = 0;
202: for (Iterator cells = row.getContent().iterator(); cells
203: .hasNext();) {
204: cell = (SimpleCell) cells.next();
205: if (Float.isNaN(cell.getSpacing_left())) {
206: cell.setSpacing_left(cellspacing / 2f);
207: }
208: if (Float.isNaN(cell.getSpacing_right())) {
209: cell.setSpacing_right(cellspacing / 2f);
210: }
211: if (Float.isNaN(cell.getSpacing_top())) {
212: cell.setSpacing_top(cellspacing / 2f);
213: }
214: if (Float.isNaN(cell.getSpacing_bottom())) {
215: cell.setSpacing_bottom(cellspacing / 2f);
216: }
217: cell.setPadding(cellpadding);
218: table.addCell(cell.createPdfPCell(row));
219: if (cell.getColspan() == 1) {
220: if (cell.getWidth() > 0)
221: widths[pos] = cell.getWidth();
222: if (cell.getWidthpercentage() > 0)
223: widthpercentages[pos] = cell
224: .getWidthpercentage();
225: }
226: pos += cell.getColspan();
227: }
228: }
229: float sumWidths = 0f;
230: for (int i = 0; i < columns; i++) {
231: if (widths[i] == 0) {
232: sumWidths = 0;
233: break;
234: }
235: sumWidths += widths[i];
236: }
237: if (sumWidths > 0) {
238: table.setTotalWidth(sumWidths);
239: table.setWidths(widths);
240: } else {
241: for (int i = 0; i < columns; i++) {
242: if (widthpercentages[i] == 0) {
243: sumWidths = 0;
244: break;
245: }
246: sumWidths += widthpercentages[i];
247: }
248: if (sumWidths > 0) {
249: table.setWidths(widthpercentages);
250: }
251: }
252: if (width > 0) {
253: table.setTotalWidth(width);
254: }
255: if (widthpercentage > 0) {
256: table.setWidthPercentage(widthpercentage);
257: }
258: return table;
259: }
260:
261: /**
262: * @param rectangle
263: * @param spacing
264: * @return a rectangle
265: */
266: public static SimpleTable getDimensionlessInstance(
267: Rectangle rectangle, float spacing) {
268: SimpleTable event = new SimpleTable();
269: event.cloneNonPositionParameters(rectangle);
270: event.setCellspacing(spacing);
271: return event;
272: }
273:
274: /**
275: * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable, float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
276: */
277: public void tableLayout(PdfPTable table, float[][] widths,
278: float[] heights, int headerRows, int rowStart,
279: PdfContentByte[] canvases) {
280: float[] width = widths[0];
281: Rectangle rect = new Rectangle(width[0],
282: heights[heights.length - 1], width[width.length - 1],
283: heights[0]);
284: rect.cloneNonPositionParameters(this );
285: int bd = rect.getBorder();
286: rect.setBorder(Rectangle.NO_BORDER);
287: canvases[PdfPTable.BACKGROUNDCANVAS].rectangle(rect);
288: rect.setBorder(bd);
289: rect.setBackgroundColor(null);
290: canvases[PdfPTable.LINECANVAS].rectangle(rect);
291: }
292:
293: /**
294: * @return Returns the cellpadding.
295: */
296: public float getCellpadding() {
297: return cellpadding;
298: }
299:
300: /**
301: * @param cellpadding The cellpadding to set.
302: */
303: public void setCellpadding(float cellpadding) {
304: this .cellpadding = cellpadding;
305: }
306:
307: /**
308: * @return Returns the cellspacing.
309: */
310: public float getCellspacing() {
311: return cellspacing;
312: }
313:
314: /**
315: * @param cellspacing The cellspacing to set.
316: */
317: public void setCellspacing(float cellspacing) {
318: this .cellspacing = cellspacing;
319: }
320:
321: /**
322: * @return Returns the alignment.
323: */
324: public int getAlignment() {
325: return alignment;
326: }
327:
328: /**
329: * @param alignment The alignment to set.
330: */
331: public void setAlignment(int alignment) {
332: this .alignment = alignment;
333: }
334:
335: /**
336: * @return Returns the width.
337: */
338: public float getWidth() {
339: return width;
340: }
341:
342: /**
343: * @param width The width to set.
344: */
345: public void setWidth(float width) {
346: this .width = width;
347: }
348:
349: /**
350: * @return Returns the widthpercentage.
351: */
352: public float getWidthpercentage() {
353: return widthpercentage;
354: }
355:
356: /**
357: * @param widthpercentage The widthpercentage to set.
358: */
359: public void setWidthpercentage(float widthpercentage) {
360: this .widthpercentage = widthpercentage;
361: }
362:
363: /**
364: * @see com.lowagie.text.Element#type()
365: */
366: public int type() {
367: return Element.TABLE;
368: }
369:
370: /**
371: * @see com.lowagie.text.TextElementArray#add(java.lang.Object)
372: */
373: public boolean add(Object o) {
374: try {
375: addElement((SimpleCell) o);
376: return true;
377: } catch (ClassCastException e) {
378: return false;
379: } catch (BadElementException e) {
380: throw new ExceptionConverter(e);
381: }
382: }
383: }
|