1 19 20 package jxl.write.biff; 21 22 import common.Assert; 23 import common.Logger; 24 25 import jxl.Cell; 26 import jxl.Sheet; 27 import jxl.CellFeatures; 28 import jxl.biff.Type; 29 import jxl.biff.IntegerHelper; 30 import jxl.biff.WritableRecordData; 31 import jxl.biff.XFRecord; 32 import jxl.biff.FormattingRecords; 33 import jxl.biff.NumFormatRecordsException; 34 import jxl.biff.drawing.Comment; 35 import jxl.write.WritableCell; 36 import jxl.write.WritableWorkbook; 37 import jxl.write.WritableCellFeatures; 38 import jxl.write.WritableCellFormat; 39 import jxl.format.CellFormat; 40 import jxl.format.Alignment; 41 import jxl.format.Border; 42 import jxl.format.BorderLineStyle; 43 import jxl.format.Font; 44 import jxl.format.VerticalAlignment; 45 46 53 public abstract class CellValue extends WritableRecordData 54 implements WritableCell 55 { 56 59 private static Logger logger = Logger.getLogger(CellValue.class); 60 61 64 private int row; 65 68 private int column; 69 72 private XFRecord format; 73 74 79 private FormattingRecords formattingRecords; 80 81 85 private boolean referenced; 86 87 90 private WritableSheetImpl sheet; 91 92 95 private WritableCellFeatures features; 96 97 101 private boolean copied; 102 103 110 protected CellValue(Type t, int c, int r) 111 { 112 this(t, c, r, WritableWorkbookImpl.NORMAL_STYLE); 113 copied = false; 114 } 115 116 123 protected CellValue(Type t, Cell c) 124 { 125 this(t, c.getColumn(), c.getRow()); 126 copied = true; 127 128 format = (XFRecord) c.getCellFormat(); 129 130 if (c.getCellFeatures() != null) 131 { 132 features = new WritableCellFeatures(c.getCellFeatures()); 133 } 134 } 135 136 145 protected CellValue(Type t, int c, int r, CellFormat st) 146 { 147 super(t); 148 row = r; 149 column = c; 150 format = (XFRecord) st; 151 referenced = false; 152 copied = false; 153 } 154 155 163 protected CellValue(Type t, int c, int r, CellValue cv) 164 { 165 super(t); 166 row = r; 167 column = c; 168 format = cv.format; 169 referenced = false; 170 copied = false; 173 if (cv.features != null) 174 { 175 features = new WritableCellFeatures(cv.features); 176 } 177 } 178 179 184 public void setCellFormat(CellFormat cf) 185 { 186 format = (XFRecord) cf; 187 188 if (!referenced) 192 { 193 return; 194 } 195 196 Assert.verify(formattingRecords != null); 199 200 addCellFormat(); 201 } 202 203 208 public int getRow() 209 { 210 return row; 211 } 212 213 218 public int getColumn() 219 { 220 return column; 221 } 222 223 229 public boolean isHidden() 230 { 231 ColumnInfoRecord cir = sheet.getColumnInfo(column); 232 233 if (cir != null && cir.getWidth() == 0) 234 { 235 return true; 236 } 237 238 RowRecord rr = sheet.getRowInfo(row); 239 240 if (rr != null && (rr.getRowHeight() == 0 || rr.isCollapsed())) 241 { 242 return true; 243 } 244 245 return false; 246 } 247 248 253 public byte[] getData() 254 { 255 byte[] mydata = new byte[6]; 256 IntegerHelper.getTwoBytes(row, mydata, 0); 257 IntegerHelper.getTwoBytes(column, mydata, 2); 258 IntegerHelper.getTwoBytes(format.getXFIndex(), mydata, 4); 259 return mydata; 260 } 261 262 272 void setCellDetails(FormattingRecords fr, SharedStrings ss, 273 WritableSheetImpl s) 274 { 275 referenced = true; 276 sheet = s; 277 formattingRecords = fr; 278 279 addCellFormat(); 280 addCellFeatures(); 281 } 282 283 289 final boolean isReferenced() 290 { 291 return referenced; 292 } 293 294 299 final int getXFIndex() 300 { 301 return format.getXFIndex(); 302 } 303 304 309 public CellFormat getCellFormat() 310 { 311 return format; 312 } 313 314 318 void incrementRow() 319 { 320 row++; 321 322 if (features != null) 323 { 324 Comment c = features.getCommentDrawing(); 325 c.setX(column); 326 c.setY(row); 327 } 328 } 329 330 334 void decrementRow() 335 { 336 row--; 337 338 if (features != null) 339 { 340 Comment c = features.getCommentDrawing(); 341 c.setX(column); 342 c.setY(row); 343 } 344 } 345 346 350 void incrementColumn() 351 { 352 column++; 353 354 if (features != null) 355 { 356 Comment c = features.getCommentDrawing(); 357 c.setX(column); 358 c.setY(row); 359 } 360 361 } 362 363 367 void decrementColumn() 368 { 369 column--; 370 371 if (features != null) 372 { 373 Comment c = features.getCommentDrawing(); 374 c.setX(column); 375 c.setY(row); 376 } 377 378 } 379 380 388 void columnInserted(Sheet s, int sheetIndex, int col) 389 { 390 } 391 392 400 void columnRemoved(Sheet s, int sheetIndex, int col) 401 { 402 } 403 404 412 void rowInserted(Sheet s, int sheetIndex, int row) 413 { 414 } 415 416 424 void rowRemoved(Sheet s, int sheetIndex, int row) 425 { 426 } 427 428 433 protected WritableSheetImpl getSheet() 434 { 435 return sheet; 436 } 437 438 443 private void addCellFormat() 444 { 445 Styles styles = sheet.getWorkbook().getStyles(); 448 format = styles.getFormat(format); 449 450 try 451 { 452 if (!format.isInitialized()) 453 { 454 formattingRecords.addStyle(format); 455 } 456 } 457 catch (NumFormatRecordsException e) 458 { 459 logger.warn("Maximum number of format records exceeded. Using " + 460 "default format."); 461 format = styles.getNormalStyle(); 462 } 463 } 464 465 470 public CellFeatures getCellFeatures() 471 { 472 return features; 473 } 474 475 480 public WritableCellFeatures getWritableCellFeatures() 481 { 482 return features; 483 } 484 485 490 public void setCellFeatures(WritableCellFeatures cf) 491 { 492 if (features != null) 493 { 494 logger.warn("current cell features not null - overwriting"); 495 } 496 497 features = cf; 498 cf.setWritableCell(this); 499 500 if (referenced) 503 { 504 addCellFeatures(); 505 } 506 } 507 508 514 public final void addCellFeatures() 515 { 516 if (features == null) 517 { 518 return; 519 } 520 521 if (copied == true) 522 { 523 copied = false; return; 525 } 526 527 if (features.getComment() != null) 528 { 529 Comment comment = new Comment(features.getComment(), 530 column, row); 531 comment.setWidth(features.getCommentWidth()); 532 comment.setHeight(features.getCommentHeight()); 533 sheet.addDrawing(comment); 534 sheet.getWorkbook().addDrawing(comment); 535 features.setCommentDrawing(comment); 536 } 537 } 538 } 539 | Popular Tags |