KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > HSSFFooter


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17
18 package org.apache.poi.hssf.usermodel;
19
20 import org.apache.poi.hssf.record.FooterRecord;
21
22 /**
23  * Class to read and manipulate the footer.
24  * <P>
25  * The footer works by having a left, center, and right side. The total cannot
26  * be more that 255 bytes long. One uses this class by getting the HSSFFooter
27  * from HSSFSheet and then getting or setting the left, center, and right side.
28  * For special things (such as page numbers and date), one can use a the methods
29  * that return the characters used to represent these. One can also change the
30  * fonts by using similar methods.
31  * <P>
32  * @author Shawn Laubach (slaubach at apache dot org)
33  */

34 public class HSSFFooter extends Object JavaDoc {
35
36   FooterRecord footerRecord;
37   String JavaDoc left;
38   String JavaDoc center;
39   String JavaDoc right;
40
41   /**
42    * Constructor. Creates a new footer interface from a footer record
43    * @param footerRecord Footer record to create the footer with
44    */

45   protected HSSFFooter(FooterRecord footerRecord) {
46     this.footerRecord = footerRecord;
47     String JavaDoc foot = footerRecord.getFooter();
48     while (foot != null && foot.length() > 1) {
49     int pos = foot.length();
50     switch (foot.substring(1, 2).charAt(0)) {
51     case 'L' :
52         if (foot.indexOf("&C") >= 0) {
53         pos = Math.min(pos, foot.indexOf("&C"));
54         }
55         if (foot.indexOf("&R") >= 0) {
56         pos = Math.min(pos, foot.indexOf("&R"));
57         }
58         left = foot.substring(2, pos);
59         foot = foot.substring(pos);
60         break;
61     case 'C' :
62         if (foot.indexOf("&L") >= 0) {
63         pos = Math.min(pos, foot.indexOf("&L"));
64         }
65         if (foot.indexOf("&R") >= 0) {
66         pos = Math.min(pos, foot.indexOf("&R"));
67         }
68         center = foot.substring(2, pos);
69         foot = foot.substring(pos);
70         break;
71     case 'R' :
72         if (foot.indexOf("&C") >= 0) {
73         pos = Math.min(pos, foot.indexOf("&C"));
74         }
75         if (foot.indexOf("&L") >= 0) {
76         pos = Math.min(pos, foot.indexOf("&L"));
77         }
78         right = foot.substring(2, pos);
79         foot = foot.substring(pos);
80         break;
81     default : foot = null;
82     }
83     }
84   }
85
86   /**
87    * Get the left side of the footer.
88    * @return The string representing the left side.
89    */

90   public String JavaDoc getLeft() {
91     return left;
92   }
93
94   /**
95    * Sets the left string.
96    * @param newLeft The string to set as the left side.
97    */

98   public void setLeft(String JavaDoc newLeft) {
99     left = newLeft;
100     createFooterString();
101   }
102
103   /**
104    * Get the center of the footer.
105    * @return The string representing the center.
106    */

107   public String JavaDoc getCenter() {
108     return center;
109   }
110
111   /**
112    * Sets the center string.
113    * @param newCenter The string to set as the center.
114    */

115   public void setCenter(String JavaDoc newCenter) {
116     center = newCenter;
117     createFooterString();
118   }
119
120   /**
121    * Get the right side of the footer.
122    * @return The string representing the right side.
123    */

124   public String JavaDoc getRight() {
125     return right;
126   }
127
128   /**
129    * Sets the right string.
130    * @param newRight The string to set as the right side.
131    */

132   public void setRight(String JavaDoc newRight) {
133     right = newRight;
134     createFooterString();
135   }
136
137   /**
138    * Creates the complete footer string based on the left, center, and middle
139    * strings.
140    */

141   private void createFooterString() {
142     footerRecord.setFooter(
143     "&C" + (center == null ? "" : center) +
144     "&L" + (left == null ? "" : left) +
145     "&R" + (right == null ? "" : right));
146     footerRecord.setFooterLength((byte)footerRecord.getFooter().length());
147   }
148
149   /**
150    * Returns the string that represents the change in font size.
151    * @param size the new font size
152    * @return The special string to represent a new font size
153    */

154   public static String JavaDoc fontSize(short size) {
155     return "&" + size;
156   }
157
158   /**
159    * Returns the string that represents the change in font.
160    * @param font the new font
161    * @param style the fonts style
162    * @return The special string to represent a new font size
163    */

164   public static String JavaDoc font(String JavaDoc font, String JavaDoc style) {
165     return "&\"" + font + "," + style + "\"";
166   }
167
168   /**
169    * Returns the string representing the current page number
170    * @return The special string for page number
171    */

172   public static String JavaDoc page() {
173     return "&P";
174   }
175
176   /**
177    * Returns the string representing the number of pages.
178    * @return The special string for the number of pages
179    */

180   public static String JavaDoc numPages() {
181     return "&N";
182   }
183
184   /**
185    * Returns the string representing the current date
186    * @return The special string for the date
187    */

188   public static String JavaDoc date() {
189     return "&D";
190   }
191
192   /**
193    * Returns the string representing the current time
194    * @return The special string for the time
195    */

196   public static String JavaDoc time() {
197     return "&T";
198   }
199
200   /**
201    * Returns the string representing the current file name
202    * @return The special string for the file name
203    */

204   public static String JavaDoc file() {
205     return "&F";
206   }
207
208   /**
209    * Returns the string representing the current tab (sheet) name
210    * @return The special string for tab name
211    */

212   public static String JavaDoc tab() {
213     return "&A";
214   }
215 }
216
217
Popular Tags