1 16 17 package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements; 18 19 import java.io.IOException ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 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 43 44 46 class Sheet extends AbstractLogEnabled { 47 48 private HSSFSheet _sheet; 49 private String _name; 50 private int _physical_index; 51 private Workbook _workbook; 52 53 private Map _rows; 55 56 private Map regions; 57 58 private final static int ROWS_CAPACITY = 200; 60 61 private final static int REGION_CAPACITY = 20; 63 64 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 (ROWS_CAPACITY); 74 regions = new HashMap (REGION_CAPACITY); 75 } 76 77 81 void renameSheet(final String new_name) { 82 if (!_name.equals(new_name)) { 83 _workbook.renameSheet(_physical_index, new_name); 84 _name = new_name; 85 } 86 } 87 88 94 void setColumnWidth(final int number, final double points) 95 throws IOException { 96 if (number < 0 || number > Short.MAX_VALUE) { 97 throw new IOException ("column number " + number + " is too large"); 98 } 99 if (!isValidColumnPoints(points)) { 100 throw new IOException ("points " + points + " is out of range"); 101 } 102 _sheet.setColumnWidth((short)number, (short) ((points * 48) + .5)); 103 } 104 105 110 short getColumnWidth(short number) { 111 return _sheet.getColumnWidth(number); 112 } 113 114 119 void setDefaultColumnWidth(double width) throws IOException { 120 if (width < 0 || (width >= (4.8 * (0.5 + Short.MAX_VALUE)))) { 121 throw new IOException ("Invalid width (" + width + ")"); 122 } _sheet.setDefaultColumnWidth((short) ((width / 4.8) + 0.5)); 124 } 125 126 129 short getDefaultColumnWidth() { 130 return _sheet.getDefaultColumnWidth(); 131 } 132 133 138 void setDefaultRowHeight(double height) throws IOException { 139 if (!isValidPoints(height)) { 140 throw new IOException ("Invalid height (" + height + ")"); 141 } 142 _sheet.setDefaultRowHeight((short) ((height * 20) + .5)); 143 } 144 145 148 short getDefaultRowHeight() { 149 return _sheet.getDefaultRowHeight(); 150 } 151 152 155 String getName() { 156 return _name; 157 } 158 159 162 int getIndex() { 163 return _physical_index; 164 } 165 166 172 Row getRow(int rowNo) throws IOException { 173 if (rowNo < 0) { 174 throw new IOException ("Illegal row number: " + rowNo); 175 } 176 Short key = new Short ((short)rowNo); 177 Object 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 201 HSSFCellStyle getCellStyleForRegion(int row, short col) { 202 Iterator 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 229 230 234 void addMergedRegion(Region region) { 235 this._sheet.addMergedRegion(region); 236 } 237 238 246 public void assignBlanksToRegions() { 247 Iterator 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 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 298 299 void setPaperSize(short paperSize) { 300 _sheet.getPrintSetup().setPaperSize(paperSize); 301 } 302 303 307 308 void setOrientation(boolean ls) { 309 _sheet.getPrintSetup().setLandscape(ls); 310 } 311 312 317 318 void setPrintGridLines(boolean gridLines) { 319 _sheet.setPrintGridlines(gridLines); 320 } 321 322 326 void setHCenter(boolean hCenter) { 327 _sheet.setHorizontallyCenter(hCenter); 328 } 329 330 334 void setVCenter(boolean vCenter) { 335 _sheet.setVerticallyCenter(vCenter); 336 } 337 338 341 void setMonochrome(boolean noColor) { 342 _sheet.getPrintSetup().setNoColor(noColor); 343 } 344 345 348 void setDraft(boolean draftMode) { 349 _sheet.getPrintSetup().setDraft(draftMode); 350 } 351 352 355 void setHeader(String left, String middle, String right) { 356 HSSFHeader header = _sheet.getHeader(); 357 header.setLeft(left); 358 header.setCenter(middle); 359 header.setRight(right); 360 } 361 362 365 void setFooter(String left, String middle, String right) { 366 HSSFFooter footer = _sheet.getFooter(); 367 footer.setLeft(left); 368 footer.setCenter(middle); 369 footer.setRight(right); 370 } 371 372 375 void setTopMargin(double points) { 376 _sheet.setMargin(HSSFSheet.TopMargin, points); 377 } 378 379 382 void setLeftMargin(double points) { 383 _sheet.setMargin(HSSFSheet.LeftMargin, points); 384 } 385 386 389 void setRightMargin(double points) { 390 _sheet.setMargin(HSSFSheet.RightMargin, points); 391 } 392 393 396 void setBottomMargin(double points) { 397 _sheet.setMargin(HSSFSheet.BottomMargin, points); 398 } 399 400 403 void setHeaderMargin(double points) { 404 _sheet.getPrintSetup().setHeaderMargin(points); 405 } 406 407 410 void setFooterMargin(double points) { 411 _sheet.getPrintSetup().setFooterMargin(points); 412 } 413 414 } | Popular Tags |