001: /**
002: * $Id: RtfTable.java 2748 2007-05-12 15:11:48Z blowagie $
003: * $Name$
004: *
005: * Copyright 2001, 2002 by Mark Hall
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: */package com.lowagie.text.rtf;
050:
051: import java.io.ByteArrayOutputStream;
052: import java.io.IOException;
053: import java.util.ArrayList;
054: import java.util.Iterator;
055:
056: import com.lowagie.text.DocumentException;
057: import com.lowagie.text.Element;
058: import com.lowagie.text.Row;
059: import com.lowagie.text.Table;
060:
061: /**
062: * A Helper Class for the <CODE>RtfWriter</CODE>.
063: * <P>
064: * Do not use it directly, except if you want to write a <CODE>DocumentListener</CODE> for Rtf
065: *
066: * ONLY FOR USE WITH THE RtfWriter NOT with the RtfWriter2.
067: *
068: * Parts of this Class were contributed by Steffen Stundzig. Many thanks for the
069: * improvements.
070: * Updates Benoit Wiart
071: * @deprecated Please move to the RtfWriter2 and associated classes.
072: */
073: public class RtfTable {
074: /** Stores the different rows. */
075: private ArrayList rowsList = new ArrayList();
076: /** Stores the RtfWriter, which created this RtfTable. */
077: private RtfWriter writer = null;
078: /** Stores the Table, which this RtfTable is based on. */
079: private Table origTable = null;
080:
081: /**
082: * Create a new <code>RtfTable</code>.
083: *
084: * @param writer The <code>RtfWriter</code> that created this Table
085: */
086: public RtfTable(RtfWriter writer) {
087: super ();
088: this .writer = writer;
089: }
090:
091: /**
092: * Import a <CODE>Table</CODE> into the <CODE>RtfTable</CODE>.
093: * <P>
094: * @param table A <code>Table</code> specifying the <code>Table</code> to be imported
095: * @param pageWidth An <code>int</code> specifying the page width
096: * @return true if importing the table succeeded
097: */
098: public boolean importTable(Table table, int pageWidth) {
099: origTable = table;
100: // All Cells are pregenerated first, so that cell and rowspanning work
101: Iterator rows = table.iterator();
102: Row row = null;
103:
104: int tableWidth = (int) table.getWidth();
105: int cellpadding = (int) (table.getPadding() * RtfWriter.TWIPSFACTOR);
106: int cellspacing = (int) (table.getSpacing() * RtfWriter.TWIPSFACTOR);
107: float[] propWidths = table.getProportionalWidths();
108:
109: int borders = table.getBorder();
110: java.awt.Color borderColor = table.getBorderColor();
111: float borderWidth = table.getBorderWidth();
112:
113: for (int i = 0; i < table.size(); i++) {
114: RtfRow rtfRow = new RtfRow(writer, this );
115: rtfRow.pregenerateRows(table.getColumns());
116: rowsList.add(rtfRow);
117: }
118: int i = 0;
119: while (rows.hasNext()) {
120: row = (Row) rows.next();
121: row.setHorizontalAlignment(table.getAlignment());
122: RtfRow rtfRow = (RtfRow) rowsList.get(i);
123: rtfRow.importRow(row, propWidths, tableWidth, pageWidth,
124: cellpadding, cellspacing, borders, borderColor,
125: borderWidth, i);
126: i++;
127: }
128: return true;
129: }
130:
131: /**
132: * Output the content of the <CODE>RtfTable</CODE> to an OutputStream.
133: *
134: * @param os The <code>OutputStream</code> that the content of the <code>RtfTable</code> is to be written to
135: * @return true if writing the table succeeded
136: * @throws DocumentException
137: * @throws IOException
138: */
139: public boolean writeTable(ByteArrayOutputStream os)
140: throws DocumentException, IOException {
141:
142: if (!this .writer.writingHeaderFooter()) {
143: // Added by Benoit Wiart (b.wiart@proxiad.com)
144: // Add a new line before each table
145: os.write(RtfWriter.escape);
146: os.write(RtfWriter.paragraph);
147: }
148:
149: int size = rowsList.size();
150: for (int i = 0; i < size; i++) {
151: RtfRow row = (RtfRow) rowsList.get(i);
152: row.writeRow(os, i, origTable);
153: os.write((byte) '\n');
154: }
155: if (!writer.writingHeaderFooter()) {
156: os.write(RtfWriter.escape);
157: os.write(RtfWriter.paragraphDefaults);
158: os.write(RtfWriter.escape);
159: os.write(RtfWriter.paragraph);
160: switch (origTable.getAlignment()) {
161: case Element.ALIGN_LEFT:
162: os.write(RtfWriter.escape);
163: os.write(RtfWriter.alignLeft);
164: break;
165: case Element.ALIGN_RIGHT:
166: os.write(RtfWriter.escape);
167: os.write(RtfWriter.alignRight);
168: break;
169: case Element.ALIGN_CENTER:
170: os.write(RtfWriter.escape);
171: os.write(RtfWriter.alignCenter);
172: break;
173: case Element.ALIGN_JUSTIFIED:
174: case Element.ALIGN_JUSTIFIED_ALL:
175: os.write(RtfWriter.escape);
176: os.write(RtfWriter.alignJustify);
177: break;
178: }
179: }
180: return true;
181: }
182:
183: /**
184: * <code>RtfCell</code>s call this method to specify that a certain other cell is to be merged with it.
185: *
186: * @param x The column position of the cell to be merged
187: * @param y The row position of the cell to be merged
188: * @param mergeType The merge type specifies the kind of merge to be applied (MERGE_HORIZ_PREV, MERGE_VERT_PREV, MERGE_BOTH_PREV)
189: * @param mergeCell The <code>RtfCell</code> that the cell at x and y is to be merged with
190: */
191: public void setMerge(int x, int y, int mergeType, RtfCell mergeCell) {
192: RtfRow row = (RtfRow) rowsList.get(y);
193: row.setMerge(x, mergeType, mergeCell);
194: }
195:
196: /**
197: * This method allows access to the original Table that led to this RtfTable.
198: *
199: * @return The Table object that is the basis of this RtfTable.
200: */
201: protected Table getOriginalTable() {
202: return origTable;
203: }
204: }
|