KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > list > RtfListItem


1 /*
2  * $Id: RtfListItem.java 2811 2007-05-31 18:51:52Z hallm $
3  * $Name: $
4  *
5  * Copyright 2001, 2002, 2003, 2004, 2005 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.list;
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.ListItem;
58 import com.lowagie.text.rtf.RtfBasicElement;
59 import com.lowagie.text.rtf.document.RtfDocument;
60 import com.lowagie.text.rtf.style.RtfParagraphStyle;
61 import com.lowagie.text.rtf.text.RtfChunk;
62 import com.lowagie.text.rtf.text.RtfParagraph;
63
64
65 /**
66  * The RtfListItem acts as a wrapper for a ListItem.
67  *
68  * @version $Id: RtfListItem.java 2811 2007-05-31 18:51:52Z hallm $
69  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
70  * @author Thomas Bickel (tmb99@inode.at)
71  */

72 public class RtfListItem extends RtfParagraph {
73
74     /**
75      * The RtfList this RtfListItem belongs to.
76      */

77     private RtfList parentList = null;
78     /**
79      * Whether this RtfListItem contains further RtfLists.
80      */

81     private boolean containsInnerList = false;
82     
83     /**
84      * Constructs a RtfListItem for a ListItem belonging to a RtfDocument.
85      *
86      * @param doc The RtfDocument this RtfListItem belongs to.
87      * @param listItem The ListItem this RtfListItem is based on.
88      */

89     public RtfListItem(RtfDocument doc, ListItem listItem) {
90         super(doc, listItem);
91     }
92     
93     /**
94      * Writes the content of this RtfListItem.
95      *
96      * @return A byte array with the content of this RtfListItem.
97      * @deprecated replaced by {@link #writeContent(OutputStream)}
98      */

99     public byte[] write()
100     {
101         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
102         try {
103             writeContent(result);
104         } catch(IOException JavaDoc ioe) {
105             ioe.printStackTrace();
106         }
107         return result.toByteArray();
108     }
109     /**
110      * Writes the content of this RtfListItem.
111      */

112     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
113     {
114         if(this.paragraphStyle.getSpacingBefore() > 0) {
115             result.write(RtfParagraphStyle.SPACING_BEFORE);
116             result.write(intToByteArray(paragraphStyle.getSpacingBefore()));
117         }
118         if(this.paragraphStyle.getSpacingAfter() > 0) {
119             result.write(RtfParagraphStyle.SPACING_AFTER);
120             result.write(intToByteArray(this.paragraphStyle.getSpacingAfter()));
121         }
122         for(int i = 0; i < chunks.size(); i++) {
123             RtfBasicElement rtfElement = (RtfBasicElement) chunks.get(i);
124             if(rtfElement instanceof RtfChunk) {
125                 ((RtfChunk) rtfElement).setSoftLineBreaks(true);
126             } else if(rtfElement instanceof RtfList) {
127                 result.write(RtfParagraph.PARAGRAPH);
128                 this.containsInnerList = true;
129             }
130             //.result.write(rtfElement.write());
131
rtfElement.writeContent(result);
132             if(rtfElement instanceof RtfList) {
133                 result.write(this.parentList.writeListBeginning());
134                 result.write("\\tab".getBytes());
135             }
136         }
137     }
138
139     /**
140      * Returns the definition of the first list contained in this RtfListItem or
141      * an empty byte array if no inner RtfLists exist.
142      *
143      * @return The definition of the first inner RtfList or an empty byte array.
144      * @deprecated replaced by {@link #writeDefinition(OutputStream)}
145      */

146     public byte[] writeDefinition() {
147         for(int i = 0; i < chunks.size(); i++) {
148             RtfBasicElement rtfElement = (RtfBasicElement) chunks.get(i);
149             if(rtfElement instanceof RtfList) {
150                 return ((RtfList) rtfElement).writeDefinition();
151             }
152         }
153         return new byte[0];
154     }
155     /**
156      * Writes the definition of the first element in this RtfListItem that is
157      * an instanceof {@link RtfList} to the given stream.<br>
158      * If this item does not contain a {@link RtfList} element nothing is written
159      * and the method returns <code>false</code>.
160      *
161      * @param out destination stream
162      * @return <code>true</code> if a RtfList definition was written, <code>false</code> otherwise
163      * @throws IOException
164      */

165     public boolean writeDefinition(OutputStream JavaDoc out) throws IOException JavaDoc
166     {
167         for(int i = 0; i < chunks.size(); i++) {
168             RtfBasicElement rtfElement = (RtfBasicElement)chunks.get(i);
169             if(rtfElement instanceof RtfList) {
170                 RtfList rl = (RtfList)rtfElement;
171                 rl.writeDefinition(out);
172                 return(true);
173             }
174         }
175         return(false);
176     }
177     
178     /**
179      * Inherit the list settings from the parent list to RtfLists that
180      * are contained in this RtfListItem.
181      *
182      * @param listNumber The list number to inherit.
183      * @param listLevel The list level to inherit.
184      */

185     public void inheritListSettings(int listNumber, int listLevel) {
186         for(int i = 0; i < chunks.size(); i++) {
187             RtfBasicElement rtfElement = (RtfBasicElement) chunks.get(i);
188             if(rtfElement instanceof RtfList) {
189                 ((RtfList) rtfElement).setListNumber(listNumber);
190                 ((RtfList) rtfElement).setListLevel(listLevel);
191                 ((RtfList) rtfElement).setParent(this.parentList);
192             }
193         }
194     }
195         
196     /**
197      * Correct the indentation of RtfLists in this RtfListItem by adding left/first line indentation
198      * from the parent RtfList. Also calls correctIndentation on all child RtfLists.
199      */

200     protected void correctIndentation() {
201         for(int i = 0; i < chunks.size(); i++) {
202             RtfBasicElement rtfElement = (RtfBasicElement) chunks.get(i);
203             if(rtfElement instanceof RtfList) {
204                 ((RtfList) rtfElement).correctIndentation();
205             }
206         }
207     }
208     
209     /**
210      * Set the parent RtfList.
211      *
212      * @param parentList The parent RtfList to use.
213      */

214     public void setParent(RtfList parentList) {
215         this.parentList = parentList;
216     }
217
218     /**
219      * Gets whether this RtfListItem contains further RtfLists.
220      *
221      * @return Whether this RtfListItem contains further RtfLists.
222      */

223     public boolean isContainsInnerList() {
224         return this.containsInnerList;
225     }
226 }
227
Popular Tags