KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > text > RtfSection


1 /*
2  * $Id: RtfSection.java 2776 2007-05-23 20:01:40Z hallm $
3  * $Name$
4  *
5  * Copyright 2001, 2002, 2003, 2004 by Mark Hall
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.rtf.text;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.Iterator JavaDoc;
58
59 import com.lowagie.text.Chunk;
60 import com.lowagie.text.DocumentException;
61 import com.lowagie.text.Element;
62 import com.lowagie.text.Section;
63 import com.lowagie.text.rtf.RtfBasicElement;
64 import com.lowagie.text.rtf.RtfElement;
65 import com.lowagie.text.rtf.document.RtfDocument;
66 import com.lowagie.text.rtf.field.RtfTOCEntry;
67
68
69 /**
70  * The RtfSection wraps a Section element.
71  * INTERNAL CLASS
72  *
73  * @version $Id: RtfSection.java 2776 2007-05-23 20:01:40Z hallm $
74  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
75  * @author Thomas Bickel (tmb99@inode.at)
76  */

77 public class RtfSection extends RtfElement {
78
79     /**
80      * The title paragraph of this RtfSection
81      */

82     protected RtfParagraph title = null;
83     /**
84      * The sub-items of this RtfSection
85      */

86     protected ArrayList JavaDoc items = null;
87     
88     /**
89      * Constructs a RtfSection for a given Section. If the autogenerateTOCEntries
90      * property of the RtfDocument is set and the title is not empty then a TOC entry
91      * is generated for the title.
92      *
93      * @param doc The RtfDocument this RtfSection belongs to
94      * @param section The Section this RtfSection is based on
95      */

96     public RtfSection(RtfDocument doc, Section section) {
97         super(doc);
98         items = new ArrayList JavaDoc();
99         try {
100             if(section.getTitle() != null) {
101                 this.title = (RtfParagraph) doc.getMapper().mapElement(section.getTitle());
102             }
103             if(document.getAutogenerateTOCEntries()) {
104                 StringBuffer JavaDoc titleText = new StringBuffer JavaDoc();
105                 Iterator JavaDoc it = section.getTitle().iterator();
106                 while(it.hasNext()) {
107                     Element element = (Element) it.next();
108                     if(element.type() == Element.CHUNK) {
109                         titleText.append(((Chunk) element).getContent());
110                     }
111                 }
112                 if(titleText.toString().trim().length() > 0) {
113                     RtfTOCEntry tocEntry = new RtfTOCEntry(titleText.toString());
114                     tocEntry.setRtfDocument(this.document);
115                     this.items.add(tocEntry);
116                 }
117             }
118             Iterator JavaDoc iterator = section.iterator();
119             while(iterator.hasNext()) {
120                 Element element = (Element) iterator.next();
121                 RtfBasicElement rtfElement = doc.getMapper().mapElement(element);
122                 if(rtfElement != null) {
123                     items.add(rtfElement);
124                 }
125             }
126             
127             updateIndentation(section.getIndentationLeft(), section.getIndentationRight(), section.getIndentation());
128         } catch(DocumentException de) {
129             de.printStackTrace();
130         }
131     }
132     
133     /**
134      * Write this RtfSection and its contents
135      *
136      * @return A byte array with the RtfSection and its contents
137      * @deprecated replaced by {@link #writeContent(OutputStream)}
138      */

139     public byte[] write()
140     {
141         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
142         try {
143             writeContent(result);
144         } catch(IOException JavaDoc ioe) {
145             ioe.printStackTrace();
146         }
147         return result.toByteArray();
148     }
149     /**
150      * Write this RtfSection and its contents
151      */

152     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
153     {
154         result.write(RtfParagraph.PARAGRAPH);
155         if(this.title != null) {
156             //.result.write(this.title.write());
157
this.title.writeContent(result);
158         }
159         for(int i = 0; i < items.size(); i++) {
160             RtfBasicElement rbe = (RtfBasicElement) items.get(i);
161             //.result.write((rbe).write());
162
rbe.writeContent(result);
163         }
164     }
165
166     
167     /**
168      * Sets whether this RtfSection is in a table. Sets the correct inTable setting for all
169      * child elements.
170      *
171      * @param inTable <code>True</code> if this RtfSection is in a table, <code>false</code> otherwise
172      */

173     public void setInTable(boolean inTable) {
174         super.setInTable(inTable);
175         for(int i = 0; i < this.items.size(); i++) {
176             ((RtfBasicElement) this.items.get(i)).setInTable(inTable);
177         }
178     }
179     
180     /**
181      * Sets whether this RtfSection is in a header. Sets the correct inTable setting for all
182      * child elements.
183      *
184      * @param inHeader <code>True</code> if this RtfSection is in a header, <code>false</code> otherwise
185      */

186     public void setInHeader(boolean inHeader) {
187         super.setInHeader(inHeader);
188         for(int i = 0; i < this.items.size(); i++) {
189             ((RtfBasicElement) this.items.get(i)).setInHeader(inHeader);
190         }
191     }
192
193     /**
194      * Updates the left, right and content indentation of all RtfParagraph and RtfSection
195      * elements that this RtfSection contains.
196      *
197      * @param indentLeft The left indentation to add.
198      * @param indentRight The right indentation to add.
199      * @param indentContent The content indentation to add.
200      */

201     private void updateIndentation(float indentLeft, float indentRight, float indentContent) {
202         if(this.title != null) {
203             this.title.setIndentLeft((int) (this.title.getIndentLeft() + indentLeft * RtfElement.TWIPS_FACTOR));
204             this.title.setIndentRight((int) (this.title.getIndentRight() + indentRight * RtfElement.TWIPS_FACTOR));
205         }
206         for(int i = 0; i < this.items.size(); i++) {
207             RtfBasicElement rtfElement = (RtfBasicElement) this.items.get(i);
208             if(rtfElement instanceof RtfSection) {
209                 ((RtfSection) rtfElement).updateIndentation(indentLeft + indentContent, indentRight, 0);
210             } else if(rtfElement instanceof RtfParagraph) {
211                 ((RtfParagraph) rtfElement).setIndentLeft((int) (((RtfParagraph) rtfElement).getIndentLeft() + (indentLeft + indentContent) * RtfElement.TWIPS_FACTOR));
212                 ((RtfParagraph) rtfElement).setIndentRight((int) (((RtfParagraph) rtfElement).getIndentRight() + indentRight * RtfElement.TWIPS_FACTOR));
213             }
214         }
215     }
216 }
217
Popular Tags