KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > RtfRow


1 /**
2  * $Id: RtfRow.java 2748 2007-05-12 15:11:48Z blowagie $
3  * $Name$
4  *
5  * Copyright 2001, 2002 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;
52
53 import java.awt.Color JavaDoc;
54 import java.io.ByteArrayOutputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.Iterator JavaDoc;
58
59 import com.lowagie.text.Cell;
60 import com.lowagie.text.DocumentException;
61 import com.lowagie.text.Element;
62 import com.lowagie.text.Rectangle;
63 import com.lowagie.text.Row;
64 import com.lowagie.text.Table;
65
66 /**
67  * A Helper Class for the <CODE>RtfWriter</CODE>.
68  * <P>
69  * Do not use it directly
70  *
71  * ONLY FOR USE WITH THE RtfWriter NOT with the RtfWriter2.
72  *
73  * Parts of this Class were contributed by Steffen Stundzig. Many thanks for the
74  * improvements.
75  * Code added by c
76  * @deprecated Please move to the RtfWriter2 and associated classes.
77  */

78 public class RtfRow {
79     /** Table border solid */
80     public static final byte[] tableBorder = "brdrs".getBytes();
81     /** Table border width */
82     public static final byte[] tableBorderWidth = "brdrw".getBytes();
83     /** Table border color */
84     public static final byte[] tableBorderColor = "brdrcf".getBytes();
85
86     /** Table row defaults */
87     private static final byte[] rowBegin = "trowd".getBytes();
88     /** End of table row */
89     private static final byte[] rowEnd = "row".getBytes();
90     /** Table row autofit */
91     private static final byte[] rowAutofit = "trautofit1".getBytes();
92     private static final byte[] graphLeft = "trgaph".getBytes();
93     /** Row border left */
94     private static final byte[] rowBorderLeft = "trbrdrl".getBytes();
95     /** Row border right */
96     private static final byte[] rowBorderRight = "trbrdrr".getBytes();
97     /** Row border top */
98     private static final byte[] rowBorderTop = "trbrdrt".getBytes();
99     /** Row border bottom */
100     private static final byte[] rowBorderBottom = "trbrdrb".getBytes();
101     /** Row border horiz inline */
102     private static final byte[] rowBorderInlineHorizontal = "trbrdrh".getBytes();
103     /** Row border bottom */
104     private static final byte[] rowBorderInlineVertical = "trbrdrv".getBytes();
105     /** Default cell spacing left */
106     private static final byte[] rowSpacingLeft = "trspdl".getBytes();
107     /** Default cell spacing right */
108     private static final byte[] rowSpacingRight = "trspdr".getBytes();
109     /** Default cell spacing top */
110     private static final byte[] rowSpacingTop = "trspdt".getBytes();
111     /** Default cell spacing bottom */
112     private static final byte[] rowSpacingBottom = "trspdb".getBytes();
113     /** Default cell spacing format left */
114     private static final byte[] rowSpacingLeftStyle = "trspdfl3".getBytes();
115     /** Default cell spacing format right */
116     private static final byte[] rowSpacingRightStyle = "trspdfr3".getBytes();
117     /** Default cell spacing format top */
118     private static final byte[] rowSpacingTopStyle = "trspdft3".getBytes();
119     /** Default cell spacing format bottom */
120     private static final byte[] rowSpacingBottomStyle = "trspdfb3".getBytes();
121     /** Default cell padding left */
122     private static final byte[] rowPaddingLeft = "trpaddl".getBytes();
123     /** Default cell padding right */
124     private static final byte[] rowPaddingRight = "trpaddr".getBytes();
125     /** Default cell padding format left */
126     private static final byte[] rowPaddingLeftStyle = "trpaddfl3".getBytes();
127     /** Default cell padding format right */
128     private static final byte[] rowPaddingRightStyle = "trpaddfr3".getBytes();
129     /** Row width format */
130     private static final byte[] rowWidthStyle = "trftsWidth3".getBytes();
131     /** Row width */
132     private static final byte[] rowWidth = "trwWidth".getBytes();
133     /**
134      * Table row header. This row should appear at the top of every
135      * page the current table appears on.
136      */

137     private static final byte[] rowHeader = "trhdr".getBytes();
138     /**
139      * Table row keep together. This row cannot be split by a page break.
140      * This property is assumed to be off unless the control word is
141      * present.
142      */

143     private static final byte[] rowKeep = "trkeep".getBytes();
144     /** Table alignment left */
145     private static final byte[] rowAlignLeft = "trql".getBytes();
146     /** Table alignment center */
147     private static final byte[] rowAlignCenter = "trqc".getBytes();
148     /** Table alignment right */
149     private static final byte[] rowAlignRight = "trqr".getBytes();
150
151     /** List of <code>RtfCell</code>s in this <code>RtfRow</code> */
152     private ArrayList JavaDoc cells = new ArrayList JavaDoc();
153     /** The <code>RtfWriter</code> to which this <code>RtfRow</code> belongs */
154     private RtfWriter writer = null;
155     /** The <coce>RtfTable</code> to which this <code>RtfRow</code> belongs */
156     private RtfTable mainTable = null;
157
158     /** The width of this <code>RtfRow</code> (in percent) */
159     private int width = 100;
160     /** The default cellpadding of <code>RtfCells</code> in this
161      * <code>RtfRow</code> */

162     private int cellpadding = 115;
163     /** The default cellspacing of <code>RtfCells</code> in this
164      * <code>RtfRow</code> */

165     private int cellspacing = 14;
166     /** The borders of this <code>RtfRow</code> */
167     private int borders = 0;
168     /** The border color of this <code>RtfRow</code> */
169     private java.awt.Color JavaDoc borderColor = null;
170     /** The border width of this <code>RtfRow</code> */
171     private float borderWidth = 0;
172
173     /** Original Row */
174     private Row origRow = null;
175
176     /**
177      * Create a new <code>RtfRow</code>.
178      *
179      * @param writer The <code>RtfWriter</code> that this <code>RtfRow</code> belongs to
180      * @param mainTable The <code>RtfTable</code> that created this
181      * <code>RtfRow</code>
182      */

183     public RtfRow(RtfWriter writer, RtfTable mainTable) {
184         super();
185         this.writer = writer;
186         this.mainTable = mainTable;
187     }
188
189     /**
190      * Pregenerate the <code>RtfCell</code>s in this <code>RtfRow</code>.
191      *
192      * @param columns The number of <code>RtfCell</code>s to be generated.
193      */

194     public void pregenerateRows(int columns) {
195         for (int i = 0; i < columns; i++) {
196             RtfCell rtfCell = new RtfCell(writer, mainTable);
197             cells.add(rtfCell);
198         }
199     }
200
201     /**
202      * Import a <code>Row</code>.
203      * <P>
204      * All the parameters are taken from the <code>RtfTable</code> which contains
205      * this <code>RtfRow</code> and they do exactely what they say
206      * @param row
207      * @param propWidths in percent
208      * @param tableWidth in percent
209      * @param pageWidth
210      * @param cellpadding
211      * @param cellspacing
212      * @param borders
213      * @param borderColor
214      * @param borderWidth
215      * @param y
216      * @return true if importing the row succeeded
217      */

218     public boolean importRow(Row row, float[] propWidths, int tableWidth, int pageWidth, int cellpadding,
219                              int cellspacing, int borders, java.awt.Color JavaDoc borderColor, float borderWidth,
220                              int y) {
221         // the width of this row is the absolute witdh, calculated from the
222
// proportional with of the table and the total width of the page
223
this.origRow = row;
224         this.width = pageWidth * tableWidth / 100;
225         this.cellpadding = cellpadding;
226         this.cellspacing = cellspacing;
227         this.borders = borders;
228         this.borderColor = borderColor;
229         this.borderWidth = borderWidth;
230
231         if (this.borderWidth > 2) this.borderWidth = 2;
232
233         int cellLeft = 0;
234         for (int i = 0; i < row.getColumns(); i++) {
235             Element cell = (Element) row.getCell(i);
236
237             // cellWidth is an absolute argument
238
// it's based on the absolute of this row and the proportional
239
// width of this column
240
int cellWidth = (int) (width * propWidths[i] / 100);
241             if (cell != null) {
242                 if (cell.type() == Element.CELL) {
243                     RtfCell rtfCell = (RtfCell) cells.get(i);
244                     cellLeft = rtfCell.importCell((Cell) cell, cellLeft, cellWidth, i, y, cellpadding);
245                 }
246             } else {
247                 RtfCell rtfCell = (RtfCell) cells.get(i);
248                 cellLeft = rtfCell.importCell(null, cellLeft, cellWidth, i, y, cellpadding);
249             }
250         }
251
252         // recalculate the cell right border and the cumulative width
253
// on col spanning cells.
254
// col + row spanning cells are also handled by this loop, because the real cell of
255
// the upper left corner in such an col, row matrix is copied as first cell
256
// in each row in this matrix
257
int columns = row.getColumns();
258         for (int i = 0; i < columns; i++) {
259             RtfCell firstCell = (RtfCell) cells.get(i);
260             Cell cell = firstCell.getStore();
261             int cols = 0;
262             if(cell != null) {
263                 cols = cell.getColspan();
264             }
265             if (cols > 1) {
266                 RtfCell lastCell = (RtfCell) cells.get(i + cols - 1);
267                 firstCell.setCellRight(lastCell.getCellRight());
268                 int width = firstCell.getCellWidth();
269                 for (int j = i + 1; j < i + cols; j++) {
270                     RtfCell cCell = (RtfCell) cells.get(j);
271                     width += cCell.getCellWidth();
272                 }
273                 firstCell.setCellWidth(width);
274                 i += cols - 1;
275             }
276         }
277         return true;
278     }
279
280     /**
281      * Write the <code>RtfRow</code> to the specified <code>OutputStream</code>.
282      *
283      * @param os The <code>OutputStream</code> to which this <code>RtfRow</code>
284      * should be written to.
285      * @param rowNum The <code>index</code> of this row in the containing table.
286      * @param table The <code>Table</code> which contains the original <code>Row</code>.
287      * @return true if writing the row succeeded
288      * @throws DocumentException
289      * @throws IOException
290      */

291     public boolean writeRow(ByteArrayOutputStream JavaDoc os, int rowNum, Table table) throws DocumentException,
292             IOException JavaDoc {
293         os.write(RtfWriter.escape);
294         os.write(rowBegin);
295         os.write((byte) '\n');
296         os.write(RtfWriter.escape);
297         os.write(rowWidthStyle);
298         os.write(RtfWriter.escape);
299         os.write(rowWidth);
300         writeInt(os, width);
301 // os.write(RtfWriter.escape);
302
// os.write(rowAutofit);
303
if (mainTable.getOriginalTable().isCellsFitPage()) {
304             os.write(RtfWriter.escape);
305             os.write(rowKeep);
306         }
307         // check if this row is a header row
308
if (rowNum < table.getLastHeaderRow() + 1) {
309             os.write(RtfWriter.escape);
310             os.write(rowHeader);
311         }
312         os.write(RtfWriter.escape);
313         switch (this.origRow.getHorizontalAlignment()) {
314             case Element.ALIGN_LEFT:
315                 os.write(rowAlignLeft);
316                 break;
317             case Element.ALIGN_CENTER:
318                 os.write(rowAlignCenter);
319                 break;
320             case Element.ALIGN_RIGHT:
321                 os.write(rowAlignRight);
322                 break;
323             default :
324                 os.write(rowAlignLeft);
325                 break;
326         }
327         os.write(RtfWriter.escape);
328         os.write(graphLeft);
329         writeInt(os, 10);
330         if (((borders & Rectangle.LEFT) == Rectangle.LEFT) && (borderWidth > 0)) {
331             writeBorder(os, rowBorderLeft);
332         }
333         if (((borders & Rectangle.TOP) == Rectangle.TOP) && (borderWidth > 0)) {
334             writeBorder(os, rowBorderTop);
335         }
336         if (((borders & Rectangle.BOTTOM) == Rectangle.BOTTOM) && (borderWidth > 0)) {
337             writeBorder(os, rowBorderBottom);
338         }
339         if (((borders & Rectangle.RIGHT) == Rectangle.RIGHT) && (borderWidth > 0)) {
340             writeBorder(os, rowBorderRight);
341         }
342         if (((borders & Rectangle.BOX) == Rectangle.BOX) && (borderWidth > 0)) {
343             writeBorder(os, rowBorderInlineHorizontal);
344             writeBorder(os, rowBorderInlineVertical);
345         }
346
347         if (cellspacing > 0) {
348             os.write(RtfWriter.escape);
349             os.write(rowSpacingLeft);
350             writeInt(os, cellspacing / 2);
351             os.write(RtfWriter.escape);
352             os.write(rowSpacingLeftStyle);
353             os.write(RtfWriter.escape);
354             os.write(rowSpacingTop);
355             writeInt(os, cellspacing / 2);
356             os.write(RtfWriter.escape);
357             os.write(rowSpacingTopStyle);
358             os.write(RtfWriter.escape);
359             os.write(rowSpacingBottom);
360             writeInt(os, cellspacing / 2);
361             os.write(RtfWriter.escape);
362             os.write(rowSpacingBottomStyle);
363             os.write(RtfWriter.escape);
364             os.write(rowSpacingRight);
365             writeInt(os, cellspacing / 2);
366             os.write(RtfWriter.escape);
367             os.write(rowSpacingRightStyle);
368         }
369         os.write(RtfWriter.escape);
370         os.write(rowPaddingLeft);
371         writeInt(os, cellpadding / 2);
372         os.write(RtfWriter.escape);
373         os.write(rowPaddingRight);
374         writeInt(os, cellpadding / 2);
375         os.write(RtfWriter.escape);
376         os.write(rowPaddingLeftStyle);
377         os.write(RtfWriter.escape);
378         os.write(rowPaddingRightStyle);
379         os.write((byte) '\n');
380
381         Iterator JavaDoc cellIterator = cells.iterator();
382         while (cellIterator.hasNext()) {
383             RtfCell cell = (RtfCell) cellIterator.next();
384             cell.writeCellSettings(os);
385         }
386
387         os.write(RtfWriter.escape);
388         os.write("intbl".getBytes());
389
390         cellIterator = cells.iterator();
391         while (cellIterator.hasNext()) {
392             RtfCell cell = (RtfCell) cellIterator.next();
393             cell.writeCellContent(os);
394         }
395         os.write(RtfWriter.delimiter);
396         os.write(RtfWriter.escape);
397         os.write(rowEnd);
398         return true;
399     }
400
401
402     private void writeBorder(ByteArrayOutputStream JavaDoc os, byte[] borderType) throws IOException JavaDoc {
403         // horizontal and vertical, top, left, bottom, right
404
os.write(RtfWriter.escape);
405         os.write(borderType);
406         // line style
407
os.write(RtfWriter.escape);
408         os.write(RtfRow.tableBorder);
409         // borderwidth
410
os.write(RtfWriter.escape);
411         os.write(RtfRow.tableBorderWidth);
412         writeInt(os, (int) (borderWidth * RtfWriter.TWIPSFACTOR));
413         // border color
414
os.write(RtfWriter.escape);
415         os.write(RtfRow.tableBorderColor);
416         if (borderColor == null) {
417             writeInt(os, writer.addColor(new Color JavaDoc(0, 0, 0)));
418         } else {
419             writeInt(os, writer.addColor(borderColor));
420         }
421         os.write((byte) '\n');
422     }
423
424
425     /**
426      * <code>RtfTable</code>s call this method from their own setMerge() to
427      * specify that a certain other cell is to be merged with it.
428      *
429      * @param x The column position of the cell to be merged
430      * @param mergeType The merge type specifies the kind of merge to be applied
431      * (MERGE_HORIZ_PREV, MERGE_VERT_PREV, MERGE_BOTH_PREV)
432      * @param mergeCell The <code>RtfCell</code> that the cell at x and y is to
433      * be merged with
434      */

435     public void setMerge(int x, int mergeType, RtfCell mergeCell) {
436         RtfCell cell = (RtfCell) cells.get(x);
437         cell.setMerge(mergeType, mergeCell);
438     }
439
440     /*
441      * Write an Integer to the Outputstream.
442      *
443      * @param out The <code>OutputStream</code> to be written to.
444      * @param i The int to be written.
445      */

446     private void writeInt(ByteArrayOutputStream JavaDoc out, int i) throws IOException JavaDoc {
447         out.write(Integer.toString(i).getBytes());
448     }
449 }
450
Popular Tags