001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel;
019:
020: import org.apache.poi.hssf.record.FooterRecord;
021:
022: /**
023: * Class to read and manipulate the footer.
024: * <P>
025: * The footer works by having a left, center, and right side. The total cannot
026: * be more that 255 bytes long. One uses this class by getting the HSSFFooter
027: * from HSSFSheet and then getting or setting the left, center, and right side.
028: * For special things (such as page numbers and date), one can use a the methods
029: * that return the characters used to represent these. One can also change the
030: * fonts by using similar methods.
031: * <P>
032: * @author Shawn Laubach (slaubach at apache dot org)
033: */
034: public class HSSFFooter extends Object {
035:
036: FooterRecord footerRecord;
037: String left;
038: String center;
039: String right;
040:
041: /**
042: * Constructor. Creates a new footer interface from a footer record
043: * @param footerRecord Footer record to create the footer with
044: */
045: protected HSSFFooter(FooterRecord footerRecord) {
046: this .footerRecord = footerRecord;
047: String foot = footerRecord.getFooter();
048: while (foot != null && foot.length() > 1) {
049: int pos = foot.length();
050: switch (foot.substring(1, 2).charAt(0)) {
051: case 'L':
052: if (foot.indexOf("&C") >= 0) {
053: pos = Math.min(pos, foot.indexOf("&C"));
054: }
055: if (foot.indexOf("&R") >= 0) {
056: pos = Math.min(pos, foot.indexOf("&R"));
057: }
058: left = foot.substring(2, pos);
059: foot = foot.substring(pos);
060: break;
061: case 'C':
062: if (foot.indexOf("&L") >= 0) {
063: pos = Math.min(pos, foot.indexOf("&L"));
064: }
065: if (foot.indexOf("&R") >= 0) {
066: pos = Math.min(pos, foot.indexOf("&R"));
067: }
068: center = foot.substring(2, pos);
069: foot = foot.substring(pos);
070: break;
071: case 'R':
072: if (foot.indexOf("&C") >= 0) {
073: pos = Math.min(pos, foot.indexOf("&C"));
074: }
075: if (foot.indexOf("&L") >= 0) {
076: pos = Math.min(pos, foot.indexOf("&L"));
077: }
078: right = foot.substring(2, pos);
079: foot = foot.substring(pos);
080: break;
081: default:
082: foot = null;
083: }
084: }
085: }
086:
087: /**
088: * Get the left side of the footer.
089: * @return The string representing the left side.
090: */
091: public String getLeft() {
092: return left;
093: }
094:
095: /**
096: * Sets the left string.
097: * @param newLeft The string to set as the left side.
098: */
099: public void setLeft(String newLeft) {
100: left = newLeft;
101: createFooterString();
102: }
103:
104: /**
105: * Get the center of the footer.
106: * @return The string representing the center.
107: */
108: public String getCenter() {
109: return center;
110: }
111:
112: /**
113: * Sets the center string.
114: * @param newCenter The string to set as the center.
115: */
116: public void setCenter(String newCenter) {
117: center = newCenter;
118: createFooterString();
119: }
120:
121: /**
122: * Get the right side of the footer.
123: * @return The string representing the right side.
124: */
125: public String getRight() {
126: return right;
127: }
128:
129: /**
130: * Sets the right string.
131: * @param newRight The string to set as the right side.
132: */
133: public void setRight(String newRight) {
134: right = newRight;
135: createFooterString();
136: }
137:
138: /**
139: * Creates the complete footer string based on the left, center, and middle
140: * strings.
141: */
142: private void createFooterString() {
143: footerRecord.setFooter("&C" + (center == null ? "" : center)
144: + "&L" + (left == null ? "" : left) + "&R"
145: + (right == null ? "" : right));
146: footerRecord.setFooterLength((byte) footerRecord.getFooter()
147: .length());
148: }
149:
150: /**
151: * Returns the string that represents the change in font size.
152: * @param size the new font size
153: * @return The special string to represent a new font size
154: */
155: public static String fontSize(short size) {
156: return "&" + size;
157: }
158:
159: /**
160: * Returns the string that represents the change in font.
161: * @param font the new font
162: * @param style the fonts style
163: * @return The special string to represent a new font size
164: */
165: public static String font(String font, String style) {
166: return "&\"" + font + "," + style + "\"";
167: }
168:
169: /**
170: * Returns the string representing the current page number
171: * @return The special string for page number
172: */
173: public static String page() {
174: return "&P";
175: }
176:
177: /**
178: * Returns the string representing the number of pages.
179: * @return The special string for the number of pages
180: */
181: public static String numPages() {
182: return "&N";
183: }
184:
185: /**
186: * Returns the string representing the current date
187: * @return The special string for the date
188: */
189: public static String date() {
190: return "&D";
191: }
192:
193: /**
194: * Returns the string representing the current time
195: * @return The special string for the time
196: */
197: public static String time() {
198: return "&T";
199: }
200:
201: /**
202: * Returns the string representing the current file name
203: * @return The special string for the file name
204: */
205: public static String file() {
206: return "&F";
207: }
208:
209: /**
210: * Returns the string representing the current tab (sheet) name
211: * @return The special string for tab name
212: */
213: public static String tab() {
214: return "&A";
215: }
216:
217: /**
218: * Returns the string representing the start underline
219: *
220: * @return The special string for start underline
221: */
222: public static String startUnderline() {
223: return "&U";
224: }
225:
226: /**
227: * Returns the string representing the end underline
228: *
229: * @return The special string for end underline
230: */
231: public static String endUnderline() {
232: return "&U";
233: }
234:
235: /**
236: * Returns the string representing the start double underline
237: *
238: * @return The special string for start double underline
239: */
240: public static String startDoubleUnderline() {
241: return "&E";
242: }
243:
244: /**
245: * Returns the string representing the end double underline
246: *
247: * @return The special string for end double underline
248: */
249: public static String endDoubleUnderline() {
250: return "&E";
251: }
252: }
|