KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Section.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 import java.util.ArrayList JavaDoc;
54 import java.util.Collection JavaDoc;
55 import java.util.Iterator JavaDoc;
56
57 /**
58  * A <CODE>Section</CODE> is a part of a <CODE>Document</CODE> containing
59  * other <CODE>Section</CODE>s, <CODE>Paragraph</CODE>s, <CODE>List</CODE>
60  * and/or <CODE>Table</CODE>s.
61  * <P>
62  * Remark: you can not construct a <CODE>Section</CODE> yourself.
63  * You will have to ask an instance of <CODE>Section</CODE> to the
64  * <CODE>Chapter</CODE> or <CODE>Section</CODE> to which you want to
65  * add the new <CODE>Section</CODE>.
66  * <P>
67  * Example:
68  * <BLOCKQUOTE><PRE>
69  * Paragraph title2 = new Paragraph("This is Chapter 2", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
70  * Chapter chapter2 = new Chapter(title2, 2);
71  * Paragraph someText = new Paragraph("This is some text");
72  * chapter2.add(someText);
73  * Paragraph title21 = new Paragraph("This is Section 1 in Chapter 2", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
74  * <STRONG>Section section1 = chapter2.addSection(title21);</STRONG>
75  * Paragraph someSectionText = new Paragraph("This is some silly paragraph in a chapter and/or section. It contains some text to test the functionality of Chapters and Section.");
76  * <STRONG>section1.add(someSectionText);</STRONG>
77  * Paragraph title211 = new Paragraph("This is SubSection 1 in Section 1 in Chapter 2", FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD, new Color(255, 0, 0)));
78  * <STRONG>Section section11 = section1.addSection(40, title211, 2);</STRONG>
79  * <STRONG>section11.add(someSectionText);</STRONG>
80  * </PRE></BLOCKQUOTE>
81  */

82
83 public class Section extends ArrayList JavaDoc implements TextElementArray {
84     
85     // constant
86
private static final long serialVersionUID = 3324172577544748043L;
87
88     // member variables
89

90     /** The title of this section. */
91     protected Paragraph title;
92     
93     /** The bookmark title if different from the content title */
94     protected String JavaDoc bookmarkTitle;
95     
96     /** The number of sectionnumbers that has to be shown before the section title. */
97     protected int numberDepth;
98     
99     /** The indentation of this section on the left side. */
100     protected float indentationLeft;
101     
102     /** The indentation of this section on the right side. */
103     protected float indentationRight;
104     
105     /** The additional indentation of the content of this section. */
106     protected float indentation;
107     
108     /** false if the bookmark children are not visible */
109     protected boolean bookmarkOpen = true;
110     
111     /** true if the section has to trigger a new page */
112     protected boolean triggerNewPage = false;
113     
114     /** This is the number of subsections. */
115     protected int subsections = 0;
116     
117     /** This is the complete list of sectionnumbers of this section and the parents of this section. */
118     protected ArrayList JavaDoc numbers = null;
119     
120     // constructors
121

122     /**
123      * Constructs a new <CODE>Section</CODE>.
124      */

125     protected Section() {
126         title = new Paragraph();
127         numberDepth = 1;
128     }
129     
130     /**
131      * Constructs a new <CODE>Section</CODE>.
132      *
133      * @param title a <CODE>Paragraph</CODE>
134      * @param numberDepth the numberDepth
135      */

136     protected Section(Paragraph title, int numberDepth) {
137         this.numberDepth = numberDepth;
138         this.title = title;
139     }
140     
141     // implementation of the Element-methods
142

143     /**
144      * Processes the element by adding it (or the different parts) to an
145      * <CODE>ElementListener</CODE>.
146      *
147      * @param listener the <CODE>ElementListener</CODE>
148      * @return <CODE>true</CODE> if the element was processed successfully
149      */

150     public boolean process(ElementListener listener) {
151         try {
152             Element element;
153             for (Iterator JavaDoc i = iterator(); i.hasNext(); ) {
154                 element = (Element)i.next();
155                 listener.add(element);
156             }
157             return true;
158         }
159         catch(DocumentException de) {
160             return false;
161         }
162     }
163     
164     /**
165      * Gets the type of the text element.
166      *
167      * @return a type
168      */

169     public int type() {
170         return Element.SECTION;
171     }
172     
173     /**
174      * Checks if this object is a <CODE>Chapter</CODE>.
175      *
176      * @return <CODE>true</CODE> if it is a <CODE>Chapter</CODE>,
177      * <CODE>false</CODE> if it is a <CODE>Section</CODE>.
178      */

179     public boolean isChapter() {
180         return type() == Element.CHAPTER;
181     }
182     
183     /**
184      * Checks if this object is a <CODE>Section</CODE>.
185      *
186      * @return <CODE>true</CODE> if it is a <CODE>Section</CODE>,
187      * <CODE>false</CODE> if it is a <CODE>Chapter</CODE>.
188      */

189     public boolean isSection() {
190         return type() == Element.SECTION;
191     }
192     
193     /**
194      * Gets all the chunks in this element.
195      *
196      * @return an <CODE>ArrayList</CODE>
197      */

198     public ArrayList JavaDoc getChunks() {
199         ArrayList JavaDoc tmp = new ArrayList JavaDoc();
200         for (Iterator JavaDoc i = iterator(); i.hasNext(); ) {
201             tmp.addAll(((Element) i.next()).getChunks());
202         }
203         return tmp;
204     }
205     
206     // overriding some of the ArrayList-methods
207

208     /**
209      * Adds a <CODE>Paragraph</CODE>, <CODE>List</CODE> or <CODE>Table</CODE>
210      * to this <CODE>Section</CODE>.
211      *
212      * @param index index at which the specified element is to be inserted
213      * @param o an object of type <CODE>Paragraph</CODE>, <CODE>List</CODE> or <CODE>Table</CODE>=
214      * @throws ClassCastException if the object is not a <CODE>Paragraph</CODE>, <CODE>List</CODE> or <CODE>Table</CODE>
215      */

216     public void add(int index, Object JavaDoc o) {
217         try {
218             Element element = (Element) o;
219             if (element.type() == Element.PARAGRAPH ||
220             element.type() == Element.LIST ||
221             element.type() == Element.CHUNK ||
222             element.type() == Element.PHRASE ||
223             element.type() == Element.ANCHOR ||
224             element.type() == Element.ANNOTATION ||
225             element.type() == Element.TABLE ||
226             element.type() == Element.PTABLE ||
227             element.type() == Element.IMGTEMPLATE ||
228             element.type() == Element.JPEG ||
229             element.type() == Element.IMGRAW) {
230                 super.add(index, element);
231             }
232             else {
233                 throw new ClassCastException JavaDoc("You can add a " + element.getClass().getName() + " to a Section.");
234             }
235         }
236         catch(ClassCastException JavaDoc cce) {
237             throw new ClassCastException JavaDoc("Insertion of illegal Element: " + cce.getMessage());
238         }
239     }
240     
241     /**
242      * Adds a <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE> or another <CODE>Section</CODE>
243      * to this <CODE>Section</CODE>.
244      *
245      * @param o an object of type <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE> or another <CODE>Section</CODE>
246      * @return a boolean
247      * @throws ClassCastException if the object is not a <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE> or <CODE>Section</CODE>
248      */

249     public boolean add(Object JavaDoc o) {
250         try {
251             Element element = (Element) o;
252             if (element.type() == Element.PARAGRAPH ||
253             element.type() == Element.LIST ||
254             element.type() == Element.CHUNK ||
255             element.type() == Element.PHRASE ||
256             element.type() == Element.ANCHOR ||
257             element.type() == Element.ANNOTATION ||
258             element.type() == Element.TABLE ||
259             element.type() == Element.IMGTEMPLATE ||
260             element.type() == Element.PTABLE ||
261             element.type() == Element.JPEG ||
262             element.type() == Element.IMGRAW) {
263                 return super.add(o);
264             }
265             else if (element.type() == Element.SECTION) {
266                 Section section = (Section) o;
267                 section.setNumbers(++subsections, numbers);
268                 return super.add(section);
269             }
270             else if (o instanceof MarkedSection && ((MarkedObject)o).element.type() == Element.SECTION) {
271                 MarkedSection mo = (MarkedSection)o;
272                 Section section = (Section)mo.element;
273                 section.setNumbers(++subsections, numbers);
274                 return super.add(mo);
275             }
276             else if (element instanceof MarkedObject) {
277                 return super.add(o);
278             }
279             else {
280                 throw new ClassCastException JavaDoc("You can add a " + element.getClass().getName() + " to a Section.");
281             }
282         }
283         catch(ClassCastException JavaDoc cce) {
284             throw new ClassCastException JavaDoc("Insertion of illegal Element: " + cce.getMessage());
285         }
286     }
287     
288     /**
289      * Adds a collection of <CODE>Element</CODE>s
290      * to this <CODE>Section</CODE>.
291      *
292      * @param collection a collection of <CODE>Paragraph</CODE>s, <CODE>List</CODE>s and/or <CODE>Table</CODE>s
293      * @return <CODE>true</CODE> if the action succeeded, <CODE>false</CODE> if not.
294      * @throws ClassCastException if one of the objects isn't a <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE>
295      */

296     public boolean addAll(Collection JavaDoc collection) {
297         for (Iterator JavaDoc iterator = collection.iterator(); iterator.hasNext(); ) {
298             this.add(iterator.next());
299         }
300         return true;
301     }
302     
303     // methods that return a Section
304

305     /**
306      * Creates a <CODE>Section</CODE>, adds it to this <CODE>Section</CODE> and returns it.
307      *
308      * @param indentation the indentation of the new section
309      * @param title the title of the new section
310      * @param numberDepth the numberDepth of the section
311      * @return a new Section object
312      */

313     public Section addSection(float indentation, Paragraph title, int numberDepth) {
314         Section section = new Section(title, numberDepth);
315         section.setIndentation(indentation);
316         add(section);
317         return section;
318     }
319     
320     /**
321      * Creates a <CODE>Section</CODE>, adds it to this <CODE>Section</CODE> and returns it.
322      *
323      * @param indentation the indentation of the new section
324      * @param title the title of the new section
325      * @return a new Section object
326      */

327     public Section addSection(float indentation, Paragraph title) {
328         return addSection(indentation, title, numberDepth + 1);
329     }
330     
331     /**
332      * Creates a <CODE>Section</CODE>, add it to this <CODE>Section</CODE> and returns it.
333      *
334      * @param title the title of the new section
335      * @param numberDepth the numberDepth of the section
336      * @return a new Section object
337      */

338     public Section addSection(Paragraph title, int numberDepth) {
339         return addSection(0, title, numberDepth);
340     }
341     
342     /**
343      * Adds a marked section. For use in class MarkedSection only!
344      */

345     public MarkedSection addMarkedSection() {
346         MarkedSection section = new MarkedSection(new Section(null, numberDepth + 1));
347         add(section);
348         return section;
349     }
350     
351     /**
352      * Creates a <CODE>Section</CODE>, adds it to this <CODE>Section</CODE> and returns it.
353      *
354      * @param title the title of the new section
355      * @return a new Section object
356      */

357     public Section addSection(Paragraph title) {
358         return addSection(0, title, numberDepth + 1);
359     }
360     
361     /**
362      * Adds a <CODE>Section</CODE> to this <CODE>Section</CODE> and returns it.
363      *
364      * @param indentation the indentation of the new section
365      * @param title the title of the new section
366      * @param numberDepth the numberDepth of the section
367      * @return a new Section object
368      */

369     public Section addSection(float indentation, String JavaDoc title, int numberDepth) {
370         return addSection(indentation, new Paragraph(title), numberDepth);
371     }
372     
373     /**
374      * Adds a <CODE>Section</CODE> to this <CODE>Section</CODE> and returns it.
375      *
376      * @param title the title of the new section
377      * @param numberDepth the numberDepth of the section
378      * @return a new Section object
379      */

380     public Section addSection(String JavaDoc title, int numberDepth) {
381         return addSection(new Paragraph(title), numberDepth);
382     }
383     
384     /**
385      * Adds a <CODE>Section</CODE> to this <CODE>Section</CODE> and returns it.
386      *
387      * @param indentation the indentation of the new section
388      * @param title the title of the new section
389      * @return a new Section object
390      */

391     public Section addSection(float indentation, String JavaDoc title) {
392         return addSection(indentation, new Paragraph(title));
393     }
394     
395     /**
396      * Adds a <CODE>Section</CODE> to this <CODE>Section</CODE> and returns it.
397      *
398      * @param title the title of the new section
399      * @return a new Section object
400      */

401     public Section addSection(String JavaDoc title) {
402         return addSection(new Paragraph(title));
403     }
404     
405     // public methods
406

407     /**
408      * Sets the title of this section.
409      *
410      * @param title the new title
411      */

412     public void setTitle(Paragraph title) {
413         this.title = title;
414     }
415
416     /**
417      * Returns the title, preceeded by a certain number of sectionnumbers.
418      *
419      * @return a <CODE>Paragraph</CODE>
420      */

421     public Paragraph getTitle() {
422         if (title == null) {
423             return null;
424         }
425         int depth = Math.min(numbers.size(), numberDepth);
426         if (depth < 1) {
427             return title;
428         }
429         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(" ");
430         for (int i = 0; i < depth; i++) {
431             buf.insert(0, ".");
432             buf.insert(0, ((Integer JavaDoc) numbers.get(i)).intValue());
433         }
434         Paragraph result = new Paragraph(title);
435         result.add(0, new Chunk(buf.toString(), title.getFont()));
436         return result;
437     }
438     
439     /**
440      * Sets the depth of the sectionnumbers that will be shown preceding the title.
441      * <P>
442      * If the numberdepth is 0, the sections will not be numbered. If the numberdepth
443      * is 1, the section will be numbered with their own number. If the numberdepth is
444      * higher (for instance x > 1), the numbers of x - 1 parents will be shown.
445      *
446      * @param numberDepth the new numberDepth
447      */

448     public void setNumberDepth(int numberDepth) {
449         this.numberDepth = numberDepth;
450     }
451     
452     /**
453      * Returns the numberdepth of this <CODE>Section</CODE>.
454      *
455      * @return the numberdepth
456      */

457     public int getNumberDepth() {
458         return numberDepth;
459     }
460     
461     /**
462      * Sets the indentation of this <CODE>Section</CODE> on the left side.
463      *
464      * @param indentation the indentation
465      */

466     public void setIndentationLeft(float indentation) {
467         indentationLeft = indentation;
468     }
469
470     /**
471      * Returns the indentation of this <CODE>Section</CODE> on the left side.
472      *
473      * @return the indentation
474      */

475     public float getIndentationLeft() {
476         return indentationLeft;
477     }
478     
479     /**
480      * Sets the indentation of this <CODE>Section</CODE> on the right side.
481      *
482      * @param indentation the indentation
483      */

484     public void setIndentationRight(float indentation) {
485         indentationRight = indentation;
486     }
487
488     /**
489      * Returns the indentation of this <CODE>Section</CODE> on the right side.
490      *
491      * @return the indentation
492      */

493     public float getIndentationRight() {
494         return indentationRight;
495     }
496     
497     /**
498      * Sets the indentation of the content of this <CODE>Section</CODE>.
499      *
500      * @param indentation the indentation
501      */

502     public void setIndentation(float indentation) {
503         this.indentation = indentation;
504     }
505
506     /**
507      * Returns the indentation of the content of this <CODE>Section</CODE>.
508      *
509      * @return the indentation
510      */

511     public float getIndentation() {
512         return indentation;
513     }
514     
515     /** Setter for property bookmarkOpen.
516      * @param bookmarkOpen false if the bookmark children are not
517      * visible.
518      */

519     public void setBookmarkOpen(boolean bookmarkOpen) {
520         this.bookmarkOpen = bookmarkOpen;
521     }
522     
523     /**
524      * Getter for property bookmarkOpen.
525      * @return Value of property bookmarkOpen.
526      */

527     public boolean isBookmarkOpen() {
528         return bookmarkOpen;
529     }
530     
531     /**
532      * Setter for property triggerNewPage.
533      * @param triggerNewPage true if a new page has to be triggered.
534      */

535     public void setTriggerNewPage(boolean triggerNewPage) {
536         this.triggerNewPage = triggerNewPage;
537     }
538
539     /**
540      * Getter for property bookmarkOpen.
541      * @return Value of property triggerNewPage.
542      */

543     public boolean isTriggerNewPage() {
544         return triggerNewPage;
545     }
546     
547     /**
548      * Sets the bookmark title. The bookmark title is the same as the section title but
549      * can be changed with this method.
550      * @param bookmarkTitle the bookmark title
551      */

552     public void setBookmarkTitle(String JavaDoc bookmarkTitle) {
553         this.bookmarkTitle = bookmarkTitle;
554     }
555
556     /**
557      * Gets the bookmark title.
558      * @return the bookmark title
559      */

560     public Paragraph getBookmarkTitle() {
561         if (bookmarkTitle == null)
562             return getTitle();
563         else
564             return new Paragraph(bookmarkTitle);
565     }
566     
567     /**
568      * Changes the Chapter number.
569      */

570     public void setChapterNumber(int number) {
571         numbers.set(numbers.size() - 1, new Integer JavaDoc(number));
572         Object JavaDoc s;
573         for (Iterator JavaDoc i = iterator(); i.hasNext(); ) {
574             s = i.next();
575             if (s instanceof Section) {
576                 ((Section)s).setChapterNumber(number);
577             }
578         }
579     }
580
581     /**
582      * Returns the depth of this section.
583      *
584      * @return the depth
585      */

586     public int getDepth() {
587         return numbers.size();
588     }
589     
590     // private methods
591

592     /**
593      * Sets the number of this section.
594      *
595      * @param number the number of this section
596      * @param numbers an <CODE>ArrayList</CODE>, containing the numbers of the Parent
597      */

598     private void setNumbers(int number, ArrayList JavaDoc numbers) {
599         this.numbers = new ArrayList JavaDoc();
600         this.numbers.add(new Integer JavaDoc(number));
601         this.numbers.addAll(numbers);
602     }
603     
604     // deprecated stuff
605

606     /**
607      * Returns the title, preceeded by a certain number of sectionnumbers.
608      *
609      * @return a <CODE>Paragraph</CODE>
610      * @deprecated Use {@link #getTitle()} instead
611      */

612     public Paragraph title() {
613         return getTitle();
614     }
615     
616     /**
617      * Returns the numberdepth of this <CODE>Section</CODE>.
618      *
619      * @return the numberdepth
620      * @deprecated Use {@link #getNumberDepth()} instead
621      */

622     public int numberDepth() {
623         return getNumberDepth();
624     }
625     
626     /**
627      * Returns the indentation of this <CODE>Section</CODE> on the left side.
628      *
629      * @return the indentation
630      * @deprecated Use {@link #getIndentationLeft()} instead
631      */

632     public float indentationLeft() {
633         return getIndentationLeft();
634     }
635     
636     /**
637      * Returns the indentation of this <CODE>Section</CODE> on the right side.
638      *
639      * @return the indentation
640      * @deprecated Use {@link #getIndentationRight()} instead
641      */

642     public float indentationRight() {
643         return getIndentationRight();
644     }
645     
646     /**
647      * Returns the indentation of the content of this <CODE>Section</CODE>.
648      *
649      * @return the indentation
650      * @deprecated Use {@link #getIndentation()} instead
651      */

652     public float indentation() {
653         return getIndentation();
654     }
655     
656     /**
657      * Returns the depth of this section.
658      *
659      * @return the depth
660      * @deprecated Use {@link #getDepth()} instead
661      */

662     public int depth() {
663         return getDepth();
664     }
665     
666     /**
667      * Creates a given <CODE>Section</CODE> following a set of attributes and adds it to this one.
668      *
669      * @param attributes the attributes
670      * @return a Section
671      * @deprecated Use ElementFactory.getSection(this, attributes)
672      */

673     public Section addSection(java.util.Properties JavaDoc attributes) {
674         return com.lowagie.text.factories.ElementFactory.getSection(this, attributes);
675     }
676 }
Popular Tags