2
* $Id: RtfSection.java,v 1.12 2006/07/25 14:50:57 hallm Exp $
5
* Copyright 2001, 2002, 2003, 2004 by Mark Hall
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/
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.
15
* The Original Code is 'iText, a free JAVA-PDF library'.
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.
23
* Contributor(s): all the names of the contributors are added in the source code
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.
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.
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
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/
51
package com.lowagie.text.rtf.text;
53
import java.io.ByteArrayOutputStream;
54
import java.io.IOException;
55
import java.util.Iterator;
56
import java.util.ArrayList;
58
import com.lowagie.text.Chunk;
59
import com.lowagie.text.DocumentException;
60
import com.lowagie.text.Element;
61
import com.lowagie.text.Section;
62
import com.lowagie.text.rtf.RtfBasicElement;
63
import com.lowagie.text.rtf.RtfElement;
64
import com.lowagie.text.rtf.document.RtfDocument;
65
import com.lowagie.text.rtf.field.RtfTOCEntry;
69
* The RtfSection wraps a Section element.
72
* @version $Revision: 1.12 $
73
* @author Mark Hall (mhall@edu.uni-klu.ac.at)
75
public class RtfSection extends RtfElement {
78
* The title paragraph of this RtfSection
80
protected RtfParagraph title = null;
82
* The sub-items of this RtfSection
84
protected ArrayList items = null;
87
* Constructs a RtfSection for a given Section. If the autogenerateTOCEntries
88
* property of the RtfDocument is set and the title is not empty then a TOC entry
89
* is generated for the title.
91
* @param doc The RtfDocument this RtfSection belongs to
92
* @param section The Section this RtfSection is based on
94
public RtfSection(RtfDocument doc, Section section) {
96
items = new ArrayList();
98
if(section.title() != null) {
99
this.title = (RtfParagraph) doc.getMapper().mapElement(section.title());
101
if(document.getAutogenerateTOCEntries()) {
102
StringBuffer titleText = new StringBuffer();
103
Iterator it = section.title().iterator();
104
while(it.hasNext()) {
105
Element element = (Element) it.next();
106
if(element.type() == Element.CHUNK) {
107
titleText.append(((Chunk) element).content());
110
if(titleText.toString().trim().length() > 0) {
111
RtfTOCEntry tocEntry = new RtfTOCEntry(titleText.toString());
112
tocEntry.setRtfDocument(this.document);
113
this.items.add(tocEntry);
116
Iterator iterator = section.iterator();
117
while(iterator.hasNext()) {
118
Element element = (Element) iterator.next();
119
RtfBasicElement rtfElement = doc.getMapper().mapElement(element);
120
if(rtfElement != null) {
121
items.add(rtfElement);
125
updateIndentation(section.indentationLeft(), section.indentationRight(), section.indentation());
126
} catch(DocumentException de) {
127
de.printStackTrace();
132
* Write this RtfSection and its contents
134
* @return A byte array with the RtfSection and its contents
136
public byte[] write() {
137
ByteArrayOutputStream result = new ByteArrayOutputStream();
139
result.write(RtfParagraph.PARAGRAPH);
140
if(this.title != null) {
141
result.write(this.title.write());
143
for(int i = 0; i < items.size(); i++) {
144
result.write(((RtfBasicElement) items.get(i)).write());
146
} catch(IOException ioe) {
147
ioe.printStackTrace();
149
return result.toByteArray();
153
* Sets whether this RtfSection is in a table. Sets the correct inTable setting for all
156
* @param inTable <code>True</code> if this RtfSection is in a table, <code>false</code> otherwise
158
public void setInTable(boolean inTable) {
159
super.setInTable(inTable);
160
for(int i = 0; i < this.items.size(); i++) {
161
((RtfBasicElement) this.items.get(i)).setInTable(inTable);
166
* Sets whether this RtfSection is in a header. Sets the correct inTable setting for all
169
* @param inHeader <code>True</code> if this RtfSection is in a header, <code>false</code> otherwise
171
public void setInHeader(boolean inHeader) {
172
super.setInHeader(inHeader);
173
for(int i = 0; i < this.items.size(); i++) {
174
((RtfBasicElement) this.items.get(i)).setInHeader(inHeader);
179
* Updates the left, right and content indentation of all RtfParagraph and RtfSection
180
* elements that this RtfSection contains.
182
* @param indentLeft The left indentation to add.
183
* @param indentRight The right indentation to add.
184
* @param indentContent The content indentation to add.
186
private void updateIndentation(float indentLeft, float indentRight, float indentContent) {
187
if(this.title != null) {
188
this.title.setIndentLeft((int) (this.title.getIndentLeft() + indentLeft * RtfElement.TWIPS_FACTOR));
189
this.title.setIndentRight((int) (this.title.getIndentRight() + indentRight * RtfElement.TWIPS_FACTOR));
191
for(int i = 0; i < this.items.size(); i++) {
192
RtfBasicElement rtfElement = (RtfBasicElement) this.items.get(i);
193
if(rtfElement instanceof RtfSection) {
194
((RtfSection) rtfElement).updateIndentation(indentLeft + indentContent, indentRight, 0);
195
} else if(rtfElement instanceof RtfParagraph) {
196
((RtfParagraph) rtfElement).setIndentLeft((int) (((RtfParagraph) rtfElement).getIndentLeft() + (indentLeft + indentContent) * RtfElement.TWIPS_FACTOR));
197
((RtfParagraph) rtfElement).setIndentRight((int) (((RtfParagraph) rtfElement).getIndentRight() + indentRight * RtfElement.TWIPS_FACTOR));