1 /*
2 * $Id: RtfHeaderFooterGroup.java 3373 2008-05-12 16:21:24Z xlv $
3 *
4 * Copyright 2001, 2002, 2003, 2004 by Mark Hall
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * (the "License"); you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the License.
13 *
14 * The Original Code is 'iText, a free JAVA-PDF library'.
15 *
16 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18 * All Rights Reserved.
19 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21 *
22 * Contributor(s): all the names of the contributors are added in the source code
23 * where applicable.
24 *
25 * Alternatively, the contents of this file may be used under the terms of the
26 * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
27 * provisions of LGPL are applicable instead of those above. If you wish to
28 * allow use of your version of this file only under the terms of the LGPL
29 * License and not to allow others to use your version of this file under
30 * the MPL, indicate your decision by deleting the provisions above and
31 * replace them with the notice and other provisions required by the LGPL.
32 * If you do not delete the provisions above, a recipient may use your version
33 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34 *
35 * This library is free software; you can redistribute it and/or modify it
36 * under the terms of the MPL as stated above or under the terms of the GNU
37 * Library General Public License as published by the Free Software Foundation;
38 * either version 2 of the License, or any later version.
39 *
40 * This library is distributed in the hope that it will be useful, but WITHOUT
41 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43 * details.
44 *
45 * If you didn't download this code from the following link, you should check if
46 * you aren't using an obsolete version:
47 * http://www.lowagie.com/iText/
48 */
49
50 package com.lowagie.text.rtf.headerfooter;
51
52 import java.io.IOException;
53 import java.io.OutputStream;
54
55 import com.lowagie.text.HeaderFooter;
56 import com.lowagie.text.Phrase;
57 import com.lowagie.text.rtf.RtfBasicElement;
58 import com.lowagie.text.rtf.document.RtfDocument;
59
60
61 /**
62 * The RtfHeaderFooterGroup holds 0 - 3 RtfHeaderFooters that create a group
63 * of headers or footers.
64 *
65 * @version $Id: RtfHeaderFooterGroup.java 3373 2008-05-12 16:21:24Z xlv $
66 * @author Mark Hall (Mark.Hall@mail.room3b.eu)
67 * @author Thomas Bickel (tmb99@inode.at)
68 */
69 public class RtfHeaderFooterGroup extends HeaderFooter implements RtfBasicElement {
70
71 /**
72 * This RtfHeaderFooterGroup contains no RtfHeaderFooter objects
73 */
74 private static final int MODE_NONE = 0;
75 /**
76 * This RtfHeaderFooterGroup contains one RtfHeaderFooter object
77 */
78 private static final int MODE_SINGLE = 1;
79 /**
80 * This RtfHeaderFooterGroup contains two or three RtfHeaderFooter objects
81 */
82 private static final int MODE_MULTIPLE = 2;
83
84 /**
85 * The current mode of this RtfHeaderFooterGroup. Defaults to MODE_NONE
86 */
87 private int mode = MODE_NONE;
88 /**
89 * The current type of this RtfHeaderFooterGroup. Defaults to RtfHeaderFooter.TYPE_HEADER
90 */
91 private int type = RtfHeaderFooter.TYPE_HEADER;
92
93 /**
94 * The RtfHeaderFooter for all pages
95 */
96 private RtfHeaderFooter headerAll = null;
97 /**
98 * The RtfHeaderFooter for the first page
99 */
100 private RtfHeaderFooter headerFirst = null;
101 /**
102 * The RtfHeaderFooter for the left hand pages
103 */
104 private RtfHeaderFooter headerLeft = null;
105 /**
106 * The RtfHeaderFooter for the right hand pages
107 */
108 private RtfHeaderFooter headerRight = null;
109 /**
110 * The RtfDocument this RtfHeaderFooterGroup belongs to
111 */
112 private RtfDocument document = null;
113
114 /**
115 * Constructs a RtfHeaderGroup to which you add headers/footers using
116 * via the setHeaderFooter method.
117 *
118 */
119 public RtfHeaderFooterGroup() {
120 super(new Phrase(""), false);
121 this.mode = MODE_NONE;
122 }
123
124 /**
125 * Constructs a certain type of RtfHeaderFooterGroup. RtfHeaderFooter.TYPE_HEADER
126 * and RtfHeaderFooter.TYPE_FOOTER are valid values for type.
127 *
128 * @param doc The RtfDocument this RtfHeaderFooter belongs to
129 * @param type The type of RtfHeaderFooterGroup to create
130 */
131 public RtfHeaderFooterGroup(RtfDocument doc, int type) {
132 super(new Phrase(""), false);
133 this.document = doc;
134 this.type = type;
135 }
136
137 /**
138 * Constructs a RtfHeaderFooterGroup by copying the content of the original
139 * RtfHeaderFooterGroup
140 *
141 * @param doc The RtfDocument this RtfHeaderFooter belongs to
142 * @param headerFooter The RtfHeaderFooterGroup to copy
143 * @param type The type of RtfHeaderFooterGroup to create
144 */
145 public RtfHeaderFooterGroup(RtfDocument doc, RtfHeaderFooterGroup headerFooter, int type) {
146 super(new Phrase(""), false);
147 this.document = doc;
148 this.mode = headerFooter.getMode();
149 this.type = type;
150 if(headerFooter.getHeaderAll() != null) {
151 this.headerAll = new RtfHeaderFooter(this.document, headerFooter.getHeaderAll(), RtfHeaderFooter.DISPLAY_ALL_PAGES);
152 }
153 if(headerFooter.getHeaderFirst() != null) {
154 this.headerFirst = new RtfHeaderFooter(this.document, headerFooter.getHeaderFirst(), RtfHeaderFooter.DISPLAY_FIRST_PAGE);
155 }
156 if(headerFooter.getHeaderLeft() != null) {
157 this.headerLeft = new RtfHeaderFooter(this.document, headerFooter.getHeaderLeft(), RtfHeaderFooter.DISPLAY_LEFT_PAGES);
158 }
159 if(headerFooter.getHeaderRight() != null) {
160 this.headerRight = new RtfHeaderFooter(this.document, headerFooter.getHeaderRight(), RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
161 }
162 setType(this.type);
163 }
164
165 /**
166 * Constructs a RtfHeaderFooterGroup for a certain RtfHeaderFooter.
167 *
168 * @param doc The RtfDocument this RtfHeaderFooter belongs to
169 * @param headerFooter The RtfHeaderFooter to display
170 * @param type The type of RtfHeaderFooterGroup to create
171 */
172 public RtfHeaderFooterGroup(RtfDocument doc, RtfHeaderFooter headerFooter, int type) {
173 super(new Phrase(""), false);
174 this.document = doc;
175 this.type = type;
176 this.mode = MODE_SINGLE;
177 headerAll = new RtfHeaderFooter(doc, headerFooter, RtfHeaderFooter.DISPLAY_ALL_PAGES);
178 headerAll.setType(this.type);
179 }
180
181 /**
182 * Constructs a RtfHeaderGroup for a certain HeaderFooter
183 *
184 * @param doc The RtfDocument this RtfHeaderFooter belongs to
185 * @param headerFooter The HeaderFooter to display
186 * @param type The type of RtfHeaderFooterGroup to create
187 */
188 public RtfHeaderFooterGroup(RtfDocument doc, HeaderFooter headerFooter, int type) {
189 super(new Phrase(""), false);
190 this.document = doc;
191 this.type = type;
192 this.mode = MODE_SINGLE;
193 headerAll = new RtfHeaderFooter(doc, headerFooter, type, RtfHeaderFooter.DISPLAY_ALL_PAGES);
194 headerAll.setType(this.type);
195 }
196
197 /**
198 * Sets the RtfDocument this RtfElement belongs to
199 *
200 * @param doc The RtfDocument to use
201 */
202 public void setRtfDocument(RtfDocument doc) {
203 this.document = doc;
204 if(headerAll != null) {
205 headerAll.setRtfDocument(this.document);
206 }
207 if(headerFirst != null) {
208 headerFirst.setRtfDocument(this.document);
209 }
210 if(headerLeft != null) {
211 headerLeft.setRtfDocument(this.document);
212 }
213 if(headerRight != null) {
214 headerRight.setRtfDocument(this.document);
215 }
216 }
217
218 /**
219 * Write the content of this RtfHeaderFooterGroup.
220 */
221 public void writeContent(final OutputStream result) throws IOException
222 {
223 if(this.mode == MODE_SINGLE) {
224 headerAll.writeContent(result);
225 } else if(this.mode == MODE_MULTIPLE) {
226 if(headerFirst != null) {
227 headerFirst.writeContent(result);
228 }
229 if(headerLeft != null) {
230 headerLeft.writeContent(result);
231 }
232 if(headerRight != null) {
233 headerRight.writeContent(result);
234 }
235 if(headerAll != null) {
236 headerAll.writeContent(result);
237 }
238 }
239 }
240
241 /**
242 * Set a RtfHeaderFooter to be displayed at a certain position
243 *
244 * @param headerFooter The RtfHeaderFooter to display
245 * @param displayAt The display location to use
246 */
247 public void setHeaderFooter(RtfHeaderFooter headerFooter, int displayAt) {
248 this.mode = MODE_MULTIPLE;
249 headerFooter.setRtfDocument(this.document);
250 headerFooter.setType(this.type);
251 headerFooter.setDisplayAt(displayAt);
252 switch(displayAt) {
253 case RtfHeaderFooter.DISPLAY_ALL_PAGES:
254 headerAll = headerFooter;
255 break;
256 case RtfHeaderFooter.DISPLAY_FIRST_PAGE:
257 headerFirst = headerFooter;
258 break;
259 case RtfHeaderFooter.DISPLAY_LEFT_PAGES:
260 headerLeft = headerFooter;
261 break;
262 case RtfHeaderFooter.DISPLAY_RIGHT_PAGES:
263 headerRight = headerFooter;
264 break;
265 }
266 }
267
268 /**
269 * Set a HeaderFooter to be displayed at a certain position
270 *
271 * @param headerFooter The HeaderFooter to set
272 * @param displayAt The display location to use
273 */
274 public void setHeaderFooter(HeaderFooter headerFooter, int displayAt) {
275 this.mode = MODE_MULTIPLE;
276 switch(displayAt) {
277 case RtfHeaderFooter.DISPLAY_ALL_PAGES:
278 headerAll = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt);
279 break;
280 case RtfHeaderFooter.DISPLAY_FIRST_PAGE:
281 headerFirst = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt);
282 break;
283 case RtfHeaderFooter.DISPLAY_LEFT_PAGES:
284 headerLeft = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt);
285 break;
286 case RtfHeaderFooter.DISPLAY_RIGHT_PAGES:
287 headerRight = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt);
288 break;
289 }
290 }
291
292 /**
293 * Set that this RtfHeaderFooterGroup should have a title page. If only
294 * a header / footer for all pages exists, then it will be copied to the
295 * first page as well.
296 */
297 public void setHasTitlePage() {
298 if(this.mode == MODE_SINGLE) {
299 this.mode = MODE_MULTIPLE;
300 headerFirst = new RtfHeaderFooter(this.document, headerAll, RtfHeaderFooter.DISPLAY_FIRST_PAGE);
301 headerFirst.setType(this.type);
302 }
303 }
304
305 /**
306 * Set that this RtfHeaderFooterGroup should have facing pages. If only
307 * a header / footer for all pages exists, then it will be copied to the left
308 * and right pages as well.
309 */
310 public void setHasFacingPages() {
311 if(this.mode == MODE_SINGLE) {
312 this.mode = MODE_MULTIPLE;
313 this.headerLeft = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_LEFT_PAGES);
314 this.headerLeft.setType(this.type);
315 this.headerRight = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
316 this.headerRight.setType(this.type);
317 this.headerAll = null;
318 } else if(this.mode == MODE_MULTIPLE) {
319 if(this.headerLeft == null && this.headerAll != null) {
320 this.headerLeft = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_LEFT_PAGES);
321 this.headerLeft.setType(this.type);
322 }
323 if(this.headerRight == null && this.headerAll != null) {
324 this.headerRight = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
325 this.headerRight.setType(this.type);
326 }
327 this.headerAll = null;
328 }
329 }
330
331 /**
332 * Get whether this RtfHeaderFooterGroup has a titlepage
333 *
334 * @return Whether this RtfHeaderFooterGroup has a titlepage
335 */
336 public boolean hasTitlePage() {
337 return (headerFirst != null);
338 }
339
340 /**
341 * Get whether this RtfHeaderFooterGroup has facing pages
342 *
343 * @return Whether this RtfHeaderFooterGroup has facing pages
344 */
345 public boolean hasFacingPages() {
346 return (headerLeft != null || headerRight != null);
347 }
348
349 /**
350 * Unused
351 * @param inTable
352 */
353 public void setInTable(boolean inTable) {
354 }
355
356 /**
357 * Unused
358 * @param inHeader
359 */
360 public void setInHeader(boolean inHeader) {
361 }
362
363 /**
364 * Set the type of this RtfHeaderFooterGroup. RtfHeaderFooter.TYPE_HEADER
365 * or RtfHeaderFooter.TYPE_FOOTER. Also sets the type for all RtfHeaderFooters
366 * of this RtfHeaderFooterGroup.
367 *
368 * @param type The type to use
369 */
370 public void setType(int type) {
371 this.type = type;
372 if(headerAll != null) {
373 headerAll.setType(this.type);
374 }
375 if(headerFirst != null) {
376 headerFirst.setType(this.type);
377 }
378 if(headerLeft != null) {
379 headerLeft.setType(this.type);
380 }
381 if(headerRight != null) {
382 headerRight.setType(this.type);
383 }
384 }
385
386 /**
387 * Gets the mode of this RtfHeaderFooterGroup
388 *
389 * @return The mode of this RtfHeaderFooterGroup
390 */
391 protected int getMode() {
392 return this.mode;
393 }
394
395 /**
396 * Gets the RtfHeaderFooter for all pages
397 *
398 * @return The RtfHeaderFooter for all pages
399 */
400 protected RtfHeaderFooter getHeaderAll() {
401 return headerAll;
402 }
403
404 /**
405 * Gets the RtfHeaderFooter for the title page
406 *
407 * @return The RtfHeaderFooter for the title page
408 */
409 protected RtfHeaderFooter getHeaderFirst() {
410 return headerFirst;
411 }
412
413 /**
414 * Gets the RtfHeaderFooter for all left hand pages
415 *
416 * @return The RtfHeaderFooter for all left hand pages
417 */
418 protected RtfHeaderFooter getHeaderLeft() {
419 return headerLeft;
420 }
421
422 /**
423 * Gets the RtfHeaderFooter for all right hand pages
424 *
425 * @return The RtfHeaderFooter for all right hand pages
426 */
427 protected RtfHeaderFooter getHeaderRight() {
428 return headerRight;
429 }
430 }