KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RtfHeaderFooter.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.DocumentException;
58 import com.lowagie.text.Element;
59 import com.lowagie.text.HeaderFooter;
60 import com.lowagie.text.Image;
61 import com.lowagie.text.Paragraph;
62 import com.lowagie.text.Phrase;
63 import com.lowagie.text.Table;
64 import com.lowagie.text.rtf.RtfBasicElement;
65 import com.lowagie.text.rtf.document.RtfDocument;
66 import com.lowagie.text.rtf.field.RtfPageNumber;
67
68
69 /**
70  * The RtfHeaderFooter represents one header or footer. This class can be used
71  * directly.
72  *
73  * @version $Id: RtfHeaderFooter.java 2776 2007-05-23 20:01:40Z hallm $
74  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
75  * @author Thomas Bickel (tmb99@inode.at)
76  */

77 public class RtfHeaderFooter extends HeaderFooter implements RtfBasicElement {
78
79     /**
80      * Constant for the header type
81      */

82     public static final int TYPE_HEADER = 1;
83     /**
84      * Constant for the footer type
85      */

86     public static final int TYPE_FOOTER = 2;
87     /**
88      * Constant for displaying the header/footer on the first page
89      */

90     public static final int DISPLAY_FIRST_PAGE = 0;
91     /**
92      * Constant for displaying the header/footer on all pages
93      */

94     public static final int DISPLAY_ALL_PAGES = 1;
95     /**
96      * Constant for displaying the header/footer on all left hand pages
97      */

98     public static final int DISPLAY_LEFT_PAGES = 2;
99     /**
100      * Constant for displaying the header/footer on all right hand pages
101      */

102     public static final int DISPLAY_RIGHT_PAGES = 4;
103
104     /**
105      * Constant for a header on all pages
106      */

107     private static final byte[] HEADER_ALL = "\\header".getBytes();
108     /**
109      * Constant for a header on the first page
110      */

111     private static final byte[] HEADER_FIRST = "\\headerf".getBytes();
112     /**
113      * Constant for a header on all left hand pages
114      */

115     private static final byte[] HEADER_LEFT = "\\headerl".getBytes();
116     /**
117      * Constant for a header on all right hand pages
118      */

119     private static final byte[] HEADER_RIGHT = "\\headerr".getBytes();
120     /**
121      * Constant for a footer on all pages
122      */

123     private static final byte[] FOOTER_ALL = "\\footer".getBytes();
124     /**
125      * Constant for a footer on the first page
126      */

127     private static final byte[] FOOTER_FIRST = "\\footerf".getBytes();
128     /**
129      * Constnat for a footer on the left hand pages
130      */

131     private static final byte[] FOOTER_LEFT = "\\footerl".getBytes();
132     /**
133      * Constant for a footer on the right hand pages
134      */

135     private static final byte[] FOOTER_RIGHT = "\\footerr".getBytes();
136     
137     /**
138      * The RtfDocument this RtfHeaderFooter belongs to
139      */

140     private RtfDocument document = null;
141     /**
142      * The content of this RtfHeaderFooter
143      */

144     private Object JavaDoc[] content = null;
145     /**
146      * The display type of this RtfHeaderFooter. TYPE_HEADER or TYPE_FOOTER
147      */

148     private int type = TYPE_HEADER;
149     /**
150      * The display location of this RtfHeaderFooter. DISPLAY_FIRST_PAGE,
151      * DISPLAY_LEFT_PAGES, DISPLAY_RIGHT_PAGES or DISPLAY_ALL_PAGES
152      */

153     private int displayAt = DISPLAY_ALL_PAGES;
154    
155     /**
156      * Constructs a RtfHeaderFooter based on a HeaderFooter with a certain type and displayAt
157      * location. For internal use only.
158      *
159      * @param doc The RtfDocument this RtfHeaderFooter belongs to
160      * @param headerFooter The HeaderFooter to base this RtfHeaderFooter on
161      * @param type The type of RtfHeaderFooter
162      * @param displayAt The display location of this RtfHeaderFooter
163      */

164     protected RtfHeaderFooter(RtfDocument doc, HeaderFooter headerFooter, int type, int displayAt) {
165         super(new Phrase(""), false);
166         this.document = doc;
167         this.type = type;
168         this.displayAt = displayAt;
169         Paragraph par = new Paragraph();
170         par.setAlignment(headerFooter.alignment());
171         if (headerFooter.getBefore() != null) {
172             par.add(headerFooter.getBefore());
173         }
174         if (headerFooter.isNumbered()) {
175             par.add(new RtfPageNumber(this.document));
176         }
177         if (headerFooter.getAfter() != null) {
178             par.add(headerFooter.getAfter());
179         }
180         try {
181             this.content = new Object JavaDoc[1];
182             if(this.document != null) {
183                 this.content[0] = this.document.getMapper().mapElement(par);
184                 ((RtfBasicElement) this.content[0]).setInHeader(true);
185             } else {
186                 this.content[0] = par;
187             }
188         } catch(DocumentException de) {
189             de.printStackTrace();
190         }
191     }
192     
193     /**
194      * Constructs a RtfHeaderFooter as a copy of an existing RtfHeaderFooter.
195      * For internal use only.
196      *
197      * @param doc The RtfDocument this RtfHeaderFooter belongs to
198      * @param headerFooter The RtfHeaderFooter to copy
199      * @param displayAt The display location of this RtfHeaderFooter
200      */

201     protected RtfHeaderFooter(RtfDocument doc, RtfHeaderFooter headerFooter, int displayAt) {
202         super(new Phrase(""), false);
203         this.document = doc;
204         this.content = headerFooter.getContent();
205         this.displayAt = displayAt;
206         for(int i = 0; i < this.content.length; i++) {
207             if(this.content[i] instanceof Element) {
208                 try {
209                     this.content[i] = this.document.getMapper().mapElement((Element) this.content[i]);
210                 } catch(DocumentException de) {
211                     de.printStackTrace();
212                 }
213             }
214             if(this.content[i] instanceof RtfBasicElement) {
215                 ((RtfBasicElement) this.content[i]).setInHeader(true);
216             }
217         }
218     }
219     
220     /**
221      * Constructs a RtfHeaderFooter for a HeaderFooter.
222      *
223      * @param doc The RtfDocument this RtfHeaderFooter belongs to
224      * @param headerFooter The HeaderFooter to base this RtfHeaderFooter on
225      */

226     protected RtfHeaderFooter(RtfDocument doc, HeaderFooter headerFooter) {
227         super(new Phrase(""), false);
228         this.document = doc;
229         Paragraph par = new Paragraph();
230         par.setAlignment(headerFooter.alignment());
231         if (headerFooter.getBefore() != null) {
232             par.add(headerFooter.getBefore());
233         }
234         if (headerFooter.isNumbered()) {
235             par.add(new RtfPageNumber(this.document));
236         }
237         if (headerFooter.getAfter() != null) {
238             par.add(headerFooter.getAfter());
239         }
240         try {
241             this.content = new Object JavaDoc[1];
242             this.content[0] = doc.getMapper().mapElement(par);
243             ((RtfBasicElement) this.content[0]).setInHeader(true);
244         } catch(DocumentException de) {
245             de.printStackTrace();
246         }
247     }
248     
249     /**
250      * Constructs a RtfHeaderFooter for any Element.
251      *
252      * @param element The Element to display as content of this RtfHeaderFooter
253      */

254     public RtfHeaderFooter(Element element) {
255         this(new Element[]{element});
256     }
257
258     /**
259      * Constructs a RtfHeaderFooter for an array of Elements.
260      *
261      * @param elements The Elements to display as the content of this RtfHeaderFooter.
262      */

263     public RtfHeaderFooter(Element[] elements) {
264         super(new Phrase(""), false);
265         this.content = new Object JavaDoc[elements.length];
266         for(int i = 0; i < elements.length; i++) {
267             this.content[i] = elements[i];
268         }
269     }
270     
271     /**
272      * Sets the RtfDocument this RtfElement belongs to
273      *
274      * @param doc The RtfDocument to use
275      */

276     public void setRtfDocument(RtfDocument doc) {
277         this.document = doc;
278         if(this.document != null) {
279             for(int i = 0; i < this.content.length; i++) {
280                 try {
281                     if(this.content[i] instanceof Element) {
282                         this.content[i] = this.document.getMapper().mapElement((Element) this.content[i]);
283                         ((RtfBasicElement) this.content[i]).setInHeader(true);
284                     } else if(this.content[i] instanceof RtfBasicElement){
285                         ((RtfBasicElement) this.content[i]).setRtfDocument(this.document);
286                         ((RtfBasicElement) this.content[i]).setInHeader(true);
287                     }
288                 } catch(DocumentException de) {
289                     de.printStackTrace();
290                 }
291             }
292         }
293     }
294     
295     /**
296      * Writes the content of this RtfHeaderFooter
297      *
298      * @return A byte array with the content of this RtfHeaderFooter
299      * @deprecated replaced by {@link #writeContent(OutputStream)}
300      */

301     public byte[] write()
302     {
303         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
304         try {
305             writeContent(result);
306         } catch(IOException JavaDoc ioe) {
307             ioe.printStackTrace();
308         }
309         return result.toByteArray();
310     }
311     /**
312      * Writes the content of this RtfHeaderFooter
313      */

314     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
315     {
316         result.write(OPEN_GROUP);
317         if(this.type == TYPE_HEADER) {
318             if(this.displayAt == DISPLAY_ALL_PAGES) {
319                 result.write(HEADER_ALL);
320             } else if(this.displayAt == DISPLAY_FIRST_PAGE) {
321                 result.write(HEADER_FIRST);
322             } else if(this.displayAt == DISPLAY_LEFT_PAGES) {
323                 result.write(HEADER_LEFT);
324             } else if(this.displayAt == DISPLAY_RIGHT_PAGES) {
325                 result.write(HEADER_RIGHT);
326             }
327         } else {
328             if(this.displayAt == DISPLAY_ALL_PAGES) {
329                 result.write(FOOTER_ALL);
330             } else if(this.displayAt == DISPLAY_FIRST_PAGE) {
331                 result.write(FOOTER_FIRST);
332             } else if(this.displayAt == DISPLAY_LEFT_PAGES) {
333                 result.write(FOOTER_LEFT);
334             } else if(this.displayAt == DISPLAY_RIGHT_PAGES) {
335                 result.write(FOOTER_RIGHT);
336             }
337         }
338         result.write(DELIMITER);
339         for(int i = 0; i < this.content.length; i++) {
340             if(this.content[i] instanceof RtfBasicElement) {
341                 //result.write(((RtfBasicElement) this.content[i]).write());
342
RtfBasicElement rbe = (RtfBasicElement)this.content[i];
343                 rbe.writeContent(result);
344             }
345         }
346         result.write(CLOSE_GROUP);
347     }
348     
349     
350     /**
351      * Sets the display location of this RtfHeaderFooter
352      *
353      * @param displayAt The display location to use.
354      */

355     public void setDisplayAt(int displayAt) {
356         this.displayAt = displayAt;
357     }
358     
359     /**
360      * Sets the type of this RtfHeaderFooter
361      *
362      * @param type The type to use.
363      */

364     public void setType(int type) {
365         this.type = type;
366     }
367     
368     /**
369      * Gets the content of this RtfHeaderFooter
370      *
371      * @return The content of this RtfHeaderFooter
372      */

373     private Object JavaDoc[] getContent() {
374         return this.content;
375     }
376
377     /**
378      * Unused
379      * @param inTable
380      */

381     public void setInTable(boolean inTable) {
382     }
383     
384     /**
385      * Unused
386      * @param inHeader
387      */

388     public void setInHeader(boolean inHeader) {
389     }
390     
391     /**
392      * Set the alignment of this RtfHeaderFooter. Passes the setting
393      * on to the contained element.
394      */

395     public void setAlignment(int alignment) {
396         super.setAlignment(alignment);
397         for(int i = 0; i < this.content.length; i++) {
398             if(this.content[i] instanceof Paragraph) {
399                 ((Paragraph) this.content[i]).setAlignment(alignment);
400             } else if(this.content[i] instanceof Table) {
401                 ((Table) this.content[i]).setAlignment(alignment);
402             } else if(this.content[i] instanceof Image) {
403                 ((Image) this.content[i]).setAlignment(alignment);
404             }
405         }
406     }
407 }
408
Popular Tags