001: /*
002: * $Id: RtfCell.java 2796 2007-05-27 09:42:20Z psoares33 $
003: * $Name: $
004: *
005: * Copyright 2001, 2002, 2003, 2004 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: */
050:
051: package com.lowagie.text.rtf.table;
052:
053: import java.io.ByteArrayOutputStream;
054: import java.io.IOException;
055: import java.io.OutputStream;
056: import java.util.ArrayList;
057: import java.util.Iterator;
058: import java.util.Properties;
059:
060: import com.lowagie.text.BadElementException;
061: import com.lowagie.text.Cell;
062: import com.lowagie.text.DocumentException;
063: import com.lowagie.text.Element;
064: import com.lowagie.text.List;
065: import com.lowagie.text.Paragraph;
066: import com.lowagie.text.rtf.RtfBasicElement;
067: import com.lowagie.text.rtf.RtfExtendedElement;
068: import com.lowagie.text.rtf.document.RtfDocument;
069: import com.lowagie.text.rtf.style.RtfColor;
070: import com.lowagie.text.rtf.style.RtfParagraphStyle;
071: import com.lowagie.text.rtf.text.RtfParagraph;
072:
073: /**
074: * The RtfCell wraps a Cell, but can also be added directly to a Table.
075: * The RtfCell is an extension of Cell, that supports a multitude of different
076: * borderstyles.
077: *
078: * @version $Id: RtfCell.java 2796 2007-05-27 09:42:20Z psoares33 $
079: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
080: * @author Steffen Stundzig
081: * @author Benoit Wiart
082: * @author Thomas Bickel (tmb99@inode.at)
083: * @see com.lowagie.text.rtf.table.RtfBorder
084: */
085: public class RtfCell extends Cell implements RtfExtendedElement {
086:
087: /**
088: * This cell is not merged
089: */
090: private static final int MERGE_NONE = 0;
091: /**
092: * This cell is the parent cell of a vertical merge operation
093: */
094: private static final int MERGE_VERT_PARENT = 1;
095: /**
096: * This cell is a child cell of a vertical merge operation
097: */
098: private static final int MERGE_VERT_CHILD = 2;
099:
100: /**
101: * The parent RtfRow of this RtfCell
102: */
103: private RtfRow parentRow = null;
104: /**
105: * The content of this RtfCell
106: */
107: private ArrayList content = null;
108: /**
109: * The right margin of this RtfCell
110: */
111: private int cellRight = 0;
112: /**
113: * The width of this RtfCell
114: */
115: private int cellWidth = 0;
116: /**
117: * The borders of this RtfCell
118: */
119: private RtfBorderGroup borders = null;
120:
121: /**
122: * The background color of this RtfCell
123: */
124: private RtfColor backgroundColor = null;
125: /**
126: * The padding of this RtfCell
127: */
128: private int cellPadding = 0;
129: /**
130: * The merge type of this RtfCell
131: */
132: private int mergeType = MERGE_NONE;
133: /**
134: * The RtfDocument this RtfCell belongs to
135: */
136: private RtfDocument document = null;
137: /**
138: * Whether this RtfCell is in a header
139: */
140: private boolean inHeader = false;
141: /**
142: * Whether this RtfCell is a placeholder for a removed table cell.
143: */
144: private boolean deleted = false;
145:
146: /**
147: * Constructs an empty RtfCell
148: */
149: public RtfCell() {
150: super ();
151: this .borders = new RtfBorderGroup();
152: verticalAlignment = ALIGN_MIDDLE;
153: }
154:
155: /**
156: * Constructs a RtfCell based upon a String
157: *
158: * @param content The String to base the RtfCell on
159: */
160: public RtfCell(String content) {
161: super (content);
162: this .borders = new RtfBorderGroup();
163: verticalAlignment = ALIGN_MIDDLE;
164: }
165:
166: /**
167: * Constructs a RtfCell based upon an Element
168: *
169: * @param element The Element to base the RtfCell on
170: * @throws BadElementException If the Element is not valid
171: */
172: public RtfCell(Element element) throws BadElementException {
173: super (element);
174: this .borders = new RtfBorderGroup();
175: verticalAlignment = ALIGN_MIDDLE;
176: }
177:
178: /**
179: * Constructs a RtfCell based upon certain Properties
180: *
181: * @param properties The Properties for this RtfCell
182: */
183: public RtfCell(Properties properties) {
184: super (properties);
185: this .borders = new RtfBorderGroup();
186: verticalAlignment = ALIGN_MIDDLE;
187: }
188:
189: /**
190: * Constructs a deleted RtfCell.
191: *
192: * @param deleted Whether this RtfCell is actually deleted.
193: */
194: protected RtfCell(boolean deleted) {
195: super ();
196: this .deleted = deleted;
197: verticalAlignment = ALIGN_MIDDLE;
198: }
199:
200: /**
201: * Constructs a RtfCell based on a Cell.
202: *
203: * @param doc The RtfDocument this RtfCell belongs to
204: * @param row The RtfRow this RtfCell lies in
205: * @param cell The Cell to base this RtfCell on
206: */
207: protected RtfCell(RtfDocument doc, RtfRow row, Cell cell) {
208: this .document = doc;
209: this .parentRow = row;
210: importCell(cell);
211: }
212:
213: /**
214: * Imports the Cell properties into the RtfCell
215: *
216: * @param cell The Cell to import
217: */
218: private void importCell(Cell cell) {
219: this .content = new ArrayList();
220:
221: if (cell == null) {
222: this .borders = new RtfBorderGroup(this .document,
223: RtfBorder.CELL_BORDER, this .parentRow
224: .getParentTable().getBorders());
225: return;
226: }
227:
228: this .colspan = cell.getColspan();
229: this .rowspan = cell.getRowspan();
230: if (cell.getRowspan() > 1) {
231: this .mergeType = MERGE_VERT_PARENT;
232: }
233: if (cell instanceof RtfCell) {
234: this .borders = new RtfBorderGroup(this .document,
235: RtfBorder.CELL_BORDER, ((RtfCell) cell)
236: .getBorders());
237: } else {
238: this .borders = new RtfBorderGroup(this .document,
239: RtfBorder.CELL_BORDER, cell.getBorder(), cell
240: .getBorderWidth(), cell.getBorderColor());
241: }
242: this .verticalAlignment = cell.getVerticalAlignment();
243: if (cell.getBackgroundColor() == null) {
244: this .backgroundColor = new RtfColor(this .document, 255,
245: 255, 255);
246: } else {
247: this .backgroundColor = new RtfColor(this .document, cell
248: .getBackgroundColor());
249: }
250:
251: this .cellPadding = (int) this .parentRow.getParentTable()
252: .getCellPadding();
253:
254: Iterator cellIterator = cell.getElements();
255: Paragraph container = null;
256: while (cellIterator.hasNext()) {
257: try {
258: Element element = (Element) cellIterator.next();
259: // should we wrap it in a paragraph
260: if (!(element instanceof Paragraph)
261: && !(element instanceof List)) {
262: if (container != null) {
263: container.add(element);
264: } else {
265: container = new Paragraph();
266: container.setAlignment(cell
267: .getHorizontalAlignment());
268: container.add(element);
269: }
270: } else {
271: if (container != null) {
272: RtfBasicElement rtfElement = this .document
273: .getMapper().mapElement(container);
274: rtfElement.setInTable(true);
275: this .content.add(rtfElement);
276: container = null;
277: }
278: // if horizontal alignment is undefined overwrite
279: // with that of enclosing cell
280: if (element instanceof Paragraph
281: && ((Paragraph) element).getAlignment() == Element.ALIGN_UNDEFINED) {
282: ((Paragraph) element).setAlignment(cell
283: .getHorizontalAlignment());
284: }
285:
286: RtfBasicElement rtfElement = this .document
287: .getMapper().mapElement(element);
288: rtfElement.setInTable(true);
289: this .content.add(rtfElement);
290: }
291: } catch (DocumentException de) {
292: de.printStackTrace();
293: }
294: }
295: if (container != null) {
296: try {
297: RtfBasicElement rtfElement = this .document.getMapper()
298: .mapElement(container);
299: rtfElement.setInTable(true);
300: this .content.add(rtfElement);
301: } catch (DocumentException de) {
302: de.printStackTrace();
303: }
304: }
305: }
306:
307: /**
308: * Write the cell definition part of this RtfCell
309: *
310: * @return A byte array with the cell definition
311: * @deprecated replaced by {@link #writeDefinition(OutputStream)}
312: */
313: public byte[] writeDefinition() {
314: ByteArrayOutputStream result = new ByteArrayOutputStream();
315: try {
316: writeDefinition(result);
317: } catch (IOException ioe) {
318: ioe.printStackTrace();
319: }
320:
321: return result.toByteArray();
322: }
323:
324: /**
325: * Write the cell definition part of this RtfCell
326: */
327: public void writeDefinition(final OutputStream result)
328: throws IOException {
329: if (this .mergeType == MERGE_VERT_PARENT) {
330: result.write("\\clvmgf".getBytes());
331: } else if (this .mergeType == MERGE_VERT_CHILD) {
332: result.write("\\clvmrg".getBytes());
333: }
334: switch (verticalAlignment) {
335: case Element.ALIGN_BOTTOM:
336: result.write("\\clvertalb".getBytes());
337: break;
338: case Element.ALIGN_CENTER:
339: case Element.ALIGN_MIDDLE:
340: result.write("\\clvertalc".getBytes());
341: break;
342: case Element.ALIGN_TOP:
343: result.write("\\clvertalt".getBytes());
344: break;
345: }
346: //.result.write(this.borders.write());
347: this .borders.writeContent(result);
348:
349: if (this .backgroundColor != null) {
350: result.write("\\clcbpat".getBytes());
351: result.write(intToByteArray(this .backgroundColor
352: .getColorNumber()));
353: }
354: result.write('\n');
355:
356: result.write("\\clftsWidth3".getBytes());
357: result.write('\n');
358:
359: result.write("\\clwWidth".getBytes());
360: result.write(intToByteArray(this .cellWidth));
361: result.write('\n');
362:
363: if (this .cellPadding > 0) {
364: result.write("\\clpadl".getBytes());
365: result.write(intToByteArray(this .cellPadding / 2));
366: result.write("\\clpadt".getBytes());
367: result.write(intToByteArray(this .cellPadding / 2));
368: result.write("\\clpadr".getBytes());
369: result.write(intToByteArray(this .cellPadding / 2));
370: result.write("\\clpadb".getBytes());
371: result.write(intToByteArray(this .cellPadding / 2));
372: result.write("\\clpadfl3".getBytes());
373: result.write("\\clpadft3".getBytes());
374: result.write("\\clpadfr3".getBytes());
375: result.write("\\clpadfb3".getBytes());
376: }
377: result.write("\\cellx".getBytes());
378: result.write(intToByteArray(this .cellRight));
379: }
380:
381: /**
382: * Write the content of this RtfCell
383: *
384: * @return A byte array with the content of this RtfCell
385: * @deprecated replaced by {@link #writeContent(OutputStream)}
386: */
387: public byte[] write() {
388: ByteArrayOutputStream result = new ByteArrayOutputStream();
389: try {
390: writeContent(result);
391: } catch (IOException ioe) {
392: ioe.printStackTrace();
393: }
394:
395: return result.toByteArray();
396: }
397:
398: /**
399: * Write the content of this RtfCell
400: */
401: public void writeContent(final OutputStream result)
402: throws IOException {
403: if (this .content.size() == 0) {
404: result.write(RtfParagraph.PARAGRAPH_DEFAULTS);
405: if (this .parentRow.getParentTable().getTableFitToPage()) {
406: result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT);
407: }
408: result.write(RtfParagraph.IN_TABLE);
409: } else {
410: for (int i = 0; i < this .content.size(); i++) {
411: RtfBasicElement rtfElement = (RtfBasicElement) this .content
412: .get(i);
413: if (rtfElement instanceof RtfParagraph) {
414: ((RtfParagraph) rtfElement)
415: .setKeepTogetherWithNext(this .parentRow
416: .getParentTable()
417: .getTableFitToPage());
418: }
419: //.result.write(rtfElement.write());
420: rtfElement.writeContent(result);
421: if (rtfElement instanceof RtfParagraph
422: && i < (this .content.size() - 1)) {
423: result.write(RtfParagraph.PARAGRAPH);
424: }
425: }
426: }
427: result.write("\\cell".getBytes());
428: }
429:
430: /**
431: * Sets the right margin of this cell. Used in merge operations
432: *
433: * @param cellRight The right margin to use
434: */
435: protected void setCellRight(int cellRight) {
436: this .cellRight = cellRight;
437: }
438:
439: /**
440: * Gets the right margin of this RtfCell
441: *
442: * @return The right margin of this RtfCell.
443: */
444: protected int getCellRight() {
445: return this .cellRight;
446: }
447:
448: /**
449: * Sets the cell width of this RtfCell. Used in merge operations.
450: *
451: * @param cellWidth The cell width to use
452: */
453: protected void setCellWidth(int cellWidth) {
454: this .cellWidth = cellWidth;
455: }
456:
457: /**
458: * Gets the cell width of this RtfCell
459: *
460: * @return The cell width of this RtfCell
461: */
462: protected int getCellWidth() {
463: return this .cellWidth;
464: }
465:
466: /**
467: * Gets the cell padding of this RtfCell
468: *
469: * @return The cell padding of this RtfCell
470: */
471: protected int getCellpadding() {
472: return this .cellPadding;
473: }
474:
475: /**
476: * Gets the borders of this RtfCell
477: *
478: * @return The borders of this RtfCell
479: */
480: protected RtfBorderGroup getBorders() {
481: return this .borders;
482: }
483:
484: /**
485: * Set the borders of this RtfCell
486: *
487: * @param borderGroup The RtfBorderGroup to use as borders
488: */
489: public void setBorders(RtfBorderGroup borderGroup) {
490: this .borders = new RtfBorderGroup(this .document,
491: RtfBorder.CELL_BORDER, borderGroup);
492: }
493:
494: /**
495: * Get the background color of this RtfCell
496: *
497: * @return The background color of this RtfCell
498: */
499: protected RtfColor getRtfBackgroundColor() {
500: return this .backgroundColor;
501: }
502:
503: /**
504: * Merge this cell into the parent cell.
505: *
506: * @param mergeParent The RtfCell to merge with
507: */
508: protected void setCellMergeChild(RtfCell mergeParent) {
509: this .mergeType = MERGE_VERT_CHILD;
510: this .cellWidth = mergeParent.getCellWidth();
511: this .cellRight = mergeParent.getCellRight();
512: this .cellPadding = mergeParent.getCellpadding();
513: this .borders = mergeParent.getBorders();
514: this .verticalAlignment = mergeParent.getVerticalAlignment();
515: this .backgroundColor = mergeParent.getRtfBackgroundColor();
516: }
517:
518: /**
519: * Sets the RtfDocument this RtfCell belongs to
520: *
521: * @param doc The RtfDocument to use
522: */
523: public void setRtfDocument(RtfDocument doc) {
524: this .document = doc;
525: }
526:
527: /**
528: * Unused
529: * @param inTable
530: */
531: public void setInTable(boolean inTable) {
532: }
533:
534: /**
535: * Sets whether this RtfCell is in a header
536: *
537: * @param inHeader <code>True</code> if this RtfCell is in a header, <code>false</code> otherwise
538: */
539: public void setInHeader(boolean inHeader) {
540: this .inHeader = inHeader;
541: for (int i = 0; i < this .content.size(); i++) {
542: ((RtfBasicElement) this .content.get(i))
543: .setInHeader(inHeader);
544: }
545: }
546:
547: /**
548: * Transforms an integer into its String representation and then returns the bytes
549: * of that string.
550: *
551: * @param i The integer to convert
552: * @return A byte array representing the integer
553: */
554: private byte[] intToByteArray(int i) {
555: return Integer.toString(i).getBytes();
556: }
557:
558: /**
559: * Checks whether this RtfCell is a placeholder for
560: * a table cell that has been removed due to col/row spanning.
561: *
562: * @return <code>True</code> if this RtfCell is deleted, <code>false</code> otherwise.
563: */
564: public boolean isDeleted() {
565: return this.deleted;
566: }
567: }
|