KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Paragraph.java 2748 2007-05-12 15:11:48Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 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
51 package com.lowagie.text;
52
53 /**
54  * A <CODE>Paragraph</CODE> is a series of <CODE>Chunk</CODE>s and/or <CODE>Phrases</CODE>.
55  * <P>
56  * A <CODE>Paragraph</CODE> has the same qualities of a <CODE>Phrase</CODE>, but also
57  * some additional layout-parameters:
58  * <UL>
59  * <LI>the indentation
60  * <LI>the alignment of the text
61  * </UL>
62  *
63  * Example:
64  * <BLOCKQUOTE><PRE>
65  * <STRONG>Paragraph p = new Paragraph("This is a paragraph",
66  * FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));</STRONG>
67  * </PRE></BLOCKQUOTE>
68  *
69  * @see Element
70  * @see Phrase
71  * @see ListItem
72  */

73
74 public class Paragraph extends Phrase {
75     
76     // constants
77
private static final long serialVersionUID = 7852314969733375514L;
78     
79     // membervariables
80

81     /** The alignment of the text. */
82     protected int alignment = Element.ALIGN_UNDEFINED;
83     
84     /** The text leading that is multiplied by the biggest font size in the line. */
85     protected float multipliedLeading = 0;
86     
87     /** The indentation of this paragraph on the left side. */
88     protected float indentationLeft;
89     
90     /** The indentation of this paragraph on the right side. */
91     protected float indentationRight;
92     
93     /** Holds value of property firstLineIndent. */
94     private float firstLineIndent = 0;
95     
96     /** The spacing before the paragraph. */
97     protected float spacingBefore;
98     
99     /** The spacing after the paragraph. */
100     protected float spacingAfter;
101     
102     /** Holds value of property extraParagraphSpace. */
103     private float extraParagraphSpace = 0;
104     
105     /** Does the paragraph has to be kept together on 1 page. */
106     protected boolean keeptogether = false;
107     
108     // constructors
109

110     /**
111      * Constructs a <CODE>Paragraph</CODE>.
112      */

113     public Paragraph() {
114         super();
115     }
116     
117     /**
118      * Constructs a <CODE>Paragraph</CODE> with a certain leading.
119      *
120      * @param leading the leading
121      */

122     public Paragraph(float leading) {
123         super(leading);
124     }
125     
126     /**
127      * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>Chunk</CODE>.
128      *
129      * @param chunk a <CODE>Chunk</CODE>
130      */

131     public Paragraph(Chunk chunk) {
132         super(chunk);
133     }
134     
135     /**
136      * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>Chunk</CODE>
137      * and a certain leading.
138      *
139      * @param leading the leading
140      * @param chunk a <CODE>Chunk</CODE>
141      */

142     public Paragraph(float leading, Chunk chunk) {
143         super(leading, chunk);
144     }
145     
146     /**
147      * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>String</CODE>.
148      *
149      * @param string a <CODE>String</CODE>
150      */

151     public Paragraph(String JavaDoc string) {
152         super(string);
153     }
154     
155     /**
156      * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>String</CODE>
157      * and a certain <CODE>Font</CODE>.
158      *
159      * @param string a <CODE>String</CODE>
160      * @param font a <CODE>Font</CODE>
161      */

162     public Paragraph(String JavaDoc string, Font font) {
163         super(string, font);
164     }
165     
166     /**
167      * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>String</CODE>
168      * and a certain leading.
169      *
170      * @param leading the leading
171      * @param string a <CODE>String</CODE>
172      */

173     public Paragraph(float leading, String JavaDoc string) {
174         super(leading, string);
175     }
176     
177     /**
178      * Constructs a <CODE>Paragraph</CODE> with a certain leading, <CODE>String</CODE>
179      * and <CODE>Font</CODE>.
180      *
181      * @param leading the leading
182      * @param string a <CODE>String</CODE>
183      * @param font a <CODE>Font</CODE>
184      */

185     public Paragraph(float leading, String JavaDoc string, Font font) {
186         super(leading, string, font);
187     }
188     
189     /**
190      * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>Phrase</CODE>.
191      *
192      * @param phrase a <CODE>Phrase</CODE>
193      */

194     public Paragraph(Phrase phrase) {
195         super(phrase);
196         if (phrase instanceof Paragraph) {
197             Paragraph p = (Paragraph)phrase;
198             setAlignment(p.alignment);
199             setLeading(phrase.getLeading(), p.multipliedLeading);
200             setIndentationLeft(p.getIndentationLeft());
201             setIndentationRight(p.getIndentationRight());
202             setFirstLineIndent(p.getFirstLineIndent());
203             setSpacingAfter(p.spacingAfter());
204             setSpacingBefore(p.spacingBefore());
205             setExtraParagraphSpace(p.getExtraParagraphSpace());
206         }
207     }
208     
209     // implementation of the Element-methods
210

211     /**
212      * Gets the type of the text element.
213      *
214      * @return a type
215      */

216     public int type() {
217         return Element.PARAGRAPH;
218     }
219     
220     // methods
221

222     /**
223      * Adds an <CODE>Object</CODE> to the <CODE>Paragraph</CODE>.
224      *
225      * @param o object the object to add.
226      * @return true is adding the object succeeded
227      */

228     public boolean add(Object JavaDoc o) {
229         if (o instanceof List) {
230             List list = (List) o;
231             list.setIndentationLeft(list.getIndentationLeft() + indentationLeft);
232             list.setIndentationRight(indentationRight);
233             return super.add(list);
234         }
235         else if (o instanceof Image) {
236             super.addSpecial(o);
237             return true;
238         }
239         else if (o instanceof Paragraph) {
240             super.add(o);
241             super.add(Chunk.NEWLINE);
242             return true;
243         }
244         return super.add(o);
245     }
246     
247     // setting the membervariables
248

249     /**
250      * Sets the alignment of this paragraph.
251      *
252      * @param alignment the new alignment
253      */

254     public void setAlignment(int alignment) {
255         this.alignment = alignment;
256     }
257     
258     /**
259      * Sets the alignment of this paragraph.
260      *
261      * @param alignment the new alignment as a <CODE>String</CODE>
262      */

263     public void setAlignment(String JavaDoc alignment) {
264         if (ElementTags.ALIGN_CENTER.equalsIgnoreCase(alignment)) {
265             this.alignment = Element.ALIGN_CENTER;
266             return;
267         }
268         if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(alignment)) {
269             this.alignment = Element.ALIGN_RIGHT;
270             return;
271         }
272         if (ElementTags.ALIGN_JUSTIFIED.equalsIgnoreCase(alignment)) {
273             this.alignment = Element.ALIGN_JUSTIFIED;
274             return;
275         }
276         if (ElementTags.ALIGN_JUSTIFIED_ALL.equalsIgnoreCase(alignment)) {
277             this.alignment = Element.ALIGN_JUSTIFIED_ALL;
278             return;
279         }
280         this.alignment = Element.ALIGN_LEFT;
281     }
282     
283     /**
284      * @see com.lowagie.text.Phrase#setLeading(float)
285      */

286     public void setLeading(float fixedLeading) {
287         this.leading = fixedLeading;
288         this.multipliedLeading = 0;
289     }
290     
291     /**
292      * Sets the variable leading. The resultant leading will be
293      * multipliedLeading*maxFontSize where maxFontSize is the
294      * size of the bigest font in the line.
295      * @param multipliedLeading the variable leading
296      */

297     public void setMultipliedLeading(float multipliedLeading) {
298         this.leading = 0;
299         this.multipliedLeading = multipliedLeading;
300     }
301     
302     /**
303      * Sets the leading fixed and variable. The resultant leading will be
304      * fixedLeading+multipliedLeading*maxFontSize where maxFontSize is the
305      * size of the bigest font in the line.
306      * @param fixedLeading the fixed leading
307      * @param multipliedLeading the variable leading
308      */

309     public void setLeading(float fixedLeading, float multipliedLeading) {
310         this.leading = fixedLeading;
311         this.multipliedLeading = multipliedLeading;
312     }
313     
314     /**
315      * Sets the indentation of this paragraph on the left side.
316      *
317      * @param indentation the new indentation
318      */

319     public void setIndentationLeft(float indentation) {
320         this.indentationLeft = indentation;
321     }
322     
323     /**
324      * Sets the indentation of this paragraph on the right side.
325      *
326      * @param indentation the new indentation
327      */

328     public void setIndentationRight(float indentation) {
329         this.indentationRight = indentation;
330     }
331     
332     /**
333      * Setter for property firstLineIndent.
334      * @param firstLineIndent New value of property firstLineIndent.
335      */

336     public void setFirstLineIndent(float firstLineIndent) {
337         this.firstLineIndent = firstLineIndent;
338     }
339     
340     /**
341      * Sets the spacing before this paragraph.
342      *
343      * @param spacing the new spacing
344      */

345     public void setSpacingBefore(float spacing) {
346         this.spacingBefore = spacing;
347     }
348     
349     /**
350      * Sets the spacing after this paragraph.
351      *
352      * @param spacing the new spacing
353      */

354     public void setSpacingAfter(float spacing) {
355         this.spacingAfter = spacing;
356     }
357     
358     /**
359      * Indicates that the paragraph has to be kept together on one page.
360      *
361      * @param keeptogether true of the paragraph may not be split over 2 pages
362      */

363     public void setKeepTogether(boolean keeptogether) {
364         this.keeptogether = keeptogether;
365     }
366     
367     /**
368      * Checks if this paragraph has to be kept together on one page.
369      *
370      * @return true if the paragraph may not be split over 2 pages.
371      */

372     public boolean getKeepTogether() {
373         return keeptogether;
374     }
375
376     // methods to retrieve information
377

378     /**
379      * Gets the alignment of this paragraph.
380      *
381      * @return alignment
382      */

383     public int getAlignment() {
384         return alignment;
385     }
386     
387     /**
388      * Gets the variable leading
389      * @return the leading
390      */

391     public float getMultipliedLeading() {
392         return multipliedLeading;
393     }
394     
395     /**
396      * Gets the total leading.
397      * This method is based on the assumption that the
398      * font of the Paragraph is the font of all the elements
399      * that make part of the paragraph. This isn't necessarily
400      * true.
401      * @return the total leading (fixed and multiplied)
402      */

403     public float getTotalLeading() {
404         float m = font == null ?
405                 Font.DEFAULTSIZE * multipliedLeading : font.getCalculatedLeading(multipliedLeading);
406         if (m > 0 && !hasLeading()) {
407             return m;
408         }
409         return getLeading() + m;
410     }
411
412     /**
413      * Gets the indentation of this paragraph on the left side.
414      *
415      * @return the indentation
416      */

417     public float getIndentationLeft() {
418         return indentationLeft;
419     }
420
421     /**
422      * Gets the indentation of this paragraph on the right side.
423      *
424      * @return the indentation
425      */

426     public float getIndentationRight() {
427         return indentationRight;
428     }
429     
430     /**
431      * Getter for property firstLineIndent.
432      * @return Value of property firstLineIndent.
433      */

434     public float getFirstLineIndent() {
435         return this.firstLineIndent;
436     }
437     
438     /**
439      * Gets the spacing before this paragraph.
440      *
441      * @return the spacing
442      */

443     public float spacingBefore() {
444         return spacingBefore;
445     }
446
447     /**
448      * Gets the spacing after this paragraph.
449      *
450      * @return the spacing
451      */

452     public float spacingAfter() {
453         return spacingAfter;
454     }
455     
456     /**
457      * Getter for property extraParagraphSpace.
458      * @return Value of property extraParagraphSpace.
459      */

460     public float getExtraParagraphSpace() {
461         return this.extraParagraphSpace;
462     }
463     
464     /**
465      * Setter for property extraParagraphSpace.
466      * @param extraParagraphSpace New value of property extraParagraphSpace.
467      */

468     public void setExtraParagraphSpace(float extraParagraphSpace) {
469         this.extraParagraphSpace = extraParagraphSpace;
470     }
471
472     // deprecated stuff
473

474     /**
475      * Returns a <CODE>Paragraph</CODE> that has been constructed taking in account
476      * the value of some <VAR>attributes</VAR>.
477      *
478      * @param attributes Some attributes
479      * @deprecated use ElementFactory.getParagraph(attributes)
480      */

481     public Paragraph(java.util.Properties JavaDoc attributes) {
482         this(com.lowagie.text.factories.ElementFactory.getParagraph(attributes));
483     }
484     
485     /**
486      * Gets the alignment of this paragraph.
487      *
488      * @return alignment
489      * @deprecated Use {@link #getAlignment()} instead
490      */

491     public int alignment() {
492         return getAlignment();
493     }
494     
495     /**
496      * Gets the indentation of this paragraph on the left side.
497      *
498      * @return the indentation
499      * @deprecated Use {@link #getIndentationLeft()} instead
500      */

501     public float indentationLeft() {
502         return getIndentationLeft();
503     }
504     
505     /**
506      * Gets the indentation of this paragraph on the right side.
507      *
508      * @return the indentation
509      * @deprecated Use {@link #getIndentationRight()} instead
510      */

511     public float indentationRight() {
512         return getIndentationRight();
513     }
514 }
515
Popular Tags