1 19 20 package jxl.biff.drawing; 21 22 import java.io.IOException ; 23 import java.io.FileInputStream ; 24 25 import common.Assert; 26 import common.Logger; 27 28 import jxl.WorkbookSettings; 29 import jxl.biff.ByteData; 30 import jxl.biff.IntegerHelper; 31 import jxl.biff.StringHelper; 32 import jxl.biff.IndexMapping; 33 import jxl.biff.Type; 34 import jxl.biff.ContinueRecord; 35 import jxl.write.biff.File; 36 37 41 public class Comment implements DrawingGroupObject 42 { 43 46 private static Logger logger = Logger.getLogger(Comment.class); 47 48 51 private EscherContainer readSpContainer; 52 53 56 private EscherContainer spContainer; 57 58 61 private MsoDrawingRecord msoDrawingRecord; 62 63 66 private ObjRecord objRecord; 67 68 71 private boolean initialized = false; 72 73 76 private int objectId; 77 78 81 private int blipId; 82 83 86 private int shapeId; 87 88 91 private int column; 92 93 96 private int row; 97 98 101 private double width; 102 103 106 private double height; 107 108 111 private int referenceCount; 112 113 116 private EscherContainer escherData; 117 118 121 private Origin origin; 122 123 126 private DrawingGroup drawingGroup; 127 128 131 private DrawingData drawingData; 132 133 136 private ShapeType type; 137 138 141 private int drawingNumber; 142 143 146 private MsoDrawingRecord mso; 147 148 151 private TextObjectRecord txo; 152 153 156 private NoteRecord note; 157 158 161 private ContinueRecord text; 162 163 166 private ContinueRecord formatting; 167 168 171 private String commentText; 172 173 176 private WorkbookSettings workbookSettings; 177 178 187 public Comment(MsoDrawingRecord mso, ObjRecord obj, DrawingData dd, 188 DrawingGroup dg, WorkbookSettings ws) 189 { 190 drawingGroup = dg; 191 msoDrawingRecord = mso; 192 drawingData = dd; 193 objRecord = obj; 194 initialized = false; 195 workbookSettings = ws; 196 origin = Origin.READ; 197 drawingData.addData(msoDrawingRecord.getData()); 198 drawingNumber = drawingData.getNumDrawings() - 1; 199 drawingGroup.addDrawing(this); 200 201 Assert.verify(mso != null && obj != null); 202 203 if (!initialized) 204 { 205 initialize(); 206 } 207 } 208 209 214 public Comment(DrawingGroupObject dgo, 215 DrawingGroup dg, 216 WorkbookSettings ws) 217 { 218 Comment d = (Comment) dgo; 219 Assert.verify(d.origin == Origin.READ); 220 msoDrawingRecord = d.msoDrawingRecord; 221 objRecord = d.objRecord; 222 initialized = false; 223 origin = Origin.READ; 224 drawingData = d.drawingData; 225 drawingGroup = dg; 226 drawingNumber = d.drawingNumber; 227 drawingGroup.addDrawing(this); 228 mso = d.mso; 229 txo = d.txo; 230 text = d.text; 231 formatting = d.formatting; 232 note = d.note; 233 width = d.width; 234 height = d.height; 235 workbookSettings = ws; 236 } 237 238 247 public Comment(String text, int c, int r) 248 { 249 initialized = true; 250 origin = Origin.WRITE; 251 column = c; 252 row = r; 253 referenceCount = 1; 254 type = ShapeType.TEXT_BOX; 255 commentText = text; 256 width = 3; 257 height = 4; 258 } 259 260 263 private void initialize() 264 { 265 readSpContainer = drawingData.getSpContainer(drawingNumber); 266 Assert.verify(readSpContainer != null); 267 268 EscherRecord[] children = readSpContainer.getChildren(); 269 270 Sp sp = (Sp) readSpContainer.getChildren()[0]; 271 objectId = objRecord.getObjectId(); 272 shapeId = sp.getShapeId(); 273 type = ShapeType.getType(sp.getShapeType()); 274 275 if (type == ShapeType.UNKNOWN) 276 { 277 logger.warn("Unknown shape type"); 278 } 279 280 Opt opt = (Opt) readSpContainer.getChildren()[1]; 281 282 ClientAnchor clientAnchor = null; 283 for (int i = 0 ; i < children.length && clientAnchor == null ; i++) 284 { 285 if (children[i].getType() == EscherRecordType.CLIENT_ANCHOR) 286 { 287 clientAnchor = (ClientAnchor) children[i]; 288 } 289 } 290 291 if (clientAnchor == null) 292 { 293 logger.warn("client anchor not found"); 294 } 295 else 296 { 297 column = (int) clientAnchor.getX1() - 1; 298 row = (int) clientAnchor.getY1() + 1; 299 width = clientAnchor.getX2() - clientAnchor.getX1(); 300 height = clientAnchor.getY2() - clientAnchor.getY1(); 301 } 302 303 initialized = true; 304 } 305 306 307 315 public final void setObjectId(int objid, int bip, int sid) 316 { 317 objectId = objid; 318 blipId = bip; 319 shapeId = sid; 320 321 if (origin == Origin.READ) 322 { 323 origin = Origin.READ_WRITE; 324 } 325 } 326 327 332 public final int getObjectId() 333 { 334 if (!initialized) 335 { 336 initialize(); 337 } 338 339 return objectId; 340 } 341 342 347 public final int getShapeId() 348 { 349 if (!initialized) 350 { 351 initialize(); 352 } 353 354 return shapeId; 355 } 356 357 362 public final int getBlipId() 363 { 364 if (!initialized) 365 { 366 initialize(); 367 } 368 369 return blipId; 370 } 371 372 377 public MsoDrawingRecord getMsoDrawingRecord() 378 { 379 return msoDrawingRecord; 380 } 381 382 387 public EscherContainer getSpContainer() 388 { 389 if (!initialized) 390 { 391 initialize(); 392 } 393 394 if (origin == Origin.READ) 395 { 396 return getReadSpContainer(); 397 } 398 399 if (spContainer == null) 400 { 401 spContainer = new SpContainer(); 402 Sp sp = new Sp(type, shapeId, 2560); 403 spContainer.add(sp); 404 Opt opt = new Opt(); 405 opt.addProperty(344, false, false, 0); opt.addProperty(385, false, false, 134217808); opt.addProperty(387, false, false, 134217808); opt.addProperty(959, false, false, 131074); spContainer.add(opt); 410 411 ClientAnchor clientAnchor = new ClientAnchor(column + 1.3, 412 Math.max(0, row - 0.6), 413 column + width, 414 row + height); 415 416 spContainer.add(clientAnchor); 417 418 ClientData clientData = new ClientData(); 419 spContainer.add(clientData); 420 421 ClientTextBox clientTextBox = new ClientTextBox(); 422 spContainer.add(clientTextBox); 423 } 424 425 return spContainer; 426 } 427 428 434 public void setDrawingGroup(DrawingGroup dg) 435 { 436 drawingGroup = dg; 437 } 438 439 444 public DrawingGroup getDrawingGroup() 445 { 446 return drawingGroup; 447 } 448 449 454 public Origin getOrigin() 455 { 456 return origin; 457 } 458 459 464 public int getReferenceCount() 465 { 466 return referenceCount; 467 } 468 469 474 public void setReferenceCount(int r) 475 { 476 referenceCount = r; 477 } 478 479 484 public double getX() 485 { 486 if (!initialized) 487 { 488 initialize(); 489 } 490 return column; 491 } 492 493 499 public void setX(double x) 500 { 501 if (origin == Origin.READ) 502 { 503 if (!initialized) 504 { 505 initialize(); 506 } 507 origin = Origin.READ_WRITE; 508 } 509 510 column = (int) x; 511 } 512 513 518 public double getY() 519 { 520 if (!initialized) 521 { 522 initialize(); 523 } 524 525 return row; 526 } 527 528 533 public void setY(double y) 534 { 535 if (origin == Origin.READ) 536 { 537 if (!initialized) 538 { 539 initialize(); 540 } 541 origin = Origin.READ_WRITE; 542 } 543 544 row = (int) y; 545 } 546 547 548 553 public double getWidth() 554 { 555 if (!initialized) 556 { 557 initialize(); 558 } 559 560 return width; 561 } 562 563 568 public void setWidth(double w) 569 { 570 if (origin == Origin.READ) 571 { 572 if (!initialized) 573 { 574 initialize(); 575 } 576 origin = Origin.READ_WRITE; 577 } 578 579 width = w; 580 } 581 582 587 public double getHeight() 588 { 589 if (!initialized) 590 { 591 initialize(); 592 } 593 594 return height; 595 } 596 597 602 public void setHeight(double h) 603 { 604 if (origin == Origin.READ) 605 { 606 if (!initialized) 607 { 608 initialize(); 609 } 610 origin = Origin.READ_WRITE; 611 } 612 613 height = h; 614 } 615 616 617 622 private EscherContainer getReadSpContainer() 623 { 624 if (!initialized) 625 { 626 initialize(); 627 } 628 629 return readSpContainer; 630 } 631 632 637 public byte[] getImageData() 638 { 639 Assert.verify(origin == Origin.READ || origin == Origin.READ_WRITE); 640 641 if (!initialized) 642 { 643 initialize(); 644 } 645 646 return drawingGroup.getImageData(blipId); 647 } 648 649 654 public ShapeType getType() 655 { 656 return type; 657 } 658 659 662 public void setTextObject(TextObjectRecord t) 663 { 664 txo = t; 665 } 666 667 670 public void setNote(NoteRecord t) 671 { 672 note = t; 673 } 674 675 678 public void setText(ContinueRecord t) 679 { 680 text = t; 681 } 682 683 686 public void setFormatting(ContinueRecord t) 687 { 688 formatting = t; 689 } 690 691 696 public byte[] getImageBytes() 697 { 698 Assert.verify(false); 699 return null; 700 } 701 702 709 public String getImageFilePath() 710 { 711 Assert.verify(false); 712 return null; 713 } 714 715 public void addMso(MsoDrawingRecord d) 716 { 717 mso = d; 718 drawingData.addRawData(mso.getData()); 719 } 720 721 public void writeAdditionalRecords(File outputFile) throws IOException 722 { 723 if (origin == Origin.READ) 724 { 725 outputFile.write(objRecord); 726 727 if (mso != null) 728 { 729 outputFile.write(mso); 730 } 731 outputFile.write(txo); 732 outputFile.write(text); 733 if (formatting != null) 734 { 735 outputFile.write(formatting); 736 } 737 return; 738 } 739 740 ObjRecord objRecord = new ObjRecord(objectId, 742 ObjRecord.EXCELNOTE); 743 744 outputFile.write(objRecord); 745 746 ClientTextBox textBox = new ClientTextBox(); 749 MsoDrawingRecord msod = new MsoDrawingRecord(textBox.getData()); 750 outputFile.write(msod); 751 752 TextObjectRecord txo = new TextObjectRecord(getText()); 753 outputFile.write(txo); 754 755 byte[] textData = new byte[commentText.length() * 2 + 1]; 757 textData[0] = 0x1; StringHelper.getUnicodeBytes(commentText, textData, 1); 759 ContinueRecord textContinue = new ContinueRecord(textData); 761 outputFile.write(textContinue); 762 763 765 byte[] frData = new byte[16]; 766 767 IntegerHelper.getTwoBytes(0, frData, 0); IntegerHelper.getTwoBytes(0, frData, 2); IntegerHelper.getTwoBytes(commentText.length(), frData, 8); 772 IntegerHelper.getTwoBytes(0, frData, 10); 774 ContinueRecord frContinue = new ContinueRecord(frData); 775 outputFile.write(frContinue); 776 } 777 778 783 public void writeTailRecords(File outputFile) throws IOException 784 { 785 if (origin == Origin.READ) 786 { 787 outputFile.write(note); 788 return; 789 } 790 791 NoteRecord noteRecord = new NoteRecord(column, row, objectId); 793 outputFile.write(noteRecord); 794 } 795 796 799 public int getRow() 800 { 801 return note.getRow(); 802 } 803 804 807 public int getColumn() 808 { 809 return note.getColumn(); 810 } 811 812 815 public String getText() 816 { 817 if (commentText == null) 818 { 819 Assert.verify(text != null); 820 821 byte[] td = text.getData(); 822 if (td[0] == 0) 823 { 824 commentText = StringHelper.getString 825 (td, td.length-1, 1, workbookSettings); 826 } 827 else 828 { 829 commentText = StringHelper.getUnicodeString 830 (td, (td.length - 1)/2, 1); 831 } 832 } 833 834 return commentText; 835 } 836 837 840 public int hashCode() 841 { 842 return commentText.hashCode(); 843 } 844 845 850 public void setCommentText(String t) 851 { 852 commentText = t; 853 854 if (origin == Origin.READ) 855 { 856 origin = Origin.READ_WRITE; 857 } 858 } 859 860 867 public boolean isFirst() 868 { 869 return msoDrawingRecord.isFirst(); 870 } 871 872 878 public boolean isFormObject() 879 { 880 return true; 881 } 882 } 883 884 885 886 | Popular Tags |