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 else { 974 int cols = lDummyTable.getDimension().width; 975 float [] tmpWidthsN = new float[ cols * tmpWidths.length]; 976 float tpW=0, btW=0, totW=0; 977 int tpI=0, btI=0, totI=0; 978 tpW+=tmpWidths[0]; 979 btW+=lDummyTable.widths[0]; 980 while( tpI<tmpWidths.length && btI<cols) { 981 if( btW>tpW) { 982 tmpWidthsN[totI] = tpW-totW; 983 tpI++; 984 if(tpI<tmpWidths.length) { 985 tpW+=tmpWidths[tpI]; 986 } 987 } 988 else { 989 tmpWidthsN[totI] = btW-totW; 990 btI++; 991 if(Math.abs(btW - tpW) < 0.0001) { 992 tpI++; 993 if(tpI<tmpWidths.length) { 994 tpW+=tmpWidths[tpI]; 995 } 996 } 997 if(btI<cols) { 998 btW+=lDummyTable.widths[btI]; 999 } 1000 } 1001 totW+=tmpWidthsN[totI]; 1002 totI++; 1003 } 1004 1013 tmpWidths = new float[totI]; 1014 System.arraycopy(tmpWidthsN, 0, tmpWidths, 0, totI); 1015 lNewMaxColumns=totI; 1016 } 1017 1022 } 1023 } 1024 lDummyColumnWidths[j] = tmpWidths; 1025 lTotalColumns += lNewMaxColumns; 1026 lDummyWidths [j] = lNewMaxColumns; 1027 } 1028 1029 for (i=0; i < rows.size(); i++) { 1031 lNewMaxRows = 1; for (j=0; j < columns; j++) { 1033 if ( Table.class.isInstance(((Row) rows.get(i)).getCell(j)) ) { 1034 isTable=true; 1035 lDummyTable = (Table) ((Row) rows.get(i)).getCell(j); 1036 if ( lDummyTable.getDimension().height > lNewMaxRows ) { 1037 lNewMaxRows = lDummyTable.getDimension().height; 1038 } 1039 } 1040 } 1041 lTotalRows += lNewMaxRows; 1042 lDummyHeights [i] = lNewMaxRows; 1043 } 1044 1045 if ( (lTotalColumns != columns) || (lTotalRows != rows.size()) || isTable) { 1047 lNewWidths = new float [lTotalColumns]; 1052 int lDummy = 0; 1053 for (int tel=0; tel < widths.length;tel++) { 1054 if ( lDummyWidths[tel] != 1) { 1055 for (int tel2 = 0; tel2 < lDummyWidths[tel]; tel2++) { 1057 lNewWidths[lDummy] = widths[tel] * lDummyColumnWidths[tel][tel2] / 100f; lDummy++; 1060 } 1061 } 1062 else { 1063 lNewWidths[lDummy] = widths[tel]; 1064 lDummy++; 1065 } 1066 } 1067 1068 newRows = new ArrayList (lTotalRows); 1073 for (i = 0; i < lTotalRows; i++) { 1074 newRows.add(new Row(lTotalColumns)); 1075 } 1076 int lDummyRow = 0, lDummyColumn = 0; Object lDummyElement = null; 1078 for (i=0; i < rows.size(); i++) { 1079 lDummyColumn = 0; 1080 lNewMaxRows = 1; 1081 for (j=0; j < columns; j++) { 1082 if ( Table.class.isInstance(((Row) rows.get(i)).getCell(j)) ) { 1084 lDummyTable = (Table) ((Row) rows.get(i)).getCell(j); 1085 1086 int colMap[] = new int[lDummyTable.widths.length+1]; 1088 int cb=0, ct=0; 1089 1090 for( ; cb<lDummyTable.widths.length;cb++) { 1091 colMap[cb] = lDummyColumn+ct; 1092 1093 float wb; 1094 wb = lDummyTable.widths[cb]; 1095 1096 float wt=0; 1097 while( ct<lDummyWidths[j]) { 1098 wt+=lDummyColumnWidths[j][ct++]; 1099 if(Math.abs(wb - wt) < 0.0001) break; 1100 } 1101 } 1102 colMap[cb] = lDummyColumn+ct; 1103 1104 for (int k=0; k < lDummyTable.getDimension().height; k++) { 1106 for (int l=0; l < lDummyTable.getDimension().width; l++) { 1107 lDummyElement = lDummyTable.getElement(k,l); 1108 if (lDummyElement != null) { 1109 int col=lDummyColumn+l; 1110 1111 if ( Cell.class.isInstance(lDummyElement) ) { 1112 Cell lDummyC = (Cell)lDummyElement; 1113 col = colMap[l]; 1115 int ot = colMap[l+lDummyC.getColspan()]; 1116 1117 lDummyC.setColspan(ot-col); 1118 } 1119 1120 ((Row) newRows.get(k + lDummyRow)).addElement(lDummyElement,col); } 1122 } 1123 } 1124 } 1125 else { 1127 Object aElement = getElement(i,j); 1128 1129 if ( Cell.class.isInstance(aElement) ) { 1130 1131 ((Cell) aElement).setRowspan(((Cell) ((Row) rows.get(i)).getCell(j)).getRowspan() + lDummyHeights[i] - 1); 1133 ((Cell) aElement).setColspan(((Cell) ((Row) rows.get(i)).getCell(j)).getColspan() + lDummyWidths[j] - 1); 1134 1135 placeCell(newRows,((Cell) aElement), new Point (lDummyRow,lDummyColumn)); 1137 } 1138 } 1139 lDummyColumn += lDummyWidths[j]; 1140 } 1141 lDummyRow += lDummyHeights[i]; 1142 } 1143 1144 columns = lTotalColumns; 1146 rows = newRows; 1147 this.widths = lNewWidths; 1148 } 1149 } 1150 1151 1154 private void fillEmptyMatrixCells() { 1155 try { 1156 for (int i=0; i < rows.size(); i++) { 1157 for (int j=0; j < columns; j++) { 1158 if (!((Row) rows.get(i)).isReserved(j)) { 1159 addCell(defaultLayout, new Point (i, j)); 1160 } 1161 } 1162 } 1163 } 1164 catch(BadElementException bee) { 1165 throw new ExceptionConverter(bee); 1166 } 1167 } 1168 1169 1179 private boolean isValidLocation(Cell aCell, Point aLocation) { 1180 if ( aLocation.x < rows.size() ) { 1183 if ((aLocation.y + aCell.getColspan()) > columns) { 1184 return false; 1185 } 1186 1187 int difx = ((rows.size() - aLocation.x) > aCell.getRowspan()) ? aCell.getRowspan() : rows.size() - aLocation.x; 1188 int dify = ((columns - aLocation.y) > aCell.getColspan()) ? aCell.getColspan() : columns - aLocation.y; 1189 for (int i=aLocation.x; i < (aLocation.x + difx); i++) { 1191 for (int j=aLocation.y; j < (aLocation.y + dify); j++) { 1192 if (((Row) rows.get(i)).isReserved(j)) { 1193 return false; 1194 } 1195 } 1196 } 1197 } 1198 else { 1199 if ((aLocation.y + aCell.getColspan()) > columns) { 1200 return false; 1201 } 1202 } 1203 1204 return true; 1205 } 1206 1207 1212 private void assumeTableDefaults(Cell aCell) { 1213 1214 if (aCell.getBorder() == Rectangle.UNDEFINED) { 1215 aCell.setBorder(defaultLayout.getBorder()); 1216 } 1217 if (aCell.getBorderWidth() == Rectangle.UNDEFINED) { 1218 aCell.setBorderWidth(defaultLayout.getBorderWidth()); 1219 } 1220 if (aCell.getBorderColor() == null) { 1221 aCell.setBorderColor(defaultLayout.getBorderColor()); 1222 } 1223 if (aCell.getBackgroundColor() == null) { 1224 aCell.setBackgroundColor(defaultLayout.getBackgroundColor()); 1225 } 1226 if (aCell.getHorizontalAlignment() == Element.ALIGN_UNDEFINED) { 1227 aCell.setHorizontalAlignment(defaultLayout.getHorizontalAlignment()); 1228 } 1229 if (aCell.getVerticalAlignment() == Element.ALIGN_UNDEFINED) { 1230 aCell.setVerticalAlignment(defaultLayout.getVerticalAlignment()); 1231 } 1232 } 1233 1234 1241 private void placeCell(ArrayList someRows, Cell aCell, Point aPosition) { 1242 int i; 1243 Row row = null; 1244 int lColumns = ((Row) someRows.get(0)).getColumns(); 1245 int rowCount = aPosition.x + aCell.getRowspan() - someRows.size(); 1246 assumeTableDefaults(aCell); 1247 if ( (aPosition.x + aCell.getRowspan()) > someRows.size() ) { 1248 for (i = 0; i < rowCount; i++) { 1249 row = new Row(lColumns); 1250 someRows.add(row); 1251 } 1252 } 1253 1254 for (i = aPosition.x + 1; i < (aPosition.x + aCell.getRowspan()); i++) { 1256 if ( !((Row) someRows.get(i)).reserve(aPosition.y, aCell.getColspan())) { 1257 1258 throw new RuntimeException ("addCell - error in reserve"); 1260 } 1261 } 1262 row = (Row) someRows.get(aPosition.x); 1263 row.addElement(aCell, aPosition.y); 1264 1265 } 1266 1267 1271 private void setCurrentLocationToNextValidPosition(Point aLocation) { 1272 int i, j; 1274 i = aLocation.x; 1275 j = aLocation.y; 1276 do { 1277 if ( (j + 1) == columns ) { i++; 1279 j = 0; 1280 } 1281 else { 1282 j++; 1283 } 1284 } 1285 while ( 1286 (i < rows.size()) && (j < columns) && (((Row) rows.get(i)).isReserved(j)) 1287 ); 1288 curPosition = new Point (i, j); 1289 } 1290 1291 1293 1304 public float[] getWidths(float left, float totalWidth) { 1305 float[] w = new float[columns + 1]; 1307 float wPercentage; 1308 if (locked) { 1309 wPercentage = 100 * width / totalWidth; 1310 } 1311 else { 1312 wPercentage = width; 1313 } 1314 switch(alignment) { 1316 case Element.ALIGN_LEFT: 1317 w[0] = left; 1318 break; 1319 case Element.ALIGN_RIGHT: 1320 w[0] = left + (totalWidth * (100 - wPercentage)) / 100; 1321 break; 1322 case Element.ALIGN_CENTER: 1323 default: 1324 w[0] = left + (totalWidth * (100 - wPercentage)) / 200; 1325 } 1326 totalWidth = (totalWidth * wPercentage) / 100; 1328 for (int i = 1; i < columns; i++) { 1330 w[i] = w[i - 1] + (widths[i - 1] * totalWidth / 100); 1331 } 1332 w[columns] = w[0] + totalWidth; 1334 return w; 1335 } 1336 1337 1342 public Iterator iterator() { 1343 return rows.iterator(); 1344 } 1345 1346 1351 public PdfPTable createPdfPTable() throws BadElementException { 1352 if (!convert2pdfptable) { 1353 throw new BadElementException("No error, just an old style table"); 1354 } 1355 setAutoFillEmptyCells(true); 1356 complete(); 1357 PdfPTable pdfptable = new PdfPTable(widths); 1358 pdfptable.setTableEvent(SimpleTable.getDimensionlessInstance(this, cellspacing)); 1359 pdfptable.setHeaderRows(lastHeaderRow + 1); 1360 pdfptable.setSplitLate(cellsFitPage); 1361 pdfptable.setKeepTogether(tableFitsPage); 1362 if (!Float.isNaN(offset)) { 1363 pdfptable.setSpacingBefore(offset); 1364 } 1365 pdfptable.setHorizontalAlignment(alignment); 1366 if (locked) { 1367 pdfptable.setTotalWidth(width); 1368 pdfptable.setLockedWidth(true); 1369 } 1370 else { 1371 pdfptable.setWidthPercentage(width); 1372 } 1373 Row row; 1374 for (Iterator iterator = iterator(); iterator.hasNext(); ) { 1375 row = (Row) iterator.next(); 1376 Element cell; 1377 PdfPCell pcell; 1378 for (int i = 0; i < row.getColumns(); i++) { 1379 if ((cell = (Element)row.getCell(i)) != null) { 1380 if (cell instanceof Table) { 1381 pcell = new PdfPCell(((Table)cell).createPdfPTable()); 1382 } 1383 else if (cell instanceof Cell) { 1384 pcell = ((Cell)cell).createPdfPCell(); 1385 pcell.setPadding(cellpadding + cellspacing / 2f); 1386 pcell.setCellEvent(SimpleCell.getDimensionlessInstance((Cell)cell, cellspacing)); 1387 } 1388 else { 1389 pcell = new PdfPCell(); 1390 } 1391 pdfptable.addCell(pcell); 1392 } 1393 } 1394 } 1395 return pdfptable; 1396 } 1397 1398 1400 1408 public Table(java.util.Properties attributes) { 1409 this(com.lowagie.text.factories.ElementFactory.getTable(attributes)); 1410 } 1411 1412 1418 public int columns() { 1419 return getColumns(); 1420 } 1421 1422 1428 public int alignment() { 1429 return getAlignment(); 1430 } 1431 1432 1438 public float cellpadding() { 1439 return getPadding(); 1440 } 1441 1442 1448 public float cellspacing() { 1449 return getSpacing(); 1450 } 1451 1452 1458 public void setSpaceInsideCell(float value) { 1459 cellpadding = value; 1460 } 1461 1462 1468 public void setSpaceBetweenCells(float value) { 1469 cellspacing = value; 1470 } 1471 1472 1478 public int lastHeaderRow() { 1479 return getLastHeaderRow(); 1480 } 1481 1482 1488 public float widthPercentage() { 1489 return getWidth(); 1490 } 1491 1492 1498 public void setAbsWidth(String width) { 1499 setWidth(Float.parseFloat(width + "f")); 1500 setLocked(true); 1501 } 1502 1503 1509 public String absWidth() { 1510 if (isLocked()) 1511 return String.valueOf(width); 1512 else 1513 return ""; 1514 } 1515 1516 1518 1525 public void setDefaultCellBorder(int value) { 1526 defaultLayout.setBorder(value); 1527 } 1528 1529 1536 public void setDefaultCellBorderWidth(float value) { 1537 defaultLayout.setBorderWidth(value); 1538 } 1539 1540 1547 public void setDefaultCellBorderColor(Color color) { 1548 defaultLayout.setBorderColor(color); 1549 } 1550 1551 1558 public void setDefaultCellBackgroundColor(Color color) { 1559 defaultLayout.setBackgroundColor(color); 1560 } 1561 1562 1569 public void setDefaultCellGrayFill(float value) { 1570 if (value >= 0 && value <= 1) { 1571 defaultLayout.setGrayFill(value); 1572 } 1573 } 1574 1575 1582 public void setDefaultHorizontalAlignment(int value) { 1583 defaultLayout.setHorizontalAlignment(value); 1584 } 1585 1586 1593 public void setDefaultVerticalAlignment(int value) { 1594 defaultLayout.setVerticalAlignment(value); 1595 } 1596 1597 1604 public void setDefaultRowspan(int value) { 1605 defaultLayout.setRowspan(value); 1606 } 1607 1608 1615 public void setDefaultColspan(int value) { 1616 defaultLayout.setColspan(value); 1617 } 1618 1619} | Popular Tags |