KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MarkedSection.java 2748 2007-05-12 15:11:48Z blowagie $
3  * $Name$
4  *
5  * Copyright 2007 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-2007 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-2007 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.Collection JavaDoc;
54 import java.util.Iterator JavaDoc;
55
56 /**
57  * Wrapper that allows to add properties to a Chapter/Section object.
58  * Before iText 1.5 every 'basic building block' implemented the MarkupAttributes interface.
59  * By setting attributes, you could add markup to the corresponding XML and/or HTML tag.
60  * This functionality was hardly used by anyone, so it was removed, and replaced by
61  * the MarkedObject functionality.
62  */

63
64 public class MarkedSection extends MarkedObject {
65
66     /** This is the title of this section. */
67     protected MarkedObject title = null;
68         
69     /**
70      * Creates a MarkedObject with a Section or Chapter object.
71      * @param section the marked section
72      */

73     public MarkedSection(Section section) {
74         super();
75         if (section.title != null) {
76             title = new MarkedObject(section.title);
77             section.setTitle(null);
78         }
79         this.element = section;
80     }
81     
82     /**
83      * Adds a <CODE>Paragraph</CODE>, <CODE>List</CODE> or <CODE>Table</CODE>
84      * to this <CODE>Section</CODE>.
85      *
86      * @param index index at which the specified element is to be inserted
87      * @param o an object of type <CODE>Paragraph</CODE>, <CODE>List</CODE> or <CODE>Table</CODE>=
88      * @throws ClassCastException if the object is not a <CODE>Paragraph</CODE>, <CODE>List</CODE> or <CODE>Table</CODE>
89      */

90     public void add(int index, Object JavaDoc o) {
91         ((Section)element).add(index, o);
92     }
93         
94     /**
95      * Adds a <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE> or another <CODE>Section</CODE>
96      * to this <CODE>Section</CODE>.
97      *
98      * @param o an object of type <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE> or another <CODE>Section</CODE>
99      * @return a boolean
100      * @throws ClassCastException if the object is not a <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE> or <CODE>Section</CODE>
101      */

102     public boolean add(Object JavaDoc o) {
103         return ((Section)element).add(o);
104     }
105
106     /**
107      * Processes the element by adding it (or the different parts) to an
108      * <CODE>ElementListener</CODE>.
109      *
110      * @param listener an <CODE>ElementListener</CODE>
111      * @return <CODE>true</CODE> if the element was processed successfully
112      */

113     public boolean process(ElementListener listener) {
114         try {
115             Element element;
116             for (Iterator JavaDoc i = ((Section)this.element).iterator(); i.hasNext(); ) {
117                 element = (Element)i.next();
118                 listener.add(element);
119             }
120             return true;
121         }
122         catch(DocumentException de) {
123             return false;
124         }
125     }
126     
127     /**
128      * Adds a collection of <CODE>Element</CODE>s
129      * to this <CODE>Section</CODE>.
130      *
131      * @param collection a collection of <CODE>Paragraph</CODE>s, <CODE>List</CODE>s and/or <CODE>Table</CODE>s
132      * @return <CODE>true</CODE> if the action succeeded, <CODE>false</CODE> if not.
133      * @throws ClassCastException if one of the objects isn't a <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE>
134      */

135     public boolean addAll(Collection JavaDoc collection) {
136         return ((Section)element).addAll(collection);
137     }
138       
139     /**
140      * Creates a <CODE>Section</CODE>, adds it to this <CODE>Section</CODE> and returns it.
141      *
142      * @param indentation the indentation of the new section
143      * @param numberDepth the numberDepth of the section
144      * @return a new Section object
145      */

146     public MarkedSection addSection(float indentation, int numberDepth) {
147         MarkedSection section = ((Section)element).addMarkedSection();
148         section.setIndentation(indentation);
149         section.setNumberDepth(numberDepth);
150         return section;
151     }
152         
153     /**
154      * Creates a <CODE>Section</CODE>, adds it to this <CODE>Section</CODE> and returns it.
155      *
156      * @param indentation the indentation of the new section
157      * @return a new Section object
158      */

159     public MarkedSection addSection(float indentation) {
160         MarkedSection section = ((Section)element).addMarkedSection();
161         section.setIndentation(indentation);
162         return section;
163     }
164         
165     /**
166      * Creates a <CODE>Section</CODE>, add it to this <CODE>Section</CODE> and returns it.
167      *
168      * @param numberDepth the numberDepth of the section
169      * @return a new Section object
170      */

171     public MarkedSection addSection(int numberDepth) {
172         MarkedSection section = ((Section)element).addMarkedSection();
173         section.setNumberDepth(numberDepth);
174         return section;
175     }
176         
177     /**
178      * Creates a <CODE>Section</CODE>, adds it to this <CODE>Section</CODE> and returns it.
179      *
180      * @return a new Section object
181      */

182     public MarkedSection addSection() {
183         return ((Section)element).addMarkedSection();
184     }
185         
186     // public methods
187

188     /**
189      * Sets the title of this section.
190      *
191      * @param title the new title
192      */

193     public void setTitle(MarkedObject title) {
194         if (title.element instanceof Paragraph)
195             this.title = title;
196     }
197        
198     /**
199      * Gets the title of this MarkedSection.
200      */

201     public MarkedObject title() {
202         if (title == null) {
203             return null;
204         }
205         int depth = Math.min(((Section)element).numbers.size(), ((Section)element).numberDepth);
206         if (depth < 1) {
207             return title;
208         }
209         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
210         for (int i = 0; i < depth; i++) {
211             buf.insert(0, ".");
212             buf.insert(0, ((Integer JavaDoc) ((Section)element).numbers.get(i)).intValue());
213         }
214         if (buf.length() > 0) buf.append(' ');
215         Paragraph result = new Paragraph((Paragraph)title.element);
216         result.add(0, new Chunk(buf.toString(), ((Paragraph)title.element).getFont()));
217         MarkedObject mo = new MarkedObject(result);
218         mo.markupAttributes = title.markupAttributes;
219         return mo;
220     }
221     
222     /**
223      * Sets the depth of the sectionnumbers that will be shown preceding the title.
224      * <P>
225      * If the numberdepth is 0, the sections will not be numbered. If the numberdepth
226      * is 1, the section will be numbered with their own number. If the numberdepth is
227      * higher (for instance x > 1), the numbers of x - 1 parents will be shown.
228      *
229      * @param numberDepth the new numberDepth
230      */

231     public void setNumberDepth(int numberDepth) {
232         ((Section)element).setNumberDepth(numberDepth);
233     }
234         
235     /**
236      * Sets the indentation of this <CODE>Section</CODE> on the left side.
237      *
238      * @param indentation the indentation
239      */

240     public void setIndentationLeft(float indentation) {
241         ((Section)element).setIndentationLeft(indentation);
242     }
243         
244     /**
245      * Sets the indentation of this <CODE>Section</CODE> on the right side.
246      *
247      * @param indentation the indentation
248      */

249     public void setIndentationRight(float indentation) {
250         ((Section)element).setIndentationRight(indentation);
251     }
252         
253     /**
254      * Sets the indentation of the content of this <CODE>Section</CODE>.
255      *
256      * @param indentation the indentation
257      */

258     public void setIndentation(float indentation) {
259         ((Section)element).setIndentation(indentation);
260     }
261         
262     /** Setter for property bookmarkOpen.
263      * @param bookmarkOpen false if the bookmark children are not
264      * visible.
265      */

266     public void setBookmarkOpen(boolean bookmarkOpen) {
267         ((Section)element).setBookmarkOpen(bookmarkOpen);
268     }
269         
270     /**
271      * Setter for property triggerNewPage.
272      * @param triggerNewPage true if a new page has to be triggered.
273      */

274     public void setTriggerNewPage(boolean triggerNewPage) {
275         ((Section)element).setTriggerNewPage(triggerNewPage);
276     }
277         
278     /**
279      * Sets the bookmark title. The bookmark title is the same as the section title but
280      * can be changed with this method.
281      * @param bookmarkTitle the bookmark title
282      */

283     public void setBookmarkTitle(String JavaDoc bookmarkTitle) {
284         ((Section)element).setBookmarkTitle(bookmarkTitle);
285     }
286 }
Popular Tags