KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RtfCell.java 2796 2007-05-27 09:42:20Z psoares33 $
3  * $Name: $
4  *
5  * Copyright 2001, 2002, 2003, 2004 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 import java.util.Iterator JavaDoc;
58 import java.util.Properties JavaDoc;
59
60 import com.lowagie.text.BadElementException;
61 import com.lowagie.text.Cell;
62 import com.lowagie.text.DocumentException;
63 import com.lowagie.text.Element;
64 import com.lowagie.text.List;
65 import com.lowagie.text.Paragraph;
66 import com.lowagie.text.rtf.RtfBasicElement;
67 import com.lowagie.text.rtf.RtfExtendedElement;
68 import com.lowagie.text.rtf.document.RtfDocument;
69 import com.lowagie.text.rtf.style.RtfColor;
70 import com.lowagie.text.rtf.style.RtfParagraphStyle;
71 import com.lowagie.text.rtf.text.RtfParagraph;
72
73
74 /**
75  * The RtfCell wraps a Cell, but can also be added directly to a Table.
76  * The RtfCell is an extension of Cell, that supports a multitude of different
77  * borderstyles.
78  *
79  * @version $Id: RtfCell.java 2796 2007-05-27 09:42:20Z psoares33 $
80  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
81  * @author Steffen Stundzig
82  * @author Benoit Wiart
83  * @author Thomas Bickel (tmb99@inode.at)
84  * @see com.lowagie.text.rtf.table.RtfBorder
85  */

86 public class RtfCell extends Cell implements RtfExtendedElement {
87
88     /**
89      * This cell is not merged
90      */

91     private static final int MERGE_NONE = 0;
92     /**
93      * This cell is the parent cell of a vertical merge operation
94      */

95     private static final int MERGE_VERT_PARENT = 1;
96     /**
97      * This cell is a child cell of a vertical merge operation
98      */

99     private static final int MERGE_VERT_CHILD = 2;
100
101     /**
102      * The parent RtfRow of this RtfCell
103      */

104     private RtfRow parentRow = null;
105     /**
106      * The content of this RtfCell
107      */

108     private ArrayList JavaDoc content = null;
109     /**
110      * The right margin of this RtfCell
111      */

112     private int cellRight = 0;
113     /**
114      * The width of this RtfCell
115      */

116     private int cellWidth = 0;
117     /**
118      * The borders of this RtfCell
119      */

120     private RtfBorderGroup borders = null;
121     
122     /**
123      * The background color of this RtfCell
124      */

125     private RtfColor backgroundColor = null;
126     /**
127      * The padding of this RtfCell
128      */

129     private int cellPadding = 0;
130     /**
131      * The merge type of this RtfCell
132      */

133     private int mergeType = MERGE_NONE;
134     /**
135      * The RtfDocument this RtfCell belongs to
136      */

137     private RtfDocument document = null;
138     /**
139      * Whether this RtfCell is in a header
140      */

141     private boolean inHeader = false;
142     /**
143      * Whether this RtfCell is a placeholder for a removed table cell.
144      */

145     private boolean deleted = false;
146
147     /**
148      * Constructs an empty RtfCell
149      */

150     public RtfCell() {
151         super();
152         this.borders = new RtfBorderGroup();
153         verticalAlignment = ALIGN_MIDDLE;
154     }
155     
156     /**
157      * Constructs a RtfCell based upon a String
158      *
159      * @param content The String to base the RtfCell on
160      */

161     public RtfCell(String JavaDoc content) {
162         super(content);
163         this.borders = new RtfBorderGroup();
164         verticalAlignment = ALIGN_MIDDLE;
165     }
166     
167     /**
168      * Constructs a RtfCell based upon an Element
169      *
170      * @param element The Element to base the RtfCell on
171      * @throws BadElementException If the Element is not valid
172      */

173     public RtfCell(Element element) throws BadElementException {
174         super(element);
175         this.borders = new RtfBorderGroup();
176         verticalAlignment = ALIGN_MIDDLE;
177     }
178     
179     /**
180      * Constructs a RtfCell based upon certain Properties
181      *
182      * @param properties The Properties for this RtfCell
183      */

184     public RtfCell(Properties JavaDoc properties) {
185         super(properties);
186         this.borders = new RtfBorderGroup();
187         verticalAlignment = ALIGN_MIDDLE;
188     }
189     
190     /**
191      * Constructs a deleted RtfCell.
192      *
193      * @param deleted Whether this RtfCell is actually deleted.
194      */

195     protected RtfCell(boolean deleted) {
196         super();
197         this.deleted = deleted;
198         verticalAlignment = ALIGN_MIDDLE;
199     }
200     
201     /**
202      * Constructs a RtfCell based on a Cell.
203      *
204      * @param doc The RtfDocument this RtfCell belongs to
205      * @param row The RtfRow this RtfCell lies in
206      * @param cell The Cell to base this RtfCell on
207      */

208     protected RtfCell(RtfDocument doc, RtfRow row, Cell cell) {
209         this.document = doc;
210         this.parentRow = row;
211         importCell(cell);
212     }
213
214     /**
215      * Imports the Cell properties into the RtfCell
216      *
217      * @param cell The Cell to import
218      */

219     private void importCell(Cell cell) {
220         this.content = new ArrayList JavaDoc();
221         
222         if(cell == null) {
223             this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, this.parentRow.getParentTable().getBorders());
224             return;
225         }
226         
227         this.colspan = cell.getColspan();
228         this.rowspan = cell.getRowspan();
229         if(cell.getRowspan() > 1) {
230             this.mergeType = MERGE_VERT_PARENT;
231         }
232         if(cell instanceof RtfCell) {
233             this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, ((RtfCell) cell).getBorders());
234         } else {
235             this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, cell.getBorder(), cell.getBorderWidth(), cell.getBorderColor());
236         }
237         this.verticalAlignment = cell.getVerticalAlignment();
238         if(cell.getBackgroundColor() == null) {
239             this.backgroundColor = new RtfColor(this.document, 255, 255, 255);
240         } else {
241             this.backgroundColor = new RtfColor(this.document, cell.getBackgroundColor());
242         }
243         
244         this.cellPadding = (int) this.parentRow.getParentTable().getCellPadding();
245         
246         Iterator JavaDoc cellIterator = cell.getElements();
247         Paragraph container = null;
248         while(cellIterator.hasNext()) {
249             try {
250                 Element element = (Element) cellIterator.next();
251                 // should we wrap it in a paragraph
252
if(!(element instanceof Paragraph) && !(element instanceof List JavaDoc)) {
253                     if(container != null) {
254                         container.add(element);
255                     } else {
256                         container = new Paragraph();
257                         container.setAlignment(cell.getHorizontalAlignment());
258                         container.add(element);
259                     }
260                 } else {
261                     if(container != null) {
262                         RtfBasicElement rtfElement = this.document.getMapper().mapElement(container);
263                         rtfElement.setInTable(true);
264                         this.content.add(rtfElement);
265                         container = null;
266                     }
267                     // if horizontal alignment is undefined overwrite
268
// with that of enclosing cell
269
if (element instanceof Paragraph && ((Paragraph) element).getAlignment() == Element.ALIGN_UNDEFINED) {
270                         ((Paragraph) element).setAlignment(cell.getHorizontalAlignment());
271                     }
272
273                     RtfBasicElement rtfElement = this.document.getMapper().mapElement(element);
274                     rtfElement.setInTable(true);
275                     this.content.add(rtfElement);
276                 }
277             } catch(DocumentException de) {
278                 de.printStackTrace();
279             }
280         }
281         if(container != null) {
282             try {
283                 RtfBasicElement rtfElement = this.document.getMapper().mapElement(container);
284                 rtfElement.setInTable(true);
285                 this.content.add(rtfElement);
286             } catch(DocumentException de) {
287                 de.printStackTrace();
288             }
289         }
290     }
291     
292     /**
293      * Write the cell definition part of this RtfCell
294      *
295      * @return A byte array with the cell definition
296      * @deprecated replaced by {@link #writeDefinition(OutputStream)}
297      */

298     public byte[] writeDefinition() {
299         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
300         try {
301             writeDefinition(result);
302         } catch(IOException JavaDoc ioe) {
303             ioe.printStackTrace();
304         }
305         
306         return result.toByteArray();
307     }
308     
309     /**
310      * Write the cell definition part of this RtfCell
311      */

312     public void writeDefinition(final OutputStream JavaDoc result) throws IOException JavaDoc
313     {
314         if(this.mergeType == MERGE_VERT_PARENT) {
315             result.write("\\clvmgf".getBytes());
316         } else if(this.mergeType == MERGE_VERT_CHILD) {
317             result.write("\\clvmrg".getBytes());
318         }
319         switch (verticalAlignment) {
320             case Element.ALIGN_BOTTOM:
321                 result.write("\\clvertalb".getBytes());
322                 break;
323             case Element.ALIGN_CENTER:
324             case Element.ALIGN_MIDDLE:
325                 result.write("\\clvertalc".getBytes());
326                 break;
327             case Element.ALIGN_TOP:
328                 result.write("\\clvertalt".getBytes());
329                 break;
330         }
331         //.result.write(this.borders.write());
332
this.borders.writeContent(result);
333
334         if(this.backgroundColor != null) {
335             result.write("\\clcbpat".getBytes());
336             result.write(intToByteArray(this.backgroundColor.getColorNumber()));
337         }
338         result.write('\n');
339         
340         result.write("\\clftsWidth3".getBytes());
341         result.write('\n');
342         
343         result.write("\\clwWidth".getBytes());
344         result.write(intToByteArray(this.cellWidth));
345         result.write('\n');
346         
347         if(this.cellPadding > 0) {
348             result.write("\\clpadl".getBytes());
349             result.write(intToByteArray(this.cellPadding / 2));
350             result.write("\\clpadt".getBytes());
351             result.write(intToByteArray(this.cellPadding / 2));
352             result.write("\\clpadr".getBytes());
353             result.write(intToByteArray(this.cellPadding / 2));
354             result.write("\\clpadb".getBytes());
355             result.write(intToByteArray(this.cellPadding / 2));
356             result.write("\\clpadfl3".getBytes());
357             result.write("\\clpadft3".getBytes());
358             result.write("\\clpadfr3".getBytes());
359             result.write("\\clpadfb3".getBytes());
360         }
361         result.write("\\cellx".getBytes());
362         result.write(intToByteArray(this.cellRight));
363     }
364
365     
366     /**
367      * Write the content of this RtfCell
368      *
369      * @return A byte array with the content of this RtfCell
370      * @deprecated replaced by {@link #writeContent(OutputStream)}
371      */

372     public byte[] write()
373     {
374         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
375         try {
376             writeContent(result);
377         } catch(IOException JavaDoc ioe) {
378             ioe.printStackTrace();
379         }
380         
381         return result.toByteArray();
382     }
383     /**
384      * Write the content of this RtfCell
385      */

386     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
387     {
388         if(this.content.size() == 0) {
389             result.write(RtfParagraph.PARAGRAPH_DEFAULTS);
390             if(this.parentRow.getParentTable().getTableFitToPage()) {
391                 result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT);
392             }
393             result.write(RtfParagraph.IN_TABLE);
394         } else {
395             for(int i = 0; i < this.content.size(); i++) {
396                 RtfBasicElement rtfElement = (RtfBasicElement) this.content.get(i);
397                 if(rtfElement instanceof RtfParagraph) {
398                     ((RtfParagraph) rtfElement).setKeepTogetherWithNext(this.parentRow.getParentTable().getTableFitToPage());
399                 }
400                 //.result.write(rtfElement.write());
401
rtfElement.writeContent(result);
402                 if(rtfElement instanceof RtfParagraph && i < (this.content.size() - 1)) {
403                     result.write(RtfParagraph.PARAGRAPH);
404                 }
405             }
406         }
407         result.write("\\cell".getBytes());
408     }
409
410     /**
411      * Sets the right margin of this cell. Used in merge operations
412      *
413      * @param cellRight The right margin to use
414      */

415     protected void setCellRight(int cellRight) {
416         this.cellRight = cellRight;
417     }
418     
419     /**
420      * Gets the right margin of this RtfCell
421      *
422      * @return The right margin of this RtfCell.
423      */

424     protected int getCellRight() {
425         return this.cellRight;
426     }
427     
428     /**
429      * Sets the cell width of this RtfCell. Used in merge operations.
430      *
431      * @param cellWidth The cell width to use
432      */

433     protected void setCellWidth(int cellWidth) {
434         this.cellWidth = cellWidth;
435     }
436     
437     /**
438      * Gets the cell width of this RtfCell
439      *
440      * @return The cell width of this RtfCell
441      */

442     protected int getCellWidth() {
443         return this.cellWidth;
444     }
445     
446     /**
447      * Gets the cell padding of this RtfCell
448      *
449      * @return The cell padding of this RtfCell
450      */

451     protected int getCellpadding() {
452         return this.cellPadding;
453     }
454
455     /**
456      * Gets the borders of this RtfCell
457      *
458      * @return The borders of this RtfCell
459      */

460     protected RtfBorderGroup getBorders() {
461         return this.borders;
462     }
463     
464     /**
465      * Set the borders of this RtfCell
466      *
467      * @param borderGroup The RtfBorderGroup to use as borders
468      */

469     public void setBorders(RtfBorderGroup borderGroup) {
470         this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, borderGroup);
471     }
472
473     /**
474      * Get the background color of this RtfCell
475      *
476      * @return The background color of this RtfCell
477      */

478     protected RtfColor getRtfBackgroundColor() {
479         return this.backgroundColor;
480     }
481
482     /**
483      * Merge this cell into the parent cell.
484      *
485      * @param mergeParent The RtfCell to merge with
486      */

487     protected void setCellMergeChild(RtfCell mergeParent) {
488         this.mergeType = MERGE_VERT_CHILD;
489         this.cellWidth = mergeParent.getCellWidth();
490         this.cellRight = mergeParent.getCellRight();
491         this.cellPadding = mergeParent.getCellpadding();
492         this.borders = mergeParent.getBorders();
493         this.verticalAlignment = mergeParent.getVerticalAlignment();
494         this.backgroundColor = mergeParent.getRtfBackgroundColor();
495     }
496
497     /**
498      * Sets the RtfDocument this RtfCell belongs to
499      *
500      * @param doc The RtfDocument to use
501      */

502     public void setRtfDocument(RtfDocument doc) {
503         this.document = doc;
504     }
505     
506     /**
507      * Unused
508      * @param inTable
509      */

510     public void setInTable(boolean inTable) {
511     }
512     
513     /**
514      * Sets whether this RtfCell is in a header
515      *
516      * @param inHeader <code>True</code> if this RtfCell is in a header, <code>false</code> otherwise
517      */

518     public void setInHeader(boolean inHeader) {
519         this.inHeader = inHeader;
520         for(int i = 0; i < this.content.size(); i++) {
521             ((RtfBasicElement) this.content.get(i)).setInHeader(inHeader);
522         }
523     }
524
525     /**
526      * Transforms an integer into its String representation and then returns the bytes
527      * of that string.
528      *
529      * @param i The integer to convert
530      * @return A byte array representing the integer
531      */

532     private byte[] intToByteArray(int i) {
533         return Integer.toString(i).getBytes();
534     }
535     
536     /**
537      * Checks whether this RtfCell is a placeholder for
538      * a table cell that has been removed due to col/row spanning.
539      *
540      * @return <code>True</code> if this RtfCell is deleted, <code>false</code> otherwise.
541      */

542     public boolean isDeleted() {
543         return this.deleted;
544     }
545 }
546
Popular Tags