KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RtfRow.java 2776 2007-05-23 20:01:40Z hallm $
3  * $Name$
4  *
5  * Copyright 2001, 2002, 2003, 2004, 2005 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.table;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56 import java.util.ArrayList JavaDoc;
57
58 import com.lowagie.text.Cell;
59 import com.lowagie.text.Element;
60 import com.lowagie.text.Row;
61 import com.lowagie.text.rtf.RtfElement;
62 import com.lowagie.text.rtf.document.RtfDocument;
63
64
65 /**
66  * The RtfRow wraps one Row for a RtfTable.
67  * INTERNAL USE ONLY
68  *
69  * @version $Id: RtfRow.java 2776 2007-05-23 20:01:40Z hallm $
70  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
71  * @author Steffen Stundzig
72  * @author Lorenz Maierhofer
73  * @author Thomas Bickel (tmb99@inode.at)
74  */

75 public class RtfRow extends RtfElement {
76
77     /**
78      * Constant for the RtfRow beginning
79      */

80     private static final byte[] ROW_BEGIN = "\\trowd".getBytes();
81     /**
82      * Constant for the RtfRow width style
83      */

84     private static final byte[] ROW_WIDTH_STYLE = "\\trftsWidth3".getBytes();
85     /**
86      * Constant for the RtfRow width
87      */

88     private static final byte[] ROW_WIDTH = "\\trwWidth".getBytes();
89     /**
90      * Constant to specify that this RtfRow are not to be broken across pages
91      */

92     private static final byte[] ROW_KEEP_TOGETHER = "\\trkeep".getBytes();
93     /**
94      * Constant to specify that this is a header RtfRow
95      */

96     private static final byte[] ROW_HEADER_ROW = "\\trhdr".getBytes();
97     /**
98      * Constant for left alignment of this RtfRow
99      */

100     private static final byte[] ROW_ALIGN_LEFT = "\\trql".getBytes();
101     /**
102      * Constant for right alignment of this RtfRow
103      */

104     private static final byte[] ROW_ALIGN_RIGHT = "\\trqr".getBytes();
105     /**
106      * Constant for center alignment of this RtfRow
107      */

108     private static final byte[] ROW_ALIGN_CENTER = "\\trqc".getBytes();
109     /**
110      * Constant for justified alignment of this RtfRow
111      */

112     private static final byte[] ROW_ALIGN_JUSTIFIED = "\\trqj".getBytes();
113     /**
114      * Constant for the graph style of this RtfRow
115      */

116     private static final byte[] ROW_GRAPH = "\\trgaph10".getBytes();
117     /**
118      * Constant for the cell left spacing
119      */

120     private static final byte[] ROW_CELL_SPACING_LEFT = "\\trspdl".getBytes();
121     /**
122      * Constant for the cell top spacing
123      */

124     private static final byte[] ROW_CELL_SPACING_TOP = "\\trspdt".getBytes();
125     /**
126      * Constant for the cell right spacing
127      */

128     private static final byte[] ROW_CELL_SPACING_RIGHT = "\\trspdr".getBytes();
129     /**
130      * Constant for the cell bottom spacing
131      */

132     private static final byte[] ROW_CELL_SPACING_BOTTOM = "\\trspdb".getBytes();
133     /**
134      * Constant for the cell left spacing style
135      */

136     private static final byte[] ROW_CELL_SPACING_LEFT_STYLE = "\\trspdfl3".getBytes();
137     /**
138      * Constant for the cell top spacing style
139      */

140     private static final byte[] ROW_CELL_SPACING_TOP_STYLE = "\\trspdft3".getBytes();
141     /**
142      * Constant for the cell right spacing style
143      */

144     private static final byte[] ROW_CELL_SPACING_RIGHT_STYLE = "\\trspdfr3".getBytes();
145     /**
146      * Constant for the cell bottom spacing style
147      */

148     private static final byte[] ROW_CELL_SPACING_BOTTOM_STYLE = "\\trspdfb3".getBytes();
149     /**
150      * Constant for the cell left padding
151      */

152     private static final byte[] ROW_CELL_PADDING_LEFT = "\\trpaddl".getBytes();
153     /**
154      * Constant for the cell right padding
155      */

156     private static final byte[] ROW_CELL_PADDING_RIGHT = "\\trpaddr".getBytes();
157     /**
158      * Constant for the cell left padding style
159      */

160     private static final byte[] ROW_CELL_PADDING_LEFT_STYLE = "\\trpaddfl3".getBytes();
161     /**
162      * Constant for the cell right padding style
163      */

164     private static final byte[] ROW_CELL_PADDING_RIGHT_STYLE = "\\trpaddfr3".getBytes();
165     /**
166      * Constant for the end of a row
167      */

168     private static final byte[] ROW_END = "\\row".getBytes();
169
170     /**
171      * The RtfTable this RtfRow belongs to
172      */

173     private RtfTable parentTable = null;
174     /**
175      * The cells of this RtfRow
176      */

177     private ArrayList JavaDoc cells = null;
178     /**
179      * The width of this row
180      */

181     private int width = 0;
182     /**
183      * The row number
184      */

185     private int rowNumber = 0;
186     
187     /**
188      * Constructs a RtfRow for a Row.
189      *
190      * @param doc The RtfDocument this RtfRow belongs to
191      * @param rtfTable The RtfTable this RtfRow belongs to
192      * @param row The Row this RtfRow is based on
193      * @param rowNumber The number of this row
194      */

195     protected RtfRow(RtfDocument doc, RtfTable rtfTable, Row row, int rowNumber) {
196         super(doc);
197         this.parentTable = rtfTable;
198         this.rowNumber = rowNumber;
199         importRow(row);
200     }
201     
202     /**
203      * Imports a Row and copies all settings
204      *
205      * @param row The Row to import
206      */

207     private void importRow(Row row) {
208         this.cells = new ArrayList JavaDoc();
209         this.width = this.document.getDocumentHeader().getPageSetting().getPageWidth() - this.document.getDocumentHeader().getPageSetting().getMarginLeft() - this.document.getDocumentHeader().getPageSetting().getMarginRight();
210         this.width = (int) (this.width * this.parentTable.getTableWidthPercent() / 100);
211         
212         int cellRight = 0;
213         int cellWidth = 0;
214         for(int i = 0; i < row.getColumns(); i++) {
215             cellWidth = (int) (this.width * this.parentTable.getProportionalWidths()[i] / 100);
216             cellRight = cellRight + cellWidth;
217             
218             Cell cell = (Cell) row.getCell(i);
219             RtfCell rtfCell = new RtfCell(this.document, this, cell);
220             rtfCell.setCellRight(cellRight);
221             rtfCell.setCellWidth(cellWidth);
222             this.cells.add(rtfCell);
223         }
224     }
225     
226     /**
227      * Performs a second pass over all cells to handle cell row/column spanning.
228      */

229     protected void handleCellSpanning() {
230         RtfCell deletedCell = new RtfCell(true);
231         for(int i = 0; i < this.cells.size(); i++) {
232             RtfCell rtfCell = (RtfCell) this.cells.get(i);
233             if(rtfCell.getColspan() > 1) {
234                 int cSpan = rtfCell.getColspan();
235                 for(int j = i + 1; j < i + cSpan; j++) {
236                     if(j < this.cells.size()) {
237                         RtfCell rtfCellMerge = (RtfCell) this.cells.get(j);
238                         rtfCell.setCellRight(rtfCell.getCellRight() + rtfCellMerge.getCellWidth());
239                         rtfCell.setCellWidth(rtfCell.getCellWidth() + rtfCellMerge.getCellWidth());
240                         this.cells.set(j, deletedCell);
241                     }
242                 }
243             }
244             if(rtfCell.getRowspan() > 1) {
245                 ArrayList JavaDoc rows = this.parentTable.getRows();
246                 for(int j = 1; j < rtfCell.getRowspan(); j++) {
247                     RtfRow mergeRow = (RtfRow) rows.get(this.rowNumber + j);
248                     if(this.rowNumber + j < rows.size()) {
249                         RtfCell rtfCellMerge = (RtfCell) mergeRow.getCells().get(i);
250                         rtfCellMerge.setCellMergeChild(rtfCell);
251                     }
252                     if(rtfCell.getColspan() > 1) {
253                         int cSpan = rtfCell.getColspan();
254                         for(int k = i + 1; k < i + cSpan; k++) {
255                             if(k < mergeRow.getCells().size()) {
256                                 mergeRow.getCells().set(k, deletedCell);
257                             }
258                         }
259                     }
260                 }
261             }
262         }
263     }
264
265     /**
266      * Cleans the deleted RtfCells from the total RtfCells.
267      */

268     protected void cleanRow() {
269         int i = 0;
270         while(i < this.cells.size()) {
271             if(((RtfCell) this.cells.get(i)).isDeleted()) {
272                 this.cells.remove(i);
273             } else {
274                 i++;
275             }
276         }
277     }
278     
279     /**
280      * Writes the row definition/settings.
281      *
282      * @return A byte array with the row definitions/settings.
283      * @deprecated replaced by {@link #writeRowDefinitions(OutputStream)}
284      */

285     private byte[] writeRowDefinitions() {
286         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
287         try {
288             writeRowDefinitions(result);
289         } catch(IOException JavaDoc ioe) {
290             ioe.printStackTrace();
291         }
292         return result.toByteArray();
293     }
294     /**
295      * Writes the row definition/settings.
296      */

297     private void writeRowDefinitions(final OutputStream JavaDoc result) throws IOException JavaDoc
298     {
299         result.write(ROW_BEGIN);
300         result.write('\n');
301         result.write(ROW_WIDTH_STYLE);
302         result.write(ROW_WIDTH);
303         result.write(intToByteArray(this.width));
304         if(this.parentTable.getCellsFitToPage()) {
305             result.write(ROW_KEEP_TOGETHER);
306         }
307         if(this.rowNumber <= this.parentTable.getHeaderRows()) {
308             result.write(ROW_HEADER_ROW);
309         }
310         switch (this.parentTable.getAlignment()) {
311             case Element.ALIGN_LEFT:
312                 result.write(ROW_ALIGN_LEFT);
313                 break;
314             case Element.ALIGN_RIGHT:
315                 result.write(ROW_ALIGN_RIGHT);
316                 break;
317             case Element.ALIGN_CENTER:
318                 result.write(ROW_ALIGN_CENTER);
319                 break;
320             case Element.ALIGN_JUSTIFIED:
321             case Element.ALIGN_JUSTIFIED_ALL:
322                 result.write(ROW_ALIGN_JUSTIFIED);
323                 break;
324         }
325         result.write(ROW_GRAPH);
326         
327         //.result.write(this.parentTable.getBorders().write());
328
this.parentTable.getBorders().writeContent(result);
329         
330         if(this.parentTable.getCellSpacing() > 0) {
331             result.write(ROW_CELL_SPACING_LEFT);
332             result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2)));
333             result.write(ROW_CELL_SPACING_LEFT_STYLE);
334             result.write(ROW_CELL_SPACING_TOP);
335             result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2)));
336             result.write(ROW_CELL_SPACING_TOP_STYLE);
337             result.write(ROW_CELL_SPACING_RIGHT);
338             result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2)));
339             result.write(ROW_CELL_SPACING_RIGHT_STYLE);
340             result.write(ROW_CELL_SPACING_BOTTOM);
341             result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2)));
342             result.write(ROW_CELL_SPACING_BOTTOM_STYLE);
343         }
344         
345         result.write(ROW_CELL_PADDING_LEFT);
346         result.write(intToByteArray((int) (this.parentTable.getCellPadding() / 2)));
347         result.write(ROW_CELL_PADDING_RIGHT);
348         result.write(intToByteArray((int) (this.parentTable.getCellPadding() / 2)));
349         result.write(ROW_CELL_PADDING_LEFT_STYLE);
350         result.write(ROW_CELL_PADDING_RIGHT_STYLE);
351         
352         result.write('\n');
353         
354         for(int i = 0; i < this.cells.size(); i++) {
355             RtfCell rtfCell = (RtfCell) this.cells.get(i);
356             //result.write(rtfCell.writeDefinition());
357
rtfCell.writeDefinition(result);
358         }
359     }
360     
361     /**
362      * Writes the content of this RtfRow
363      *
364      * @return A byte array with the content of this RtfRow
365      * @deprecated replaced by {@link #writeContent(OutputStream)}
366      */

367     public byte[] write()
368     {
369         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
370         try {
371             writeContent(result);
372         } catch(IOException JavaDoc ioe) {
373             ioe.printStackTrace();
374         }
375         return result.toByteArray();
376     }
377     /**
378      * Writes the content of this RtfRow
379      */

380     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
381     {
382         //.result.write(writeRowDefinitions());
383
writeRowDefinitions(result);
384         
385         for(int i = 0; i < this.cells.size(); i++) {
386             RtfCell rtfCell = (RtfCell) this.cells.get(i);
387             //.result.write(rtfCell.write());
388
rtfCell.writeContent(result);
389         }
390
391         result.write(DELIMITER);
392
393         if(this.document.getDocumentSettings().isOutputTableRowDefinitionAfter()) {
394             //.result.write(writeRowDefinitions());
395
writeRowDefinitions(result);
396         }
397
398         result.write(ROW_END);
399         result.write("\n".getBytes());
400     }
401     
402     /**
403      * Gets the parent RtfTable of this RtfRow
404      *
405      * @return The parent RtfTable of this RtfRow
406      */

407     protected RtfTable getParentTable() {
408         return this.parentTable;
409     }
410     
411     /**
412      * Gets the cells of this RtfRow
413      *
414      * @return The cells of this RtfRow
415      */

416     protected ArrayList JavaDoc getCells() {
417         return this.cells;
418     }
419 }
420
Popular Tags