1 16 17 22 package org.apache.poi.hssf.usermodel; 23 24 import org.apache.poi.hssf.model.Sheet; 25 import org.apache.poi.hssf.model.Workbook; 26 import org.apache.poi.hssf.record.CellValueRecordInterface; 27 import org.apache.poi.hssf.record.RowRecord; 28 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 32 40 41 public class HSSFRow 42 implements Comparable 43 { 44 45 public final static int INITIAL_CAPACITY = 5; 47 private int rowNum; 49 private HashMap cells; 50 53 56 57 private RowRecord row; 58 59 62 63 private Workbook book; 64 65 68 69 private Sheet sheet; 70 71 protected HSSFRow() 72 { 73 } 74 75 83 84 protected HSSFRow(Workbook book, Sheet sheet, int rowNum) 86 { 87 cells = new HashMap (10); this.book = book; 89 this.sheet = sheet; 90 row = new RowRecord(); 91 row.setOptionFlags( (short)0x100 ); row.setHeight((short) 0xff); 93 row.setLastCol((short) -1); 94 row.setFirstCol((short) -1); 95 96 setRowNum(rowNum); 97 } 98 99 108 109 protected HSSFRow(Workbook book, Sheet sheet, RowRecord record) 110 { 111 cells = new HashMap (); this.book = book; 113 this.sheet = sheet; 114 row = record; 115 116 setRowNum(record.getRowNumber()); 117 } 118 119 129 130 public HSSFCell createCell(short column) 131 { 132 HSSFCell cell = new HSSFCell(book, sheet, getRowNum(), column); 133 134 addCell(cell); 135 sheet.addValueRecord(getRowNum(), cell.getCellValueRecord()); 136 return cell; 137 } 138 139 151 152 public HSSFCell createCell(short column, int type) 153 { 154 HSSFCell cell = new HSSFCell(book, sheet, getRowNum(), column, type); 155 156 addCell(cell); 157 sheet.addValueRecord(getRowNum(), cell.getCellValueRecord()); 158 return cell; 159 } 160 161 165 public void removeCell(HSSFCell cell) 166 { 167 CellValueRecordInterface cval = cell.getCellValueRecord(); 168 169 sheet.removeValueRecord(getRowNum(), cval); 170 cells.remove(new Integer (cell.getCellNum())); 171 172 if (cell.getCellNum() == row.getLastCol()) 173 { 174 row.setLastCol(findLastCell(row.getLastCol())); 175 } 176 if (cell.getCellNum() == row.getFirstCol()) 177 { 178 row.setFirstCol(findFirstCell(row.getFirstCol())); 179 } 180 } 181 182 188 189 protected HSSFCell createCellFromRecord(CellValueRecordInterface cell) 190 { 191 HSSFCell hcell = new HSSFCell(book, sheet, getRowNum(), cell); 192 193 addCell(hcell); 194 195 return hcell; 197 } 198 199 204 205 public void setRowNum(int rowNum) 207 { 208 if ((rowNum < 0) || (rowNum > RowRecord.MAX_ROW_NUMBER)) 209 throw new IndexOutOfBoundsException ("Row number must be between 0 and "+RowRecord.MAX_ROW_NUMBER+", was <"+rowNum+">"); 210 this.rowNum = rowNum; 211 if (row != null) 212 { 213 row.setRowNumber(rowNum); } 215 } 216 217 221 222 public int getRowNum() 224 { 225 return rowNum; 226 } 227 228 231 232 private void addCell(HSSFCell cell) 233 { 234 if (row.getFirstCol() == -1) 235 { 236 row.setFirstCol(cell.getCellNum()); 237 } 238 if (row.getLastCol() == -1) 239 { 240 row.setLastCol(cell.getCellNum()); 241 } 242 cells.put(new Integer (cell.getCellNum()), cell); 243 244 if (cell.getCellNum() < row.getFirstCol()) 245 { 246 row.setFirstCol(cell.getCellNum()); 247 } 248 if (cell.getCellNum() > row.getLastCol()) 249 { 250 row.setLastCol(cell.getCellNum()); 251 } 252 } 253 254 261 262 public HSSFCell getCell(short cellnum) 263 { 264 265 274 return (HSSFCell) cells.get(new Integer (cellnum)); 275 } 276 277 281 282 public short getFirstCellNum() 283 { 284 if (getPhysicalNumberOfCells() == 0) 285 return -1; 286 else 287 return row.getFirstCol(); 288 } 289 290 294 295 public short getLastCellNum() 296 { 297 if (getPhysicalNumberOfCells() == 0) 298 return -1; 299 else 300 return row.getLastCol(); 301 } 302 303 304 309 310 public int getPhysicalNumberOfCells() 311 { 312 if (cells == null) 313 { 314 return 0; } 316 return cells.size(); 317 } 318 319 324 325 public void setHeight(short height) 326 { 327 328 row.setBadFontHeight(true); 330 row.setHeight(height); 331 } 332 333 337 public void setZeroHeight(boolean zHeight) { 338 row.setZeroHeight(zHeight); 339 } 340 341 345 public boolean getZeroHeight() { 346 return row.getZeroHeight(); 347 } 348 349 353 354 public void setHeightInPoints(float height) 355 { 356 357 row.setBadFontHeight(true); 359 row.setHeight((short) (height * 20)); 360 } 361 362 366 367 public short getHeight() 368 { 369 return row.getHeight(); 370 } 371 372 376 377 public float getHeightInPoints() 378 { 379 return (row.getHeight() / 20); 380 } 381 382 388 389 protected RowRecord getRowRecord() 390 { 391 return row; 392 } 393 394 397 398 private short findLastCell(short lastcell) 399 { 400 short cellnum = (short) (lastcell - 1); 401 HSSFCell r = getCell(cellnum); 402 403 while (r == null && cellnum >= 0) 404 { 405 r = getCell(--cellnum); 406 } 407 return cellnum; 408 } 409 410 413 414 private short findFirstCell(short firstcell) 415 { 416 short cellnum = (short) (firstcell + 1); 417 HSSFCell r = getCell(cellnum); 418 419 while (r == null && cellnum <= getLastCellNum()) 420 { 421 r = getCell(++cellnum); 422 } 423 if (cellnum > getLastCellNum()) 424 return -1; 425 return cellnum; 426 } 427 428 432 433 public Iterator cellIterator() 434 { 435 return cells.values().iterator(); 436 } 437 438 public int compareTo(Object obj) 439 { 440 HSSFRow loc = (HSSFRow) obj; 441 442 if (this.getRowNum() == loc.getRowNum()) 443 { 444 return 0; 445 } 446 if (this.getRowNum() < loc.getRowNum()) 447 { 448 return -1; 449 } 450 if (this.getRowNum() > loc.getRowNum()) 451 { 452 return 1; 453 } 454 return -1; 455 } 456 457 public boolean equals(Object obj) 458 { 459 if (!(obj instanceof HSSFRow)) 460 { 461 return false; 462 } 463 HSSFRow loc = (HSSFRow) obj; 464 465 if (this.getRowNum() == loc.getRowNum()) 466 { 467 return true; 468 } 469 return false; 470 } 471 } 472 | Popular Tags |