KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > SimpleCell


1 /*
2  * $Id: SimpleCell.java 2748 2007-05-12 15:11:48Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999-2005 by 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 package com.lowagie.text;
51
52 import java.util.ArrayList JavaDoc;
53 import java.util.Iterator JavaDoc;
54
55 import com.lowagie.text.pdf.PdfContentByte;
56 import com.lowagie.text.pdf.PdfPCell;
57 import com.lowagie.text.pdf.PdfPCellEvent;
58 import com.lowagie.text.pdf.PdfPTable;
59
60 /**
61  * Rectangle that can be used for Cells.
62  * This Rectangle is padded and knows how to draw itself in a PdfPTable or PdfPcellEvent.
63  */

64 public class SimpleCell extends Rectangle implements PdfPCellEvent, TextElementArray {
65
66     // constants
67
/** the CellAttributes object represents a row. */
68     public static final boolean ROW = true;
69     /** the CellAttributes object represents a cell. */
70     public static final boolean CELL = false;
71     
72     // member variables
73
/** the content of the Cell. */
74     private ArrayList JavaDoc content = new ArrayList JavaDoc();
75     /** the width of the Cell. */
76     private float width = 0f;
77     /** the widthpercentage of the Cell. */
78     private float widthpercentage = 0f;
79     /** an extra spacing variable */
80     private float spacing_left = Float.NaN;
81     /** an extra spacing variable */
82     private float spacing_right = Float.NaN;
83     /** an extra spacing variable */
84     private float spacing_top = Float.NaN;
85     /** an extra spacing variable */
86     private float spacing_bottom = Float.NaN;
87     /** an extra padding variable */
88     private float padding_left = Float.NaN;
89     /** an extra padding variable */
90     private float padding_right = Float.NaN;
91     /** an extra padding variable */
92     private float padding_top = Float.NaN;
93     /** an extra padding variable */
94     private float padding_bottom = Float.NaN;
95     /** the colspan of a Cell */
96     private int colspan = 1;
97     /** horizontal alignment inside the Cell. */
98     private int horizontalAlignment = Element.ALIGN_UNDEFINED;
99     /** vertical alignment inside the Cell. */
100     private int verticalAlignment = Element.ALIGN_UNDEFINED;
101     /** indicates if these are the attributes of a single Cell (false) or a group of Cells (true). */
102     private boolean cellgroup = false;
103     /** Indicates that the largest ascender height should be used to determine the
104      * height of the first line. Note that this only has an effect when rendered
105      * to PDF. Setting this to true can help with vertical alignment problems. */

106     protected boolean useAscender = false;
107     /** Indicates that the largest descender height should be added to the height of
108      * the last line (so characters like y don't dip into the border). Note that
109      * this only has an effect when rendered to PDF. */

110     protected boolean useDescender = false;
111     /**
112      * Adjusts the cell contents to compensate for border widths. Note that
113      * this only has an effect when rendered to PDF.
114      */

115     protected boolean useBorderPadding;
116     
117     /**
118      * A CellAttributes object is always constructed without any dimensions.
119      * Dimensions are defined after creation.
120      * @param row only true if the CellAttributes object represents a row.
121      */

122     public SimpleCell(boolean row) {
123         super(0f, 0f, 0f, 0f);
124         cellgroup = row;
125         setBorder(BOX);
126     }
127     
128     /**
129      * Adds content to this object.
130      * @param element
131      * @throws BadElementException
132      */

133     public void addElement(Element element) throws BadElementException {
134         if (cellgroup) {
135             if (element instanceof SimpleCell) {
136                 if(((SimpleCell)element).isCellgroup()) {
137                     throw new BadElementException("You can't add one row to another row.");
138                 }
139                 content.add(element);
140                 return;
141             }
142             else {
143                 throw new BadElementException("You can only add cells to rows, no objects of type " + element.getClass().getName());
144             }
145         }
146         if (element.type() == Element.PARAGRAPH
147                 || element.type() == Element.PHRASE
148                 || element.type() == Element.ANCHOR
149                 || element.type() == Element.CHUNK
150                 || element.type() == Element.LIST
151                 || element.type() == Element.MARKED
152                 || element.type() == Element.JPEG
153                 || element.type() == Element.IMGRAW
154                 || element.type() == Element.IMGTEMPLATE) {
155             content.add(element);
156         }
157         else {
158             throw new BadElementException("You can't add an element of type " + element.getClass().getName() + " to a SimpleCell.");
159         }
160     }
161     
162     /**
163      * Creates a Cell with these attributes.
164      * @param rowAttributes
165      * @return a cell based on these attributes.
166      * @throws BadElementException
167      */

168     public Cell createCell(SimpleCell rowAttributes) throws BadElementException {
169         Cell cell = new Cell();
170         cell.cloneNonPositionParameters(rowAttributes);
171         cell.softCloneNonPositionParameters(this);
172         cell.setColspan(colspan);
173         cell.setHorizontalAlignment(horizontalAlignment);
174         cell.setVerticalAlignment(verticalAlignment);
175         cell.setUseAscender(useAscender);
176         cell.setUseBorderPadding(useBorderPadding);
177         cell.setUseDescender(useDescender);
178         Element element;
179         for (Iterator JavaDoc i = content.iterator(); i.hasNext(); ) {
180             element = (Element)i.next();
181             cell.addElement(element);
182         }
183         return cell;
184     }
185     
186     /**
187      * Creates a PdfPCell with these attributes.
188      * @param rowAttributes
189      * @return a PdfPCell based on these attributes.
190      */

191     public PdfPCell createPdfPCell(SimpleCell rowAttributes) {
192         PdfPCell cell = new PdfPCell();
193         cell.setBorder(NO_BORDER);
194         SimpleCell tmp = new SimpleCell(CELL);
195         tmp.setSpacing_left(spacing_left);
196         tmp.setSpacing_right(spacing_right);
197         tmp.setSpacing_top(spacing_top);
198         tmp.setSpacing_bottom(spacing_bottom);
199         tmp.cloneNonPositionParameters(rowAttributes);
200         tmp.softCloneNonPositionParameters(this);
201         cell.setCellEvent(tmp);
202         cell.setHorizontalAlignment(rowAttributes.horizontalAlignment);
203         cell.setVerticalAlignment(rowAttributes.verticalAlignment);
204         cell.setUseAscender(rowAttributes.useAscender);
205         cell.setUseBorderPadding(rowAttributes.useBorderPadding);
206         cell.setUseDescender(rowAttributes.useDescender);
207         cell.setColspan(colspan);
208         if (horizontalAlignment != Element.ALIGN_UNDEFINED)
209             cell.setHorizontalAlignment(horizontalAlignment);
210         if (verticalAlignment != Element.ALIGN_UNDEFINED)
211             cell.setVerticalAlignment(verticalAlignment);
212         if (useAscender)
213             cell.setUseAscender(useAscender);
214         if (useBorderPadding)
215             cell.setUseBorderPadding(useBorderPadding);
216         if (useDescender)
217             cell.setUseDescender(useDescender);
218         float p;
219         float sp_left = spacing_left;
220         if (Float.isNaN(sp_left)) sp_left = 0f;
221         float sp_right = spacing_right;
222         if (Float.isNaN(sp_right)) sp_right = 0f;
223         float sp_top = spacing_top;
224         if (Float.isNaN(sp_top)) sp_top = 0f;
225         float sp_bottom = spacing_bottom;
226         if (Float.isNaN(sp_bottom)) sp_bottom = 0f;
227         p = padding_left;
228         if (Float.isNaN(p)) p = 0f;
229         cell.setPaddingLeft(p + sp_left);
230         p = padding_right;
231         if (Float.isNaN(p)) p = 0f;
232         cell.setPaddingRight(p + sp_right);
233         p = padding_top;
234         if (Float.isNaN(p)) p = 0f;
235         cell.setPaddingTop(p + sp_top);
236         p = padding_bottom;
237         if (Float.isNaN(p)) p = 0f;
238         cell.setPaddingBottom(p + sp_bottom);
239         Element element;
240         for (Iterator JavaDoc i = content.iterator(); i.hasNext(); ) {
241             element = (Element)i.next();
242             cell.addElement(element);
243         }
244         return cell;
245     }
246     
247     /**
248      * @param rectangle
249      * @param spacing
250      * @return a rectangle
251      */

252     public static SimpleCell getDimensionlessInstance(Rectangle rectangle, float spacing) {
253         SimpleCell event = new SimpleCell(CELL);
254         event.cloneNonPositionParameters(rectangle);
255         event.setSpacing(spacing * 2f);
256         return event;
257     }
258
259     /**
260      * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[])
261      */

262     public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
263         float sp_left = spacing_left;
264         if (Float.isNaN(sp_left)) sp_left = 0f;
265         float sp_right = spacing_right;
266         if (Float.isNaN(sp_right)) sp_right = 0f;
267         float sp_top = spacing_top;
268         if (Float.isNaN(sp_top)) sp_top = 0f;
269         float sp_bottom = spacing_bottom;
270         if (Float.isNaN(sp_bottom)) sp_bottom = 0f;
271         Rectangle rect = new Rectangle(position.getLeft(sp_left), position.getBottom(sp_bottom), position.getRight(sp_right), position.getTop(sp_top));
272         rect.cloneNonPositionParameters(this);
273         canvases[PdfPTable.BACKGROUNDCANVAS].rectangle(rect);
274         rect.setBackgroundColor(null);
275         canvases[PdfPTable.LINECANVAS].rectangle(rect);
276     }
277     
278     /** Sets the padding parameters if they are undefined.
279      * @param padding*/

280     public void setPadding(float padding) {
281         if (Float.isNaN(padding_right)) {
282             setPadding_right(padding);
283         }
284         if (Float.isNaN(padding_left)) {
285             setPadding_left(padding);
286         }
287         if (Float.isNaN(padding_top)) {
288             setPadding_top(padding);
289         }
290         if (Float.isNaN(padding_bottom)) {
291             setPadding_bottom(padding);
292         }
293     }
294     
295     /**
296      * @return Returns the colspan.
297      */

298     public int getColspan() {
299         return colspan;
300     }
301     /**
302      * @param colspan The colspan to set.
303      */

304     public void setColspan(int colspan) {
305         if (colspan > 0) this.colspan = colspan;
306     }
307     /**
308      * @return Returns the padding_bottom.
309      */

310     public float getPadding_bottom() {
311         return padding_bottom;
312     }
313     /**
314      * @param padding_bottom The padding_bottom to set.
315      */

316     public void setPadding_bottom(float padding_bottom) {
317         this.padding_bottom = padding_bottom;
318     }
319     /**
320      * @return Returns the padding_left.
321      */

322     public float getPadding_left() {
323         return padding_left;
324     }
325     /**
326      * @param padding_left The padding_left to set.
327      */

328     public void setPadding_left(float padding_left) {
329         this.padding_left = padding_left;
330     }
331     /**
332      * @return Returns the padding_right.
333      */

334     public float getPadding_right() {
335         return padding_right;
336     }
337     /**
338      * @param padding_right The padding_right to set.
339      */

340     public void setPadding_right(float padding_right) {
341         this.padding_right = padding_right;
342     }
343     /**
344      * @return Returns the padding_top.
345      */

346     public float getPadding_top() {
347         return padding_top;
348     }
349     /**
350      * @param padding_top The padding_top to set.
351      */

352     public void setPadding_top(float padding_top) {
353         this.padding_top = padding_top;
354     }
355     /**
356      * @return Returns the spacing.
357      */

358     public float getSpacing_left() {
359         return spacing_left;
360     }
361     /**
362      * @return Returns the spacing.
363      */

364     public float getSpacing_right() {
365         return spacing_right;
366     }
367     /**
368      * @return Returns the spacing.
369      */

370     public float getSpacing_top() {
371         return spacing_top;
372     }
373     /**
374      * @return Returns the spacing.
375      */

376     public float getSpacing_bottom() {
377         return spacing_bottom;
378     }
379     
380     /**
381      * @param spacing The spacing to set.
382      */

383     public void setSpacing(float spacing) {
384         this.spacing_left = spacing;
385         this.spacing_right = spacing;
386         this.spacing_top = spacing;
387         this.spacing_bottom = spacing;
388     }
389     
390     /**
391      * @param spacing The spacing to set.
392      */

393     public void setSpacing_left(float spacing) {
394         this.spacing_left = spacing;
395     }
396     
397     /**
398      * @param spacing The spacing to set.
399      */

400     public void setSpacing_right(float spacing) {
401         this.spacing_right = spacing;
402     }
403     
404     /**
405      * @param spacing The spacing to set.
406      */

407     public void setSpacing_top(float spacing) {
408         this.spacing_top = spacing;
409     }
410     
411     /**
412      * @param spacing The spacing to set.
413      */

414     public void setSpacing_bottom(float spacing) {
415         this.spacing_bottom = spacing;
416     }
417     
418     /**
419      * @return Returns the cellgroup.
420      */

421     public boolean isCellgroup() {
422         return cellgroup;
423     }
424     /**
425      * @param cellgroup The cellgroup to set.
426      */

427     public void setCellgroup(boolean cellgroup) {
428         this.cellgroup = cellgroup;
429     }
430     /**
431      * @return Returns the horizontal alignment.
432      */

433     public int getHorizontalAlignment() {
434         return horizontalAlignment;
435     }
436     /**
437      * @param horizontalAlignment The horizontalAlignment to set.
438      */

439     public void setHorizontalAlignment(int horizontalAlignment) {
440         this.horizontalAlignment = horizontalAlignment;
441     }
442     /**
443      * @return Returns the vertical alignment.
444      */

445     public int getVerticalAlignment() {
446         return verticalAlignment;
447     }
448     /**
449      * @param verticalAlignment The verticalAligment to set.
450      */

451     public void setVerticalAlignment(int verticalAlignment) {
452         this.verticalAlignment = verticalAlignment;
453     }
454     /**
455      * @return Returns the width.
456      */

457     public float getWidth() {
458         return width;
459     }
460     /**
461      * @param width The width to set.
462      */

463     public void setWidth(float width) {
464         this.width = width;
465     }
466     /**
467      * @return Returns the widthpercentage.
468      */

469     public float getWidthpercentage() {
470         return widthpercentage;
471     }
472     /**
473      * @param widthpercentage The widthpercentage to set.
474      */

475     public void setWidthpercentage(float widthpercentage) {
476         this.widthpercentage = widthpercentage;
477     }
478     /**
479      * @return Returns the useAscender.
480      */

481     public boolean isUseAscender() {
482         return useAscender;
483     }
484     /**
485      * @param useAscender The useAscender to set.
486      */

487     public void setUseAscender(boolean useAscender) {
488         this.useAscender = useAscender;
489     }
490     /**
491      * @return Returns the useBorderPadding.
492      */

493     public boolean isUseBorderPadding() {
494         return useBorderPadding;
495     }
496     /**
497      * @param useBorderPadding The useBorderPadding to set.
498      */

499     public void setUseBorderPadding(boolean useBorderPadding) {
500         this.useBorderPadding = useBorderPadding;
501     }
502     /**
503      * @return Returns the useDescender.
504      */

505     public boolean isUseDescender() {
506         return useDescender;
507     }
508     /**
509      * @param useDescender The useDescender to set.
510      */

511     public void setUseDescender(boolean useDescender) {
512         this.useDescender = useDescender;
513     }
514     
515     /**
516      * @return Returns the content.
517      */

518     ArrayList JavaDoc getContent() {
519         return content;
520     }
521
522     /**
523      * @see com.lowagie.text.TextElementArray#add(java.lang.Object)
524      */

525     public boolean add(Object JavaDoc o) {
526         try {
527             addElement((Element)o);
528             return true;
529         }
530         catch(ClassCastException JavaDoc e) {
531             return false;
532         }
533         catch(BadElementException e) {
534             throw new ExceptionConverter(e);
535         }
536     }
537     /**
538      * @see com.lowagie.text.Element#type()
539      */

540     public int type() {
541         return Element.CELL;
542     }
543 }
Popular Tags