~ubuntu-branches/ubuntu/feisty/libitext-java/feisty

« back to all changes in this revision

Viewing changes to com/lowagie/text/rtf/text/RtfSection.java

  • Committer: Bazaar Package Importer
  • Author(s): Gerardo Curiel
  • Date: 2006-09-21 00:08:53 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060921000853-le9rrpcnw9fc57sm
Tags: 1.4.5-1
* New upstream release
* debian/rules modified due to a new build.xml file
* Patched Pdfgraphics2d.java to remove privative dependencie on com.sun.image.codec.jpeg.*
  (more information on
  http://developer.classpath.org/mediation/ClasspathMigration#head-d4ee9efe53a641e29ffdcd96e985bf38bbc671c1 )
* ant/.ant.properties paths fixed
* Build package with the packages provided by java-gcj-compat-dev
* Removed unused README.Debian
* Removed unused debian/patches/01libitext-java-addbuildscript.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: RtfSection.java,v 1.12 2006/07/25 14:50:57 hallm Exp $
 
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;
 
54
import java.io.IOException;
 
55
import java.util.Iterator;
 
56
import java.util.ArrayList;
 
57
 
 
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;
 
66
 
 
67
 
 
68
/**
 
69
 * The RtfSection wraps a Section element.
 
70
 * INTERNAL CLASS
 
71
 * 
 
72
 * @version $Revision: 1.12 $
 
73
 * @author Mark Hall (mhall@edu.uni-klu.ac.at)
 
74
 */
 
75
public class RtfSection extends RtfElement {
 
76
 
 
77
    /**
 
78
     * The title paragraph of this RtfSection
 
79
     */
 
80
    protected RtfParagraph title = null;
 
81
    /**
 
82
     * The sub-items of this RtfSection
 
83
     */
 
84
    protected ArrayList items = null;
 
85
    
 
86
    /**
 
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.
 
90
     *  
 
91
     * @param doc The RtfDocument this RtfSection belongs to
 
92
     * @param section The Section this RtfSection is based on
 
93
     */
 
94
    public RtfSection(RtfDocument doc, Section section) {
 
95
        super(doc);
 
96
        items = new ArrayList();
 
97
        try {
 
98
            if(section.title() != null) {
 
99
                this.title = (RtfParagraph) doc.getMapper().mapElement(section.title());
 
100
            }
 
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());
 
108
                    }
 
109
                }
 
110
                if(titleText.toString().trim().length() > 0) {
 
111
                    RtfTOCEntry tocEntry = new RtfTOCEntry(titleText.toString());
 
112
                    tocEntry.setRtfDocument(this.document);
 
113
                    this.items.add(tocEntry);
 
114
                }
 
115
            }
 
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);
 
122
                }
 
123
            }
 
124
            
 
125
            updateIndentation(section.indentationLeft(), section.indentationRight(), section.indentation());
 
126
        } catch(DocumentException de) {
 
127
            de.printStackTrace();
 
128
        }
 
129
    }
 
130
    
 
131
    /**
 
132
     * Write this RtfSection and its contents
 
133
     * 
 
134
     * @return A byte array with the RtfSection and its contents
 
135
     */
 
136
    public byte[] write() {
 
137
        ByteArrayOutputStream result = new ByteArrayOutputStream();
 
138
        try {
 
139
            result.write(RtfParagraph.PARAGRAPH);
 
140
            if(this.title != null) {
 
141
                result.write(this.title.write());
 
142
            }
 
143
            for(int i = 0; i < items.size(); i++) {
 
144
                result.write(((RtfBasicElement) items.get(i)).write());
 
145
            }
 
146
        } catch(IOException ioe) {
 
147
            ioe.printStackTrace();
 
148
        }
 
149
        return result.toByteArray();
 
150
    }
 
151
    
 
152
    /**
 
153
     * Sets whether this RtfSection is in a table. Sets the correct inTable setting for all
 
154
     * child elements.
 
155
     * 
 
156
     * @param inTable <code>True</code> if this RtfSection is in a table, <code>false</code> otherwise
 
157
     */
 
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);
 
162
        }
 
163
    }
 
164
    
 
165
    /**
 
166
     * Sets whether this RtfSection is in a header. Sets the correct inTable setting for all
 
167
     * child elements.
 
168
     * 
 
169
     * @param inHeader <code>True</code> if this RtfSection is in a header, <code>false</code> otherwise
 
170
     */
 
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);
 
175
        }
 
176
    }
 
177
 
 
178
    /**
 
179
     * Updates the left, right and content indentation of all RtfParagraph and RtfSection
 
180
     * elements that this RtfSection contains.
 
181
     * 
 
182
     * @param indentLeft The left indentation to add.
 
183
     * @param indentRight The right indentation to add.
 
184
     * @param indentContent The content indentation to add.
 
185
     */
 
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));
 
190
        }
 
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));
 
198
            }
 
199
        }
 
200
    }
 
201
}