001: /*
002: * $Id: RtfHeaderFooterGroup.java 2776 2007-05-23 20:01:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2001, 2002, 2003, 2004 by Mark Hall
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.rtf.headerfooter;
052:
053: import java.io.ByteArrayOutputStream;
054: import java.io.IOException;
055: import java.io.OutputStream;
056:
057: import com.lowagie.text.HeaderFooter;
058: import com.lowagie.text.Phrase;
059: import com.lowagie.text.rtf.RtfBasicElement;
060: import com.lowagie.text.rtf.document.RtfDocument;
061:
062: /**
063: * The RtfHeaderFooterGroup holds 0 - 3 RtfHeaderFooters that create a group
064: * of headers or footers.
065: *
066: * @version $Id: RtfHeaderFooterGroup.java 2776 2007-05-23 20:01:40Z hallm $
067: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
068: * @author Thomas Bickel (tmb99@inode.at)
069: */
070: public class RtfHeaderFooterGroup extends HeaderFooter implements
071: RtfBasicElement {
072:
073: /**
074: * This RtfHeaderFooterGroup contains no RtfHeaderFooter objects
075: */
076: private static final int MODE_NONE = 0;
077: /**
078: * This RtfHeaderFooterGroup contains one RtfHeaderFooter object
079: */
080: private static final int MODE_SINGLE = 1;
081: /**
082: * This RtfHeaderFooterGroup contains two or three RtfHeaderFooter objects
083: */
084: private static final int MODE_MULTIPLE = 2;
085:
086: /**
087: * The current mode of this RtfHeaderFooterGroup. Defaults to MODE_NONE
088: */
089: private int mode = MODE_NONE;
090: /**
091: * The current type of this RtfHeaderFooterGroup. Defaults to RtfHeaderFooter.TYPE_HEADER
092: */
093: private int type = RtfHeaderFooter.TYPE_HEADER;
094:
095: /**
096: * The RtfHeaderFooter for all pages
097: */
098: private RtfHeaderFooter headerAll = null;
099: /**
100: * The RtfHeaderFooter for the first page
101: */
102: private RtfHeaderFooter headerFirst = null;
103: /**
104: * The RtfHeaderFooter for the left hand pages
105: */
106: private RtfHeaderFooter headerLeft = null;
107: /**
108: * The RtfHeaderFooter for the right hand pages
109: */
110: private RtfHeaderFooter headerRight = null;
111: /**
112: * The RtfDocument this RtfHeaderFooterGroup belongs to
113: */
114: private RtfDocument document = null;
115:
116: /**
117: * Constructs a RtfHeaderGroup to which you add headers/footers using
118: * via the setHeaderFooter method.
119: *
120: */
121: public RtfHeaderFooterGroup() {
122: super (new Phrase(""), false);
123: this .mode = MODE_NONE;
124: }
125:
126: /**
127: * Constructs a certain type of RtfHeaderFooterGroup. RtfHeaderFooter.TYPE_HEADER
128: * and RtfHeaderFooter.TYPE_FOOTER are valid values for type.
129: *
130: * @param doc The RtfDocument this RtfHeaderFooter belongs to
131: * @param type The type of RtfHeaderFooterGroup to create
132: */
133: public RtfHeaderFooterGroup(RtfDocument doc, int type) {
134: super (new Phrase(""), false);
135: this .document = doc;
136: this .type = type;
137: }
138:
139: /**
140: * Constructs a RtfHeaderFooterGroup by copying the content of the original
141: * RtfHeaderFooterGroup
142: *
143: * @param doc The RtfDocument this RtfHeaderFooter belongs to
144: * @param headerFooter The RtfHeaderFooterGroup to copy
145: * @param type The type of RtfHeaderFooterGroup to create
146: */
147: public RtfHeaderFooterGroup(RtfDocument doc,
148: RtfHeaderFooterGroup headerFooter, int type) {
149: super (new Phrase(""), false);
150: this .document = doc;
151: this .mode = headerFooter.getMode();
152: this .type = type;
153: if (headerFooter.getHeaderAll() != null) {
154: this .headerAll = new RtfHeaderFooter(this .document,
155: headerFooter.getHeaderAll(),
156: RtfHeaderFooter.DISPLAY_ALL_PAGES);
157: }
158: if (headerFooter.getHeaderFirst() != null) {
159: this .headerFirst = new RtfHeaderFooter(this .document,
160: headerFooter.getHeaderFirst(),
161: RtfHeaderFooter.DISPLAY_FIRST_PAGE);
162: }
163: if (headerFooter.getHeaderLeft() != null) {
164: this .headerLeft = new RtfHeaderFooter(this .document,
165: headerFooter.getHeaderLeft(),
166: RtfHeaderFooter.DISPLAY_LEFT_PAGES);
167: }
168: if (headerFooter.getHeaderRight() != null) {
169: this .headerRight = new RtfHeaderFooter(this .document,
170: headerFooter.getHeaderRight(),
171: RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
172: }
173: setType(this .type);
174: }
175:
176: /**
177: * Constructs a RtfHeaderFooterGroup for a certain RtfHeaderFooter.
178: *
179: * @param doc The RtfDocument this RtfHeaderFooter belongs to
180: * @param headerFooter The RtfHeaderFooter to display
181: * @param type The typ of RtfHeaderFooterGroup to create
182: */
183: public RtfHeaderFooterGroup(RtfDocument doc,
184: RtfHeaderFooter headerFooter, int type) {
185: super (new Phrase(""), false);
186: this .document = doc;
187: this .type = type;
188: this .mode = MODE_SINGLE;
189: headerAll = new RtfHeaderFooter(doc, headerFooter,
190: RtfHeaderFooter.DISPLAY_ALL_PAGES);
191: headerAll.setType(this .type);
192: }
193:
194: /**
195: * Constructs a RtfHeaderGroup for a certain HeaderFooter
196: *
197: * @param doc The RtfDocument this RtfHeaderFooter belongs to
198: * @param headerFooter The HeaderFooter to display
199: * @param type The typ of RtfHeaderFooterGroup to create
200: */
201: public RtfHeaderFooterGroup(RtfDocument doc,
202: HeaderFooter headerFooter, int type) {
203: super (new Phrase(""), false);
204: this .document = doc;
205: this .type = type;
206: this .mode = MODE_SINGLE;
207: headerAll = new RtfHeaderFooter(doc, headerFooter, type,
208: RtfHeaderFooter.DISPLAY_ALL_PAGES);
209: headerAll.setType(this .type);
210: }
211:
212: /**
213: * Sets the RtfDocument this RtfElement belongs to
214: *
215: * @param doc The RtfDocument to use
216: */
217: public void setRtfDocument(RtfDocument doc) {
218: this .document = doc;
219: if (headerAll != null) {
220: headerAll.setRtfDocument(this .document);
221: }
222: if (headerFirst != null) {
223: headerFirst.setRtfDocument(this .document);
224: }
225: if (headerLeft != null) {
226: headerLeft.setRtfDocument(this .document);
227: }
228: if (headerRight != null) {
229: headerRight.setRtfDocument(this .document);
230: }
231: }
232:
233: /**
234: * Write the content of this RtfHeaderFooterGroup.
235: *
236: * @return A byte array with the content of this RtfHeaderFooterGroup
237: * @deprecated replaced by {@link #writeContent(OutputStream)}
238: */
239: public byte[] write() {
240: ByteArrayOutputStream result = new ByteArrayOutputStream();
241: try {
242: writeContent(result);
243: } catch (IOException ioe) {
244: ioe.printStackTrace();
245: }
246: return result.toByteArray();
247: }
248:
249: /**
250: * Write the content of this RtfHeaderFooterGroup.
251: */
252: public void writeContent(final OutputStream result)
253: throws IOException {
254: if (this .mode == MODE_SINGLE) {
255: //result.write(headerAll.write());
256: headerAll.writeContent(result);
257: } else if (this .mode == MODE_MULTIPLE) {
258: if (headerFirst != null) {
259: //result.write(headerFirst.write());
260: headerFirst.writeContent(result);
261: }
262: if (headerLeft != null) {
263: //result.write(headerLeft.write());
264: headerLeft.writeContent(result);
265: }
266: if (headerRight != null) {
267: //result.write(headerRight.write());
268: headerRight.writeContent(result);
269: }
270: if (headerAll != null) {
271: //result.write(headerAll.write());
272: headerAll.writeContent(result);
273: }
274: }
275: }
276:
277: /**
278: * Set a RtfHeaderFooter to be displayed at a certain position
279: *
280: * @param headerFooter The RtfHeaderFooter to display
281: * @param displayAt The display location to use
282: */
283: public void setHeaderFooter(RtfHeaderFooter headerFooter,
284: int displayAt) {
285: this .mode = MODE_MULTIPLE;
286: headerFooter.setRtfDocument(this .document);
287: headerFooter.setType(this .type);
288: headerFooter.setDisplayAt(displayAt);
289: switch (displayAt) {
290: case RtfHeaderFooter.DISPLAY_ALL_PAGES:
291: headerAll = headerFooter;
292: break;
293: case RtfHeaderFooter.DISPLAY_FIRST_PAGE:
294: headerFirst = headerFooter;
295: break;
296: case RtfHeaderFooter.DISPLAY_LEFT_PAGES:
297: headerLeft = headerFooter;
298: break;
299: case RtfHeaderFooter.DISPLAY_RIGHT_PAGES:
300: headerRight = headerFooter;
301: break;
302: }
303: }
304:
305: /**
306: * Set a HeaderFooter to be displayed at a certain position
307: *
308: * @param headerFooter The HeaderFooter to set
309: * @param displayAt The display location to use
310: */
311: public void setHeaderFooter(HeaderFooter headerFooter, int displayAt) {
312: this .mode = MODE_MULTIPLE;
313: switch (displayAt) {
314: case RtfHeaderFooter.DISPLAY_ALL_PAGES:
315: headerAll = new RtfHeaderFooter(this .document,
316: headerFooter, this .type, displayAt);
317: break;
318: case RtfHeaderFooter.DISPLAY_FIRST_PAGE:
319: headerFirst = new RtfHeaderFooter(this .document,
320: headerFooter, this .type, displayAt);
321: break;
322: case RtfHeaderFooter.DISPLAY_LEFT_PAGES:
323: headerLeft = new RtfHeaderFooter(this .document,
324: headerFooter, this .type, displayAt);
325: break;
326: case RtfHeaderFooter.DISPLAY_RIGHT_PAGES:
327: headerRight = new RtfHeaderFooter(this .document,
328: headerFooter, this .type, displayAt);
329: break;
330: }
331: }
332:
333: /**
334: * Set that this RtfHeaderFooterGroup should have a title page. If only
335: * a header / footer for all pages exists, then it will be copied to the
336: * first page aswell.
337: */
338: public void setHasTitlePage() {
339: if (this .mode == MODE_SINGLE) {
340: this .mode = MODE_MULTIPLE;
341: headerFirst = new RtfHeaderFooter(this .document, headerAll,
342: RtfHeaderFooter.DISPLAY_FIRST_PAGE);
343: headerFirst.setType(this .type);
344: }
345: }
346:
347: /**
348: * Set that this RtfHeaderFooterGroup should have facing pages. If only
349: * a header / footer for all pages exists, then it will be copied to the left
350: * and right pages aswell.
351: */
352: public void setHasFacingPages() {
353: if (this .mode == MODE_SINGLE) {
354: this .mode = MODE_MULTIPLE;
355: this .headerLeft = new RtfHeaderFooter(this .document,
356: this .headerAll, RtfHeaderFooter.DISPLAY_LEFT_PAGES);
357: this .headerLeft.setType(this .type);
358: this .headerRight = new RtfHeaderFooter(this .document,
359: this .headerAll, RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
360: this .headerRight.setType(this .type);
361: this .headerAll = null;
362: } else if (this .mode == MODE_MULTIPLE) {
363: if (this .headerLeft == null && this .headerAll != null) {
364: this .headerLeft = new RtfHeaderFooter(this .document,
365: this .headerAll,
366: RtfHeaderFooter.DISPLAY_LEFT_PAGES);
367: this .headerLeft.setType(this .type);
368: }
369: if (this .headerRight == null && this .headerAll != null) {
370: this .headerRight = new RtfHeaderFooter(this .document,
371: this .headerAll,
372: RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
373: this .headerRight.setType(this .type);
374: }
375: this .headerAll = null;
376: }
377: }
378:
379: /**
380: * Get whether this RtfHeaderFooterGroup has a titlepage
381: *
382: * @return Whether this RtfHeaderFooterGroup has a titlepage
383: */
384: public boolean hasTitlePage() {
385: return (headerFirst != null);
386: }
387:
388: /**
389: * Get whether this RtfHeaderFooterGroup has facing pages
390: *
391: * @return Whether this RtfHeaderFooterGroup has facing pages
392: */
393: public boolean hasFacingPages() {
394: return (headerLeft != null || headerRight != null);
395: }
396:
397: /**
398: * Unused
399: * @param inTable
400: */
401: public void setInTable(boolean inTable) {
402: }
403:
404: /**
405: * Unused
406: * @param inHeader
407: */
408: public void setInHeader(boolean inHeader) {
409: }
410:
411: /**
412: * Set the type of this RtfHeaderFooterGroup. RtfHeaderFooter.TYPE_HEADER
413: * or RtfHeaderFooter.TYPE_FOOTER. Also sets the type for all RtfHeaderFooters
414: * of this RtfHeaderFooterGroup.
415: *
416: * @param type The type to use
417: */
418: public void setType(int type) {
419: this .type = type;
420: if (headerAll != null) {
421: headerAll.setType(this .type);
422: }
423: if (headerFirst != null) {
424: headerFirst.setType(this .type);
425: }
426: if (headerLeft != null) {
427: headerLeft.setType(this .type);
428: }
429: if (headerRight != null) {
430: headerRight.setType(this .type);
431: }
432: }
433:
434: /**
435: * Gets the mode of this RtfHeaderFooterGroup
436: *
437: * @return The mode of this RtfHeaderFooterGroup
438: */
439: protected int getMode() {
440: return this .mode;
441: }
442:
443: /**
444: * Gets the RtfHeaderFooter for all pages
445: *
446: * @return The RtfHeaderFooter for all pages
447: */
448: protected RtfHeaderFooter getHeaderAll() {
449: return headerAll;
450: }
451:
452: /**
453: * Gets the RtfHeaderFooter for the title page
454: *
455: * @return The RtfHeaderFooter for the title page
456: */
457: protected RtfHeaderFooter getHeaderFirst() {
458: return headerFirst;
459: }
460:
461: /**
462: * Gets the RtfHeaderFooter for all left hand pages
463: *
464: * @return The RtfHeaderFooter for all left hand pages
465: */
466: protected RtfHeaderFooter getHeaderLeft() {
467: return headerLeft;
468: }
469:
470: /**
471: * Gets the RtfHeaderFooter for all right hand pages
472: *
473: * @return The RtfHeaderFooter for all right hand pages
474: */
475: protected RtfHeaderFooter getHeaderRight() {
476: return headerRight;
477: }
478: }
|