1 50 51 package com.lowagie.text; 52 53 import java.util.ArrayList ; 54 import java.util.Iterator ; 55 56 import com.lowagie.text.pdf.PdfPCell; 57 58 92 93 public class Cell extends Rectangle implements TextElementArray { 94 95 97 101 protected ArrayList arrayList = null; 102 103 104 protected int horizontalAlignment = Element.ALIGN_UNDEFINED; 105 106 107 protected int verticalAlignment = Element.ALIGN_UNDEFINED; 108 109 113 protected float width; 114 protected boolean percentage = false; 115 116 117 protected int colspan = 1; 118 119 120 protected int rowspan = 1; 121 122 123 float leading = Float.NaN; 124 125 126 protected boolean header; 127 128 133 protected int maxLines = Integer.MAX_VALUE; 134 135 142 String showTruncation; 143 144 149 protected boolean useAscender = false; 150 151 156 protected boolean useDescender = false; 157 158 162 protected boolean useBorderPadding; 163 164 165 protected boolean groupChange = true; 166 167 169 170 public Cell() { 171 super(0, 0, 0, 0); 173 setBorder(UNDEFINED); 174 setBorderWidth(0.5f); 175 arrayList = new ArrayList (); 177 } 178 179 184 public Cell(boolean dummy) { 185 this(); 186 arrayList.add(new Paragraph(0)); 187 } 188 189 194 public Cell(String content) { 195 this(); 196 try { 197 addElement(new Paragraph(content)); 198 } 199 catch(BadElementException bee) { 200 } 201 } 202 203 211 public Cell(Element element) throws BadElementException { 212 this(); 213 if(element instanceof Phrase) { 214 setLeading(((Phrase)element).getLeading()); 215 } 216 addElement(element); 217 } 218 219 221 228 public boolean process(ElementListener listener) { 229 try { 230 return listener.add(this); 231 } 232 catch(DocumentException de) { 233 return false; 234 } 235 } 236 237 242 public int type() { 243 return Element.CELL; 244 } 245 246 251 public ArrayList getChunks() { 252 ArrayList tmp = new ArrayList (); 253 for (Iterator i = arrayList.iterator(); i.hasNext(); ) { 254 tmp.addAll(((Element) i.next()).getChunks()); 255 } 256 return tmp; 257 } 258 259 261 266 public int getHorizontalAlignment() { 267 return horizontalAlignment; 268 } 269 270 274 public void setHorizontalAlignment(int value) { 275 horizontalAlignment = value; 276 } 277 278 283 public void setHorizontalAlignment(String alignment) { 284 setHorizontalAlignment(ElementTags.alignmentValue(alignment)); 285 } 286 287 291 public int getVerticalAlignment() { 292 return verticalAlignment; 293 } 294 295 299 public void setVerticalAlignment(int value) { 300 verticalAlignment = value; 301 } 302 303 308 public void setVerticalAlignment(String alignment) { 309 setVerticalAlignment(ElementTags.alignmentValue(alignment)); 310 } 311 312 317 public void setWidth(float value) { 318 this.width = value; 319 } 320 321 327 public void setWidth(String value) { 328 if (value.endsWith("%")) { 329 value = value.substring(0, value.length() - 1); 330 percentage = true; 331 } 332 width = Integer.parseInt(value); 333 } 334 335 338 public float getWidth() { 339 return width; 340 } 341 342 347 public String getWidthAsString() { 348 String w = String.valueOf(width); 349 if (w.endsWith(".0")) w = w.substring(0, w.length() - 2); 350 if (percentage) w += "%"; 351 return w; 352 } 353 354 359 public void setColspan(int value) { 360 colspan = value; 361 } 362 363 367 public int getColspan() { 368 return colspan; 369 } 370 371 376 public void setRowspan(int value) { 377 rowspan = value; 378 } 379 380 384 public int getRowspan() { 385 return rowspan; 386 } 387 388 393 public void setLeading(float value) { 394 leading = value; 395 } 396 397 402 public float getLeading() { 403 if (Float.isNaN(leading)) { 404 return 16; 405 } 406 return leading; 407 } 408 409 414 public void setHeader(boolean value) { 415 header = value; 416 } 417 418 423 public boolean isHeader() { 424 return header; 425 } 426 427 431 public void setMaxLines(int value) { 432 maxLines = value; 433 } 434 435 439 public int getMaxLines() { 440 return maxLines; 441 } 442 443 447 public void setShowTruncation(String value) { 448 showTruncation = value; 449 } 450 451 455 public String getShowTruncation() { 456 return showTruncation; 457 } 458 459 463 public void setUseAscender(boolean use) { 464 useAscender = use; 465 } 466 467 471 public boolean isUseAscender() { 472 return useAscender; 473 } 474 475 479 public void setUseDescender(boolean use) { 480 useDescender = use; 481 } 482 483 487 public boolean isUseDescender() { 488 return useDescender; 489 } 490 491 495 public void setUseBorderPadding(boolean use) { 496 useBorderPadding = use; 497 } 498 499 503 public boolean isUseBorderPadding() { 504 return useBorderPadding; 505 } 506 507 512 public boolean getGroupChange() { 513 return groupChange; 514 } 515 516 521 public void setGroupChange(boolean value) { 522 groupChange = value; 523 } 524 525 527 532 public int size() { 533 return arrayList.size(); 534 } 535 536 541 public Iterator getElements() { 542 return arrayList.iterator(); 543 } 544 545 548 public void clear() { 549 arrayList.clear(); 550 } 551 552 557 public boolean isEmpty() { 558 switch(size()) { 559 case 0: 560 return true; 561 case 1: 562 Element element = (Element) arrayList.get(0); 563 switch (element.type()) { 564 case Element.CHUNK: 565 return ((Chunk) element).isEmpty(); 566 case Element.ANCHOR: 567 case Element.PHRASE: 568 case Element.PARAGRAPH: 569 return ((Phrase) element).isEmpty(); 570 case Element.LIST: 571 return ((List) element).isEmpty(); 572 } 573 return false; 574 default: 575 return false; 576 } 577 } 578 579 584 void fill() { 585 if (size() == 0) arrayList.add(new Paragraph(0)); 586 } 587 588 593 public boolean isTable() { 594 return (size() == 1) 595 && (((Element)arrayList.get(0)).type() == Element.TABLE); 596 } 597 598 607 public void addElement(Element element) throws BadElementException { 608 if (isTable()) { 609 Table table = (Table) arrayList.get(0); 610 Cell tmp = new Cell(element); 611 tmp.setBorder(NO_BORDER); 612 tmp.setColspan(table.getColumns()); 613 table.addCell(tmp); 614 return; 615 } 616 switch(element.type()) { 617 case Element.LISTITEM: 618 case Element.ROW: 619 case Element.CELL: 620 throw new BadElementException("You can't add listitems, rows or cells to a cell."); 621 case Element.LIST: 622 List list = (List)element; 623 if (Float.isNaN(leading)) { 624 setLeading(list.getTotalLeading()); 625 } 626 if (list.isEmpty()) return; 627 arrayList.add(element); 628 return; 629 case Element.ANCHOR: 630 case Element.PARAGRAPH: 631 case Element.PHRASE: 632 Phrase p = (Phrase)element; 633 if (Float.isNaN(leading)) { 634 setLeading(p.getLeading()); 635 } 636 if (p.isEmpty()) return; 637 arrayList.add(element); 638 return; 639 case Element.CHUNK: 640 if (((Chunk) element).isEmpty()) return; 641 arrayList.add(element); 642 return; 643 case Element.TABLE: 644 Table table = new Table(3); 645 float[] widths = new float[3]; 646 widths[1] = ((Table)element).getWidth(); 647 switch(((Table)element).getAlignment()) { 648 case Element.ALIGN_LEFT: 649 widths[0] = 0f; 650 widths[2] = 100f - widths[1]; 651 break; 652 case Element.ALIGN_CENTER: 653 widths[0] = (100f - widths[1]) / 2f; 654 widths[2] = widths[0]; 655 break; 656 case Element.ALIGN_RIGHT: 657 widths[0] = 100f - widths[1]; 658 widths[2] = 0f; 659 } 660 table.setWidths(widths); 661 Cell tmp; 662 if (arrayList.isEmpty()) { 663 table.addCell(getDummyCell()); 664 } 665 else { 666 tmp = new Cell(); 667 tmp.setBorder(NO_BORDER); 668 tmp.setColspan(3); 669 for (Iterator i = arrayList.iterator(); i.hasNext(); ) { 670 tmp.add(i.next()); 671 } 672 table.addCell(tmp); 673 } 674 tmp = new Cell(); 675 tmp.setBorder(NO_BORDER); 676 table.addCell(tmp); 677 table.insertTable((Table)element); 678 tmp = new Cell(); 679 tmp.setBorder(NO_BORDER); 680 table.addCell(tmp); 681 table.addCell(getDummyCell()); 682 clear(); 683 arrayList.add(table); 684 return; 685 default: 686 arrayList.add(element); 687 } 688 } 689 690 696 public boolean add(Object o) { 697 try { 698 this.addElement((Element) o); 699 return true; 700 } 701 catch(ClassCastException cce) { 702 throw new ClassCastException ("You can only add objects that implement the Element interface."); 703 } 704 catch(BadElementException bee) { 705 throw new ClassCastException (bee.getMessage()); 706 } 707 } 708 709 711 715 private static Cell getDummyCell() { 716 Cell cell = new Cell(true); 717 cell.setColspan(3); 718 cell.setBorder(NO_BORDER); 719 return cell; 720 } 721 722 727 public PdfPCell createPdfPCell() throws BadElementException { 728 if (rowspan > 1) throw new BadElementException("PdfPCells can't have a rowspan > 1"); 729 if (isTable()) return new PdfPCell(((Table)arrayList.get(0)).createPdfPTable()); 730 PdfPCell cell = new PdfPCell(); 731 cell.setVerticalAlignment(verticalAlignment); 732 cell.setHorizontalAlignment(horizontalAlignment); 733 cell.setColspan(colspan); 734 cell.setUseBorderPadding(useBorderPadding); 735 cell.setUseDescender(useDescender); 736 cell.setLeading(getLeading(), 0); 737 cell.cloneNonPositionParameters(this); 738 cell.setNoWrap(getMaxLines() == 1); 739 for (Iterator i = getElements(); i.hasNext(); ) { 740 Element e = (Element)i.next(); 741 if (e.type() == Element.PHRASE || e.type() == Element.PARAGRAPH) { 742 Paragraph p = new Paragraph((Phrase)e); 743 p.setAlignment(horizontalAlignment); 744 e = p; 745 } 746 cell.addElement(e); 747 } 748 return cell; 749 } 750 751 753 758 public float top() { 759 return getTop(); 760 } 761 762 766 public float getTop() { 767 throw new UnsupportedOperationException ("Dimensions of a Cell can't be calculated. See the FAQ."); 768 } 769 770 775 public float bottom() { 776 return getBottom(); 777 } 778 779 783 public float getBottom() { 784 throw new UnsupportedOperationException ("Dimensions of a Cell can't be calculated. See the FAQ."); 785 } 786 787 792 public float left() { 793 return getLeft(); 794 } 795 796 800 public float getLeft() { 801 throw new UnsupportedOperationException ("Dimensions of a Cell can't be calculated. See the FAQ."); 802 } 803 804 809 public float right() { 810 return getRight(); 811 } 812 813 817 public float getRight() { 818 throw new UnsupportedOperationException ("Dimensions of a Cell can't be calculated. See the FAQ."); 819 } 820 821 826 public float top(int margin) { 827 throw new UnsupportedOperationException ("Dimensions of a Cell can't be calculated. See the FAQ."); 828 } 829 830 835 public float bottom(int margin) { 836 throw new UnsupportedOperationException ("Dimensions of a Cell can't be calculated. See the FAQ."); 837 } 838 839 844 public float left(int margin) { 845 throw new UnsupportedOperationException ("Dimensions of a Cell can't be calculated. See the FAQ."); 846 } 847 848 853 public float right(int margin) { 854 throw new UnsupportedOperationException ("Dimensions of a Cell can't be calculated. See the FAQ."); 855 } 856 857 861 public void setTop(int value) { 862 throw new UnsupportedOperationException ("Dimensions of a Cell are attributed automagically. See the FAQ."); 863 } 864 865 869 public void setBottom(int value) { 870 throw new UnsupportedOperationException ("Dimensions of a Cell are attributed automagically. See the FAQ."); 871 } 872 873 877 public void setLeft(int value) { 878 throw new UnsupportedOperationException ("Dimensions of a Cell are attributed automagically. See the FAQ."); 879 } 880 881 885 public void setRight(int value) { 886 throw new UnsupportedOperationException ("Dimensions of a Cell are attributed automagically. See the FAQ."); 887 } 888 889 891 898 public Cell(java.util.Properties attributes) { 899 this(); 900 Cell tmp = com.lowagie.text.factories.ElementFactory.getCell(attributes); 901 this.cloneNonPositionParameters(tmp); 902 this.setHorizontalAlignment(tmp.getHorizontalAlignment()); 903 this.setVerticalAlignment(tmp.getVerticalAlignment()); 904 this.setWidth(tmp.getWidth()); 905 this.setColspan(tmp.getColspan()); 906 this.setRowspan(tmp.getRowspan()); 907 this.setLeading(tmp.getLeading()); 908 this.setHeader(tmp.isHeader()); 909 this.setMaxLines(tmp.getMaxLines()); 910 } 911 912 917 public int horizontalAlignment() { 918 return getHorizontalAlignment(); 919 } 920 921 926 public int verticalAlignment() { 927 return getVerticalAlignment(); 928 } 929 930 936 public String cellWidth() { 937 return getWidthAsString(); 938 } 939 940 945 public int colspan() { 946 return getColspan(); 947 } 948 949 954 public int rowspan() { 955 return getRowspan(); 956 } 957 958 964 public float leading() { 965 return getLeading(); 966 } 967 968 974 public boolean header() { 975 return isHeader(); 976 } 977 978 984 public void setNoWrap(boolean value) { 985 if (value) 986 maxLines = 1; 987 else 988 maxLines = Integer.MAX_VALUE; 989 } 990 991 997 public boolean noWrap() { 998 return maxLines == 1; 999 } 1000} 1001 | Popular Tags |