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 Button implements DrawingGroupObject 42 { 43 46 private static Logger logger = Logger.getLogger(Button.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 ContinueRecord text; 157 158 161 private ContinueRecord formatting; 162 163 166 private String commentText; 167 168 171 private WorkbookSettings workbookSettings; 172 173 182 public Button(MsoDrawingRecord mso, ObjRecord obj, DrawingData dd, 183 DrawingGroup dg, WorkbookSettings ws) 184 { 185 drawingGroup = dg; 186 msoDrawingRecord = mso; 187 drawingData = dd; 188 objRecord = obj; 189 initialized = false; 190 workbookSettings = ws; 191 origin = Origin.READ; 192 drawingData.addData(msoDrawingRecord.getData()); 193 drawingNumber = drawingData.getNumDrawings() - 1; 194 drawingGroup.addDrawing(this); 195 196 Assert.verify(mso != null && obj != null); 197 198 initialize(); 199 } 200 201 206 public Button(DrawingGroupObject dgo, 207 DrawingGroup dg, 208 WorkbookSettings ws) 209 { 210 Button d = (Button) dgo; 211 Assert.verify(d.origin == Origin.READ); 212 msoDrawingRecord = d.msoDrawingRecord; 213 objRecord = d.objRecord; 214 initialized = false; 215 origin = Origin.READ; 216 drawingData = d.drawingData; 217 drawingGroup = dg; 218 drawingNumber = d.drawingNumber; 219 drawingGroup.addDrawing(this); 220 mso = d.mso; 221 txo = d.txo; 222 text = d.text; 223 formatting = d.formatting; 224 workbookSettings = ws; 225 } 226 227 236 248 249 252 private void initialize() 253 { 254 readSpContainer = drawingData.getSpContainer(drawingNumber); 255 Assert.verify(readSpContainer != null); 256 257 EscherRecord[] children = readSpContainer.getChildren(); 258 259 Sp sp = (Sp) readSpContainer.getChildren()[0]; 260 objectId = objRecord.getObjectId(); 261 shapeId = sp.getShapeId(); 262 type = ShapeType.getType(sp.getShapeType()); 263 264 if (type == ShapeType.UNKNOWN) 265 { 266 logger.warn("Unknown shape type"); 267 } 268 269 Opt opt = (Opt) readSpContainer.getChildren()[1]; 270 271 ClientAnchor clientAnchor = null; 272 for (int i = 0 ; i < children.length && clientAnchor == null ; i++) 273 { 274 if (children[i].getType() == EscherRecordType.CLIENT_ANCHOR) 275 { 276 clientAnchor = (ClientAnchor) children[i]; 277 } 278 } 279 280 if (clientAnchor == null) 281 { 282 logger.warn("Client anchor not found"); 283 } 284 else 285 { 286 column = (int) clientAnchor.getX1() - 1; 287 row = (int) clientAnchor.getY1() + 1; 288 } 289 290 initialized = true; 291 } 292 293 294 302 public final void setObjectId(int objid, int bip, int sid) 303 { 304 objectId = objid; 305 blipId = bip; 306 shapeId = sid; 307 308 if (origin == Origin.READ) 309 { 310 origin = Origin.READ_WRITE; 311 } 312 } 313 314 319 public final int getObjectId() 320 { 321 if (!initialized) 322 { 323 initialize(); 324 } 325 326 return objectId; 327 } 328 329 334 public final int getShapeId() 335 { 336 if (!initialized) 337 { 338 initialize(); 339 } 340 341 return shapeId; 342 } 343 344 349 public final int getBlipId() 350 { 351 if (!initialized) 352 { 353 initialize(); 354 } 355 356 return blipId; 357 } 358 359 364 public MsoDrawingRecord getMsoDrawingRecord() 365 { 366 return msoDrawingRecord; 367 } 368 369 374 public EscherContainer getSpContainer() 375 { 376 if (!initialized) 377 { 378 initialize(); 379 } 380 381 if (origin == Origin.READ) 382 { 383 return getReadSpContainer(); 384 } 385 386 Assert.verify(false); 387 388 414 415 return spContainer; 416 } 417 418 424 public void setDrawingGroup(DrawingGroup dg) 425 { 426 drawingGroup = dg; 427 } 428 429 434 public DrawingGroup getDrawingGroup() 435 { 436 return drawingGroup; 437 } 438 439 444 public Origin getOrigin() 445 { 446 return origin; 447 } 448 449 454 public int getReferenceCount() 455 { 456 return referenceCount; 457 } 458 459 464 public void setReferenceCount(int r) 465 { 466 referenceCount = r; 467 } 468 469 474 public double getX() 475 { 476 if (!initialized) 477 { 478 initialize(); 479 } 480 return column; 481 } 482 483 489 public void setX(double x) 490 { 491 if (origin == Origin.READ) 492 { 493 if (!initialized) 494 { 495 initialize(); 496 } 497 origin = Origin.READ_WRITE; 498 } 499 500 column = (int) x; 501 } 502 503 508 public double getY() 509 { 510 if (!initialized) 511 { 512 initialize(); 513 } 514 515 return row; 516 } 517 518 523 public void setY(double y) 524 { 525 if (origin == Origin.READ) 526 { 527 if (!initialized) 528 { 529 initialize(); 530 } 531 origin = Origin.READ_WRITE; 532 } 533 534 row = (int) y; 535 } 536 537 538 543 public double getWidth() 544 { 545 if (!initialized) 546 { 547 initialize(); 548 } 549 550 return width; 551 } 552 553 558 public void setWidth(double w) 559 { 560 if (origin == Origin.READ) 561 { 562 if (!initialized) 563 { 564 initialize(); 565 } 566 origin = Origin.READ_WRITE; 567 } 568 569 width = w; 570 } 571 572 577 public double getHeight() 578 { 579 if (!initialized) 580 { 581 initialize(); 582 } 583 584 return height; 585 } 586 587 592 public void setHeight(double h) 593 { 594 if (origin == Origin.READ) 595 { 596 if (!initialized) 597 { 598 initialize(); 599 } 600 origin = Origin.READ_WRITE; 601 } 602 603 height = h; 604 } 605 606 607 612 private EscherContainer getReadSpContainer() 613 { 614 if (!initialized) 615 { 616 initialize(); 617 } 618 619 return readSpContainer; 620 } 621 622 627 public byte[] getImageData() 628 { 629 Assert.verify(origin == Origin.READ || origin == Origin.READ_WRITE); 630 631 if (!initialized) 632 { 633 initialize(); 634 } 635 636 return drawingGroup.getImageData(blipId); 637 } 638 639 644 public ShapeType getType() 645 { 646 return type; 647 } 648 649 652 public void setTextObject(TextObjectRecord t) 653 { 654 txo = t; 655 } 656 657 660 public void setText(ContinueRecord t) 661 { 662 text = t; 663 } 664 665 668 public void setFormatting(ContinueRecord t) 669 { 670 formatting = t; 671 } 672 673 678 public byte[] getImageBytes() 679 { 680 Assert.verify(false); 681 return null; 682 } 683 684 691 public String getImageFilePath() 692 { 693 Assert.verify(false); 694 return null; 695 } 696 697 public void addMso(MsoDrawingRecord d) 698 { 699 mso = d; 700 drawingData.addRawData(mso.getData()); 701 } 702 703 public void writeAdditionalRecords(File outputFile) throws IOException 704 { 705 if (origin == Origin.READ) 706 { 707 outputFile.write(objRecord); 708 709 if (mso != null) 710 { 711 outputFile.write(mso); 712 } 713 outputFile.write(txo); 714 outputFile.write(text); 715 if (formatting != null) 716 { 717 outputFile.write(formatting); 718 } 719 return; 720 } 721 722 Assert.verify(false); 723 724 ObjRecord objRecord = new ObjRecord(objectId, 726 ObjRecord.EXCELNOTE); 727 728 outputFile.write(objRecord); 729 730 ClientTextBox textBox = new ClientTextBox(); 733 MsoDrawingRecord msod = new MsoDrawingRecord(textBox.getData()); 734 outputFile.write(msod); 735 736 TextObjectRecord txo = new TextObjectRecord(getText()); 737 outputFile.write(txo); 738 739 byte[] textData = new byte[commentText.length() * 2 + 1]; 741 textData[0] = 0x1; StringHelper.getUnicodeBytes(commentText, textData, 1); 743 ContinueRecord textContinue = new ContinueRecord(textData); 745 outputFile.write(textContinue); 746 747 749 byte[] frData = new byte[16]; 750 751 IntegerHelper.getTwoBytes(0, frData, 0); IntegerHelper.getTwoBytes(0, frData, 2); IntegerHelper.getTwoBytes(commentText.length(), frData, 8); 756 IntegerHelper.getTwoBytes(0, frData, 10); 758 ContinueRecord frContinue = new ContinueRecord(frData); 759 outputFile.write(frContinue); 760 } 761 762 767 public void writeTailRecords(File outputFile) throws IOException 768 { 769 } 770 771 774 public int getRow() 775 { 776 return 0; 777 } 778 779 782 public int getColumn() 783 { 784 return 0; 785 } 786 787 790 public String getText() 791 { 792 if (commentText == null) 793 { 794 Assert.verify(text != null); 795 796 byte[] td = text.getData(); 797 if (td[0] == 0) 798 { 799 commentText = StringHelper.getString 800 (td, td.length-1, 1, workbookSettings); 801 } 802 else 803 { 804 commentText = StringHelper.getUnicodeString 805 (td, (td.length - 1)/2, 1); 806 } 807 } 808 809 return commentText; 810 } 811 812 815 public int hashCode() 816 { 817 return commentText.hashCode(); 818 } 819 820 825 public void setButtonText(String t) 826 { 827 commentText = t; 828 829 if (origin == Origin.READ) 830 { 831 origin = Origin.READ_WRITE; 832 } 833 } 834 835 842 public boolean isFirst() 843 { 844 return mso.isFirst(); 845 } 846 847 854 public boolean isFormObject() 855 { 856 return true; 857 } 858 } 859 860 861 862 | Popular Tags |