KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > rtfdoc > RtfListItem


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: RtfListItem.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21
22 /*
23  * This file is part of the RTF library of the FOP project, which was originally
24  * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
25  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
26  * the FOP project.
27  */

28
29 import java.io.Writer JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTextrun;
33
34 /** Model of an RTF list item, which can contain RTF paragraphs
35  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
36  * @author Andreas Putz a.putz@skynamics.com
37  */

38 public class RtfListItem extends RtfContainer
39         implements IRtfTextrunContainer,
40                    IRtfListContainer,
41                    IRtfParagraphContainer {
42
43     private RtfList parentList;
44     private RtfParagraph paragraph;
45     private RtfListStyle listStyle;
46     private int number = 0;
47
48     /**
49      * special RtfParagraph that writes list item setup code before its content
50      */

51     private class RtfListItemParagraph extends RtfParagraph {
52
53         RtfListItemParagraph(RtfListItem rli, RtfAttributes attrs)
54         throws IOException JavaDoc {
55             super(rli, rli.writer, attrs);
56         }
57
58         protected void writeRtfPrefix() throws IOException JavaDoc {
59             super.writeRtfPrefix();
60             getRtfListStyle().writeParagraphPrefix(this);
61         }
62     }
63
64     /**
65      * special RtfTextrun that is used as list item label
66      */

67     public class RtfListItemLabel extends RtfTextrun implements IRtfTextrunContainer {
68         
69         private RtfListItem rtfListItem;
70         
71         /**
72          * Constructs the RtfListItemLabel
73          * @param item The RtfListItem the label belongs to
74          * @throws IOException Thrown when an IO-problem occurs
75          */

76         public RtfListItemLabel(RtfListItem item) throws IOException JavaDoc {
77             super(null, item.writer, null);
78             
79             rtfListItem = item;
80         }
81
82         /**
83          * Returns the current RtfTextrun object.
84          * Opens a new one if necessary.
85          * @return The RtfTextrun object
86          * @throws IOException Thrown when an IO-problem occurs
87          */

88         public RtfTextrun getTextrun() throws IOException JavaDoc {
89             return this;
90         }
91         
92         /**
93          * Sets the content of the list item label.
94          * @param s Content of the list item label.
95          * @throws IOException Thrown when an IO-problem occurs
96          */

97         public void addString(String JavaDoc s) throws IOException JavaDoc {
98             
99             final String JavaDoc label = s.trim();
100             if (label.length() > 0 && Character.isDigit(label.charAt(0))) {
101                 rtfListItem.setRtfListStyle(new RtfListStyleNumber());
102             } else {
103                 rtfListItem.setRtfListStyle(new RtfListStyleText(label));
104             }
105         }
106     }
107
108     /** Create an RTF list item as a child of given container with default attributes */
109     RtfListItem(RtfList parent, Writer JavaDoc w) throws IOException JavaDoc {
110         super((RtfContainer)parent, w);
111         parentList = parent;
112     }
113
114     /**
115      * Close current paragraph if any and start a new one
116      * @param attrs attributes of new paragraph
117      * @return new RtfParagraph
118      * @throws IOException Thrown when an IO-problem occurs
119      */

120     public RtfParagraph newParagraph(RtfAttributes attrs) throws IOException JavaDoc {
121         if (paragraph != null) {
122             paragraph.close();
123         }
124         paragraph = new RtfListItemParagraph(this, attrs);
125         return paragraph;
126     }
127
128     /**
129      * Close current paragraph if any and start a new one with default attributes
130      * @return new RtfParagraph
131      * @throws IOException Thrown when an IO-problem occurs
132      */

133     public RtfParagraph newParagraph() throws IOException JavaDoc {
134         return newParagraph(null);
135     }
136
137     /** Create an RTF list item as a child of given container with given attributes */
138     RtfListItem(RtfList parent, Writer JavaDoc w, RtfAttributes attr) throws IOException JavaDoc {
139         super((RtfContainer)parent, w, attr);
140         parentList = parent;
141     }
142
143
144     /**
145      * Get the current textrun.
146      * @return current RtfTextrun object
147      * @throws IOException Thrown when an IO-problem occurs
148      */

149     public RtfTextrun getTextrun() throws IOException JavaDoc {
150         RtfTextrun textrun = RtfTextrun.getTextrun(this, writer, null);
151         textrun.setRtfListItem(this);
152         return textrun;
153     }
154     
155     /**
156      * Start a new list after closing current paragraph, list and table
157      * @param attrs attributes of new RftList object
158      * @return new RtfList
159      * @throws IOException for I/O problems
160      */

161     public RtfList newList(RtfAttributes attrs) throws IOException JavaDoc {
162         RtfList list = new RtfList(this, writer, attrs);
163         return list;
164     }
165     
166     /**
167      * Overridden to setup the list: start a group with appropriate attributes
168      * @throws IOException for I/O problems
169      */

170     protected void writeRtfPrefix() throws IOException JavaDoc {
171        
172         // pard causes word97 (and sometimes 2000 too) to crash if the list is nested in a table
173
if (!parentList.getHasTableParent()) {
174             writeControlWord("pard");
175         }
176
177         writeOneAttribute(RtfText.LEFT_INDENT_FIRST,
178                 "360"); //attrib.getValue(RtfListTable.LIST_INDENT));
179

180         writeOneAttribute(RtfText.LEFT_INDENT_BODY,
181                 attrib.getValue(RtfText.LEFT_INDENT_BODY));
182
183         // group for list setup info
184
writeGroupMark(true);
185
186         writeStarControlWord("pn");
187         //Modified by Chris Scott
188
//fixes second line indentation
189
getRtfListStyle().writeListPrefix(this);
190
191         writeGroupMark(false);
192         writeOneAttribute(RtfListTable.LIST_NUMBER, new Integer JavaDoc(number));
193     }
194     
195     /**
196      * End the list group
197      * @throws IOException for I/O problems
198      */

199     protected void writeRtfSuffix() throws IOException JavaDoc {
200         super.writeRtfSuffix();
201
202         /* reset paragraph defaults to make sure list ends
203          * but pard causes word97 (and sometimes 2000 too) to crash if the list
204          * is nested in a table
205          */

206         if (!parentList.getHasTableParent()) {
207             writeControlWord("pard");
208         }
209         
210     }
211        
212     /**
213      * Change list style
214      * @param ls ListStyle to set
215      */

216     public void setRtfListStyle(RtfListStyle ls) {
217         listStyle = ls;
218         
219         listStyle.setRtfListItem(this);
220         number = getRtfFile().getListTable().addRtfListStyle(ls);
221     }
222
223     /**
224      * Get list style
225      * @return ListSytle of the List
226      */

227     public RtfListStyle getRtfListStyle() {
228         if (listStyle == null) {
229             return parentList.getRtfListStyle();
230         } else {
231             return listStyle;
232         }
233     }
234     
235     /**
236      * Get the parent list.
237      * @return the parent list
238      */

239     public RtfList getParentList() {
240         return parentList;
241     }
242     
243     /**
244      * Returns the list number
245      * @return list number
246      */

247     public int getNumber() {
248         return number;
249     }
250 }
251
Popular Tags