KickJava   Java API By Example, From Geeks To Geeks.

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


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

34 public class HSSFHeader
35 {
36
37     HeaderRecord headerRecord;
38     String JavaDoc left;
39     String JavaDoc center;
40     String JavaDoc right;
41
42     /**
43      * Constructor. Creates a new header interface from a header record
44      *
45      * @param headerRecord Header record to create the header with
46      */

47     protected HSSFHeader( HeaderRecord headerRecord )
48     {
49         this.headerRecord = headerRecord;
50         String JavaDoc head = headerRecord.getHeader();
51         while ( head != null && head.length() > 1 )
52         {
53             int pos = head.length();
54             switch ( head.substring( 1, 2 ).charAt( 0 ) )
55             {
56                 case 'L':
57                     if ( head.indexOf( "&C" ) >= 0 )
58                     {
59                         pos = Math.min( pos, head.indexOf( "&C" ) );
60                     }
61                     if ( head.indexOf( "&R" ) >= 0 )
62                     {
63                         pos = Math.min( pos, head.indexOf( "&R" ) );
64                     }
65                     left = head.substring( 2, pos );
66                     head = head.substring( pos );
67                     break;
68                 case 'C':
69                     if ( head.indexOf( "&L" ) >= 0 )
70                     {
71                         pos = Math.min( pos, head.indexOf( "&L" ) );
72                     }
73                     if ( head.indexOf( "&R" ) >= 0 )
74                     {
75                         pos = Math.min( pos, head.indexOf( "&R" ) );
76                     }
77                     center = head.substring( 2, pos );
78                     head = head.substring( pos );
79                     break;
80                 case 'R':
81                     if ( head.indexOf( "&C" ) >= 0 )
82                     {
83                         pos = Math.min( pos, head.indexOf( "&C" ) );
84                     }
85                     if ( head.indexOf( "&L" ) >= 0 )
86                     {
87                         pos = Math.min( pos, head.indexOf( "&L" ) );
88                     }
89                     right = head.substring( 2, pos );
90                     head = head.substring( pos );
91                     break;
92                 default :
93                     head = null;
94             }
95         }
96     }
97
98     /**
99      * Get the left side of the header.
100      *
101      * @return The string representing the left side.
102      */

103     public String JavaDoc getLeft()
104     {
105         return left;
106     }
107
108     /**
109      * Sets the left string.
110      *
111      * @param newLeft The string to set as the left side.
112      */

113     public void setLeft( String JavaDoc newLeft )
114     {
115         left = newLeft;
116         createHeaderString();
117     }
118
119     /**
120      * Get the center of the header.
121      *
122      * @return The string representing the center.
123      */

124     public String JavaDoc getCenter()
125     {
126         return center;
127     }
128
129     /**
130      * Sets the center string.
131      *
132      * @param newCenter The string to set as the center.
133      */

134     public void setCenter( String JavaDoc newCenter )
135     {
136         center = newCenter;
137         createHeaderString();
138     }
139
140     /**
141      * Get the right side of the header.
142      *
143      * @return The string representing the right side.
144      */

145     public String JavaDoc getRight()
146     {
147         return right;
148     }
149
150     /**
151      * Sets the right string.
152      *
153      * @param newRight The string to set as the right side.
154      */

155     public void setRight( String JavaDoc newRight )
156     {
157         right = newRight;
158         createHeaderString();
159     }
160
161     /**
162      * Creates the complete header string based on the left, center, and middle
163      * strings.
164      */

165     private void createHeaderString()
166     {
167         headerRecord.setHeader( "&C" + ( center == null ? "" : center ) +
168                 "&L" + ( left == null ? "" : left ) +
169                 "&R" + ( right == null ? "" : right ) );
170         headerRecord.setHeaderLength( (byte) headerRecord.getHeader().length() );
171     }
172
173     /**
174      * Returns the string that represents the change in font size.
175      *
176      * @param size the new font size
177      * @return The special string to represent a new font size
178      */

179     public static String JavaDoc fontSize( short size )
180     {
181         return "&" + size;
182     }
183
184     /**
185      * Returns the string that represents the change in font.
186      *
187      * @param font the new font
188      * @param style the fonts style
189      * @return The special string to represent a new font size
190      */

191     public static String JavaDoc font( String JavaDoc font, String JavaDoc style )
192     {
193         return "&\"" + font + "," + style + "\"";
194     }
195
196     /**
197      * Returns the string representing the current page number
198      *
199      * @return The special string for page number
200      */

201     public static String JavaDoc page()
202     {
203         return "&P";
204     }
205
206     /**
207      * Returns the string representing the number of pages.
208      *
209      * @return The special string for the number of pages
210      */

211     public static String JavaDoc numPages()
212     {
213         return "&N";
214     }
215
216     /**
217      * Returns the string representing the current date
218      *
219      * @return The special string for the date
220      */

221     public static String JavaDoc date()
222     {
223         return "&D";
224     }
225
226     /**
227      * Returns the string representing the current time
228      *
229      * @return The special string for the time
230      */

231     public static String JavaDoc time()
232     {
233         return "&T";
234     }
235
236     /**
237      * Returns the string representing the current file name
238      *
239      * @return The special string for the file name
240      */

241     public static String JavaDoc file()
242     {
243         return "&F";
244     }
245
246     /**
247      * Returns the string representing the current tab (sheet) name
248      *
249      * @return The special string for tab name
250      */

251     public static String JavaDoc tab()
252     {
253         return "&A";
254     }
255 }
256
257
Popular Tags