1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   package org.apache.poi.hssf.usermodel;
57   
58   import org.apache.poi.hssf.record.FooterRecord;
59   
60   /**
61    * Class to read and manipulate the footer.
62    * <P>
63    * The footer works by having a left, center, and right side.  The total cannot
64    * be more that 255 bytes long.  One uses this class by getting the HSSFFooter
65    * from HSSFSheet and then getting or setting the left, center, and right side.
66    * For special things (such as page numbers and date), one can use a the methods
67    * that return the characters used to represent these.  One can also change the
68    * fonts by using similar methods.
69    * <P>
70    * @author Shawn Laubach (slaubach at apache dot org)
71    */
72   public class HSSFFooter extends Object {
73   
74     FooterRecord footerRecord;
75     String left;
76     String center;
77     String right;
78   
79     /**
80      * Constructor.  Creates a new footer interface from a footer record
81      * @param footerRecord Footer record to create the footer with
82      */
83     protected HSSFFooter(FooterRecord footerRecord) {
84       this.footerRecord = footerRecord;
85       String foot = footerRecord.getFooter();
86       while (foot != null && foot.length() > 1) {
87   	int pos = foot.length();
88   	switch (foot.substring(1, 2).charAt(0)) {
89   	case 'L' :
90    	    if (foot.indexOf("&C") >= 0) {
91   		pos = Math.min(pos, foot.indexOf("&C"));
92   	    } 
93   	    if (foot.indexOf("&R") >= 0) {
94   		pos = Math.min(pos, foot.indexOf("&R"));
95   	    } 
96   	    left = foot.substring(2, pos);
97   	    foot = foot.substring(pos);
98   	    break;
99   	case 'C' : 
100  	    if (foot.indexOf("&L") >= 0) {
101  		pos = Math.min(pos, foot.indexOf("&L"));
102  	    } 
103  	    if (foot.indexOf("&R") >= 0) {
104  		pos = Math.min(pos, foot.indexOf("&R"));
105  	    } 
106  	    center = foot.substring(2, pos);
107  	    foot = foot.substring(pos);
108  	    break;
109  	case 'R' : 
110   	    if (foot.indexOf("&C") >= 0) {
111  		pos = Math.min(pos, foot.indexOf("&C"));
112  	    } 
113  	    if (foot.indexOf("&L") >= 0) {
114  		pos = Math.min(pos, foot.indexOf("&L"));
115  	    } 
116  	    right = foot.substring(2, pos);
117  	    foot = foot.substring(pos);
118  	    break;
119  	default : foot = null;
120  	}
121      }
122    }
123  
124    /**
125     * Get the left side of the footer.
126     * @return The string representing the left side.
127     */
128    public String getLeft() {
129      return left;
130    }
131  
132    /**
133     * Sets the left string.
134     * @newLeft The string to set as the left side.
135     */
136    public void setLeft(String newLeft) {
137      left = newLeft;
138      createFooterString();
139    }
140  
141    /**
142     * Get the center of the footer.
143     * @return The string representing the center.
144     */
145    public String getCenter() {
146      return center;
147    }
148  
149    /**
150     * Sets the center string.
151     * @newLeft The string to set as the center.
152     */
153    public void setCenter(String newCenter) {
154      center = newCenter;
155      createFooterString();
156    }
157  
158    /**
159     * Get the right side of the footer.
160     * @return The string representing the right side.
161     */
162    public String getRight() {
163      return right;
164    }
165  
166    /**
167     * Sets the right string.
168     * @newLeft The string to set as the right side.
169     */
170    public void setRight(String newRight) {
171      right = newRight;
172      createFooterString();
173    }
174  
175    /**
176     * Creates the complete footer string based on the left, center, and middle
177     * strings.
178     */
179    private void createFooterString() {
180      footerRecord.setFooter(
181      "&C" + (center == null ? "" : center) +
182      "&L" + (left == null ? "" : left) +
183      "&R" + (right == null ? "" : right));
184      footerRecord.setFooterLength((byte)footerRecord.getFooter().length());
185    }
186  
187    /**
188     * Returns the string that represents the change in font size.
189     * @param size the new font size
190     * @return The special string to represent a new font size
191     */
192    public static String fontSize(short size) {
193      return "&" + size;
194    }
195  
196    /**
197     * Returns the string that represents the change in font.
198     * @param font the new font
199     * @param style the fonts style
200     * @return The special string to represent a new font size
201     */
202    public static String font(String font, String style) {
203      return "&\"" + font + "," + style + "\"";
204    }
205  
206    /**
207     * Returns the string representing the current page number
208     * @return The special string for page number
209     */
210    public static String page() {
211      return "&P";
212    }
213  
214    /**
215     * Returns the string representing the number of pages.
216     * @return The special string for the number of pages
217     */
218    public static String numPages() {
219      return "&N";
220    }
221  
222    /**
223     * Returns the string representing the current date
224     * @return The special string for the date
225     */
226    public static String date() {
227      return "&D";
228    }
229  
230    /**
231     * Returns the string representing the current time
232     * @return The special string for the time
233     */
234    public static String time() {
235      return "&T";
236    }
237  
238    /**
239     * Returns the string representing the current file name
240     * @return The special string for the file name
241     */
242    public static String file() {
243      return "&F";
244    }
245  
246    /**
247     * Returns the string representing the current tab (sheet) name
248     * @return The special string for tab name
249     */
250    public static String tab() {
251      return "&A";
252    }
253  }
254  
255