KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RtfParagraph.java 2813 2007-06-01 11:27:52Z psoares33 $
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
57 import com.lowagie.text.Chunk;
58 import com.lowagie.text.DocumentException;
59 import com.lowagie.text.Element;
60 import com.lowagie.text.Paragraph;
61 import com.lowagie.text.rtf.RtfBasicElement;
62 import com.lowagie.text.rtf.RtfElement;
63 import com.lowagie.text.rtf.document.RtfDocument;
64 import com.lowagie.text.rtf.graphic.RtfImage;
65 import com.lowagie.text.rtf.style.RtfFont;
66 import com.lowagie.text.rtf.style.RtfParagraphStyle;
67
68
69 /**
70  * The RtfParagraph is an extension of the RtfPhrase that adds alignment and
71  * indentation properties. It wraps a Paragraph.
72  *
73  * @version $Id: RtfParagraph.java 2813 2007-06-01 11:27:52Z psoares33 $
74  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
75  * @author Thomas Bickel (tmb99@inode.at)
76  */

77 public class RtfParagraph extends RtfPhrase {
78
79     /**
80      * Constant for the end of a paragraph
81      */

82     public static final byte[] PARAGRAPH = "\\par".getBytes();
83     
84     /**
85      * An optional RtfParagraphStyle to use for styling.
86      */

87     protected RtfParagraphStyle paragraphStyle = null;
88     
89     /**
90      * Constructs a RtfParagraph belonging to a RtfDocument based on a Paragraph.
91      *
92      * @param doc The RtfDocument this RtfParagraph belongs to
93      * @param paragraph The Paragraph that this RtfParagraph is based on
94      */

95     public RtfParagraph(RtfDocument doc, Paragraph paragraph) {
96         super(doc);
97         
98         RtfFont baseFont = null;
99         if(paragraph.getFont() instanceof RtfParagraphStyle) {
100             this.paragraphStyle = this.document.getDocumentHeader().getRtfParagraphStyle(((RtfParagraphStyle) paragraph.getFont()).getStyleName());
101             baseFont = this.paragraphStyle;
102         } else {
103             baseFont = new RtfFont(this.document, paragraph.getFont());
104             this.paragraphStyle = new RtfParagraphStyle(this.document, this.document.getDocumentHeader().getRtfParagraphStyle("Normal"));
105             this.paragraphStyle.setAlignment(paragraph.getAlignment());
106             this.paragraphStyle.setFirstLineIndent((int) (paragraph.getFirstLineIndent() * RtfElement.TWIPS_FACTOR));
107             this.paragraphStyle.setIndentLeft((int) (paragraph.getIndentationLeft() * RtfElement.TWIPS_FACTOR));
108             this.paragraphStyle.setIndentRight((int) (paragraph.getIndentationRight() * RtfElement.TWIPS_FACTOR));
109             this.paragraphStyle.setSpacingBefore((int) (paragraph.spacingBefore() * RtfElement.TWIPS_FACTOR));
110             this.paragraphStyle.setSpacingAfter((int) (paragraph.spacingAfter() * RtfElement.TWIPS_FACTOR));
111             if(paragraph.hasLeading()) {
112                 this.paragraphStyle.setLineLeading((int) (paragraph.getLeading() * RtfElement.TWIPS_FACTOR));
113             }
114             this.paragraphStyle.setKeepTogether(paragraph.getKeepTogether());
115         }
116         
117         for(int i = 0; i < paragraph.size(); i++) {
118             Element chunk = (Element) paragraph.get(i);
119             if(chunk instanceof Chunk) {
120                 ((Chunk) chunk).setFont(baseFont.difference(((Chunk) chunk).getFont()));
121             } else if(chunk instanceof RtfImage) {
122                 ((RtfImage) chunks.get(i)).setAlignment(this.paragraphStyle.getAlignment());
123             }
124             try {
125                 chunks.add(doc.getMapper().mapElement(chunk));
126             } catch(DocumentException de) {
127             }
128         }
129     }
130     
131     /**
132      * Set whether this RtfParagraph must stay on the same page as the next one.
133      *
134      * @param keepTogetherWithNext Whether this RtfParagraph must keep together with the next.
135      */

136     public void setKeepTogetherWithNext(boolean keepTogetherWithNext) {
137         this.paragraphStyle.setKeepTogetherWithNext(keepTogetherWithNext);
138     }
139     
140     /**
141      * Writes the content of this RtfParagraph. First paragraph specific data is written
142      * and then the RtfChunks of this RtfParagraph are added.
143      *
144      * @return The content of this RtfParagraph
145      * @deprecated replaced by {@link #writeContent(OutputStream)}
146      */

147     public byte[] write() {
148         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
149         try {
150             writeContent(result);
151         } catch(IOException JavaDoc ioe) {
152             ioe.printStackTrace();
153         }
154         return result.toByteArray();
155     }
156     /**
157      * Writes the content of this RtfParagraph. First paragraph specific data is written
158      * and then the RtfChunks of this RtfParagraph are added.
159      */

160     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
161     {
162         result.write(PARAGRAPH_DEFAULTS);
163         result.write(PLAIN);
164
165         if(inTable) {
166             result.write(IN_TABLE);
167         }
168         
169         if(this.paragraphStyle != null) {
170             result.write(this.paragraphStyle.writeBegin());
171         }
172         result.write("\\plain".getBytes());
173         
174         for(int i = 0; i < chunks.size(); i++) {
175             RtfBasicElement rbe = (RtfBasicElement)chunks.get(i);
176             //.result.write((rbe).write());
177
rbe.writeContent(result);
178         }
179         
180         if(this.paragraphStyle != null) {
181             result.write(this.paragraphStyle.writeEnd());
182         }
183         
184         if(!inTable) {
185             result.write(PARAGRAPH);
186         }
187         if(this.document.getDocumentSettings().isOutputDebugLineBreaks()) {
188             result.write('\n');
189         }
190     }
191     
192     /**
193      * Gets the left indentation of this RtfParagraph.
194      *
195      * @return The left indentation.
196      */

197     public int getIndentLeft() {
198         return this.paragraphStyle.getIndentLeft();
199     }
200     
201     /**
202      * Sets the left indentation of this RtfParagraph.
203      *
204      * @param indentLeft The left indentation to use.
205      */

206     public void setIndentLeft(int indentLeft) {
207         this.paragraphStyle.setIndentLeft(indentLeft);
208     }
209     
210     /**
211      * Gets the right indentation of this RtfParagraph.
212      *
213      * @return The right indentation.
214      */

215     public int getIndentRight() {
216         return this.paragraphStyle.getIndentRight();
217     }
218     
219     /**
220      * Sets the right indentation of this RtfParagraph.
221      *
222      * @param indentRight The right indentation to use.
223      */

224     public void setIndentRight(int indentRight) {
225         this.paragraphStyle.setIndentRight(indentRight);
226     }
227 }
228
Popular Tags