KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfPCell


1 /*
2  * $Id: PdfPCell.java 2366 2006-09-14 23:10:58Z xlv $
3  * $Name$
4  *
5  * Copyright 2001, 2002 Paulo Soares
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.pdf;
52
53 import com.lowagie.text.Chunk;
54 import com.lowagie.text.Element;
55 import com.lowagie.text.Image;
56 import com.lowagie.text.Phrase;
57 import com.lowagie.text.Rectangle;
58 import com.lowagie.text.pdf.events.PdfPCellEventForwarder;
59
60 /** A cell in a PdfPTable.
61  */

62
63 public class PdfPCell extends Rectangle{
64     
65     private ColumnText column = new ColumnText(null);
66     
67     /** Holds value of property verticalAlignment. */
68     private int verticalAlignment = Element.ALIGN_TOP;
69     
70     /** Holds value of property paddingLeft. */
71     private float paddingLeft = 2;
72     
73     /** Holds value of property paddingLeft. */
74     private float paddingRight = 2;
75     
76     /** Holds value of property paddingTop. */
77     private float paddingTop = 2;
78     
79     /** Holds value of property paddingBottom. */
80     private float paddingBottom = 2;
81     
82     /** Holds value of property fixedHeight. */
83     private float fixedHeight = 0;
84     
85     /** Holds value of property noWrap. */
86     private boolean noWrap = false;
87     
88     /** Holds value of property table. */
89     private PdfPTable table;
90     
91     /** Holds value of property minimumHeight. */
92     private float minimumHeight;
93     
94     /** Holds value of property colspan. */
95     private int colspan = 1;
96     
97     /** Holds value of property image. */
98     private Image image;
99     
100     /** Holds value of property cellEvent. */
101     private PdfPCellEvent cellEvent;
102
103     /** Holds value of property useDescender. */
104     private boolean useDescender;
105
106     /** Increases padding to include border if true */
107     private boolean useBorderPadding = false;
108
109
110     /** The text in the cell. */
111     protected Phrase phrase;
112
113     /** Constructs an empty <CODE>PdfPCell</CODE>.
114      * The default padding is 2.
115      */

116     public PdfPCell() {
117         super(0, 0, 0, 0);
118         borderWidth = 0.5f;
119         border = BOX;
120         column.setLeading(0, 1);
121     }
122
123     /** Constructs a <CODE>PdfPCell</CODE> with a <CODE>Phrase</CODE>.
124      * The default padding is 2.
125      * @param phrase the text
126      */

127     public PdfPCell(Phrase phrase) {
128         super(0, 0, 0, 0);
129         borderWidth = 0.5f;
130         border = BOX;
131         column.addText(this.phrase = phrase);
132         column.setLeading(0, 1);
133     }
134     
135     /** Constructs a <CODE>PdfPCell</CODE> with an <CODE>Image</CODE>.
136      * The default padding is 0.
137      * @param image the <CODE>Image</CODE>
138      */

139     public PdfPCell(Image image) {
140         super(0, 0, 0, 0);
141         borderWidth = 0.5f;
142         border = BOX;
143         column.addText(this.phrase = new Phrase(new Chunk(image, 0, 0)));
144         column.setLeading(0, 1);
145         setPadding(0);
146     }
147     
148     /** Constructs a <CODE>PdfPCell</CODE> with an <CODE>Image</CODE>.
149      * The default padding is 0.25 for a border width of 0.5.
150      * @param image the <CODE>Image</CODE>
151      * @param fit <CODE>true</CODE> to fit the image to the cell
152      */

153     public PdfPCell(Image image, boolean fit) {
154         super(0, 0, 0, 0);
155         if (fit) {
156             borderWidth = 0.5f;
157             border = BOX;
158             this.image = image;
159             column.setLeading(0, 1);
160             setPadding(borderWidth / 2);
161         }
162         else {
163             borderWidth = 0.5f;
164             border = BOX;
165             column.addText(this.phrase = new Phrase(new Chunk(image, 0, 0)));
166             column.setLeading(0, 1);
167             setPadding(0);
168         }
169     }
170     
171     /** Constructs a <CODE>PdfPCell</CODE> with a <CODE>PdfPtable</CODE>.
172      * This constructor allows nested tables.
173      * The default padding is 0.
174      * @param table The <CODE>PdfPTable</CODE>
175      */

176     public PdfPCell(PdfPTable table) {
177         super(0, 0, 0, 0);
178         borderWidth = 0.5f;
179         border = BOX;
180         column.setLeading(0, 1);
181         setPadding(0);
182         this.table = table;
183         table.setWidthPercentage(100);
184         table.setExtendLastRow(true);
185         column.addElement(table);
186     }
187     
188     /** Constructs a deep copy of a <CODE>PdfPCell</CODE>.
189      * @param cell the <CODE>PdfPCell</CODE> to duplicate
190      */

191     public PdfPCell(PdfPCell cell) {
192         super(cell.llx, cell.lly, cell.urx, cell.ury);
193         cloneNonPositionParameters(cell);
194         verticalAlignment = cell.verticalAlignment;
195         paddingLeft = cell.paddingLeft;
196         paddingRight = cell.paddingRight;
197         paddingTop = cell.paddingTop;
198         paddingBottom = cell.paddingBottom;
199         phrase = cell.phrase;
200         fixedHeight = cell.fixedHeight;
201         minimumHeight = cell.minimumHeight;
202         noWrap = cell.noWrap;
203         colspan = cell.colspan;
204         if (cell.table != null)
205             table = new PdfPTable(cell.table);
206         image = Image.getInstance(cell.image);
207         cellEvent = cell.cellEvent;
208         useDescender = cell.useDescender;
209         column = ColumnText.duplicate(cell.column);
210         useBorderPadding = cell.useBorderPadding;
211         rotation = cell.rotation;
212     }
213     
214     /**
215      * Adds an iText element to the cell.
216      * @param element
217      */

218     public void addElement(Element element) {
219         if (table != null) {
220             table = null;
221             column.setText(null);
222         }
223         column.addElement(element);
224     }
225     
226     /** Gets the <CODE>Phrase</CODE> from this cell.
227      * @return the <CODE>Phrase</CODE>
228      */

229     public Phrase getPhrase() {
230         return phrase;
231     }
232     
233     /** Sets the <CODE>Phrase</CODE> for this cell.
234      * @param phrase the <CODE>Phrase</CODE>
235      */

236     public void setPhrase(Phrase phrase) {
237         table = null;
238         image = null;
239         column.setText(this.phrase = phrase);
240     }
241     
242     /** Gets the horizontal alignment for the cell.
243      * @return the horizontal alignment for the cell
244      */

245     public int getHorizontalAlignment() {
246         return column.getAlignment();
247     }
248     
249     /** Sets the horizontal alignment for the cell. It could be
250      * <CODE>Element.ALIGN_CENTER</CODE> for example.
251      * @param horizontalAlignment The horizontal alignment
252      */

253     public void setHorizontalAlignment(int horizontalAlignment) {
254         column.setAlignment(horizontalAlignment);
255     }
256     
257     /** Gets the vertical alignment for the cell.
258      * @return the vertical alignment for the cell
259      */

260     public int getVerticalAlignment() {
261         return verticalAlignment;
262     }
263     
264     /** Sets the vertical alignment for the cell. It could be
265      * <CODE>Element.ALIGN_MIDDLE</CODE> for example.
266      * @param verticalAlignment The vertical alignment
267      */

268     public void setVerticalAlignment(int verticalAlignment) {
269         if (table != null)
270             table.setExtendLastRow(verticalAlignment == Element.ALIGN_TOP);
271         this.verticalAlignment = verticalAlignment;
272     }
273     
274     /** Gets the effective left padding. This will include
275      * the left border width if {@link #isUseBorderPadding()} is true.
276      * @return effective value of property paddingLeft.
277      */

278     public float getEffectivePaddingLeft() {
279         return paddingLeft + (isUseBorderPadding() ? (getBorderWidthLeft()/(isUseVariableBorders()?1f:2f)) : 0);
280     }
281     
282     /**
283      * @return Value of property paddingLeft.
284      */

285     public float getPaddingLeft() {
286         return paddingLeft;
287     }
288
289     /**
290      * Setter for property paddingLeft.
291      * @param paddingLeft New value of property paddingLeft.
292      */

293     public void setPaddingLeft(float paddingLeft) {
294         this.paddingLeft = paddingLeft;
295     }
296     
297     /** Gets the effective right padding. This will include
298      * the right border width if {@link #isUseBorderPadding()} is true.
299      * @return effective value of property paddingRight.
300      */

301     public float getEffectivePaddingRight() {
302         return paddingRight + (isUseBorderPadding() ? (getBorderWidthRight()/(isUseVariableBorders()?1f:2f)) : 0);
303     }
304     
305     /**
306      * Getter for property paddingRight.
307      * @return Value of property paddingRight.
308      */

309     public float getPaddingRight() {
310         return paddingRight;
311     }
312
313     /**
314      * Setter for property paddingRight.
315      * @param paddingRight New value of property paddingRight.
316      */

317     public void setPaddingRight(float paddingRight) {
318         this.paddingRight = paddingRight;
319     }
320     
321     /** Gets the effective top padding. This will include
322      * the top border width if {@link #isUseBorderPadding()} is true.
323      * @return effective value of property paddingTop.
324      */

325     public float getEffectivePaddingTop() {
326         return paddingTop + (isUseBorderPadding() ? (getBorderWidthTop()/(isUseVariableBorders()?1f:2f)) : 0);
327     }
328     
329     /**
330      * Getter for property paddingTop.
331      * @return Value of property paddingTop.
332      */

333     public float getPaddingTop() {
334         return paddingTop;
335     }
336
337     /**
338      * Setter for property paddingTop.
339      * @param paddingTop New value of property paddingTop.
340      */

341     public void setPaddingTop(float paddingTop) {
342         this.paddingTop = paddingTop;
343     }
344     
345     /** Gets the effective bottom padding. This will include
346      * the bottom border width if {@link #isUseBorderPadding()} is true.
347      * @return effective value of property paddingBottom.
348      */

349     public float getEffectivePaddingBottom() {
350         return paddingBottom + (isUseBorderPadding() ? (getBorderWidthBottom()/(isUseVariableBorders()?1f:2f)) : 0);
351     }
352     
353     /**
354      * Getter for property paddingBottom.
355      * @return Value of property paddingBottom.
356      */

357     public float getPaddingBottom() {
358         return paddingBottom;
359     }
360
361     /**
362      * Setter for property paddingBottom.
363      * @param paddingBottom New value of property paddingBottom.
364      */

365     public void setPaddingBottom(float paddingBottom) {
366         this.paddingBottom = paddingBottom;
367     }
368     
369     /**
370      * Sets the padding of the contents in the cell (space between content and border).
371      * @param padding
372      */

373     public void setPadding(float padding) {
374         paddingBottom = padding;
375         paddingTop = padding;
376         paddingLeft = padding;
377         paddingRight = padding;
378     }
379
380     /**
381      * If true, then effective padding will include border widths
382      * @return true if effective padding includes border widths
383      */

384     public boolean isUseBorderPadding() {
385         return useBorderPadding;
386     }
387
388     /**
389      * Adjusts effective padding to include border widths.
390      * @param use adjust effective padding if true
391      */

392     public void setUseBorderPadding(boolean use) {
393         useBorderPadding = use;
394     }
395
396     /**
397      * Sets the leading fixed and variable. The resultant leading will be
398      * fixedLeading+multipliedLeading*maxFontSize where maxFontSize is the
399      * size of the bigest font in the line.
400      * @param fixedLeading the fixed leading
401      * @param multipliedLeading the variable leading
402      */

403     public void setLeading(float fixedLeading, float multipliedLeading) {
404         column.setLeading(fixedLeading, multipliedLeading);
405     }
406     
407     /**
408      * Gets the fixed leading
409      * @return the leading
410      */

411     public float getLeading() {
412         return column.getLeading();
413     }
414     
415     /**
416      * Gets the variable leading
417      * @return the leading
418      */

419     public float getMultipliedLeading() {
420         return column.getMultipliedLeading();
421     }
422     
423     /**
424      * Sets the first paragraph line indent.
425      * @param indent the indent
426      */

427     public void setIndent(float indent) {
428         column.setIndent(indent);
429     }
430     
431     /**
432      * Gets the first paragraph line indent.
433      * @return the indent
434      */

435     public float getIndent() {
436         return column.getIndent();
437     }
438     
439     /**
440      * Gets the extra space between paragraphs.
441      * @return the extra space between paragraphs
442      */

443     public float getExtraParagraphSpace() {
444         return column.getExtraParagraphSpace();
445     }
446     
447     /**
448      * Sets the extra space between paragraphs.
449      * @param extraParagraphSpace the extra space between paragraphs
450      */

451     public void setExtraParagraphSpace(float extraParagraphSpace) {
452         column.setExtraParagraphSpace(extraParagraphSpace);
453     }
454     
455     /**
456      * Getter for property fixedHeight.
457      * @return Value of property fixedHeight.
458      */

459     public float getFixedHeight() {
460         return fixedHeight;
461     }
462     
463     /**
464      * Setter for property fixedHeight.
465      * @param fixedHeight New value of property fixedHeight.
466      */

467     public void setFixedHeight(float fixedHeight) {
468         this.fixedHeight = fixedHeight;
469         minimumHeight = 0;
470     }
471     
472     /**
473      * Getter for property noWrap.
474      * @return Value of property noWrap.
475      */

476     public boolean isNoWrap() {
477         return noWrap;
478     }
479     
480     /**
481      * Setter for property noWrap.
482      * @param noWrap New value of property noWrap.
483      */

484     public void setNoWrap(boolean noWrap) {
485         this.noWrap = noWrap;
486     }
487     
488     /**
489      * Getter for property table.
490      * @return Value of property table.
491      */

492     PdfPTable getTable() {
493         return table;
494     }
495     
496     void setTable(PdfPTable table) {
497         this.table = table;
498         column.setText(null);
499         image = null;
500         if (table != null) {
501             table.setExtendLastRow(verticalAlignment == Element.ALIGN_TOP);
502             column.addElement(table);
503             table.setWidthPercentage(100);
504         }
505     }
506     
507     /** Getter for property minimumHeight.
508      * @return Value of property minimumHeight.
509      */

510     public float getMinimumHeight() {
511         return minimumHeight;
512     }
513     
514     /** Setter for property minimumHeight.
515      * @param minimumHeight New value of property minimumHeight.
516      */

517     public void setMinimumHeight(float minimumHeight) {
518         this.minimumHeight = minimumHeight;
519         fixedHeight = 0;
520     }
521     
522     /** Getter for property colspan.
523      * @return Value of property colspan.
524      */

525     public int getColspan() {
526         return colspan;
527     }
528     
529     /** Setter for property colspan.
530      * @param colspan New value of property colspan.
531      */

532     public void setColspan(int colspan) {
533         this.colspan = colspan;
534     }
535     
536     /**
537      * Sets the following paragraph lines indent.
538      * @param indent the indent
539      */

540     public void setFollowingIndent(float indent) {
541         column.setFollowingIndent(indent);
542     }
543     
544     /**
545      * Gets the following paragraph lines indent.
546      * @return the indent
547      */

548     public float getFollowingIndent() {
549         return column.getFollowingIndent();
550     }
551     
552     /**
553      * Sets the right paragraph lines indent.
554      * @param indent the indent
555      */

556     public void setRightIndent(float indent) {
557         column.setRightIndent(indent);
558     }
559     
560     /**
561      * Gets the right paragraph lines indent.
562      * @return the indent
563      */

564     public float getRightIndent() {
565         return column.getRightIndent();
566     }
567     
568     /** Gets the space/character extra spacing ratio for
569      * fully justified text.
570      * @return the space/character extra spacing ratio
571      */

572     public float getSpaceCharRatio() {
573         return column.getSpaceCharRatio();
574     }
575     
576     /** Sets the ratio between the extra word spacing and the extra character spacing
577      * when the text is fully justified.
578      * Extra word spacing will grow <CODE>spaceCharRatio</CODE> times more than extra character spacing.
579      * If the ratio is <CODE>PdfWriter.NO_SPACE_CHAR_RATIO</CODE> then the extra character spacing
580      * will be zero.
581      * @param spaceCharRatio the ratio between the extra word spacing and the extra character spacing
582      */

583     public void setSpaceCharRatio(float spaceCharRatio) {
584         column.setSpaceCharRatio(spaceCharRatio);
585     }
586     
587     /**
588      * Sets the run direction of the text content in the cell (PdfWriter.RUN_DIRECTION_DEFAULT, PdfWriter.RUN_DIRECTION_NO_BIDI, PdfWriter.RUN_DIRECTION_LTR or PdfWriter.RUN_DIRECTION_RTL).
589      * @param runDirection
590      */

591     public void setRunDirection(int runDirection) {
592         column.setRunDirection(runDirection);
593     }
594     
595     /**
596      * Gets the run direction of the text content in the cell
597      * @return One of the following values: PdfWriter.RUN_DIRECTION_DEFAULT, PdfWriter.RUN_DIRECTION_NO_BIDI, PdfWriter.RUN_DIRECTION_LTR or PdfWriter.RUN_DIRECTION_RTL.
598      */

599     public int getRunDirection() {
600         return column.getRunDirection();
601     }
602     
603     /** Getter for property image.
604      * @return Value of property image.
605      *
606      */

607     public Image getImage() {
608         return this.image;
609     }
610     
611     /** Setter for property image.
612      * @param image New value of property image.
613      *
614      */

615     public void setImage(Image image) {
616         column.setText(null);
617         table = null;
618         this.image = image;
619     }
620     
621     /** Gets the cell event for this cell.
622      * @return the cell event
623      *
624      */

625     public PdfPCellEvent getCellEvent() {
626         return this.cellEvent;
627     }
628     
629     /** Sets the cell event for this cell.
630      * @param event the cell event
631      *
632      */

633     public void setCellEvent(PdfPCellEvent event) {
634         if (event == null) this.cellEvent = null;
635         else if (this.cellEvent == null) this.cellEvent = event;
636         else if (this.cellEvent instanceof PdfPCellEventForwarder) ((PdfPCellEventForwarder)this.cellEvent).addCellEvent(event);
637         else {
638             PdfPCellEventForwarder forward = new PdfPCellEventForwarder();
639             forward.addCellEvent(this.cellEvent);
640             forward.addCellEvent(event);
641             this.cellEvent = forward;
642         }
643     }
644     
645     /** Gets the arabic shaping options.
646      * @return the arabic shaping options
647      */

648     public int getArabicOptions() {
649         return column.getArabicOptions();
650     }
651     
652     /** Sets the arabic shaping options. The option can be AR_NOVOWEL,
653      * AR_COMPOSEDTASHKEEL and AR_LIG.
654      * @param arabicOptions the arabic shaping options
655      */

656     public void setArabicOptions(int arabicOptions) {
657         column.setArabicOptions(arabicOptions);
658     }
659
660     /** Gets state of first line height based on max ascender
661      * @return true if an ascender is to be used.
662      */

663     public boolean isUseAscender() {
664         return column.isUseAscender();
665     }
666
667     /** Enables/ Disables adjustment of first line height based on max ascender.
668      *
669      * @param use adjust height if true
670      */

671     public void setUseAscender(boolean use) {
672         column.setUseAscender(use);
673     }
674
675
676     /** Getter for property useDescender.
677      * @return Value of property useDescender.
678      *
679      */

680     public boolean isUseDescender() {
681         return this.useDescender;
682     }
683
684     /** Setter for property useDescender.
685      * @param useDescender New value of property useDescender.
686      *
687      */

688     public void setUseDescender(boolean useDescender) {
689         this.useDescender = useDescender;
690     }
691
692     /**
693      * Gets the ColumnText with the content of the cell.
694      * @return a columntext object
695      */

696     public ColumnText getColumn() {
697         return column;
698     }
699     
700     /**
701      * Sets the columntext in the cell.
702      * @param column
703      */

704     public void setColumn(ColumnText column) {
705         this.column = column;
706     }
707
708     /**
709      * The rotation of the cell. Possible values are
710      * 0, 90, 180 and 270.
711      */

712     private int rotation;
713
714     /**
715      * Gets the rotation of the cell.
716      * @return the rotation of the cell.
717      */

718     public int getRotation() {
719         return this.rotation;
720     }
721
722     /**
723      * Sets the rotation of the cell. Possible values are
724      * 0, 90, 180 and 270.
725      * @param rotation the rotation of the cell
726      */

727     public void setRotation(int rotation) {
728         rotation %= 360;
729         if (rotation < 0)
730             rotation += 360;
731         if ((rotation % 90) != 0)
732             throw new IllegalArgumentException JavaDoc("Rotation must be a multiple of 90.");
733         this.rotation = rotation;
734     }
735 }
Popular Tags