KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: RtfCell.java 2759 2007-05-16 10:52:31Z psoares33 $
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.Iterator JavaDoc;
57
58 import com.lowagie.text.Cell;
59 import com.lowagie.text.DocumentException;
60 import com.lowagie.text.Element;
61 import com.lowagie.text.Paragraph;
62 import com.lowagie.text.Rectangle;
63
64 /**
65  * A Helper Class for the <CODE>RtfWriter</CODE>.
66  * <P>
67  * Do not use it directly
68  *
69  * ONLY FOR USE WITH THE RtfWriter NOT with the RtfWriter2.
70  *
71  * Parts of this Class were contributed by Steffen Stundzig. Many thanks for the
72  * improvements.
73  * Updates by Benoit Wiart
74  * @deprecated Please move to the RtfWriter2 and associated classes.
75  */

76 public class RtfCell {
77     /** Constants for merging Cells */
78
79     /** A possible value for merging */
80     private static final int MERGE_HORIZ_FIRST = 1;
81     /** A possible value for merging */
82     private static final int MERGE_VERT_FIRST = 2;
83     /** A possible value for merging */
84     private static final int MERGE_BOTH_FIRST = 3;
85     /** A possible value for merging */
86     private static final int MERGE_HORIZ_PREV = 4;
87     /** A possible value for merging */
88     private static final int MERGE_VERT_PREV = 5;
89     /** A possible value for merging */
90     private static final int MERGE_BOTH_PREV = 6;
91
92     /**
93      * RTF Tags
94      */

95
96     /** First cell to merge with - Horizontal */
97     private static final byte[] cellMergeFirst = "clmgf".getBytes();
98     /** First cell to merge with - Vertical */
99     private static final byte[] cellVMergeFirst = "clvmgf".getBytes();
100     /** Merge cell with previous horizontal cell */
101     private static final byte[] cellMergePrev = "clmrg".getBytes();
102     /** Merge cell with previous vertical cell */
103     private static final byte[] cellVMergePrev = "clvmrg".getBytes();
104     /** Cell content vertical alignment bottom */
105     private static final byte[] cellVerticalAlignBottom = "clvertalb".getBytes();
106     /** Cell content vertical alignment center */
107     private static final byte[] cellVerticalAlignCenter = "clvertalc".getBytes();
108     /** Cell content vertical alignment top */
109     private static final byte[] cellVerticalAlignTop = "clvertalt".getBytes();
110     /** Cell border left */
111     private static final byte[] cellBorderLeft = "clbrdrl".getBytes();
112     /** Cell border right */
113     private static final byte[] cellBorderRight = "clbrdrr".getBytes();
114     /** Cell border top */
115     private static final byte[] cellBorderTop = "clbrdrt".getBytes();
116     /** Cell border bottom */
117     private static final byte[] cellBorderBottom = "clbrdrb".getBytes();
118     /** Cell background color */
119     private static final byte[] cellBackgroundColor = "clcbpat".getBytes();
120     /** Cell width format */
121     private static final byte[] cellWidthStyle = "clftsWidth3".getBytes();
122     /** Cell width */
123     private static final byte[] cellWidthTag = "clwWidth".getBytes();
124     /** Cell right border position */
125     private static final byte[] cellRightBorder = "cellx".getBytes();
126     /** Cell is part of table */
127     protected static final byte[] cellInTable = "intbl".getBytes();
128     /** End of cell */
129     private static final byte[] cellEnd = "cell".getBytes();
130
131     /** padding top */
132     private static final byte[] cellPaddingTop = "clpadt".getBytes();
133     /** padding top unit */
134     private static final byte[] cellPaddingTopUnit = "clpadft3".getBytes();
135     /** padding bottom */
136     private static final byte[] cellPaddingBottom = "clpadb".getBytes();
137     /** padding bottom unit */
138     private static final byte[] cellPaddingBottomUnit = "clpadfb3".getBytes();
139     /** padding left */
140     private static final byte[] cellPaddingLeft = "clpadl".getBytes();
141     /** padding left unit */
142     private static final byte[] cellPaddingLeftUnit = "clpadfl3".getBytes();
143     /** padding right */
144     private static final byte[] cellPaddingRight = "clpadr".getBytes();
145     /** padding right unit */
146     private static final byte[] cellPaddingRightUnit = "clpadfr3".getBytes();
147
148     /** The <code>RtfWriter</code> to which this <code>RtfCell</code> belongs. */
149     private RtfWriter writer = null;
150     /** The <code>RtfTable</code> to which this <code>RtfCell</code> belongs. */
151     private RtfTable mainTable = null;
152
153     /** Cell width */
154     private int cellWidth = 0;
155     /** Cell right border position */
156     private int cellRight = 0;
157     /** <code>Cell</code> containing the actual data */
158     private Cell store = null;
159     /** Is this an empty cell */
160     private boolean emptyCell = true;
161     /** Type of merging to do */
162     private int mergeType = 0;
163     /** cell padding, because the table only renders the left and right cell padding
164      * and not the top and bottom one
165      */

166     private int cellpadding = 0;
167
168     /**
169      * Create a new <code>RtfCell</code>.
170      *
171      * @param writer The <code>RtfWriter</code> that this <code>RtfCell</code> belongs to
172      * @param mainTable The <code>RtfTable</code> that created the
173      * <code>RtfRow</code> that created the <code>RtfCell</code> :-)
174      */

175     public RtfCell(RtfWriter writer, RtfTable mainTable) {
176         super();
177         this.writer = writer;
178         this.mainTable = mainTable;
179     }
180
181     /**
182      * Import a <code>Cell</code>.
183      * <P>
184      * @param cell The <code>Cell</code> containing the data for this
185      * <code>RtfCell</code>
186      * @param cellLeft The position of the left border
187      * @param cellWidth The default width of a cell
188      * @param x The column index of this <code>RtfCell</code>
189      * @param y The row index of this <code>RtfCell</code>
190      * @param cellpadding the cellpadding
191      * @return the position of the right side of the cell
192      */

193     public int importCell(Cell cell, int cellLeft, int cellWidth, int x, int y, int cellpadding) {
194         this.cellpadding = cellpadding;
195
196         // set this value in any case
197
this.cellWidth = cellWidth;
198         if (cell == null) {
199             cellRight = cellLeft + cellWidth;
200             return cellRight;
201         }
202         if (cell.getWidth() != 0) {
203             this.cellWidth = (int) (cell.getWidth() * RtfWriter.TWIPSFACTOR);
204         }
205         cellRight = cellLeft + this.cellWidth;
206         store = cell;
207         emptyCell = false;
208         if (cell.getColspan() > 1) {
209             if (cell.getRowspan() > 1) {
210                 mergeType = MERGE_BOTH_FIRST;
211                 for (int i = y; i < y + cell.getRowspan(); i++) {
212                     if (i > y) mainTable.setMerge(x, i, MERGE_VERT_PREV, this);
213                     for (int j = x + 1; j < x + cell.getColspan(); j++) {
214                         mainTable.setMerge(j, i, MERGE_BOTH_PREV, this);
215                     }
216                 }
217             } else {
218                 mergeType = MERGE_HORIZ_FIRST;
219                 for (int i = x + 1; i < x + cell.getColspan(); i++) {
220                     mainTable.setMerge(i, y, MERGE_HORIZ_PREV, this);
221                 }
222             }
223         } else if (cell.getRowspan() > 1) {
224             mergeType = MERGE_VERT_FIRST;
225             for (int i = y + 1; i < y + cell.getRowspan(); i++) {
226                 mainTable.setMerge(x, i, MERGE_VERT_PREV, this);
227             }
228         }
229         return cellRight;
230     }
231
232     /**
233      * Write the properties of the <code>RtfCell</code>.
234      *
235      * @param os The <code>OutputStream</code> to which to write the properties
236      * of the <code>RtfCell</code> to.
237      * @return true if writing the cell settings succeeded
238      * @throws DocumentException
239      */

240     public boolean writeCellSettings(ByteArrayOutputStream JavaDoc os) {
241         try {
242             float lWidth, tWidth, rWidth, bWidth;
243             byte[] lStyle, tStyle, rStyle, bStyle;
244
245             if (store instanceof RtfTableCell) {
246                 RtfTableCell c = (RtfTableCell) store;
247                 lWidth = c.leftBorderWidth();
248                 tWidth = c.topBorderWidth();
249                 rWidth = c.rightBorderWidth();
250                 bWidth = c.bottomBorderWidth();
251                 lStyle = RtfTableCell.getStyleControlWord(c.leftBorderStyle());
252                 tStyle = RtfTableCell.getStyleControlWord(c.topBorderStyle());
253                 rStyle = RtfTableCell.getStyleControlWord(c.rightBorderStyle());
254                 bStyle = RtfTableCell.getStyleControlWord(c.bottomBorderStyle());
255             } else {
256                 lWidth = tWidth = rWidth = bWidth = store.getBorderWidth();
257                 lStyle = tStyle = rStyle = bStyle = RtfRow.tableBorder;
258             }
259
260             if (mergeType == MERGE_HORIZ_PREV || mergeType == MERGE_BOTH_PREV) {
261                 return true;
262             }
263             switch (mergeType) {
264                 case MERGE_VERT_FIRST:
265                     os.write(RtfWriter.escape);
266                     os.write(cellVMergeFirst);
267                     break;
268                 case MERGE_BOTH_FIRST:
269                     os.write(RtfWriter.escape);
270                     os.write(cellVMergeFirst);
271                     break;
272                 case MERGE_HORIZ_PREV:
273                     os.write(RtfWriter.escape);
274                     os.write(cellMergePrev);
275                     break;
276                 case MERGE_VERT_PREV:
277                     os.write(RtfWriter.escape);
278                     os.write(cellVMergePrev);
279                     break;
280                 case MERGE_BOTH_PREV:
281                     os.write(RtfWriter.escape);
282                     os.write(cellMergeFirst);
283                     break;
284             }
285             switch (store.getVerticalAlignment()) {
286                 case Element.ALIGN_BOTTOM:
287                     os.write(RtfWriter.escape);
288                     os.write(cellVerticalAlignBottom);
289                     break;
290                 case Element.ALIGN_CENTER:
291                 case Element.ALIGN_MIDDLE:
292                     os.write(RtfWriter.escape);
293                     os.write(cellVerticalAlignCenter);
294                     break;
295                 case Element.ALIGN_TOP:
296                     os.write(RtfWriter.escape);
297                     os.write(cellVerticalAlignTop);
298                     break;
299             }
300
301             if (((store.getBorder() & Rectangle.LEFT) == Rectangle.LEFT) &&
302                     (lWidth > 0)) {
303                 os.write(RtfWriter.escape);
304                 os.write(cellBorderLeft);
305                 os.write(RtfWriter.escape);
306                 os.write(lStyle);
307                 os.write(RtfWriter.escape);
308                 os.write(RtfRow.tableBorderWidth);
309                 writeInt(os, (int) (lWidth * RtfWriter.TWIPSFACTOR));
310                 os.write(RtfWriter.escape);
311                 os.write(RtfRow.tableBorderColor);
312                 if (store.getBorderColor() == null)
313                     writeInt(os, writer.addColor(new
314                             Color JavaDoc(0, 0, 0)));
315                 else
316                     writeInt(os, writer.addColor(store.getBorderColor()));
317                 os.write((byte) '\n');
318             }
319             if (((store.getBorder() & Rectangle.TOP) == Rectangle.TOP) && (tWidth > 0)) {
320                 os.write(RtfWriter.escape);
321                 os.write(cellBorderTop);
322                 os.write(RtfWriter.escape);
323                 os.write(tStyle);
324                 os.write(RtfWriter.escape);
325                 os.write(RtfRow.tableBorderWidth);
326                 writeInt(os, (int) (tWidth * RtfWriter.TWIPSFACTOR));
327                 os.write(RtfWriter.escape);
328                 os.write(RtfRow.tableBorderColor);
329                 if (store.getBorderColor() == null)
330                     writeInt(os, writer.addColor(new
331                             Color JavaDoc(0, 0, 0)));
332                 else
333                     writeInt(os, writer.addColor(store.getBorderColor()));
334                 os.write((byte) '\n');
335             }
336             if (((store.getBorder() & Rectangle.BOTTOM) == Rectangle.BOTTOM) &&
337                     (bWidth > 0)) {
338                 os.write(RtfWriter.escape);
339                 os.write(cellBorderBottom);
340                 os.write(RtfWriter.escape);
341                 os.write(bStyle);
342                 os.write(RtfWriter.escape);
343                 os.write(RtfRow.tableBorderWidth);
344                 writeInt(os, (int) (bWidth * RtfWriter.TWIPSFACTOR));
345                 os.write(RtfWriter.escape);
346                 os.write(RtfRow.tableBorderColor);
347                 if (store.getBorderColor() == null)
348                     writeInt(os, writer.addColor(new
349                             Color JavaDoc(0, 0, 0)));
350                 else
351                     writeInt(os, writer.addColor(store.getBorderColor()));
352                 os.write((byte) '\n');
353             }
354             if (((store.getBorder() & Rectangle.RIGHT) == Rectangle.RIGHT) &&
355                     (rWidth > 0)) {
356                 os.write(RtfWriter.escape);
357                 os.write(cellBorderRight);
358                 os.write(RtfWriter.escape);
359                 os.write(rStyle);
360                 os.write(RtfWriter.escape);
361                 os.write(RtfRow.tableBorderWidth);
362                 writeInt(os, (int) (rWidth * RtfWriter.TWIPSFACTOR));
363                 os.write(RtfWriter.escape);
364                 os.write(RtfRow.tableBorderColor);
365                 if (store.getBorderColor() == null)
366                     writeInt(os, writer.addColor(new
367                             Color JavaDoc(0, 0, 0)));
368                 else
369                     writeInt(os, writer.addColor(store.getBorderColor()));
370                 os.write((byte) '\n');
371             }
372             os.write(RtfWriter.escape);
373             os.write(cellBackgroundColor);
374             if (store.getBackgroundColor() == null) {
375                 writeInt(os, writer.addColor(new Color JavaDoc(255, 255, 255)));
376             } else {
377                 writeInt(os, writer.addColor(store.getBackgroundColor()));
378             }
379             os.write((byte) '\n');
380             os.write(RtfWriter.escape);
381             os.write(cellWidthStyle);
382             os.write((byte) '\n');
383             os.write(RtfWriter.escape);
384             os.write(cellWidthTag);
385             writeInt(os, cellWidth);
386             os.write((byte) '\n');
387             if (cellpadding > 0) {
388                 // values
389
os.write(RtfWriter.escape);
390                 os.write(cellPaddingLeft);
391                 writeInt(os, cellpadding / 2);
392                 os.write(RtfWriter.escape);
393                 os.write(cellPaddingTop);
394                 writeInt(os, cellpadding / 2);
395                 os.write(RtfWriter.escape);
396                 os.write(cellPaddingRight);
397                 writeInt(os, cellpadding / 2);
398                 os.write(RtfWriter.escape);
399                 os.write(cellPaddingBottom);
400                 writeInt(os, cellpadding / 2);
401                 // unit
402
os.write(RtfWriter.escape);
403                 os.write(cellPaddingLeftUnit);
404                 os.write(RtfWriter.escape);
405                 os.write(cellPaddingTopUnit);
406                 os.write(RtfWriter.escape);
407                 os.write(cellPaddingRightUnit);
408                 os.write(RtfWriter.escape);
409                 os.write(cellPaddingBottomUnit);
410             }
411             os.write(RtfWriter.escape);
412             os.write(cellRightBorder);
413             writeInt(os, cellRight);
414         } catch (IOException JavaDoc e) {
415             return false;
416         }
417         return true;
418     }
419
420     /**
421      * Write the content of the <code>RtfCell</code>.
422      *
423      * @param os The <code>OutputStream</code> to which to write the content of
424      * the <code>RtfCell</code> to.
425      * @return true if writing the cell content succeeded
426      * @throws DocumentException
427      */

428     public boolean writeCellContent(ByteArrayOutputStream JavaDoc os) throws DocumentException {
429         try {
430             if (mergeType == MERGE_HORIZ_PREV || mergeType == MERGE_BOTH_PREV) {
431                 return true;
432             }
433             
434             if (!emptyCell) {
435                 Iterator JavaDoc cellIterator = store.getElements();
436                 Paragraph container = null;
437                 while (cellIterator.hasNext()) {
438                     Element element = (Element) cellIterator.next();
439                     // should we wrap it in a paragraph
440
if(!(element instanceof Paragraph)) {
441                         if(container != null) {
442                             container.add(element);
443                         } else {
444                             container = new Paragraph();
445                             container.setAlignment(store.getHorizontalAlignment());
446                             container.add(element);
447                         }
448                     } else {
449                         if(container != null) {
450                             writer.addElement(container, os);
451                             container = null;
452                         }
453                         
454                         
455                         // if horizontal alignment is undefined overwrite
456
// with that of enclosing cell
457
if (element instanceof Paragraph && ((Paragraph) element).getAlignment() == Element.ALIGN_UNDEFINED) {
458                             ((Paragraph) element).setAlignment(store.getHorizontalAlignment());
459                         }
460                         writer.addElement(element, os);
461                         if (element.type() == Element.PARAGRAPH && cellIterator.hasNext()) {
462                             os.write(RtfWriter.escape);
463                             os.write(RtfWriter.paragraph);
464                         }
465                     }
466                 }
467                 if(container != null) {
468                     writer.addElement(container, os);
469                     container =null;
470                 }
471             } else {
472                 os.write(RtfWriter.escape);
473                 os.write(RtfWriter.paragraphDefaults);
474                 os.write(RtfWriter.escape);
475                 os.write(cellInTable);
476             }
477             os.write(RtfWriter.escape);
478             os.write(cellEnd);
479         } catch (IOException JavaDoc e) {
480             return false;
481         }
482         return true;
483     }
484
485     /**
486      * Sets the merge type and the <code>RtfCell</code> with which this
487      * <code>RtfCell</code> is to be merged.
488      *
489      * @param mergeType The merge type specifies the kind of merge to be applied
490      * (MERGE_HORIZ_PREV, MERGE_VERT_PREV, MERGE_BOTH_PREV)
491      * @param mergeCell The <code>RtfCell</code> that the cell at x and y is to
492      * be merged with
493      */

494     public void setMerge(int mergeType, RtfCell mergeCell) {
495         this.mergeType = mergeType;
496         store = mergeCell.getStore();
497     }
498
499     /**
500      * Get the <code>Cell</code> with the actual content.
501      *
502      * @return <code>Cell</code> which is contained in the <code>RtfCell</code>
503      */

504     public Cell getStore() {
505         return store;
506     }
507
508     /**
509      * Get the with of this <code>RtfCell</code>
510      *
511      * @return Width of the current <code>RtfCell</code>
512      */

513     public int getCellWidth() {
514         return cellWidth;
515     }
516
517     /**
518      * sets the width of the cell
519      * @param value a width
520      */

521     public void setCellWidth(int value) {
522         cellWidth = value;
523     }
524
525     /**
526      * Get the position of the right border of this <code>RtfCell</code>.
527      * @return position of the right border
528      */

529     public int getCellRight() {
530         return cellRight;
531     }
532
533
534     /**
535      * Sets the right position of the cell
536      * @param value a cell position
537      */

538     public void setCellRight(int value) {
539         cellRight = value;
540     }
541
542     /**
543      * Write an Integer to the Outputstream.
544      *
545      * @param out The <code>OutputStream</code> to be written to.
546      * @param i The int to be written.
547      * @throws IOException
548      */

549     private void writeInt(ByteArrayOutputStream JavaDoc out, int i) throws IOException JavaDoc {
550         out.write(Integer.toString(i).getBytes());
551     }
552 }
553
Popular Tags