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: RtfSection.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: /** Models a section in an RTF document
033: * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
034: */
035:
036: public class RtfSection extends RtfContainer implements
037: IRtfParagraphContainer, IRtfTableContainer, IRtfListContainer,
038: IRtfExternalGraphicContainer, IRtfBeforeContainer,
039: IRtfParagraphKeepTogetherContainer, IRtfAfterContainer,
040: IRtfJforCmdContainer, IRtfTextrunContainer {
041: private RtfParagraph paragraph;
042: private RtfTable table;
043: private RtfList list;
044: private RtfExternalGraphic externalGraphic;
045: private RtfBefore before;
046: private RtfAfter after;
047: private RtfJforCmd jforCmd;
048:
049: /** Create an RTF container as a child of given container */
050: RtfSection(RtfDocumentArea parent, Writer w) throws IOException {
051: super (parent, w);
052: }
053:
054: /**
055: * Start a new external graphic after closing current paragraph, list and table
056: * @return new RtfExternalGraphic object
057: * @throws IOException for I/O problems
058: */
059: public RtfExternalGraphic newImage() throws IOException {
060: closeAll();
061: externalGraphic = new RtfExternalGraphic(this , writer);
062: return externalGraphic;
063: }
064:
065: /**
066: * Start a new paragraph after closing current paragraph, list and table
067: * @param attrs attributes for new RtfParagraph
068: * @return new RtfParagraph object
069: * @throws IOException for I/O problems
070: */
071: public RtfParagraph newParagraph(RtfAttributes attrs)
072: throws IOException {
073: closeAll();
074: paragraph = new RtfParagraph(this , writer, attrs);
075: return paragraph;
076: }
077:
078: /**
079: * Close current paragraph if any and start a new one with default attributes
080: * @return new RtfParagraph
081: * @throws IOException for I/O problems
082: */
083: public RtfParagraph newParagraph() throws IOException {
084: return newParagraph(null);
085: }
086:
087: /**
088: * Close current paragraph if any and start a new one
089: * @return new RtfParagraphKeepTogether
090: * @throws IOException for I/O problems
091: */
092: public RtfParagraphKeepTogether newParagraphKeepTogether()
093: throws IOException {
094: return new RtfParagraphKeepTogether(this , writer);
095: }
096:
097: /**
098: * Start a new table after closing current paragraph, list and table
099: * @param tc Table context used for number-columns-spanned attribute (added by
100: * Boris Poudérous on july 2002)
101: * @return new RtfTable object
102: * @throws IOException for I/O problems
103: */
104: public RtfTable newTable(ITableColumnsInfo tc) throws IOException {
105: closeAll();
106: table = new RtfTable(this , writer, tc);
107: return table;
108: }
109:
110: /**
111: * Start a new table after closing current paragraph, list and table
112: * @param attrs attributes of new RtfTable
113: * @param tc Table context used for number-columns-spanned attribute (added by
114: * Boris Poudérous on july 2002)
115: * @return new RtfTable object
116: * @throws IOException for I/O problems
117: */
118: public RtfTable newTable(RtfAttributes attrs, ITableColumnsInfo tc)
119: throws IOException {
120: closeAll();
121: table = new RtfTable(this , writer, attrs, tc);
122: return table;
123: }
124:
125: /**
126: * Start a new list after closing current paragraph, list and table
127: * @param attrs attributes of new RftList object
128: * @return new RtfList
129: * @throws IOException for I/O problems
130: */
131: public RtfList newList(RtfAttributes attrs) throws IOException {
132: closeAll();
133: list = new RtfList(this , writer, attrs);
134: return list;
135: }
136:
137: /**
138: * IRtfBeforeContainer
139: * @param attrs attributes of new RtfBefore object
140: * @return new RtfBefore object
141: * @throws IOException for I/O problems
142: */
143: public RtfBefore newBefore(RtfAttributes attrs) throws IOException {
144: closeAll();
145: before = new RtfBefore(this , writer, attrs);
146: return before;
147: }
148:
149: /**
150: * IRtfAfterContainer
151: * @param attrs attributes of new RtfAfter object
152: * @return new RtfAfter object
153: * @throws IOException for I/O problems
154: */
155: public RtfAfter newAfter(RtfAttributes attrs) throws IOException {
156: closeAll();
157: after = new RtfAfter(this , writer, attrs);
158: return after;
159: }
160:
161: /**
162: *
163: * @param attrs attributes of new RtfJforCmd
164: * @return the new RtfJforCmd
165: * @throws IOException for I/O problems
166: */
167: public RtfJforCmd newJforCmd(RtfAttributes attrs)
168: throws IOException {
169: jforCmd = new RtfJforCmd(this , writer, attrs);
170: return jforCmd;
171: }
172:
173: /**
174: * Can be overridden to write RTF prefix code, what comes before our children
175: * @throws IOException for I/O problems
176: */
177: protected void writeRtfPrefix() throws IOException {
178: writeAttributes(attrib, RtfPage.PAGE_ATTR);
179: newLine();
180: writeControlWord("sectd");
181: }
182:
183: /**
184: * Can be overridden to write RTF suffix code, what comes after our children
185: * @throws IOException for I/O problems
186: */
187: protected void writeRtfSuffix() throws IOException {
188: writeControlWord("sect");
189: }
190:
191: private void closeCurrentTable() throws IOException {
192: if (table != null) {
193: table.close();
194: }
195: }
196:
197: private void closeCurrentParagraph() throws IOException {
198: if (paragraph != null) {
199: paragraph.close();
200: }
201: }
202:
203: private void closeCurrentList() throws IOException {
204: if (list != null) {
205: list.close();
206: }
207: }
208:
209: private void closeCurrentExternalGraphic() throws IOException {
210: if (externalGraphic != null) {
211: externalGraphic.close();
212: }
213: }
214:
215: private void closeCurrentBefore() throws IOException {
216: if (before != null) {
217: before.close();
218: }
219: }
220:
221: private void closeAll() throws IOException {
222: closeCurrentTable();
223: closeCurrentParagraph();
224: closeCurrentList();
225: closeCurrentExternalGraphic();
226: closeCurrentBefore();
227: }
228:
229: /**
230: * Returns the current RtfTextrun.
231: * @return Current RtfTextrun
232: * @throws IOException Thrown when an IO-problem occurs.
233: */
234: public RtfTextrun getTextrun() throws IOException {
235: return RtfTextrun.getTextrun(this, writer, null);
236: }
237: }
|