KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > HeaderFooter


1 /*********************************************************************
2  *
3  * Copyright (C) 2004 Andrew Khan, Eric Jung
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  ***************************************************************************/

19
20 package jxl.biff;
21
22 import common.Assert;
23 import common.Logger;
24
25 /**
26  * Class which represents an Excel header or footer. Information for this
27  * class came from Microsoft Knowledge Base Article 142136
28  * (previously Q142136).
29  *
30  * This class encapsulates three internal structures representing the header
31  * or footer contents which appear on the left, right or central part of the
32  * page
33  */

34 public abstract class HeaderFooter
35 {
36   /**
37    * The logger
38    */

39   private static Logger logger = Logger.getLogger(HeaderFooter.class);
40
41   // Codes to format text
42

43   /**
44    * Turns bold printing on or off
45    */

46   private static final String JavaDoc BOLD_TOGGLE = "&B";
47
48   /**
49    * Turns underline printing on or off
50    */

51   private static final String JavaDoc UNDERLINE_TOGGLE = "&U";
52
53   /**
54    * Turns italic printing on or off
55    */

56   private static final String JavaDoc ITALICS_TOGGLE = "&I";
57
58   /**
59    * Turns strikethrough printing on or off
60    */

61   private static final String JavaDoc STRIKETHROUGH_TOGGLE = "&S";
62
63   /**
64    * Turns double-underline printing on or off
65    */

66   private static final String JavaDoc DOUBLE_UNDERLINE_TOGGLE = "&E";
67
68   /**
69    * Turns superscript printing on or off
70    */

71   private static final String JavaDoc SUPERSCRIPT_TOGGLE = "&X";
72
73   /**
74    * Turns subscript printing on or off
75    */

76   private static final String JavaDoc SUBSCRIPT_TOGGLE = "&Y";
77
78   /**
79    * Turns outline printing on or off (Macintosh only)
80    */

81   private static final String JavaDoc OUTLINE_TOGGLE = "&O";
82
83   /**
84    * Turns shadow printing on or off (Macintosh only)
85    */

86   private static final String JavaDoc SHADOW_TOGGLE = "&H";
87
88   /**
89    * Left-aligns the characters that follow
90    */

91   private static final String JavaDoc LEFT_ALIGN = "&L";
92
93   /**
94    * Centres the characters that follow
95    */

96   private static final String JavaDoc CENTRE = "&C";
97
98   /**
99    * Right-aligns the characters that follow
100    */

101   private static final String JavaDoc RIGHT_ALIGN = "&R";
102   
103   // Codes to insert specific data
104

105   /**
106    * Prints the page number
107    */

108   private static final String JavaDoc PAGENUM = "&P";
109
110   /**
111    * Prints the total number of pages in the document
112    */

113   private static final String JavaDoc TOTAL_PAGENUM = "&N";
114
115   /**
116    * Prints the current date
117    */

118   private static final String JavaDoc DATE = "&D";
119
120   /**
121    * Prints the current time
122    */

123   private static final String JavaDoc TIME = "&T";
124
125   /**
126    * Prints the name of the workbook
127    */

128   private static final String JavaDoc WORKBOOK_NAME = "&F";
129
130   /**
131    * Prints the name of the worksheet
132    */

133   private static final String JavaDoc WORKSHEET_NAME = "&A";
134
135   /**
136    * The contents - a simple wrapper around a string buffer
137    */

138   protected static class Contents
139   {
140     /**
141      * The buffer containing the header/footer string
142      */

143     private StringBuffer JavaDoc contents;
144
145     /**
146      * The constructor
147      */

148     protected Contents()
149     {
150       contents = new StringBuffer JavaDoc();
151     }
152
153     /**
154      * Constructor used when reading worksheets. The string contains all
155      * the formatting (but not alignment characters
156      *
157      * @param s the format string
158      */

159     protected Contents(String JavaDoc s)
160     {
161       contents = new StringBuffer JavaDoc(s);
162     }
163
164     /**
165      * Copy constructor
166      *
167      * @param copy the contents to copy
168      */

169     protected Contents(Contents copy)
170     {
171       contents = new StringBuffer JavaDoc(copy.getContents());
172     }
173
174     /**
175      * Retrieves a <code>String</code>ified
176      * version of this object
177      *
178      * @return the header string
179      */

180     protected String JavaDoc getContents()
181     {
182       return contents != null ? contents.toString() : "";
183     }
184
185     /**
186      * Internal method which appends the text to the string buffer
187      *
188      * @param txt
189      */

190     private void appendInternal(String JavaDoc txt)
191     {
192       if (contents == null)
193       {
194         contents = new StringBuffer JavaDoc();
195       }
196
197       contents.append(txt);
198     }
199
200     /**
201      * Internal method which appends the text to the string buffer
202      *
203      * @param ch
204      */

205     private void appendInternal(char ch)
206     {
207       if (contents == null)
208       {
209         contents = new StringBuffer JavaDoc();
210       }
211
212       contents.append(ch);
213     }
214
215     /**
216      * Appends the text to the string buffer
217      *
218      * @param txt
219      */

220     protected void append(String JavaDoc txt)
221     {
222       appendInternal(txt);
223     }
224   
225     /**
226      * Turns bold printing on or off. Bold printing
227      * is initially off. Text subsequently appended to
228      * this object will be bolded until this method is
229      * called again.
230      */

231     protected void toggleBold()
232     {
233      appendInternal(BOLD_TOGGLE);
234     }
235
236     /**
237      * Turns underline printing on or off. Underline printing
238      * is initially off. Text subsequently appended to
239      * this object will be underlined until this method is
240      * called again.
241      */

242     protected void toggleUnderline()
243     {
244       appendInternal(UNDERLINE_TOGGLE);
245     }
246
247     /**
248      * Turns italics printing on or off. Italics printing
249      * is initially off. Text subsequently appended to
250      * this object will be italicized until this method is
251      * called again.
252      */

253     protected void toggleItalics()
254     {
255       appendInternal(ITALICS_TOGGLE);
256     }
257
258     /**
259      * Turns strikethrough printing on or off. Strikethrough printing
260      * is initially off. Text subsequently appended to
261      * this object will be striked out until this method is
262      * called again.
263      */

264     protected void toggleStrikethrough()
265     {
266       appendInternal(STRIKETHROUGH_TOGGLE);
267     }
268
269     /**
270      * Turns double-underline printing on or off. Double-underline printing
271      * is initially off. Text subsequently appended to
272      * this object will be double-underlined until this method is
273      * called again.
274      */

275     protected void toggleDoubleUnderline()
276     {
277       appendInternal(DOUBLE_UNDERLINE_TOGGLE);
278     }
279
280     /**
281      * Turns superscript printing on or off. Superscript printing
282      * is initially off. Text subsequently appended to
283      * this object will be superscripted until this method is
284      * called again.
285      */

286     protected void toggleSuperScript()
287     {
288       appendInternal(SUPERSCRIPT_TOGGLE);
289     }
290
291     /**
292      * Turns subscript printing on or off. Subscript printing
293      * is initially off. Text subsequently appended to
294      * this object will be subscripted until this method is
295      * called again.
296      */

297     protected void toggleSubScript()
298     {
299       appendInternal(SUBSCRIPT_TOGGLE);
300     }
301     
302     /**
303      * Turns outline printing on or off (Macintosh only).
304      * Outline printing is initially off. Text subsequently appended
305      * to this object will be outlined until this method is
306      * called again.
307      */

308     protected void toggleOutline()
309     {
310      appendInternal(OUTLINE_TOGGLE);
311     }
312     
313     /**
314      * Turns shadow printing on or off (Macintosh only).
315      * Shadow printing is initially off. Text subsequently appended
316      * to this object will be shadowed until this method is
317      * called again.
318      */

319     protected void toggleShadow()
320     {
321       appendInternal(SHADOW_TOGGLE);
322     }
323     
324     /**
325      * Sets the font of text subsequently appended to this
326      * object.. Previously appended text is not affected.
327      * <p/>
328      * <strong>Note:</strong> no checking is performed to
329      * determine if fontName is a valid font.
330      *
331      * @param fontName name of the font to use
332      */

333     protected void setFontName(String JavaDoc fontName)
334     {
335       // Font name must be in quotations
336
appendInternal("&\"");
337       appendInternal(fontName);
338       appendInternal('\"');
339     }
340
341     /**
342      * Sets the font size of text subsequently appended to this
343      * object. Previously appended text is not affected.
344      * <p/>
345      * Valid point sizes are between 1 and 99 (inclusive). If
346      * size is outside this range, this method returns false
347      * and does not change font size. If size is within this
348      * range, the font size is changed and true is returned.
349      *
350      * @param size The size in points. Valid point sizes are
351      * between 1 and 99 (inclusive).
352      * @return true if the font size was changed, false if font
353      * size was not changed because 1 > size > 99.
354      */

355     protected boolean setFontSize(int size)
356     {
357       if (size < 1 || size > 99)
358       {
359         return false;
360       }
361     
362       // A two digit number should be used -- even if the
363
// leading number is just a zero.
364
String JavaDoc fontSize;
365       if (size < 10)
366       {
367       // single-digit -- make two digit
368
fontSize = "0" + size;
369       }
370       else
371       {
372         fontSize = Integer.toString(size);
373       }
374     
375       appendInternal('&');
376       appendInternal(fontSize);
377       return true;
378     }
379   
380     /**
381      * Appends the page number
382      */

383     protected void appendPageNumber()
384     {
385       appendInternal(PAGENUM);
386     }
387   
388     /**
389      * Appends the total number of pages
390      */

391     protected void appendTotalPages()
392     {
393       appendInternal(TOTAL_PAGENUM);
394     }
395   
396     /**
397      * Appends the current date
398      */

399     protected void appendDate()
400     {
401       appendInternal(DATE);
402     }
403   
404     /**
405      * Appends the current time
406      */

407     protected void appendTime()
408     {
409       appendInternal(TIME);
410     }
411   
412     /**
413      * Appends the workbook name
414      */

415     protected void appendWorkbookName()
416     {
417       appendInternal(WORKBOOK_NAME);
418     }
419     
420     /**
421      * Appends the worksheet name
422      */

423     protected void appendWorkSheetName()
424     {
425       appendInternal(WORKSHEET_NAME);
426     }
427
428     /**
429      * Clears the contents of this portion
430      */

431     protected void clear()
432     {
433       contents = null;
434     }
435
436     /**
437      * Queries if the contents are empty
438      *
439      * @return TRUE if the contents are empty, FALSE otherwise
440      */

441     protected boolean empty()
442     {
443       if (contents == null || contents.length() == 0)
444       {
445         return true;
446       }
447       else
448       {
449         return false;
450       }
451     }
452   }
453
454   /**
455    * The left aligned header/footer contents
456    */

457   private Contents left;
458
459   /**
460    * The right aligned header/footer contents
461    */

462   private Contents right;
463
464   /**
465    * The centrally aligned header/footer contents
466    */

467   private Contents centre;
468
469   /**
470    * Default constructor.
471    */

472   protected HeaderFooter()
473   {
474     left = createContents();
475     right = createContents();
476     centre = createContents();
477   }
478
479   /**
480    * Copy constructor
481    *
482    * @param c the item to copy
483    */

484   protected HeaderFooter(HeaderFooter hf)
485   {
486     left = createContents(hf.left);
487     right = createContents(hf.right);
488     centre = createContents(hf.centre);
489   }
490
491   /**
492    * Constructor used when reading workbooks to separate the left, right
493    * a central part of the strings into their constituent parts
494    */

495   protected HeaderFooter(String JavaDoc s)
496   {
497     if (s == null || s.length() == 0)
498     {
499       left = createContents();
500       right = createContents();
501       centre = createContents();
502       return;
503     }
504
505     int pos = 0;
506     int leftPos = s.indexOf(LEFT_ALIGN);
507     int rightPos = s.indexOf(RIGHT_ALIGN);
508     int centrePos = s.indexOf(CENTRE);
509
510     // Do the left position string
511
if (pos == leftPos)
512     {
513       if (centrePos != -1)
514       {
515         left = createContents(s.substring(pos + 2, centrePos));
516         pos = centrePos;
517       }
518       else if (rightPos != -1 )
519       {
520         left = createContents(s.substring(pos + 2, rightPos));
521         pos = rightPos;
522       }
523       else
524       {
525         left = createContents(s.substring(pos + 2));
526         pos = s.length();
527       }
528     }
529
530     // Do the centrally positioned part of the string. This is the default
531
// if no alignment string is specified
532
if (pos == centrePos ||
533         (leftPos == -1 && rightPos == -1 && centrePos == -1))
534     {
535       if (rightPos != -1)
536       {
537         centre = createContents(s.substring(pos + 2, rightPos));
538         pos = rightPos;
539       }
540       else
541       {
542         centre = createContents(s.substring(pos + 2));
543         pos = s.length();
544       }
545     }
546
547     // Do the right positioned part of the string
548
if (pos == rightPos)
549     {
550       right = createContents(s.substring(pos + 2));
551       pos = s.length();
552     }
553
554     if (left == null)
555     {
556       left = createContents();
557     }
558
559     if (centre == null)
560     {
561       centre = createContents();
562     }
563
564     if (right == null)
565     {
566       right = createContents();
567     }
568   }
569
570   /**
571    * Retrieves a <code>String</code>ified
572    * version of this object
573    *
574    * @return the header string
575    */

576   public String JavaDoc toString()
577   {
578     StringBuffer JavaDoc hf = new StringBuffer JavaDoc();
579     if (!left.empty())
580     {
581       hf.append(LEFT_ALIGN);
582       hf.append(left.getContents());
583     }
584
585     if (!centre.empty())
586     {
587       hf.append(CENTRE);
588       hf.append(centre.getContents());
589     }
590
591     if (!right.empty())
592     {
593       hf.append(RIGHT_ALIGN);
594       hf.append(right.getContents());
595     }
596
597     return hf.toString();
598   }
599
600   /**
601    * Accessor for the contents which appear on the right hand side of the page
602    *
603    * @return the right aligned contents
604    */

605   protected Contents getRightText()
606   {
607     return right;
608   }
609
610   /**
611    * Accessor for the contents which in the centre of the page
612    *
613    * @return the centrally aligned contents
614    */

615   protected Contents getCentreText()
616   {
617     return centre;
618   }
619
620   /**
621    * Accessor for the contents which appear on the left hand side of the page
622    *
623    * @return the left aligned contents
624    */

625   protected Contents getLeftText()
626   {
627     return left;
628   }
629
630   /**
631    * Clears the contents of the header/footer
632    */

633   protected void clear()
634   {
635     left.clear();
636     right.clear();
637     centre.clear();
638   }
639
640   /**
641    * Creates internal class of the appropriate type
642    */

643   protected abstract Contents createContents();
644
645   /**
646    * Creates internal class of the appropriate type
647    */

648   protected abstract Contents createContents(String JavaDoc s);
649
650   /**
651    * Creates internal class of the appropriate type
652    */

653   protected abstract Contents createContents(Contents c);
654 }
655
Popular Tags