KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > elementprocessor > impl > poi > hssf > elements > Sheet


1 /*
2  * Copyright 1999-2004 The 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.cocoon.components.elementprocessor.impl.poi.hssf.elements;
18
19 import java.io.IOException JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.logger.AbstractLogEnabled;
25
26 import org.apache.poi.hssf.usermodel.HSSFCell;
27 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
28 import org.apache.poi.hssf.usermodel.HSSFFont;
29 import org.apache.poi.hssf.usermodel.HSSFFooter;
30 import org.apache.poi.hssf.usermodel.HSSFHeader;
31 import org.apache.poi.hssf.usermodel.HSSFRow;
32 import org.apache.poi.hssf.usermodel.HSSFSheet;
33 import org.apache.poi.hssf.util.HSSFColor;
34 import org.apache.poi.hssf.util.Region;
35
36 /**
37  * internal representation of a Sheet
38  *
39  * @author Marc Johnson (marc_johnson27591@hotmail.com)
40  * @author Andrew C. Oliver (acoliver2@users.sourceforge.net)
41  * @version CVS $Id: Sheet.java 37191 2004-08-30 10:15:06Z antonio $
42  */

43
44 // package scope
45

46 class Sheet extends AbstractLogEnabled {
47
48     private HSSFSheet _sheet;
49     private String JavaDoc _name;
50     private int _physical_index;
51     private Workbook _workbook;
52
53     // keys are Shorts (row numbers), values are Row instances
54
private Map JavaDoc _rows;
55
56     private Map JavaDoc regions;
57
58     //optimization constant
59
private final static int ROWS_CAPACITY = 200;
60
61     //optimization constant
62
private final static int REGION_CAPACITY = 20;
63
64     /**
65      * Constructor Sheet
66      * @param workbook
67      */

68     Sheet(final Workbook workbook) {
69         _workbook = workbook;
70         _name = _workbook.getNextName();
71         _sheet = _workbook.createSheet(_name);
72         _physical_index = _workbook.getPhysicalIndex(_name);
73         _rows = new HashMap JavaDoc(ROWS_CAPACITY);
74         regions = new HashMap JavaDoc(REGION_CAPACITY);
75     }
76
77     /**
78      * renameSheet
79      * @param new_name
80      */

81     void renameSheet(final String JavaDoc new_name) {
82         if (!_name.equals(new_name)) {
83             _workbook.renameSheet(_physical_index, new_name);
84             _name = new_name;
85         }
86     }
87
88     /**
89      * set a column's width
90      * @param number the column number
91      * @param points
92      * @exception IOException if any arguments are illegal
93      */

94     void setColumnWidth(final int number, final double points)
95         throws IOException JavaDoc {
96         if (number < 0 || number > Short.MAX_VALUE) {
97             throw new IOException JavaDoc("column number " + number + " is too large");
98         }
99         if (!isValidColumnPoints(points)) {
100             throw new IOException JavaDoc("points " + points + " is out of range");
101         }
102         _sheet.setColumnWidth((short)number, (short) ((points * 48) + .5));
103     }
104
105     /**
106      * get the column width of a specified column
107      * @param number the column number
108      * @return column width in characters
109      */

110     short getColumnWidth(short number) {
111         return _sheet.getColumnWidth(number);
112     }
113
114     /**
115      * set default column width
116      * @param width width, in points
117      * @exception IOException
118      */

119     void setDefaultColumnWidth(double width) throws IOException JavaDoc {
120         if (width < 0 || (width >= (4.8 * (0.5 + Short.MAX_VALUE)))) {
121             throw new IOException JavaDoc("Invalid width (" + width + ")");
122         } // 12 is being used as a "guessed" points for the font
123
_sheet.setDefaultColumnWidth((short) ((width / 4.8) + 0.5));
124     }
125
126     /**
127      * @return default column width (in 1/256ths of a character width)
128      */

129     short getDefaultColumnWidth() {
130         return _sheet.getDefaultColumnWidth();
131     }
132
133     /**
134      * set default row height
135      * @param height height, in points
136      * @exception IOException
137      */

138     void setDefaultRowHeight(double height) throws IOException JavaDoc {
139         if (!isValidPoints(height)) {
140             throw new IOException JavaDoc("Invalid height (" + height + ")");
141         }
142         _sheet.setDefaultRowHeight((short) ((height * 20) + .5));
143     }
144
145     /**
146      * @return default row height
147      */

148     short getDefaultRowHeight() {
149         return _sheet.getDefaultRowHeight();
150     }
151
152     /**
153      * @return name
154      */

155     String JavaDoc getName() {
156         return _name;
157     }
158
159     /**
160      * @return index
161      */

162     int getIndex() {
163         return _physical_index;
164     }
165
166     /**
167      * get a specified row
168      * @param rowNo the row number
169      * @return a Row object
170      * @exception IOException if rowNo is out of range
171      */

172     Row getRow(int rowNo) throws IOException JavaDoc {
173         if (rowNo < 0) {
174             throw new IOException JavaDoc("Illegal row number: " + rowNo);
175         }
176         Short JavaDoc key = new Short JavaDoc((short)rowNo);
177         Object JavaDoc o = _rows.get(key);
178         Row rval = null;
179
180         if (o == null) {
181             rval = createRow(rowNo);
182             _rows.put(key, rval);
183         } else {
184             rval = (Row)o;
185         }
186         return rval;
187     }
188
189     HSSFCellStyle addStyleRegion(Region region) {
190         HSSFCellStyle style = _workbook.createStyle();
191         regions.put(region, style);
192         return style;
193     }
194
195     /**
196      * returns the HSSFCellStyle for a cell if defined by region if there is
197      * not a definition it returns null. If you don't expect that then your
198      * code dies a horrible death.
199      * @return HSSFCellStyle
200      */

201     HSSFCellStyle getCellStyleForRegion(int row, short col) {
202         Iterator JavaDoc iregions = regions.keySet().iterator();
203         while (iregions.hasNext()) {
204             Region region = ((Region)iregions.next());
205             if (region.contains(row, col)) {
206                 return (HSSFCellStyle)regions.get(region);
207             }
208         }
209         return null;
210     }
211
212     private Row createRow(final int rowNo) {
213         return new Row(_sheet.createRow(rowNo), this);
214     }
215
216     private boolean isValidPoints(double points) {
217         return (points >= 0 && points <= ((Short.MAX_VALUE + 0.5) / 20));
218     }
219
220     private boolean isValidColumnPoints(double points) {
221         return (points >= 0 && points <= ((Short.MAX_VALUE + 0.5) / 48));
222     }
223
224     /*
225      * this method doesn't appear to be used private boolean
226      * isValidCharacters(double characters) { return ((characters >= 0) &&
227      * (characters <= ((Short.MAX_VALUE + 0.5) / 256)));
228      */

229
230     /**
231      * Flag a certain region of cells to be merged
232      * @param region the region to create as merged
233      */

234     void addMergedRegion(Region region) {
235         this._sheet.addMergedRegion(region);
236     }
237
238     /**
239      * assigns blank cells to regions where no cell is currently allocated.
240      * Meaning if there is a sheet with a cell defined at 1,1 and a style
241      * region from 0,0-1,1 then cells 0,0;0,1;1,0 will be defined as blank
242      * cells pointing to the style defined by the style region. If there is not
243      * a defined cell and no styleregion encompases the area, then no cell is
244      * defined.
245      */

246     public void assignBlanksToRegions() {
247         Iterator JavaDoc iregions = regions.keySet().iterator();
248         while (iregions.hasNext()) {
249             Region region = ((Region)iregions.next());
250             for (int rownum = region.getRowFrom(); rownum < region.getRowTo() + 1; rownum++) {
251                 HSSFRow row = _sheet.getRow(rownum);
252                 for (short colnum = region.getColumnFrom();
253                             colnum < region.getColumnTo() + 1; colnum++) {
254                     HSSFCellStyle style = (HSSFCellStyle)regions.get(region);
255                     if (!isBlank(style)) {
256                         //don't waste time with huge blocks of blankly styled cells
257
if (row == null) {
258                             if (rownum > Short.MAX_VALUE) {
259                                 rownum = Short.MAX_VALUE;
260                             }
261                             row = _sheet.createRow(rownum);
262                         }
263                         HSSFCell cell = row.getCell(colnum);
264                         if (cell == null) {
265                             cell = row.createCell(colnum);
266                             cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
267                             cell.setCellStyle(
268                                 (HSSFCellStyle)regions.get(region));
269                         }
270                     }
271                 }
272             }
273         }
274     }
275
276     private boolean isBlank(HSSFCellStyle style) {
277         HSSFFont font = null;
278         if (style.getFontIndex() > 0) {
279             font = (_workbook.getWorkbook().getFontAt(style.getFontIndex()));
280         }
281         if (style.getBorderBottom() == 0 && style.getBorderTop() == 0
282             && style.getBorderRight() == 0 && style.getBorderLeft() == 0
283             && style.getFillBackgroundColor() == HSSFColor.WHITE.index
284             && style.getFillPattern() == 0 && (style.getFontIndex() == 0
285                 || ((font.getFontName().equals("Arial")
286                     || font.getFontName().equals("Helvetica"))
287                     && font.getFontHeightInPoints() > 8
288                     && font.getFontHeightInPoints() < 12))) {
289             return true;
290         }
291         return false;
292     }
293
294     /**
295      * Set the paper size.
296      * @param paperSize the paper size.
297      */

298
299     void setPaperSize(short paperSize) {
300         _sheet.getPrintSetup().setPaperSize(paperSize);
301     }
302
303     /**
304      * Set whether to print in landscape
305      * @param ls landscape
306      */

307
308     void setOrientation(boolean ls) {
309         _sheet.getPrintSetup().setLandscape(ls);
310     }
311
312     /**
313      * Set whether or not the grid is printed for the worksheet
314      * @param gridLines boolean to turn on or off the printing of
315      * gridlines
316      */

317
318     void setPrintGridLines(boolean gridLines) {
319         _sheet.setPrintGridlines(gridLines);
320     }
321     
322     /**
323      * Set whether or not the worksheet content is centered (horizontally)
324      * on the page when it is printed
325      */

326     void setHCenter(boolean hCenter) {
327         _sheet.setHorizontallyCenter(hCenter);
328     }
329
330     /**
331      * Setwhether or not the worksheet content is centered (vertically)
332      * on the page when it is printed
333      */

334     void setVCenter(boolean vCenter) {
335         _sheet.setVerticallyCenter(vCenter);
336     }
337     
338     /**
339      * Setup whether or not printing is in monochrome (no color)
340      */

341     void setMonochrome(boolean noColor) {
342         _sheet.getPrintSetup().setNoColor(noColor);
343     }
344     
345     /**
346      * Setup whether or not the worksheet is printed in draft format
347      */

348     void setDraft(boolean draftMode) {
349         _sheet.getPrintSetup().setDraft(draftMode);
350     }
351     
352     /**
353      * Set text to be printed at the top of every page
354      */

355     void setHeader(String JavaDoc left, String JavaDoc middle, String JavaDoc right) {
356         HSSFHeader header = _sheet.getHeader();
357         header.setLeft(left);
358         header.setCenter(middle);
359         header.setRight(right);
360     }
361     
362     /**
363      * Set text to be printed at the bottom of every page
364      */

365     void setFooter(String JavaDoc left, String JavaDoc middle, String JavaDoc right) {
366         HSSFFooter footer = _sheet.getFooter();
367         footer.setLeft(left);
368         footer.setCenter(middle);
369         footer.setRight(right);
370     }
371     
372     /**
373      * Set the top margin of the page
374      */

375     void setTopMargin(double points) {
376         _sheet.setMargin(HSSFSheet.TopMargin, points);
377     }
378     
379     /**
380      * Set the left margin of the page
381      */

382     void setLeftMargin(double points) {
383         _sheet.setMargin(HSSFSheet.LeftMargin, points);
384     }
385     
386     /**
387      * Set the right margin of the page
388      */

389     void setRightMargin(double points) {
390         _sheet.setMargin(HSSFSheet.RightMargin, points);
391     }
392     
393     /**
394      * Set the bottom margin of the page
395      */

396     void setBottomMargin(double points) {
397         _sheet.setMargin(HSSFSheet.BottomMargin, points);
398     }
399     
400     /**
401      * Set the header margin of the page
402      */

403     void setHeaderMargin(double points) {
404         _sheet.getPrintSetup().setHeaderMargin(points);
405     }
406     
407     /**
408      * Set the header margin of the page
409      */

410     void setFooterMargin(double points) {
411         _sheet.getPrintSetup().setFooterMargin(points);
412     }
413
414 } // end package scope class Sheet
415
Popular Tags