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.HeaderRecord;
021:
022: /**
023: * Class to read and manipulate the header.
024: * <P>
025: * The header 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 HSSFHeader
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: *
033: * @author Shawn Laubach (slaubach at apache dot org)
034: */
035: public class HSSFHeader {
036:
037: HeaderRecord headerRecord;
038: String left;
039: String center;
040: String right;
041:
042: /**
043: * Constructor. Creates a new header interface from a header record
044: *
045: * @param headerRecord Header record to create the header with
046: */
047: protected HSSFHeader(HeaderRecord headerRecord) {
048: this .headerRecord = headerRecord;
049: String head = headerRecord.getHeader();
050: while (head != null && head.length() > 1) {
051: int pos = head.length();
052: switch (head.substring(1, 2).charAt(0)) {
053: case 'L':
054: if (head.indexOf("&C") >= 0) {
055: pos = Math.min(pos, head.indexOf("&C"));
056: }
057: if (head.indexOf("&R") >= 0) {
058: pos = Math.min(pos, head.indexOf("&R"));
059: }
060: left = head.substring(2, pos);
061: head = head.substring(pos);
062: break;
063: case 'C':
064: if (head.indexOf("&L") >= 0) {
065: pos = Math.min(pos, head.indexOf("&L"));
066: }
067: if (head.indexOf("&R") >= 0) {
068: pos = Math.min(pos, head.indexOf("&R"));
069: }
070: center = head.substring(2, pos);
071: head = head.substring(pos);
072: break;
073: case 'R':
074: if (head.indexOf("&C") >= 0) {
075: pos = Math.min(pos, head.indexOf("&C"));
076: }
077: if (head.indexOf("&L") >= 0) {
078: pos = Math.min(pos, head.indexOf("&L"));
079: }
080: right = head.substring(2, pos);
081: head = head.substring(pos);
082: break;
083: default:
084: head = null;
085: }
086: }
087: }
088:
089: /**
090: * Get the left side of the header.
091: *
092: * @return The string representing the left side.
093: */
094: public String getLeft() {
095: return left;
096: }
097:
098: /**
099: * Sets the left string.
100: *
101: * @param newLeft The string to set as the left side.
102: */
103: public void setLeft(String newLeft) {
104: left = newLeft;
105: createHeaderString();
106: }
107:
108: /**
109: * Get the center of the header.
110: *
111: * @return The string representing the center.
112: */
113: public String getCenter() {
114: return center;
115: }
116:
117: /**
118: * Sets the center string.
119: *
120: * @param newCenter The string to set as the center.
121: */
122: public void setCenter(String newCenter) {
123: center = newCenter;
124: createHeaderString();
125: }
126:
127: /**
128: * Get the right side of the header.
129: *
130: * @return The string representing the right side.
131: */
132: public String getRight() {
133: return right;
134: }
135:
136: /**
137: * Sets the right string.
138: *
139: * @param newRight The string to set as the right side.
140: */
141: public void setRight(String newRight) {
142: right = newRight;
143: createHeaderString();
144: }
145:
146: /**
147: * Creates the complete header string based on the left, center, and middle
148: * strings.
149: */
150: private void createHeaderString() {
151: headerRecord.setHeader("&C" + (center == null ? "" : center)
152: + "&L" + (left == null ? "" : left) + "&R"
153: + (right == null ? "" : right));
154: headerRecord.setHeaderLength((byte) headerRecord.getHeader()
155: .length());
156: }
157:
158: /**
159: * Returns the string that represents the change in font size.
160: *
161: * @param size the new font size
162: * @return The special string to represent a new font size
163: */
164: public static String fontSize(short size) {
165: return "&" + size;
166: }
167:
168: /**
169: * Returns the string that represents the change in font.
170: *
171: * @param font the new font
172: * @param style the fonts style
173: * @return The special string to represent a new font size
174: */
175: public static String font(String font, String style) {
176: return "&\"" + font + "," + style + "\"";
177: }
178:
179: /**
180: * Returns the string representing the current page number
181: *
182: * @return The special string for page number
183: */
184: public static String page() {
185: return "&P";
186: }
187:
188: /**
189: * Returns the string representing the number of pages.
190: *
191: * @return The special string for the number of pages
192: */
193: public static String numPages() {
194: return "&N";
195: }
196:
197: /**
198: * Returns the string representing the current date
199: *
200: * @return The special string for the date
201: */
202: public static String date() {
203: return "&D";
204: }
205:
206: /**
207: * Returns the string representing the current time
208: *
209: * @return The special string for the time
210: */
211: public static String time() {
212: return "&T";
213: }
214:
215: /**
216: * Returns the string representing the current file name
217: *
218: * @return The special string for the file name
219: */
220: public static String file() {
221: return "&F";
222: }
223:
224: /**
225: * Returns the string representing the current tab (sheet) name
226: *
227: * @return The special string for tab name
228: */
229: public static String tab() {
230: return "&A";
231: }
232:
233: /**
234: * Returns the string representing the start underline
235: *
236: * @return The special string for start underline
237: */
238: public static String startUnderline() {
239: return "&U";
240: }
241:
242: /**
243: * Returns the string representing the end underline
244: *
245: * @return The special string for end underline
246: */
247: public static String endUnderline() {
248: return "&U";
249: }
250:
251: /**
252: * Returns the string representing the start double underline
253: *
254: * @return The special string for start double underline
255: */
256: public static String startDoubleUnderline() {
257: return "&E";
258: }
259:
260: /**
261: * Returns the string representing the end double underline
262: *
263: * @return The special string for end double underline
264: */
265: public static String endDoubleUnderline() {
266: return "&E";
267: }
268: }
|