001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: RtfListItem.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.render.rtf.rtflib.rtfdoc;
021:
022: /*
023: * This file is part of the RTF library of the FOP project, which was originally
024: * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
025: * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
026: * the FOP project.
027: */
028:
029: import java.io.Writer;
030: import java.io.IOException;
031:
032: import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTextrun;
033:
034: /** Model of an RTF list item, which can contain RTF paragraphs
035: * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
036: * @author Andreas Putz a.putz@skynamics.com
037: */
038: public class RtfListItem extends RtfContainer implements
039: IRtfTextrunContainer, IRtfListContainer, IRtfParagraphContainer {
040:
041: private RtfList parentList;
042: private RtfParagraph paragraph;
043: private RtfListStyle listStyle;
044: private int number = 0;
045:
046: /**
047: * special RtfParagraph that writes list item setup code before its content
048: */
049: private class RtfListItemParagraph extends RtfParagraph {
050:
051: RtfListItemParagraph(RtfListItem rli, RtfAttributes attrs)
052: throws IOException {
053: super (rli, rli.writer, attrs);
054: }
055:
056: protected void writeRtfPrefix() throws IOException {
057: super .writeRtfPrefix();
058: getRtfListStyle().writeParagraphPrefix(this );
059: }
060: }
061:
062: /**
063: * special RtfTextrun that is used as list item label
064: */
065: public class RtfListItemLabel extends RtfTextrun implements
066: IRtfTextrunContainer {
067:
068: private RtfListItem rtfListItem;
069:
070: /**
071: * Constructs the RtfListItemLabel
072: * @param item The RtfListItem the label belongs to
073: * @throws IOException Thrown when an IO-problem occurs
074: */
075: public RtfListItemLabel(RtfListItem item) throws IOException {
076: super (null, item.writer, null);
077:
078: rtfListItem = item;
079: }
080:
081: /**
082: * Returns the current RtfTextrun object.
083: * Opens a new one if necessary.
084: * @return The RtfTextrun object
085: * @throws IOException Thrown when an IO-problem occurs
086: */
087: public RtfTextrun getTextrun() throws IOException {
088: return this ;
089: }
090:
091: /**
092: * Sets the content of the list item label.
093: * @param s Content of the list item label.
094: * @throws IOException Thrown when an IO-problem occurs
095: */
096: public void addString(String s) throws IOException {
097:
098: final String label = s.trim();
099: if (label.length() > 0
100: && Character.isDigit(label.charAt(0))) {
101: rtfListItem.setRtfListStyle(new RtfListStyleNumber());
102: } else {
103: rtfListItem
104: .setRtfListStyle(new RtfListStyleText(label));
105: }
106: }
107: }
108:
109: /** Create an RTF list item as a child of given container with default attributes */
110: RtfListItem(RtfList parent, Writer w) throws IOException {
111: super ((RtfContainer) parent, w);
112: parentList = parent;
113: }
114:
115: /**
116: * Close current paragraph if any and start a new one
117: * @param attrs attributes of new paragraph
118: * @return new RtfParagraph
119: * @throws IOException Thrown when an IO-problem occurs
120: */
121: public RtfParagraph newParagraph(RtfAttributes attrs)
122: throws IOException {
123: if (paragraph != null) {
124: paragraph.close();
125: }
126: paragraph = new RtfListItemParagraph(this , attrs);
127: return paragraph;
128: }
129:
130: /**
131: * Close current paragraph if any and start a new one with default attributes
132: * @return new RtfParagraph
133: * @throws IOException Thrown when an IO-problem occurs
134: */
135: public RtfParagraph newParagraph() throws IOException {
136: return newParagraph(null);
137: }
138:
139: /** Create an RTF list item as a child of given container with given attributes */
140: RtfListItem(RtfList parent, Writer w, RtfAttributes attr)
141: throws IOException {
142: super ((RtfContainer) parent, w, attr);
143: parentList = parent;
144: }
145:
146: /**
147: * Get the current textrun.
148: * @return current RtfTextrun object
149: * @throws IOException Thrown when an IO-problem occurs
150: */
151: public RtfTextrun getTextrun() throws IOException {
152: RtfTextrun textrun = RtfTextrun.getTextrun(this , writer, null);
153: textrun.setRtfListItem(this );
154: return textrun;
155: }
156:
157: /**
158: * Start a new list after closing current paragraph, list and table
159: * @param attrs attributes of new RftList object
160: * @return new RtfList
161: * @throws IOException for I/O problems
162: */
163: public RtfList newList(RtfAttributes attrs) throws IOException {
164: RtfList list = new RtfList(this , writer, attrs);
165: return list;
166: }
167:
168: /**
169: * Overridden to setup the list: start a group with appropriate attributes
170: * @throws IOException for I/O problems
171: */
172: protected void writeRtfPrefix() throws IOException {
173:
174: // pard causes word97 (and sometimes 2000 too) to crash if the list is nested in a table
175: if (!parentList.getHasTableParent()) {
176: writeControlWord("pard");
177: }
178:
179: writeOneAttribute(RtfText.LEFT_INDENT_FIRST, "360"); //attrib.getValue(RtfListTable.LIST_INDENT));
180:
181: writeOneAttribute(RtfText.LEFT_INDENT_BODY, attrib
182: .getValue(RtfText.LEFT_INDENT_BODY));
183:
184: // group for list setup info
185: writeGroupMark(true);
186:
187: writeStarControlWord("pn");
188: //Modified by Chris Scott
189: //fixes second line indentation
190: getRtfListStyle().writeListPrefix(this );
191:
192: writeGroupMark(false);
193: writeOneAttribute(RtfListTable.LIST_NUMBER, new Integer(number));
194: }
195:
196: /**
197: * End the list group
198: * @throws IOException for I/O problems
199: */
200: protected void writeRtfSuffix() throws IOException {
201: super .writeRtfSuffix();
202:
203: /* reset paragraph defaults to make sure list ends
204: * but pard causes word97 (and sometimes 2000 too) to crash if the list
205: * is nested in a table
206: */
207: if (!parentList.getHasTableParent()) {
208: writeControlWord("pard");
209: }
210:
211: }
212:
213: /**
214: * Change list style
215: * @param ls ListStyle to set
216: */
217: public void setRtfListStyle(RtfListStyle ls) {
218: listStyle = ls;
219:
220: listStyle.setRtfListItem(this );
221: number = getRtfFile().getListTable().addRtfListStyle(ls);
222: }
223:
224: /**
225: * Get list style
226: * @return ListSytle of the List
227: */
228: public RtfListStyle getRtfListStyle() {
229: if (listStyle == null) {
230: return parentList.getRtfListStyle();
231: } else {
232: return listStyle;
233: }
234: }
235:
236: /**
237: * Get the parent list.
238: * @return the parent list
239: */
240: public RtfList getParentList() {
241: return parentList;
242: }
243:
244: /**
245: * Returns the list number
246: * @return list number
247: */
248: public int getNumber() {
249: return number;
250: }
251: }
|