KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PdfCell.java 2762 2007-05-16 16:07:09Z psoares33 $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
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 java.util.ArrayList JavaDoc;
54 import java.util.Iterator JavaDoc;
55
56 import com.lowagie.text.Anchor;
57 import com.lowagie.text.Cell;
58 import com.lowagie.text.Chunk;
59 import com.lowagie.text.Element;
60 import com.lowagie.text.Image;
61 import com.lowagie.text.List;
62 import com.lowagie.text.ListItem;
63 import com.lowagie.text.Paragraph;
64 import com.lowagie.text.Phrase;
65 import com.lowagie.text.Rectangle;
66
67 /**
68  * A <CODE>PdfCell</CODE> is the PDF translation of a <CODE>Cell</CODE>.
69  * <P>
70  * A <CODE>PdfCell</CODE> is an <CODE>ArrayList</CODE> of <CODE>PdfLine</CODE>s.
71  * <P>
72  * When using variable borders ({@link com.lowagie.text.Rectangle#isUseVariableBorders isUseVariableBorders()} == true),
73  * the borders are drawn completely inside the cell Rectangle
74  * so that adjacent cell borders will not overlap.
75  * Otherwise, the borders are drawn on top of the edges of the
76  * cell Rectangle and will overlap the borders of adjacent
77  * cells.
78  *
79  * @see com.lowagie.text.Rectangle
80  * @see com.lowagie.text.Cell
81  * @see PdfLine
82  * @see PdfTable
83  */

84
85 public class PdfCell extends Rectangle {
86
87     // membervariables
88

89     /**
90      * These are the PdfLines in the Cell.
91      */

92     private ArrayList JavaDoc lines;
93
94     /**
95      * These are the PdfLines in the Cell.
96      */

97     private PdfLine line;
98
99     /**
100      * These are the Images in the Cell.
101      */

102     private ArrayList JavaDoc images;
103
104     /**
105      * This is the leading of the lines.
106      */

107     private float leading;
108
109     /**
110      * This is the number of the row the cell is in.
111      */

112     private int rownumber;
113
114     /**
115      * This is the rowspan of the cell.
116      */

117     private int rowspan;
118
119     /**
120      * This is the cellspacing of the cell.
121      */

122     private float cellspacing;
123
124     /**
125      * This is the cellpadding of the cell.
126      */

127     private float cellpadding;
128
129     /**
130      * Indicates if this cell belongs to the header of a <CODE>PdfTable</CODE>
131      */

132     private boolean header = false;
133
134     /**
135      * This is the total height of the content of the cell. Note that the actual cell
136      * height may be larger due to another cell on the row *
137      */

138     private float contentHeight = 0.0f;
139
140     /**
141      * Indicates that the largest ascender height should be used to
142      * determine the height of the first line. Setting this to true can help
143      * with vertical alignment problems. */

144     private boolean useAscender;
145
146     /**
147      * Indicates that the largest descender height should be added to the height of
148      * the last line (so characters like y don't dip into the border). */

149     private boolean useDescender;
150
151     /**
152      * Adjusts the cell contents to compensate for border widths.
153      */

154     private boolean useBorderPadding;
155
156     private int verticalAlignment;
157
158     private PdfLine firstLine;
159     private PdfLine lastLine;
160
161     // constructors
162

163     /**
164      * Constructs a <CODE>PdfCell</CODE>-object.
165      *
166      * @param cell the original <CODE>Cell</CODE>
167      * @param rownumber the number of the <CODE>Row</CODE> the <CODE>Cell</CODE> was in.
168      * @param left the left border of the <CODE>PdfCell</CODE>
169      * @param right the right border of the <CODE>PdfCell</CODE>
170      * @param top the top border of the <CODE>PdfCell</CODE>
171      * @param cellspacing the cellspacing of the <CODE>Table</CODE>
172      * @param cellpadding the cellpadding of the <CODE>Table</CODE>
173      */

174
175     public PdfCell(Cell cell, int rownumber, float left, float right, float top, float cellspacing, float cellpadding) {
176         // constructs a Rectangle (the bottomvalue will be changed afterwards)
177
super(left, top, right, top);
178         // copying the other Rectangle attributes from class Cell
179
cloneNonPositionParameters(cell);
180         this.cellpadding = cellpadding;
181         this.cellspacing = cellspacing;
182         this.verticalAlignment = cell.getVerticalAlignment();
183         this.useAscender = cell.isUseAscender();
184         this.useDescender = cell.isUseDescender();
185         this.useBorderPadding = cell.isUseBorderPadding();
186
187         // initialisation of some parameters
188
PdfChunk chunk;
189         Element element;
190         PdfChunk overflow;
191         lines = new ArrayList JavaDoc();
192         images = new ArrayList JavaDoc();
193         leading = cell.getLeading();
194         int alignment = cell.getHorizontalAlignment();
195         left += cellspacing + cellpadding;
196         right -= cellspacing + cellpadding;
197
198         left += getBorderWidthInside(LEFT);
199         right -= getBorderWidthInside(RIGHT);
200
201
202         contentHeight = 0;
203
204         rowspan = cell.getRowspan();
205
206         ArrayList JavaDoc allActions;
207         int aCounter;
208         // we loop over all the elements of the cell
209
for (Iterator JavaDoc i = cell.getElements(); i.hasNext();) {
210             element = (Element) i.next();
211             switch (element.type()) {
212                 case Element.JPEG:
213                 case Element.IMGRAW:
214                 case Element.IMGTEMPLATE:
215                     addImage((Image) element, left, right, 0.4f * leading, alignment); //
216
break;
217                     // if the element is a list
218
case Element.LIST:
219                     if (line != null && line.size() > 0) {
220                         line.resetAlignment();
221                         addLine(line);
222                     }
223                     // we loop over all the listitems
224
addList((List JavaDoc)element, left, right, alignment);
225                     line = new PdfLine(left, right, alignment, leading);
226                     break;
227                     // if the element is something else
228
default:
229                     allActions = new ArrayList JavaDoc();
230                     processActions(element, null, allActions);
231                     aCounter = 0;
232
233                     float currentLineLeading = leading;
234                     float currentLeft = left;
235                     float currentRight = right;
236                     if (element instanceof Phrase) {
237                         currentLineLeading = ((Phrase) element).getLeading();
238                     }
239                     if (element instanceof Paragraph) {
240                         Paragraph p = (Paragraph) element;
241                         currentLeft += p.getIndentationLeft();
242                         currentRight -= p.getIndentationRight();
243                     }
244                     if (line == null) {
245                         line = new PdfLine(currentLeft, currentRight, alignment, currentLineLeading);
246                     }
247                     // we loop over the chunks
248
ArrayList JavaDoc chunks = element.getChunks();
249                     if (chunks.isEmpty()) {
250                        addLine(line); // add empty line - all cells need some lines even if they are empty
251
line = new PdfLine(currentLeft, currentRight, alignment, currentLineLeading);
252                     }
253                     else {
254                         for (Iterator JavaDoc j = chunks.iterator(); j.hasNext();) {
255                             Chunk c = (Chunk) j.next();
256                             chunk = new PdfChunk(c, (PdfAction) (allActions.get(aCounter++)));
257                             while ((overflow = line.add(chunk)) != null) {
258                                 addLine(line);
259                                 line = new PdfLine(currentLeft, currentRight, alignment, currentLineLeading);
260                                 chunk = overflow;
261                             }
262                         }
263                     }
264                     // if the element is a paragraph, section or chapter, we reset the alignment and add the line
265
switch (element.type()) {
266                         case Element.PARAGRAPH:
267                         case Element.SECTION:
268                         case Element.CHAPTER:
269                             line.resetAlignment();
270                             flushCurrentLine();
271                     }
272             }
273         }
274         flushCurrentLine();
275         if (lines.size() > cell.getMaxLines()) {
276             while (lines.size() > cell.getMaxLines()) {
277                 removeLine(lines.size() - 1);
278             }
279             if (cell.getMaxLines() > 0) {
280                 String JavaDoc more = cell.getShowTruncation();
281                 if (more != null && more.length() > 0) {
282                     // Denote that the content has been truncated
283
lastLine = (PdfLine) lines.get(lines.size() - 1);
284                     if (lastLine.size() >= 0) {
285                         PdfChunk lastChunk = lastLine.getChunk(lastLine.size() - 1);
286                         float moreWidth = new PdfChunk(more, lastChunk).width();
287                         while (lastChunk.toString().length() > 0 && lastChunk.width() + moreWidth > right - left) {
288                             // Remove characters to leave room for the 'more' indicator
289
lastChunk.setValue(lastChunk.toString().substring(0, lastChunk.length() - 1));
290                         }
291                         lastChunk.setValue(lastChunk.toString() + more);
292                     } else {
293                         lastLine.add(new PdfChunk(new Chunk(more), null));
294                     }
295                 }
296             }
297         }
298         // we set some additional parameters
299
if (useDescender && lastLine != null) {
300             contentHeight -= lastLine.getDescender();
301         }
302
303         // adjust first line height so that it touches the top
304
if (!lines.isEmpty()) {
305             firstLine = (PdfLine) lines.get(0);
306             float firstLineRealHeight = firstLineRealHeight();
307             contentHeight -= firstLine.height();
308             firstLine.height = firstLineRealHeight;
309             contentHeight += firstLineRealHeight;
310         }
311
312         float newBottom = top - contentHeight - (2f * cellpadding()) - (2f * cellspacing());
313         newBottom -= getBorderWidthInside(TOP) + getBorderWidthInside(BOTTOM);
314         setBottom(newBottom);
315
316         this.rownumber = rownumber;
317     }
318
319     private void addList(List JavaDoc list, float left, float right, int alignment) {
320         PdfChunk chunk;
321         PdfChunk overflow;
322         ArrayList JavaDoc allActions = new ArrayList JavaDoc();
323         processActions(list, null, allActions);
324         int aCounter = 0;
325         for (Iterator JavaDoc it = list.getItems().iterator(); it.hasNext();) {
326             Element ele = (Element)it.next();
327             switch (ele.type()) {
328                 case Element.LISTITEM:
329                     ListItem item = (ListItem)ele;
330                     line = new PdfLine(left + item.getIndentationLeft(), right, alignment, item.getLeading());
331                     line.setListItem(item);
332                     for (Iterator JavaDoc j = item.getChunks().iterator(); j.hasNext();) {
333                         chunk = new PdfChunk((Chunk) j.next(), (PdfAction) (allActions.get(aCounter++)));
334                         while ((overflow = line.add(chunk)) != null) {
335                             addLine(line);
336                             line = new PdfLine(left + item.getIndentationLeft(), right, alignment, item.getLeading());
337                             chunk = overflow;
338                         }
339                         line.resetAlignment();
340                         addLine(line);
341                         line = new PdfLine(left + item.getIndentationLeft(), right, alignment, leading);
342                     }
343                     break;
344                 case Element.LIST:
345                     List JavaDoc sublist = (List JavaDoc)ele;
346                     addList(sublist, left + sublist.getIndentationLeft(), right, alignment);
347                     break;
348             }
349         }
350     }
351
352     // overriding of the Rectangle methods
353

354
355     /**
356      * Sets the bottom of the Rectangle and determines the proper {link #verticalOffset}
357      * to appropriately align the contents vertically.
358      * @param value
359      */

360     public void setBottom(float value) {
361         super.setBottom(value);
362         float firstLineRealHeight = firstLineRealHeight();
363
364         float totalHeight = ury - value; // can't use top (already compensates for cellspacing)
365
float nonContentHeight = (cellpadding() * 2f) + (cellspacing() * 2f);
366         nonContentHeight += getBorderWidthInside(TOP) + getBorderWidthInside(BOTTOM);
367
368         float interiorHeight = totalHeight - nonContentHeight;
369         float extraHeight = 0.0f;
370
371         switch (verticalAlignment) {
372             case Element.ALIGN_BOTTOM:
373                 extraHeight = interiorHeight - contentHeight;
374                 break;
375             case Element.ALIGN_MIDDLE:
376                 extraHeight = (interiorHeight - contentHeight) / 2.0f;
377                 break;
378             default: // ALIGN_TOP
379
extraHeight = 0f;
380         }
381
382         extraHeight += cellpadding() + cellspacing();
383         extraHeight += getBorderWidthInside(TOP);
384         if (firstLine != null) {
385             firstLine.height = firstLineRealHeight + extraHeight;
386         }
387     }
388
389     /**
390      * Returns the lower left x-coordinaat.
391      *
392      * @return the lower left x-coordinaat
393      * @deprecated Use {@link #getLeft()} instead
394      */

395     
396     public float left() {
397         return getLeft();
398     }
399
400
401
402
403     /**
404      * Returns the lower left x-coordinaat.
405      *
406      * @return the lower left x-coordinaat
407      */

408
409     public float getLeft() {
410         return super.getLeft(cellspacing);
411     }
412
413     /**
414      * Returns the upper right x-coordinate.
415      *
416      * @return the upper right x-coordinate
417      * @deprecated Use {@link #getRight()} instead
418      */

419     
420     public float right() {
421         return getRight();
422     }
423
424
425
426
427     /**
428      * Returns the upper right x-coordinate.
429      *
430      * @return the upper right x-coordinate
431      */

432
433     public float getRight() {
434         return super.getRight(cellspacing);
435     }
436
437     /**
438      * Returns the upper right y-coordinate.
439      *
440      * @return the upper right y-coordinate
441      * @deprecated Use {@link #getTop()} instead
442      */

443     
444     public float top() {
445         return getTop();
446     }
447
448
449
450
451     /**
452      * Returns the upper right y-coordinate.
453      *
454      * @return the upper right y-coordinate
455      */

456
457     public float getTop() {
458         return super.getTop(cellspacing);
459     }
460
461     /**
462      * Returns the lower left y-coordinate.
463      *
464      * @return the lower left y-coordinate
465      * @deprecated Use {@link #getBottom()} instead
466      */

467     
468     public float bottom() {
469         return getBottom();
470     }
471
472
473
474
475     /**
476      * Returns the lower left y-coordinate.
477      *
478      * @return the lower left y-coordinate
479      */

480
481     public float getBottom() {
482         return super.getBottom(cellspacing);
483     }
484     
485     // methods
486

487     private void addLine(PdfLine line) {
488         lines.add(line);
489         contentHeight += line.height();
490         lastLine = line;
491         this.line = null;
492     }
493
494     private PdfLine removeLine(int index) {
495         PdfLine oldLine = (PdfLine) lines.remove(index);
496         contentHeight -= oldLine.height();
497         if (index == 0) {
498             if (!lines.isEmpty()) {
499                 firstLine = (PdfLine) lines.get(0);
500                 float firstLineRealHeight = firstLineRealHeight();
501                 contentHeight -= firstLine.height();
502                 firstLine.height = firstLineRealHeight;
503                 contentHeight += firstLineRealHeight;
504             }
505         }
506         return oldLine;
507     }
508
509     private void flushCurrentLine() {
510         if (line != null && line.size() > 0) {
511             addLine(line);
512         }
513     }
514
515     /**
516      * Calculates what the height of the first line should be so that the content will be
517      * flush with the top. For text, this is the height of the ascender. For an image,
518      * it is the actual height of the image.
519      * @return the real height of the first line
520      */

521     private float firstLineRealHeight() {
522         float firstLineRealHeight = 0f;
523         if (firstLine != null) {
524             PdfChunk chunk = firstLine.getChunk(0);
525             if (chunk != null) {
526                 Image image = chunk.getImage();
527                 if (image != null) {
528                     firstLineRealHeight = firstLine.getChunk(0).getImage().getScaledHeight();
529                 } else {
530                     firstLineRealHeight = useAscender ? firstLine.getAscender() : leading;
531                 }
532             }
533         }
534         return firstLineRealHeight;
535     }
536
537     /**
538      * Gets the amount of the border for the specified side that is inside the Rectangle.
539      * For non-variable width borders this is only 1/2 the border width on that side. This
540      * always returns 0 if {@link #useBorderPadding} is false;
541      * @param side the side to check. One of the side constants in {@link com.lowagie.text.Rectangle}
542      * @return the borderwidth inside the cell
543      */

544     private float getBorderWidthInside(int side) {
545         float width = 0f;
546         if (useBorderPadding) {
547             switch (side) {
548                 case Rectangle.LEFT:
549                     width = getBorderWidthLeft();
550                     break;
551
552                 case Rectangle.RIGHT:
553                     width = getBorderWidthRight();
554                     break;
555
556                 case Rectangle.TOP:
557                     width = getBorderWidthTop();
558                     break;
559
560                 default: // default and BOTTOM
561
width = getBorderWidthBottom();
562                     break;
563             }
564             // non-variable (original style) borders overlap the rectangle (only 1/2 counts)
565
if (!isUseVariableBorders()) {
566                 width = width / 2f;
567             }
568         }
569         return width;
570     }
571
572
573     /**
574      * Adds an image to this Cell.
575      *
576      * @param i the image to add
577      * @param left the left border
578      * @param right the right border
579      * @param extraHeight extra height to add above image
580      * @param alignment horizontal alignment (constant from Element class)
581      * @return the height of the image
582      */

583
584     private float addImage(Image i, float left, float right, float extraHeight, int alignment) {
585         Image image = Image.getInstance(i);
586         if (image.getScaledWidth() > right - left) {
587             image.scaleToFit(right - left, Float.MAX_VALUE);
588         }
589         flushCurrentLine();
590         if (line == null) {
591             line = new PdfLine(left, right, alignment, leading);
592         }
593         PdfLine imageLine = line;
594
595         // left and right in chunk is relative to the start of the line
596
right = right - left;
597         left = 0f;
598
599         if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) {
600             left = right - image.getScaledWidth();
601         } else if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) {
602             left = left + ((right - left - image.getScaledWidth()) / 2f);
603         }
604         Chunk imageChunk = new Chunk(image, left, 0);
605         imageLine.add(new PdfChunk(imageChunk, null));
606         addLine(imageLine);
607         return imageLine.height();
608     }
609
610     /**
611      * Gets the lines of a cell that can be drawn between certain limits.
612      * <P>
613      * Remark: all the lines that can be drawn are removed from the object!
614      *
615      * @param top the top of the part of the table that can be drawn
616      * @param bottom the bottom of the part of the table that can be drawn
617      * @return an <CODE>ArrayList</CODE> of <CODE>PdfLine</CODE>s
618      */

619
620     public ArrayList JavaDoc getLines(float top, float bottom) {
621         float lineHeight;
622         float currentPosition = Math.min(getTop(), top);
623         setTop(currentPosition + cellspacing);
624         ArrayList JavaDoc result = new ArrayList JavaDoc();
625
626         // if the bottom of the page is higher than the top of the cell: do nothing
627
if (getTop() < bottom) {
628             return result;
629         }
630         
631         // we loop over the lines
632
int size = lines.size();
633         boolean aboveBottom = true;
634         for (int i = 0; i < size && aboveBottom; i++) {
635             line = (PdfLine) lines.get(i);
636             lineHeight = line.height();
637             currentPosition -= lineHeight;
638             // if the currentPosition is higher than the bottom, we add the line to the result
639
if (currentPosition > (bottom + cellpadding + getBorderWidthInside(BOTTOM))) {
640                 result.add(line);
641             } else {
642                 aboveBottom = false;
643             }
644         }
645         // if the bottom of the cell is higher than the bottom of the page, the cell is written, so we can remove all lines
646
float difference = 0f;
647         if (!header) {
648             if (aboveBottom) {
649                 lines = new ArrayList JavaDoc();
650                 contentHeight = 0f;
651             } else {
652                 size = result.size();
653                 for (int i = 0; i < size; i++) {
654                     line = removeLine(0);
655                     difference += line.height();
656                 }
657             }
658         }
659         if (difference > 0) {
660             Image image;
661             for (Iterator JavaDoc i = images.iterator(); i.hasNext();) {
662                 image = (Image) i.next();
663                 image.setAbsolutePosition(image.getAbsoluteX(), image.getAbsoluteY() - difference - leading);
664             }
665         }
666         return result;
667     }
668
669     /**
670      * Gets the images of a cell that can be drawn between certain limits.
671      * <P>
672      * Remark: all the lines that can be drawn are removed from the object!
673      *
674      * @param top the top of the part of the table that can be drawn
675      * @param bottom the bottom of the part of the table that can be drawn
676      * @return an <CODE>ArrayList</CODE> of <CODE>Image</CODE>s
677      */

678
679     public ArrayList JavaDoc getImages(float top, float bottom) {
680
681         // if the bottom of the page is higher than the top of the cell: do nothing
682
if (getTop() < bottom) {
683             return new ArrayList JavaDoc();
684         }
685         top = Math.min(getTop(), top);
686         // initialisations
687
Image image;
688         float height;
689         ArrayList JavaDoc result = new ArrayList JavaDoc();
690         // we loop over the images
691
for (Iterator JavaDoc i = images.iterator(); i.hasNext() && !header;) {
692             image = (Image) i.next();
693             height = image.getAbsoluteY();
694             // if the currentPosition is higher than the bottom, we add the line to the result
695
if (top - height > (bottom + cellpadding)) {
696                 image.setAbsolutePosition(image.getAbsoluteX(), top - height);
697                 result.add(image);
698                 i.remove();
699             }
700         }
701         return result;
702     }
703
704     /**
705      * Checks if this cell belongs to the header of a <CODE>PdfTable</CODE>.
706      *
707      * @return <CODE>void</CODE>
708      */

709
710     boolean isHeader() {
711         return header;
712     }
713
714     /**
715      * Indicates that this cell belongs to the header of a <CODE>PdfTable</CODE>.
716      */

717
718     void setHeader() {
719         header = true;
720     }
721
722     /**
723      * Checks if the cell may be removed.
724      * <P>
725      * Headers may allways be removed, even if they are drawn only partially:
726      * they will be repeated on each following page anyway!
727      *
728      * @return <CODE>true</CODE> if all the lines are allready drawn; <CODE>false</CODE> otherwise.
729      */

730
731     boolean mayBeRemoved() {
732         return (header || (lines.isEmpty() && images.isEmpty()));
733     }
734
735     /**
736      * Returns the number of lines in the cell.
737      *
738      * @return a value
739      */

740
741     public int size() {
742         return lines.size();
743     }
744
745     /**
746      * Returns the number of lines in the cell that are not empty.
747      *
748      * @return a value
749      */

750
751     public int remainingLines() {
752         if (lines.isEmpty()) return 0;
753         int result = 0;
754         int size = lines.size();
755         PdfLine line;
756         for (int i = 0; i < size; i++) {
757             line = (PdfLine) lines.get(i);
758             if (line.size() > 0) result++;
759         }
760         return result;
761     }
762
763     /**
764      * Returns the height needed to draw the remaining text.
765      *
766      * @return a height
767      */

768
769     public float remainingHeight() {
770         float result = 0f;
771         for (Iterator JavaDoc i = images.iterator(); i.hasNext();) {
772             Image image = (Image) i.next();
773             result += image.getScaledHeight();
774         }
775         return remainingLines() * leading + 2 * cellpadding + cellspacing + result + leading / 2.5f;
776     }
777     
778     // methods to retrieve membervariables
779

780     /**
781      * Gets the leading of a cell.
782      *
783      * @return the leading of the lines is the cell.
784      */

785
786     public float leading() {
787         return leading;
788     }
789
790     /**
791      * Gets the number of the row this cell is in..
792      *
793      * @return a number
794      */

795
796     public int rownumber() {
797         return rownumber;
798     }
799
800     /**
801      * Gets the rowspan of a cell.
802      *
803      * @return the rowspan of the cell
804      */

805
806     public int rowspan() {
807         return rowspan;
808     }
809
810     /**
811      * Gets the cellspacing of a cell.
812      *
813      * @return a value
814      */

815
816     public float cellspacing() {
817         return cellspacing;
818     }
819
820     /**
821      * Gets the cellpadding of a cell..
822      *
823      * @return a value
824      */

825
826     public float cellpadding() {
827         return cellpadding;
828     }
829
830     /**
831      * Processes all actions contained in the cell.
832      * @param element an element in the cell
833      * @param action an action that should be coupled to the cell
834      * @param allActions
835      */

836
837     protected void processActions(Element element, PdfAction action, ArrayList JavaDoc allActions) {
838         if (element.type() == Element.ANCHOR) {
839             String JavaDoc url = ((Anchor) element).getReference();
840             if (url != null) {
841                 action = new PdfAction(url);
842             }
843         }
844         Iterator JavaDoc i;
845         switch (element.type()) {
846             case Element.PHRASE:
847             case Element.SECTION:
848             case Element.ANCHOR:
849             case Element.CHAPTER:
850             case Element.LISTITEM:
851             case Element.PARAGRAPH:
852                 for (i = ((ArrayList JavaDoc) element).iterator(); i.hasNext();) {
853                     processActions((Element) i.next(), action, allActions);
854                 }
855                 break;
856             case Element.CHUNK:
857                 allActions.add(action);
858                 break;
859             case Element.LIST:
860                 for (i = ((List JavaDoc) element).getItems().iterator(); i.hasNext();) {
861                     processActions((Element) i.next(), action, allActions);
862                 }
863                 break;
864             default:
865                 int n = element.getChunks().size();
866                 while (n-- > 0)
867                     allActions.add(action);
868                 break;
869         }
870     }
871
872     /**
873      * This is the number of the group the cell is in.
874      */

875     private int groupNumber;
876
877     /**
878      * Gets the number of the group this cell is in..
879      *
880      * @return a number
881      */

882
883     public int getGroupNumber() {
884         return groupNumber;
885     }
886
887     /**
888      * Sets the group number.
889      * @param number
890      */

891
892     void setGroupNumber(int number) {
893         groupNumber = number;
894     }
895
896     /**
897      * Gets a Rectangle that is altered to fit on the page.
898      *
899      * @param top the top position
900      * @param bottom the bottom position
901      * @return a <CODE>Rectangle</CODE>
902      */

903
904     public Rectangle rectangle(float top, float bottom) {
905         Rectangle tmp = new Rectangle(getLeft(), getBottom(), getRight(), getTop());
906         tmp.cloneNonPositionParameters(this);
907         if (getTop() > top) {
908             tmp.setTop(top);
909             tmp.setBorder(border - (border & TOP));
910         }
911         if (getBottom() < bottom) {
912             tmp.setBottom(bottom);
913             tmp.setBorder(border - (border & BOTTOM));
914         }
915         return tmp;
916     }
917
918     /**
919      * Sets the value of {@link #useAscender}.
920      * @param use use ascender height if true
921      */

922     public void setUseAscender(boolean use) {
923         useAscender = use;
924     }
925
926     /**
927      * Gets the value of {@link #useAscender}
928      * @return useAscender
929      */

930     public boolean isUseAscender() {
931         return useAscender;
932     }
933
934     /**
935      * Sets the value of {@link #useDescender}.
936      * @param use use descender height if true
937      */

938     public void setUseDescender(boolean use) {
939         useDescender = use;
940     }
941
942     /**
943      * gets the value of {@link #useDescender }
944      * @return useDescender
945      */

946     public boolean isUseDescender() {
947         return useDescender;
948     }
949
950     /**
951      * Sets the value of {@link #useBorderPadding}.
952      * @param use adjust layour for borders if true
953      */

954     public void setUseBorderPadding(boolean use) {
955         useBorderPadding = use;
956     }
957
958     /**
959      * Gets the value of {@link #useBorderPadding}.
960      * @return useBorderPadding
961      */

962     public boolean isUseBorderPadding() {
963         return useBorderPadding;
964     }
965
966 }
967
Popular Tags