| 1 53 54 package com.lowagie.text; 55 56 import java.awt.Color ; 57 import java.awt.Dimension ; 58 import java.awt.Point ; 59 import java.util.ArrayList ; 60 import java.util.Iterator ; 61 62 import com.lowagie.text.pdf.PdfPCell; 63 import com.lowagie.text.pdf.PdfPTable; 64 65 154 155 public class Table extends Rectangle { 156 157 159 160 private int columns; 161 162 163 private ArrayList rows = new ArrayList (); 164 165 166 private Point curPosition = new Point (0, 0); 167 168 169 private Cell defaultLayout = new Cell(true); 170 171 172 private int lastHeaderRow = -1; 173 174 175 private int alignment = Element.ALIGN_CENTER; 176 177 178 private float cellpadding; 179 180 181 private float cellspacing; 182 183 184 private float width = 80; 185 186 187 private boolean locked = false; 188 189 190 private float[] widths; 191 192 193 private boolean mTableInserted = false; 194 195 199 protected boolean autoFillEmptyCells = false; 200 201 202 boolean tableFitsPage = false; 203 204 205 boolean cellsFitPage = false; 206 207 208 float offset = Float.NaN; 209 210 211 protected boolean convert2pdfptable = false; 212 213 215 221 public Table(int columns) throws BadElementException { 222 this(columns, 1); 223 } 224 225 233 public Table(int columns, int rows) throws BadElementException { 234 super(0, 0, 0, 0); 236 setBorder(BOX); 237 setBorderWidth(1); 238 defaultLayout.setBorder(BOX); 239 240 if (columns <= 0) { 242 throw new BadElementException("A table should have at least 1 column."); 243 } 244 this.columns = columns; 245 246 for (int i = 0; i < rows; i++) { 248 this.rows.add(new Row(columns)); 249 } 250 curPosition = new Point (0, 0); 251 252 widths = new float[columns]; 254 float width = 100f / columns; 255 for (int i = 0; i < columns; i++) { 256 widths[i] = width; 257 } 258 } 259 260 264 public Table(Table t) { 265 super(0, 0, 0, 0); 266 this.cloneNonPositionParameters(t); 267 this.columns = t.columns; 268 this.rows = t.rows; 269 this.curPosition = t.curPosition; 270 this.defaultLayout = t.defaultLayout; 271 this.lastHeaderRow = t.lastHeaderRow; 272 this.alignment = t.alignment; 273 this.cellpadding = t.cellpadding; 274 this.cellspacing = t.cellspacing; 275 this.width = t.width; 276 this.widths = t.widths; 277 this.autoFillEmptyCells = t.autoFillEmptyCells; 278 this.tableFitsPage = t.tableFitsPage; 279 this.cellsFitPage = t.cellsFitPage; 280 this.offset = t.offset; 281 this.convert2pdfptable = t.convert2pdfptable; 282 } 283 284 286 293 public boolean process(ElementListener listener) { 294 try { 295 return listener.add(this); 296 } 297 catch(DocumentException de) { 298 return false; 299 } 300 } 301 302 307 public int type() { 308 return Element.TABLE; 309 } 310 311 316 317 public ArrayList getChunks() { 318 return new ArrayList (); 319 } 320 321 323 328 public int getColumns() { 329 return columns; 330 } 331 332 337 public int size() { 338 return rows.size(); 339 } 340 341 346 public Dimension getDimension() { 347 return new Dimension (columns, size()); 348 } 349 350 354 public Cell getDefaultLayout() { 355 return defaultLayout; 356 } 357 358 363 public void setDefaultLayout(Cell value) { 364 defaultLayout = value; 365 } 366 367 372 public int getLastHeaderRow() { 373 return this.lastHeaderRow; 374 } 375 376 381 public void setLastHeaderRow(int value) { 382 lastHeaderRow = value; 383 } 384 385 390 public int endHeaders() { 391 lastHeaderRow = curPosition.x - 1; 392 return lastHeaderRow; 393 } 394 395 400 public int getAlignment() { 401 return alignment; 402 } 403 404 409 public void setAlignment(int value) { 410 alignment = value; 411 } 412 413 418 public void setAlignment(String alignment) { 419 if (ElementTags.ALIGN_LEFT.equalsIgnoreCase(alignment)) { 420 this.alignment = Element.ALIGN_LEFT; 421 return; 422 } 423 if (ElementTags.RIGHT.equalsIgnoreCase(alignment)) { 424 this.alignment = Element.ALIGN_RIGHT; 425 return; 426 } 427 this.alignment = Element.ALIGN_CENTER; 428 } 429 430 435 public float getPadding() { 436 return cellpadding; 437 } 438 439 444 public void setPadding(float value) { 445 cellpadding = value; 446 } 447 448 453 public float getSpacing() { 454 return cellspacing; 455 } 456 457 462 public void setSpacing(float value) { 463 cellspacing = value; 464 } 465 466 474 public void setAutoFillEmptyCells(boolean aDoAutoFill) { 475 autoFillEmptyCells = aDoAutoFill; 476 } 477 478 483 public float getWidth() { 484 return width; 485 } 486 487 492 public void setWidth(float width) { 493 this.width = width; 494 } 495 496 499 public boolean isLocked() { 500 return locked; 501 } 502 503 506 public void setLocked(boolean locked) { 507 this.locked = locked; 508 } 509 510 515 public float[] getProportionalWidths() { 516 return widths; 517 } 518 519 537 public void setWidths(float[] widths) throws BadElementException { 538 if (widths.length != columns) { 539 throw new BadElementException("Wrong number of columns."); 540 } 541 542 float hundredPercent = 0; 544 for (int i = 0; i < columns; i++) { 545 hundredPercent += widths[i]; 546 } 547 548 float width; 550 this.widths[columns - 1] = 100; 551 for (int i = 0; i < columns - 1; i++) { 552 width = (100.0f * widths[i]) / hundredPercent; 553 this.widths[i] = width; 554 this.widths[columns - 1] -= width; 555 } 556 } 557 558 568 public void setWidths(int[] widths) throws DocumentException { 569 float tb[] = new float[widths.length]; 570 for (int k = 0; k < widths.length; ++k) 571 tb[k] = widths[k]; 572 setWidths(tb); 573 } 574 575 580 public boolean isTableFitsPage() { 581 return tableFitsPage; 582 } 583 584 592 public void setTableFitsPage(boolean fitPage) { 593 this.tableFitsPage = fitPage; 594 if (fitPage) setCellsFitPage(true); 595 } 596 597 602 public boolean isCellsFitPage() { 603 return cellsFitPage; 604 } 605 606 614 public void setCellsFitPage(boolean fitPage) { 615 this.cellsFitPage = fitPage; 616 } 617 618 628 public void setOffset(float offset) { 629 this.offset = offset; 630 } 631 632 637 public float getOffset() { 638 return offset; 639 } 640 641 645 public boolean isConvert2pdfptable() { 646 return convert2pdfptable; 647 } 648 652 public void setConvert2pdfptable(boolean convert2pdfptable) { 653 this.convert2pdfptable = convert2pdfptable; 654 } 655 656 658 666 public void addCell(Cell aCell, int row, int column) throws BadElementException { 667 addCell(aCell, new Point (row,column)); 668 } 669 670 677 public void addCell(Cell aCell, Point aLocation) throws BadElementException { 678 if (aCell == null) throw new NullPointerException ("addCell - cell has null-value"); 679 if (aLocation == null) throw new NullPointerException ("addCell - point has null-value"); 680 if (aCell.isTable()) insertTable((Table)aCell.getElements().next(), aLocation); 681 682 if (aLocation.x < 0) throw new BadElementException("row coordinate of location must be >= 0"); 683 if ((aLocation.y <= 0) && (aLocation.y > columns)) throw new BadElementException("column coordinate of location must be >= 0 and < nr of columns"); 684 if (!isValidLocation(aCell, aLocation)) throw new BadElementException("Adding a cell at the location (" + aLocation.x + "," + aLocation.y + ") with a colspan of " + aCell.getColspan() + " and a rowspan of " + aCell.getRowspan() + " is illegal (beyond boundaries/overlapping)."); 685 686 if (aCell.getBorder() == UNDEFINED) aCell.setBorder(defaultLayout.getBorder()); 687 aCell.fill(); 688 placeCell(rows, aCell, aLocation); 689 setCurrentLocationToNextValidPosition(aLocation); 690 } 691 692 697 public void addCell(Cell cell) { 698 try { 699 addCell(cell, curPosition); 700 } 701 catch(BadElementException bee) { 702 } 704 } 705 706 715 public void addCell(Phrase content) throws BadElementException { 716 addCell(content, curPosition); 717 } 718 719 729 public void addCell(Phrase content, Point location) throws BadElementException { 730 Cell cell = new Cell(content); 731 cell.setBorder(defaultLayout.getBorder()); 732 cell.setBorderWidth(defaultLayout.getBorderWidth()); 733 cell.setBorderColor(defaultLayout.getBorderColor()); 734 cell.setBackgroundColor(defaultLayout.getBackgroundColor()); 735 cell.setHorizontalAlignment(defaultLayout.getHorizontalAlignment()); 736 cell.setVerticalAlignment(defaultLayout.getVerticalAlignment()); 737 cell.setColspan(defaultLayout.getColspan()); 738 cell.setRowspan(defaultLayout.getRowspan()); 739 addCell(cell, location); 740 } 741 742 751 752 public void addCell(String content) throws BadElementException { 753 addCell(new Phrase(content), curPosition); 754 } 755 756 766 public void addCell(String content, Point location) throws BadElementException { 767 addCell(new Phrase(content), location); 768 } 769 770 776 public void insertTable(Table aTable) { 777 if (aTable == null) throw new NullPointerException ("insertTable - table has null-value"); 778 insertTable(aTable, curPosition); 779 } 780 781 789 public void insertTable(Table aTable, int row, int column) { 790 if (aTable == null) throw new NullPointerException ("insertTable - table has null-value"); 791 insertTable(aTable, new Point (row, column)); 792 } 793 794 801 public void insertTable(Table aTable, Point aLocation) { 802 803 if (aTable == null) throw new NullPointerException ("insertTable - table has null-value"); 804 if (aLocation == null) throw new NullPointerException ("insertTable - point has null-value"); 805 mTableInserted = true; 806 aTable.complete(); 807 808 if (aLocation.y > columns) { 809 throw new IllegalArgumentException ("insertTable -- wrong columnposition("+ aLocation.y + ") of location; max =" + columns); 810 } 811 812 int rowCount = aLocation.x + 1 - rows.size(); 813 int i = 0; 814 if ( rowCount > 0 ) { for (; i < rowCount; i++) { 816 rows.add(new Row(columns)); 817 } 818 } 819 820 ((Row) rows.get(aLocation.x)).setElement(aTable,aLocation.y); 821 822 setCurrentLocationToNextValidPosition(aLocation); 823 } 824 825 830 public void addColumns(int aColumns) { 831 ArrayList newRows = new ArrayList (rows.size()); 832 833 int newColumns = columns + aColumns; 834 Row row; 835 for (int i = 0; i < rows.size(); i++) { 836 row = new Row(newColumns); 837 for (int j = 0; j < columns; j++) { 838 row.setElement(((Row) rows.get(i)).getCell(j) ,j); 839 } 840 for (int j = columns; j < newColumns && i < curPosition.x; j++) { 841 row.setElement(null, j); 842 } 843 newRows.add(row); 844 } 845 float [] newWidths = new float[newColumns]; 847 System.arraycopy(widths, 0, newWidths, 0, columns); 848 for (int j = columns; j < newColumns ; j++) { 849 newWidths[j] = 0; 850 } 851 columns = newColumns; 852 widths = newWidths; 853 rows = newRows; 854 } 855 856 862 public void deleteColumn(int column) throws BadElementException { 863 float newWidths[] = new float[--columns]; 864 System.arraycopy(widths, 0, newWidths, 0, column); 865 System.arraycopy(widths, column + 1, newWidths, column, columns - column); 866 setWidths(newWidths); 867 System.arraycopy(widths, 0, newWidths, 0, columns); 868 widths = newWidths; 869 Row row; 870 int size = rows.size(); 871 for (int i = 0; i < size; i++) { 872 row = (Row) rows.get(i); 873 row.deleteColumn(column); 874 rows.set(i, row); 875 } 876 if (column == columns) { 877 curPosition.setLocation(curPosition.x+1, 0); 878 } 879 } 880 881 887 public boolean deleteRow(int row) { 888 if (row < 0 || row >= rows.size()) { 889 return false; 890 } 891 rows.remove(row); 892 curPosition.setLocation(curPosition.x-1, curPosition.y); 893 return true; 894 } 895 896 900 public void deleteAllRows() { 901 rows.clear(); 902 rows.add(new Row(columns)); 903 curPosition.setLocation(0, 0); 904 lastHeaderRow = -1; 905 } 906 907 912 public boolean deleteLastRow() { 913 return deleteRow(rows.size() - 1); 914 } 915 916 919 public void complete() { 920 if (mTableInserted) { 921 mergeInsertedTables(); mTableInserted = false; 923 } 924 if (autoFillEmptyCells) { 925 fillEmptyMatrixCells(); 926 } 927 } 928 929 931 939 private Object getElement(int row, int column) { 940 return ((Row) rows.get(row)).getCell(column); 941 } 942 943 946 private void mergeInsertedTables() { 947 int i=0, j=0; 948 float [] lNewWidths = null; 949 int [] lDummyWidths = new int[columns]; float[][] lDummyColumnWidths = new float[columns][]; int [] lDummyHeights = new int[rows.size()]; ArrayList newRows = null; 953 boolean isTable=false; 954 int lTotalRows = 0, lTotalColumns = 0; 955 int lNewMaxRows = 0, lNewMaxColumns = 0; 956 957 Table lDummyTable = null; 958 959 for (j=0; j < columns; j++) { 963 lNewMaxColumns = 1; float [] tmpWidths = null; 965 for (i=0; i < rows.size(); i++) { 966 if ( Table.class.isInstance(((Row) rows.get(i)).getCell(j)) ) { 967 isTable=true; 968 lDummyTable = ((Table) ((Row) rows.get(i)).getCell(j)); 969 if( tmpWidths == null) { 970 tmpWidths = lDummyTable.widths; 971 lNewMaxColumns=tmpWidths.length; 972 } 973 &n
|