001: /*
002: * $Id: SimpleCell.java 2748 2007-05-12 15:11:48Z blowagie $
003: * $Name$
004: *
005: * Copyright 1999-2005 by Bruno Lowagie.
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: package com.lowagie.text;
051:
052: import java.util.ArrayList;
053: import java.util.Iterator;
054:
055: import com.lowagie.text.pdf.PdfContentByte;
056: import com.lowagie.text.pdf.PdfPCell;
057: import com.lowagie.text.pdf.PdfPCellEvent;
058: import com.lowagie.text.pdf.PdfPTable;
059:
060: /**
061: * Rectangle that can be used for Cells.
062: * This Rectangle is padded and knows how to draw itself in a PdfPTable or PdfPcellEvent.
063: */
064: public class SimpleCell extends Rectangle implements PdfPCellEvent,
065: TextElementArray {
066:
067: // constants
068: /** the CellAttributes object represents a row. */
069: public static final boolean ROW = true;
070: /** the CellAttributes object represents a cell. */
071: public static final boolean CELL = false;
072:
073: // member variables
074: /** the content of the Cell. */
075: private ArrayList content = new ArrayList();
076: /** the width of the Cell. */
077: private float width = 0f;
078: /** the widthpercentage of the Cell. */
079: private float widthpercentage = 0f;
080: /** an extra spacing variable */
081: private float spacing_left = Float.NaN;
082: /** an extra spacing variable */
083: private float spacing_right = Float.NaN;
084: /** an extra spacing variable */
085: private float spacing_top = Float.NaN;
086: /** an extra spacing variable */
087: private float spacing_bottom = Float.NaN;
088: /** an extra padding variable */
089: private float padding_left = Float.NaN;
090: /** an extra padding variable */
091: private float padding_right = Float.NaN;
092: /** an extra padding variable */
093: private float padding_top = Float.NaN;
094: /** an extra padding variable */
095: private float padding_bottom = Float.NaN;
096: /** the colspan of a Cell */
097: private int colspan = 1;
098: /** horizontal alignment inside the Cell. */
099: private int horizontalAlignment = Element.ALIGN_UNDEFINED;
100: /** vertical alignment inside the Cell. */
101: private int verticalAlignment = Element.ALIGN_UNDEFINED;
102: /** indicates if these are the attributes of a single Cell (false) or a group of Cells (true). */
103: private boolean cellgroup = false;
104: /** Indicates that the largest ascender height should be used to determine the
105: * height of the first line. Note that this only has an effect when rendered
106: * to PDF. Setting this to true can help with vertical alignment problems. */
107: protected boolean useAscender = false;
108: /** Indicates that the largest descender height should be added to the height of
109: * the last line (so characters like y don't dip into the border). Note that
110: * this only has an effect when rendered to PDF. */
111: protected boolean useDescender = false;
112: /**
113: * Adjusts the cell contents to compensate for border widths. Note that
114: * this only has an effect when rendered to PDF.
115: */
116: protected boolean useBorderPadding;
117:
118: /**
119: * A CellAttributes object is always constructed without any dimensions.
120: * Dimensions are defined after creation.
121: * @param row only true if the CellAttributes object represents a row.
122: */
123: public SimpleCell(boolean row) {
124: super (0f, 0f, 0f, 0f);
125: cellgroup = row;
126: setBorder(BOX);
127: }
128:
129: /**
130: * Adds content to this object.
131: * @param element
132: * @throws BadElementException
133: */
134: public void addElement(Element element) throws BadElementException {
135: if (cellgroup) {
136: if (element instanceof SimpleCell) {
137: if (((SimpleCell) element).isCellgroup()) {
138: throw new BadElementException(
139: "You can't add one row to another row.");
140: }
141: content.add(element);
142: return;
143: } else {
144: throw new BadElementException(
145: "You can only add cells to rows, no objects of type "
146: + element.getClass().getName());
147: }
148: }
149: if (element.type() == Element.PARAGRAPH
150: || element.type() == Element.PHRASE
151: || element.type() == Element.ANCHOR
152: || element.type() == Element.CHUNK
153: || element.type() == Element.LIST
154: || element.type() == Element.MARKED
155: || element.type() == Element.JPEG
156: || element.type() == Element.IMGRAW
157: || element.type() == Element.IMGTEMPLATE) {
158: content.add(element);
159: } else {
160: throw new BadElementException(
161: "You can't add an element of type "
162: + element.getClass().getName()
163: + " to a SimpleCell.");
164: }
165: }
166:
167: /**
168: * Creates a Cell with these attributes.
169: * @param rowAttributes
170: * @return a cell based on these attributes.
171: * @throws BadElementException
172: */
173: public Cell createCell(SimpleCell rowAttributes)
174: throws BadElementException {
175: Cell cell = new Cell();
176: cell.cloneNonPositionParameters(rowAttributes);
177: cell.softCloneNonPositionParameters(this );
178: cell.setColspan(colspan);
179: cell.setHorizontalAlignment(horizontalAlignment);
180: cell.setVerticalAlignment(verticalAlignment);
181: cell.setUseAscender(useAscender);
182: cell.setUseBorderPadding(useBorderPadding);
183: cell.setUseDescender(useDescender);
184: Element element;
185: for (Iterator i = content.iterator(); i.hasNext();) {
186: element = (Element) i.next();
187: cell.addElement(element);
188: }
189: return cell;
190: }
191:
192: /**
193: * Creates a PdfPCell with these attributes.
194: * @param rowAttributes
195: * @return a PdfPCell based on these attributes.
196: */
197: public PdfPCell createPdfPCell(SimpleCell rowAttributes) {
198: PdfPCell cell = new PdfPCell();
199: cell.setBorder(NO_BORDER);
200: SimpleCell tmp = new SimpleCell(CELL);
201: tmp.setSpacing_left(spacing_left);
202: tmp.setSpacing_right(spacing_right);
203: tmp.setSpacing_top(spacing_top);
204: tmp.setSpacing_bottom(spacing_bottom);
205: tmp.cloneNonPositionParameters(rowAttributes);
206: tmp.softCloneNonPositionParameters(this );
207: cell.setCellEvent(tmp);
208: cell.setHorizontalAlignment(rowAttributes.horizontalAlignment);
209: cell.setVerticalAlignment(rowAttributes.verticalAlignment);
210: cell.setUseAscender(rowAttributes.useAscender);
211: cell.setUseBorderPadding(rowAttributes.useBorderPadding);
212: cell.setUseDescender(rowAttributes.useDescender);
213: cell.setColspan(colspan);
214: if (horizontalAlignment != Element.ALIGN_UNDEFINED)
215: cell.setHorizontalAlignment(horizontalAlignment);
216: if (verticalAlignment != Element.ALIGN_UNDEFINED)
217: cell.setVerticalAlignment(verticalAlignment);
218: if (useAscender)
219: cell.setUseAscender(useAscender);
220: if (useBorderPadding)
221: cell.setUseBorderPadding(useBorderPadding);
222: if (useDescender)
223: cell.setUseDescender(useDescender);
224: float p;
225: float sp_left = spacing_left;
226: if (Float.isNaN(sp_left))
227: sp_left = 0f;
228: float sp_right = spacing_right;
229: if (Float.isNaN(sp_right))
230: sp_right = 0f;
231: float sp_top = spacing_top;
232: if (Float.isNaN(sp_top))
233: sp_top = 0f;
234: float sp_bottom = spacing_bottom;
235: if (Float.isNaN(sp_bottom))
236: sp_bottom = 0f;
237: p = padding_left;
238: if (Float.isNaN(p))
239: p = 0f;
240: cell.setPaddingLeft(p + sp_left);
241: p = padding_right;
242: if (Float.isNaN(p))
243: p = 0f;
244: cell.setPaddingRight(p + sp_right);
245: p = padding_top;
246: if (Float.isNaN(p))
247: p = 0f;
248: cell.setPaddingTop(p + sp_top);
249: p = padding_bottom;
250: if (Float.isNaN(p))
251: p = 0f;
252: cell.setPaddingBottom(p + sp_bottom);
253: Element element;
254: for (Iterator i = content.iterator(); i.hasNext();) {
255: element = (Element) i.next();
256: cell.addElement(element);
257: }
258: return cell;
259: }
260:
261: /**
262: * @param rectangle
263: * @param spacing
264: * @return a rectangle
265: */
266: public static SimpleCell getDimensionlessInstance(
267: Rectangle rectangle, float spacing) {
268: SimpleCell event = new SimpleCell(CELL);
269: event.cloneNonPositionParameters(rectangle);
270: event.setSpacing(spacing * 2f);
271: return event;
272: }
273:
274: /**
275: * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[])
276: */
277: public void cellLayout(PdfPCell cell, Rectangle position,
278: PdfContentByte[] canvases) {
279: float sp_left = spacing_left;
280: if (Float.isNaN(sp_left))
281: sp_left = 0f;
282: float sp_right = spacing_right;
283: if (Float.isNaN(sp_right))
284: sp_right = 0f;
285: float sp_top = spacing_top;
286: if (Float.isNaN(sp_top))
287: sp_top = 0f;
288: float sp_bottom = spacing_bottom;
289: if (Float.isNaN(sp_bottom))
290: sp_bottom = 0f;
291: Rectangle rect = new Rectangle(position.getLeft(sp_left),
292: position.getBottom(sp_bottom), position
293: .getRight(sp_right), position.getTop(sp_top));
294: rect.cloneNonPositionParameters(this );
295: canvases[PdfPTable.BACKGROUNDCANVAS].rectangle(rect);
296: rect.setBackgroundColor(null);
297: canvases[PdfPTable.LINECANVAS].rectangle(rect);
298: }
299:
300: /** Sets the padding parameters if they are undefined.
301: * @param padding*/
302: public void setPadding(float padding) {
303: if (Float.isNaN(padding_right)) {
304: setPadding_right(padding);
305: }
306: if (Float.isNaN(padding_left)) {
307: setPadding_left(padding);
308: }
309: if (Float.isNaN(padding_top)) {
310: setPadding_top(padding);
311: }
312: if (Float.isNaN(padding_bottom)) {
313: setPadding_bottom(padding);
314: }
315: }
316:
317: /**
318: * @return Returns the colspan.
319: */
320: public int getColspan() {
321: return colspan;
322: }
323:
324: /**
325: * @param colspan The colspan to set.
326: */
327: public void setColspan(int colspan) {
328: if (colspan > 0)
329: this .colspan = colspan;
330: }
331:
332: /**
333: * @return Returns the padding_bottom.
334: */
335: public float getPadding_bottom() {
336: return padding_bottom;
337: }
338:
339: /**
340: * @param padding_bottom The padding_bottom to set.
341: */
342: public void setPadding_bottom(float padding_bottom) {
343: this .padding_bottom = padding_bottom;
344: }
345:
346: /**
347: * @return Returns the padding_left.
348: */
349: public float getPadding_left() {
350: return padding_left;
351: }
352:
353: /**
354: * @param padding_left The padding_left to set.
355: */
356: public void setPadding_left(float padding_left) {
357: this .padding_left = padding_left;
358: }
359:
360: /**
361: * @return Returns the padding_right.
362: */
363: public float getPadding_right() {
364: return padding_right;
365: }
366:
367: /**
368: * @param padding_right The padding_right to set.
369: */
370: public void setPadding_right(float padding_right) {
371: this .padding_right = padding_right;
372: }
373:
374: /**
375: * @return Returns the padding_top.
376: */
377: public float getPadding_top() {
378: return padding_top;
379: }
380:
381: /**
382: * @param padding_top The padding_top to set.
383: */
384: public void setPadding_top(float padding_top) {
385: this .padding_top = padding_top;
386: }
387:
388: /**
389: * @return Returns the spacing.
390: */
391: public float getSpacing_left() {
392: return spacing_left;
393: }
394:
395: /**
396: * @return Returns the spacing.
397: */
398: public float getSpacing_right() {
399: return spacing_right;
400: }
401:
402: /**
403: * @return Returns the spacing.
404: */
405: public float getSpacing_top() {
406: return spacing_top;
407: }
408:
409: /**
410: * @return Returns the spacing.
411: */
412: public float getSpacing_bottom() {
413: return spacing_bottom;
414: }
415:
416: /**
417: * @param spacing The spacing to set.
418: */
419: public void setSpacing(float spacing) {
420: this .spacing_left = spacing;
421: this .spacing_right = spacing;
422: this .spacing_top = spacing;
423: this .spacing_bottom = spacing;
424: }
425:
426: /**
427: * @param spacing The spacing to set.
428: */
429: public void setSpacing_left(float spacing) {
430: this .spacing_left = spacing;
431: }
432:
433: /**
434: * @param spacing The spacing to set.
435: */
436: public void setSpacing_right(float spacing) {
437: this .spacing_right = spacing;
438: }
439:
440: /**
441: * @param spacing The spacing to set.
442: */
443: public void setSpacing_top(float spacing) {
444: this .spacing_top = spacing;
445: }
446:
447: /**
448: * @param spacing The spacing to set.
449: */
450: public void setSpacing_bottom(float spacing) {
451: this .spacing_bottom = spacing;
452: }
453:
454: /**
455: * @return Returns the cellgroup.
456: */
457: public boolean isCellgroup() {
458: return cellgroup;
459: }
460:
461: /**
462: * @param cellgroup The cellgroup to set.
463: */
464: public void setCellgroup(boolean cellgroup) {
465: this .cellgroup = cellgroup;
466: }
467:
468: /**
469: * @return Returns the horizontal alignment.
470: */
471: public int getHorizontalAlignment() {
472: return horizontalAlignment;
473: }
474:
475: /**
476: * @param horizontalAlignment The horizontalAlignment to set.
477: */
478: public void setHorizontalAlignment(int horizontalAlignment) {
479: this .horizontalAlignment = horizontalAlignment;
480: }
481:
482: /**
483: * @return Returns the vertical alignment.
484: */
485: public int getVerticalAlignment() {
486: return verticalAlignment;
487: }
488:
489: /**
490: * @param verticalAlignment The verticalAligment to set.
491: */
492: public void setVerticalAlignment(int verticalAlignment) {
493: this .verticalAlignment = verticalAlignment;
494: }
495:
496: /**
497: * @return Returns the width.
498: */
499: public float getWidth() {
500: return width;
501: }
502:
503: /**
504: * @param width The width to set.
505: */
506: public void setWidth(float width) {
507: this .width = width;
508: }
509:
510: /**
511: * @return Returns the widthpercentage.
512: */
513: public float getWidthpercentage() {
514: return widthpercentage;
515: }
516:
517: /**
518: * @param widthpercentage The widthpercentage to set.
519: */
520: public void setWidthpercentage(float widthpercentage) {
521: this .widthpercentage = widthpercentage;
522: }
523:
524: /**
525: * @return Returns the useAscender.
526: */
527: public boolean isUseAscender() {
528: return useAscender;
529: }
530:
531: /**
532: * @param useAscender The useAscender to set.
533: */
534: public void setUseAscender(boolean useAscender) {
535: this .useAscender = useAscender;
536: }
537:
538: /**
539: * @return Returns the useBorderPadding.
540: */
541: public boolean isUseBorderPadding() {
542: return useBorderPadding;
543: }
544:
545: /**
546: * @param useBorderPadding The useBorderPadding to set.
547: */
548: public void setUseBorderPadding(boolean useBorderPadding) {
549: this .useBorderPadding = useBorderPadding;
550: }
551:
552: /**
553: * @return Returns the useDescender.
554: */
555: public boolean isUseDescender() {
556: return useDescender;
557: }
558:
559: /**
560: * @param useDescender The useDescender to set.
561: */
562: public void setUseDescender(boolean useDescender) {
563: this .useDescender = useDescender;
564: }
565:
566: /**
567: * @return Returns the content.
568: */
569: ArrayList getContent() {
570: return content;
571: }
572:
573: /**
574: * @see com.lowagie.text.TextElementArray#add(java.lang.Object)
575: */
576: public boolean add(Object o) {
577: try {
578: addElement((Element) o);
579: return true;
580: } catch (ClassCastException e) {
581: return false;
582: } catch (BadElementException e) {
583: throw new ExceptionConverter(e);
584: }
585: }
586:
587: /**
588: * @see com.lowagie.text.Element#type()
589: */
590: public int type() {
591: return Element.CELL;
592: }
593: }
|