KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > headerfooter > RtfHeaderFooterGroup


1 /*
2  * $Id: RtfHeaderFooterGroup.java 2776 2007-05-23 20:01:40Z hallm $
3  * $Name$
4  *
5  * Copyright 2001, 2002, 2003, 2004 by Mark Hall
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.rtf.headerfooter;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56
57 import com.lowagie.text.HeaderFooter;
58 import com.lowagie.text.Phrase;
59 import com.lowagie.text.rtf.RtfBasicElement;
60 import com.lowagie.text.rtf.document.RtfDocument;
61
62
63 /**
64  * The RtfHeaderFooterGroup holds 0 - 3 RtfHeaderFooters that create a group
65  * of headers or footers.
66  *
67  * @version $Id: RtfHeaderFooterGroup.java 2776 2007-05-23 20:01:40Z hallm $
68  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
69  * @author Thomas Bickel (tmb99@inode.at)
70  */

71 public class RtfHeaderFooterGroup extends HeaderFooter implements RtfBasicElement {
72     
73     /**
74      * This RtfHeaderFooterGroup contains no RtfHeaderFooter objects
75      */

76     private static final int MODE_NONE = 0;
77     /**
78      * This RtfHeaderFooterGroup contains one RtfHeaderFooter object
79      */

80     private static final int MODE_SINGLE = 1;
81     /**
82      * This RtfHeaderFooterGroup contains two or three RtfHeaderFooter objects
83      */

84     private static final int MODE_MULTIPLE = 2;
85     
86     /**
87      * The current mode of this RtfHeaderFooterGroup. Defaults to MODE_NONE
88      */

89     private int mode = MODE_NONE;
90     /**
91      * The current type of this RtfHeaderFooterGroup. Defaults to RtfHeaderFooter.TYPE_HEADER
92      */

93     private int type = RtfHeaderFooter.TYPE_HEADER;
94     
95     /**
96      * The RtfHeaderFooter for all pages
97      */

98     private RtfHeaderFooter headerAll = null;
99     /**
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, RtfHeaderFooterGroup headerFooter, int type) {
148         super(new Phrase(""), false);
149         this.document = doc;
150         this.mode = headerFooter.getMode();
151         this.type = type;
152         if(headerFooter.getHeaderAll() != null) {
153             this.headerAll = new RtfHeaderFooter(this.document, headerFooter.getHeaderAll(), RtfHeaderFooter.DISPLAY_ALL_PAGES);
154         }
155         if(headerFooter.getHeaderFirst() != null) {
156             this.headerFirst = new RtfHeaderFooter(this.document, headerFooter.getHeaderFirst(), RtfHeaderFooter.DISPLAY_FIRST_PAGE);
157         }
158         if(headerFooter.getHeaderLeft() != null) {
159             this.headerLeft = new RtfHeaderFooter(this.document, headerFooter.getHeaderLeft(), RtfHeaderFooter.DISPLAY_LEFT_PAGES);
160         }
161         if(headerFooter.getHeaderRight() != null) {
162             this.headerRight = new RtfHeaderFooter(this.document, headerFooter.getHeaderRight(), RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
163         }
164         setType(this.type);
165     }
166     
167     /**
168      * Constructs a RtfHeaderFooterGroup for a certain RtfHeaderFooter.
169      *
170      * @param doc The RtfDocument this RtfHeaderFooter belongs to
171      * @param headerFooter The RtfHeaderFooter to display
172      * @param type The typ of RtfHeaderFooterGroup to create
173      */

174     public RtfHeaderFooterGroup(RtfDocument doc, RtfHeaderFooter headerFooter, int type) {
175         super(new Phrase(""), false);
176         this.document = doc;
177         this.type = type;
178         this.mode = MODE_SINGLE;
179         headerAll = new RtfHeaderFooter(doc, headerFooter, RtfHeaderFooter.DISPLAY_ALL_PAGES);
180         headerAll.setType(this.type);
181     }
182     
183     /**
184      * Constructs a RtfHeaderGroup for a certain HeaderFooter
185      *
186      * @param doc The RtfDocument this RtfHeaderFooter belongs to
187      * @param headerFooter The HeaderFooter to display
188      * @param type The typ of RtfHeaderFooterGroup to create
189      */

190     public RtfHeaderFooterGroup(RtfDocument doc, HeaderFooter headerFooter, int type) {
191         super(new Phrase(""), false);
192         this.document = doc;
193         this.type = type;
194         this.mode = MODE_SINGLE;
195         headerAll = new RtfHeaderFooter(doc, headerFooter, type, RtfHeaderFooter.DISPLAY_ALL_PAGES);
196         headerAll.setType(this.type);
197     }
198     
199     /**
200      * Sets the RtfDocument this RtfElement belongs to
201      *
202      * @param doc The RtfDocument to use
203      */

204     public void setRtfDocument(RtfDocument doc) {
205         this.document = doc;
206         if(headerAll != null) {
207             headerAll.setRtfDocument(this.document);
208         }
209         if(headerFirst != null) {
210             headerFirst.setRtfDocument(this.document);
211         }
212         if(headerLeft != null) {
213             headerLeft.setRtfDocument(this.document);
214         }
215         if(headerRight != null) {
216             headerRight.setRtfDocument(this.document);
217         }
218     }
219     
220     /**
221      * Write the content of this RtfHeaderFooterGroup.
222      *
223      * @return A byte array with the content of this RtfHeaderFooterGroup
224      * @deprecated replaced by {@link #writeContent(OutputStream)}
225      */

226     public byte[] write()
227     {
228         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
229         try {
230             writeContent(result);
231         } catch(IOException JavaDoc ioe) {
232             ioe.printStackTrace();
233         }
234         return result.toByteArray();
235     }
236     /**
237      * Write the content of this RtfHeaderFooterGroup.
238      */

239     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
240     {
241         if(this.mode == MODE_SINGLE) {
242             //result.write(headerAll.write());
243
headerAll.writeContent(result);
244         } else if(this.mode == MODE_MULTIPLE) {
245             if(headerFirst != null) {
246                 //result.write(headerFirst.write());
247
headerFirst.writeContent(result);
248             }
249             if(headerLeft != null) {
250                 //result.write(headerLeft.write());
251
headerLeft.writeContent(result);
252             }
253             if(headerRight != null) {
254                 //result.write(headerRight.write());
255
headerRight.writeContent(result);
256             }
257             if(headerAll != null) {
258                 //result.write(headerAll.write());
259
headerAll.writeContent(result);
260             }
261         }
262     }
263     
264     /**
265      * Set a RtfHeaderFooter to be displayed at a certain position
266      *
267      * @param headerFooter The RtfHeaderFooter to display
268      * @param displayAt The display location to use
269      */

270     public void setHeaderFooter(RtfHeaderFooter headerFooter, int displayAt) {
271         this.mode = MODE_MULTIPLE;
272         headerFooter.setRtfDocument(this.document);
273         headerFooter.setType(this.type);
274         headerFooter.setDisplayAt(displayAt);
275         switch(displayAt) {
276             case RtfHeaderFooter.DISPLAY_ALL_PAGES:
277                 headerAll = headerFooter;
278                 break;
279             case RtfHeaderFooter.DISPLAY_FIRST_PAGE:
280                 headerFirst = headerFooter;
281                 break;
282             case RtfHeaderFooter.DISPLAY_LEFT_PAGES:
283                 headerLeft = headerFooter;
284                 break;
285             case RtfHeaderFooter.DISPLAY_RIGHT_PAGES:
286                 headerRight = headerFooter;
287                 break;
288         }
289     }
290     
291     /**
292      * Set a HeaderFooter to be displayed at a certain position
293      *
294      * @param headerFooter The HeaderFooter to set
295      * @param displayAt The display location to use
296      */

297     public void setHeaderFooter(HeaderFooter headerFooter, int displayAt) {
298         this.mode = MODE_MULTIPLE;
299         switch(displayAt) {
300             case RtfHeaderFooter.DISPLAY_ALL_PAGES:
301                 headerAll = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt);
302                 break;
303             case RtfHeaderFooter.DISPLAY_FIRST_PAGE:
304                 headerFirst = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt);
305                 break;
306             case RtfHeaderFooter.DISPLAY_LEFT_PAGES:
307                 headerLeft = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt);
308                 break;
309             case RtfHeaderFooter.DISPLAY_RIGHT_PAGES:
310                 headerRight = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt);
311                 break;
312         }
313     }
314     
315     /**
316      * Set that this RtfHeaderFooterGroup should have a title page. If only
317      * a header / footer for all pages exists, then it will be copied to the
318      * first page aswell.
319      */

320     public void setHasTitlePage() {
321         if(this.mode == MODE_SINGLE) {
322             this.mode = MODE_MULTIPLE;
323             headerFirst = new RtfHeaderFooter(this.document, headerAll, RtfHeaderFooter.DISPLAY_FIRST_PAGE);
324             headerFirst.setType(this.type);
325         }
326     }
327     
328     /**
329      * Set that this RtfHeaderFooterGroup should have facing pages. If only
330      * a header / footer for all pages exists, then it will be copied to the left
331      * and right pages aswell.
332      */

333     public void setHasFacingPages() {
334         if(this.mode == MODE_SINGLE) {
335             this.mode = MODE_MULTIPLE;
336             this.headerLeft = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_LEFT_PAGES);
337             this.headerLeft.setType(this.type);
338             this.headerRight = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
339             this.headerRight.setType(this.type);
340             this.headerAll = null;
341         } else if(this.mode == MODE_MULTIPLE) {
342             if(this.headerLeft == null && this.headerAll != null) {
343                 this.headerLeft = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_LEFT_PAGES);
344                 this.headerLeft.setType(this.type);
345             }
346             if(this.headerRight == null && this.headerAll != null) {
347                 this.headerRight = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
348                 this.headerRight.setType(this.type);
349             }
350             this.headerAll = null;
351         }
352     }
353     
354     /**
355      * Get whether this RtfHeaderFooterGroup has a titlepage
356      *
357      * @return Whether this RtfHeaderFooterGroup has a titlepage
358      */

359     public boolean hasTitlePage() {
360         return (headerFirst != null);
361     }
362     
363     /**
364      * Get whether this RtfHeaderFooterGroup has facing pages
365      *
366      * @return Whether this RtfHeaderFooterGroup has facing pages
367      */

368     public boolean hasFacingPages() {
369         return (headerLeft != null || headerRight != null);
370     }
371
372     /**
373      * Unused
374      * @param inTable
375      */

376     public void setInTable(boolean inTable) {
377     }
378     
379     /**
380      * Unused
381      * @param inHeader
382      */

383     public void setInHeader(boolean inHeader) {
384     }
385     
386     /**
387      * Set the type of this RtfHeaderFooterGroup. RtfHeaderFooter.TYPE_HEADER
388      * or RtfHeaderFooter.TYPE_FOOTER. Also sets the type for all RtfHeaderFooters
389      * of this RtfHeaderFooterGroup.
390      *
391      * @param type The type to use
392      */

393     public void setType(int type) {
394         this.type = type;
395         if(headerAll != null) {
396             headerAll.setType(this.type);
397         }
398         if(headerFirst != null) {
399             headerFirst.setType(this.type);
400         }
401         if(headerLeft != null) {
402             headerLeft.setType(this.type);
403         }
404         if(headerRight != null) {
405             headerRight.setType(this.type);
406         }
407     }
408     
409     /**
410      * Gets the mode of this RtfHeaderFooterGroup
411      *
412      * @return The mode of this RtfHeaderFooterGroup
413      */

414     protected int getMode() {
415         return this.mode;
416     }
417     
418     /**
419      * Gets the RtfHeaderFooter for all pages
420      *
421      * @return The RtfHeaderFooter for all pages
422      */

423     protected RtfHeaderFooter getHeaderAll() {
424         return headerAll;
425     }
426
427     /**
428      * Gets the RtfHeaderFooter for the title page
429      *
430      * @return The RtfHeaderFooter for the title page
431      */

432     protected RtfHeaderFooter getHeaderFirst() {
433         return headerFirst;
434     }
435
436     /**
437      * Gets the RtfHeaderFooter for all left hand pages
438      *
439      * @return The RtfHeaderFooter for all left hand pages
440      */

441     protected RtfHeaderFooter getHeaderLeft() {
442         return headerLeft;
443     }
444
445     /**
446      * Gets the RtfHeaderFooter for all right hand pages
447      *
448      * @return The RtfHeaderFooter for all right hand pages
449      */

450     protected RtfHeaderFooter getHeaderRight() {
451         return headerRight;
452     }
453 }
454
Popular Tags