1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
|
/*
* $Id: Table.java,v 1.136 2005/05/11 10:39:01 blowagie Exp $
* $Name: $
*
* Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU LIBRARY GENERAL PUBLIC LICENSE for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*
* Some methods in this class were contributed by Geert Poels, Kris Jespers and
* Steve Ogryzek. Check the CVS repository.
*/
package com.lowagie.text;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;
import com.lowagie.text.markup.MarkupParser;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
/**
* A <CODE>Table</CODE> is a <CODE>Rectangle</CODE> that contains <CODE>Cell</CODE>s,
* ordered in some kind of matrix.
* <P>
* Tables that span multiple pages are cut into different parts automatically.
* If you want a table header to be repeated on every page, you may not forget to
* mark the end of the header section by using the method <CODE>endHeaders()</CODE>.
* <P>
* The matrix of a table is not necessarily an m x n-matrix. It can contain holes
* or cells that are bigger than the unit. Believe me or not, but it took some serious
* thinking to make this as userfriendly as possible. I hope you wil find the result
* quite simple (I love simple solutions, especially for complex problems).
* I didn't want it to be something as complex as the Java <CODE>GridBagLayout</CODE>.
* <P>
* Example:
* <BLOCKQUOTE><PRE>
* // Remark: You MUST know the number of columns when constructing a Table.
* // The number of rows is not important.
* <STRONG>Table table = new Table(3);</STRONG>
* <STRONG>table.setBorderWidth(1);</STRONG>
* <STRONG>table.setBorderColor(new Color(0, 0, 255));</STRONG>
* <STRONG>table.setPadding(5);</STRONG>
* <STRONG>table.setSpacing(5);</STRONG>
* Cell cell = new Cell("header");
* cell.setHeader(true);
* cell.setColspan(3);
* <STRONG>table.addCell(cell);</STRONG>
* <STRONG>table.endHeaders();</STRONG>
* cell = new Cell("example cell with colspan 1 and rowspan 2");
* cell.setRowspan(2);
* cell.setBorderColor(new Color(255, 0, 0));
* <STRONG>table.addCell(cell);</STRONG>
* <STRONG>table.addCell("1.1");</STRONG>
* <STRONG>table.addCell("2.1");</STRONG>
* <STRONG>table.addCell("1.2");</STRONG>
* <STRONG>table.addCell("2.2");</STRONG>
* <STRONG>table.addCell("cell test1");</STRONG>
* cell = new Cell("big cell");
* cell.setRowspan(2);
* cell.setColspan(2);
* <STRONG>table.addCell(cell);</STRONG>
* <STRONG>table.addCell("cell test2");</STRONG>
* </PRE></BLOCKQUOTE>
* The result of this code is a table:
* <TABLE ALIGN="Center" BORDER="1" BORDERCOLOR="#0000ff" CELLPADDING="5" CELLSPACING="5">
* <TR ALIGN="Left" VALIGN="Left">
* <TH ALIGN="Left" COLSPAN="3" VALIGN="Left">
* header
* </TH>
* </TR>
* <TR ALIGN="Left" VALIGN="Left">
* <TD ALIGN="Left" BORDERCOLOR="#ff0000" ROWSPAN="2" VALIGN="Left">
* example cell with colspan 1 and rowspan 2
* </TD>
* <TD ALIGN="Left" VALIGN="Left">
* 1.1
* </TD>
* <TD ALIGN="Left" VALIGN="Left">
* 2.1
* </TD>
* </TR>
* <TR ALIGN="Left" VALIGN="Left">
* <TD ALIGN="Left" VALIGN="Left">
* 1.2
* </TD>
* <TD ALIGN="Left" VALIGN="Left">
* 2.2
* </TD>
* </TR>
* <TR ALIGN="Left" VALIGN="Left">
* <TD ALIGN="Left" VALIGN="Left">
* cell test1
* </TD>
* <TD ALIGN="Left" COLSPAN="2" ROWSPAN="2" VALIGN="Left">
* big cell
* </TD>
* </TR>
* <TR ALIGN="Left" VALIGN="Left">
* <TD ALIGN="Left" VALIGN="Left">
* cell test2
* </TD>
* </TR>
* </TABLE>
*
* @see Rectangle
* @see Element
* @see Row
* @see Cell
*/
public class Table extends Rectangle implements Element, MarkupAttributes {
// membervariables
// these variables contain the data of the table
/** This is the number of columns in the <CODE>Table</CODE>. */
private int columns;
// this is the current Position in the table
private Point curPosition = new Point(0, 0);
/** This is the list of <CODE>Row</CODE>s. */
private ArrayList rows = new ArrayList();
// these variables contain the layout of the table
/** This Empty Cell contains the DEFAULT layout of each Cell added with the method addCell(String content). */
private Cell defaultLayout = new Cell(true);
/** This is the number of the last row of the table headers. */
private int lastHeaderRow = -1;
/** This is the horizontal alignment. */
private int alignment = Element.ALIGN_CENTER;
/** This is cellpadding. */
private float cellpadding;
/** This is cellspacing. */
private float cellspacing;
/** This is the width of the table (in percent of the available space). */
private float widthPercentage = 80;
// member variable added by Evelyne De Cordier
/** This is the width of the table (in pixels). */
private String absWidth = "";
/** This is an array containing the widths (in percentages) of every column. */
private float[] widths;
/** Boolean to track errors (some checks will be performed) */
boolean mDebug = false;
/** Boolean to track if a table was inserted (to avoid unnecessary computations afterwards) */
boolean mTableInserted = false;
/**
* Boolean to automatically fill empty cells before a table is rendered
* (takes CPU so may be set to false in case of certainty)
*/
boolean mAutoFillEmptyCells = false;
/** If true this table may not be split over two pages. */
boolean tableFitsPage = false;
/** If true cells may not be split over two pages. */
boolean cellsFitPage = false;
/** This is the offset of the table. */
float offset = Float.NaN;
/** contains the attributes that are added to each odd (or even) row */
protected Hashtable alternatingRowAttributes = null;
/** if you want to generate tables the old way, set this value to false. */
protected boolean convert2pdfptable = false;
// constructors
/**
* Constructs a <CODE>Table</CODE> with a certain number of columns.
*
* @param columns The number of columns in the table
* @throws BadElementException if the creator was called with less than 1 column
*/
public Table(int columns) throws BadElementException {
this(columns, 1);
}
/**
* Constructs a <CODE>Table</CODE> with a certain number of columns
* and a certain number of <CODE>Row</CODE>s.
*
* @param columns The number of columns in the table
* @param rows The number of rows
* @throws BadElementException if the creator was called with less than 1 column
*/
public Table(int columns, int rows) throws BadElementException {
// a Rectangle is create with BY DEFAULT a border with a width of 1
super(0, 0, 0, 0);
setBorder(BOX);
setBorderWidth(1);
defaultLayout.setBorder(BOX);
// a table should have at least 1 column
if (columns <= 0) {
throw new BadElementException("A table should have at least 1 column.");
}
this.columns = columns;
// a certain number of rows are created
for (int i = 0; i < rows; i++) {
this.rows.add(new Row(columns));
}
curPosition = new Point(0, 0);
// the DEFAULT widths are calculated
widths = new float[columns];
float width = 100f / columns;
for (int i = 0; i < columns; i++) {
widths[i] = width;
}
}
/**
* Returns a <CODE>Table</CODE> that has been constructed taking in account
* the value of some <VAR>attributes</VAR>.
*
* @param attributes Some attributes
*/
public Table(Properties attributes) {
// a Rectangle is create with BY DEFAULT a border with a width of 1
super(0, 0, 0, 0);
setBorder(BOX);
setBorderWidth(1);
defaultLayout.setBorder(BOX);
String value = (String)attributes.remove(ElementTags.COLUMNS);
if (value == null) {
columns = 1;
}
else {
columns = Integer.parseInt(value);
if (columns <= 0) {
columns = 1;
}
}
rows.add(new Row(columns));
curPosition.setLocation(0, curPosition.y);
if ((value = (String)attributes.remove(ElementTags.LASTHEADERROW)) != null) {
setLastHeaderRow(Integer.parseInt(value));
}
if ((value = (String)attributes.remove(ElementTags.ALIGN)) != null) {
setAlignment(value);
}
if ((value = (String)attributes.remove(ElementTags.CELLSPACING)) != null) {
setSpacing(Float.valueOf(value + "f").floatValue());
}
if ((value = (String)attributes.remove(ElementTags.CELLPADDING)) != null) {
setPadding(Float.valueOf(value + "f").floatValue());
}
if ((value = (String)attributes.remove(ElementTags.OFFSET)) != null) {
setOffset(Float.valueOf(value + "f").floatValue());
}
if ((value = (String)attributes.remove(ElementTags.WIDTH)) != null) {
if (value.endsWith("%"))
setWidth(Float.valueOf(value.substring(0, value.length() - 1) + "f").floatValue());
else
setAbsWidth(value);
}
widths = new float[columns];
for (int i = 0; i < columns; i++) {
widths[i] = 0;
}
if ((value = (String)attributes.remove(ElementTags.WIDTHS)) != null) {
StringTokenizer widthTokens = new StringTokenizer(value, ";");
int i = 0;
while (widthTokens.hasMoreTokens()) {
value = (String) widthTokens.nextToken();
widths[i] = Float.valueOf(value + "f").floatValue();
i++;
}
columns = i;
}
if ((value = (String)attributes.remove(ElementTags.TABLEFITSPAGE)) != null) {
tableFitsPage = new Boolean(value).booleanValue();
}
if ((value = (String)attributes.remove(ElementTags.CELLSFITPAGE)) != null) {
cellsFitPage = new Boolean(value).booleanValue();
}
if ((value = (String)attributes.remove(ElementTags.BORDERWIDTH)) != null) {
setBorderWidth(Float.valueOf(value + "f").floatValue());
}
int border = 0;
if ((value = (String)attributes.remove(ElementTags.LEFT)) != null) {
if (new Boolean(value).booleanValue()) border |= Rectangle.LEFT;
}
if ((value = (String)attributes.remove(ElementTags.RIGHT)) != null) {
if (new Boolean(value).booleanValue()) border |= Rectangle.RIGHT;
}
if ((value = (String)attributes.remove(ElementTags.TOP)) != null) {
if (new Boolean(value).booleanValue()) border |= Rectangle.TOP;
}
if ((value = (String)attributes.remove(ElementTags.BOTTOM)) != null) {
if (new Boolean(value).booleanValue()) border |= Rectangle.BOTTOM;
}
setBorder(border);
String r = (String)attributes.remove(ElementTags.RED);
String g = (String)attributes.remove(ElementTags.GREEN);
String b = (String)attributes.remove(ElementTags.BLUE);
if (r != null || g != null || b != null) {
int red = 0;
int green = 0;
int blue = 0;
if (r != null) red = Integer.parseInt(r);
if (g != null) green = Integer.parseInt(g);
if (b != null) blue = Integer.parseInt(b);
setBorderColor(new Color(red, green, blue));
}
else if ((value = attributes.getProperty(ElementTags.BORDERCOLOR)) != null) {
setBorderColor(MarkupParser.decodeColor(value));
}
r = (String)attributes.remove(ElementTags.BGRED);
g = (String)attributes.remove(ElementTags.BGGREEN);
b = (String)attributes.remove(ElementTags.BGBLUE);
if (r != null || g != null || b != null) {
int red = 0;
int green = 0;
int blue = 0;
if (r != null) red = Integer.parseInt(r);
if (g != null) green = Integer.parseInt(g);
if (b != null) blue = Integer.parseInt(b);
setBackgroundColor(new Color(red, green, blue));
}
else if ((value = (String)attributes.remove(ElementTags.BACKGROUNDCOLOR)) != null) {
setBackgroundColor(MarkupParser.decodeColor(value));
}
if ((value = (String)attributes.remove(ElementTags.GRAYFILL)) != null) {
setGrayFill(Float.valueOf(value + "f").floatValue());
}
if (attributes.size() > 0) setMarkupAttributes(attributes);
}
// implementation of the Element-methods
/**
* Processes the element by adding it (or the different parts) to an
* <CODE>ElementListener</CODE>.
*
* @param listener an <CODE>ElementListener</CODE>
* @return <CODE>true</CODE> if the element was processed successfully
*/
public boolean process(ElementListener listener) {
try {
return listener.add(this);
}
catch(DocumentException de) {
return false;
}
}
/**
* Performs extra checks when executing table code (currently only when cells are added).
* @param aDebug
*/
public void setDebug(boolean aDebug) {
mDebug = aDebug;
}
/**
* Sets the default layout of the Table to
* the provided Cell
* @param value a cell with all the defaults
*/
public void setDefaultLayout(Cell value) {
defaultLayout = value;
}
/**
* Enables/disables automatic insertion of empty cells before table is rendered. (default = false)
* As some people may want to create a table, fill only a couple of the cells and don't bother with
* investigating which empty ones need to be added, this default behaviour may be very welcome.
* Disabling is recommended to increase speed. (empty cells should be added through extra code then)
*
* @param aDoAutoFill enable/disable autofill
*/
public void setAutoFillEmptyCells(boolean aDoAutoFill) {
mAutoFillEmptyCells = aDoAutoFill;
}
/**
* Allows you to control when a page break occurs.
* <P>
* When a table doesn't fit a page, it is split in two parts.
* If you want to avoid this, you should set the <VAR>tableFitsPage</VAR> value to true.
*
* @param fitPage enter true if you don't want to split cells
*/
public void setTableFitsPage(boolean fitPage) {
this.tableFitsPage = fitPage;
if (fitPage) setCellsFitPage(true);
}
/**
* Allows you to control when a page break occurs.
* <P>
* When a cell doesn't fit a page, it is split in two parts.
* If you want to avoid this, you should set the <VAR>cellsFitPage</VAR> value to true.
*
* @param fitPage enter true if you don't want to split cells
*/
public void setCellsFitPage(boolean fitPage) {
this.cellsFitPage = fitPage;
}
/**
* Checks if this <CODE>Table</CODE> has to fit a page.
*
* @return true if the table may not be split
*/
public boolean hasToFitPageTable() {
return tableFitsPage;
}
/**
* Checks if the cells of this <CODE>Table</CODE> have to fit a page.
*
* @return true if the cells may not be split
*/
public boolean hasToFitPageCells() {
return cellsFitPage;
}
/**
* Sets the offset of this table.
*
* Normally a newline is added before you add a Table object.
* This newline uses the current leading.
* If you want to control the space between the table and the previous
* element yourself, you have to set the offset of this table.
*
* @param offset the space between this table and the previous object.
*/
public void setOffset(float offset) {
this.offset = offset;
}
/**
* Gets the offset of this table.
*
* @return the space between this table and the previous element.
*/
public float getOffset() {
return offset;
}
/**
* Gets the type of the text element.
*
* @return a type
*/
public int type() {
return Element.TABLE;
}
/**
* Gets all the chunks in this element.
*
* @return an <CODE>ArrayList</CODE>
*/
public ArrayList getChunks() {
return new ArrayList();
}
// methods to add content to the table
/**
* Adds a <CODE>Cell</CODE> to the <CODE>Table</CODE> at a certain row and column.
*
* @param aCell The <CODE>Cell</CODE> to add
* @param row The row where the <CODE>Cell</CODE> will be added
* @param column The column where the <CODE>Cell</CODE> will be added
* @throws BadElementException
*/
public void addCell(Cell aCell, int row, int column) throws BadElementException {
addCell(aCell, new Point(row,column));
}
/**
* Adds a <CODE>Cell</CODE> to the <CODE>Table</CODE> at a certain location.
*
* @param aCell The <CODE>Cell</CODE> to add
* @param aLocation The location where the <CODE>Cell</CODE> will be added
* @throws BadElementException
*/
public void addCell(Cell aCell, Point aLocation) throws BadElementException {
if (aCell == null) throw new NullPointerException("addCell - cell has null-value");
if (aLocation == null) throw new NullPointerException("addCell - point has null-value");
if (aCell.isTable()) insertTable((Table)aCell.getElements().next(), aLocation);
if (mDebug == true) {
if (aLocation.x < 0) throw new BadElementException("row coordinate of location must be >= 0");
if ((aLocation.y <= 0) && (aLocation.y > columns)) throw new BadElementException("column coordinate of location must be >= 0 and < nr of columns");
if (!isValidLocation(aCell, aLocation)) throw new BadElementException("Adding a cell at the location (" + aLocation.x + "," + aLocation.y + ") with a colspan of " + aCell.colspan() + " and a rowspan of " + aCell.rowspan() + " is illegal (beyond boundaries/overlapping).");
}
if (aCell.border() == UNDEFINED) aCell.setBorder(defaultLayout.border());
aCell.fill();
placeCell(rows, aCell, aLocation);
setCurrentLocationToNextValidPosition(aLocation);
}
/**
* Adds a <CODE>Cell</CODE> to the <CODE>Table</CODE>.
*
* @param cell a <CODE>Cell</CODE>
*/
public void addCell(Cell cell) {
try {
addCell(cell, curPosition);
}
catch(BadElementException bee) {
// don't add the cell
}
}
/**
* Adds a <CODE>Cell</CODE> to the <CODE>Table</CODE>.
* <P>
* This is a shortcut for <CODE>addCell(Cell cell)</CODE>.
* The <CODE>Phrase</CODE> will be converted to a <CODE>Cell</CODE>.
*
* @param content a <CODE>Phrase</CODE>
* @throws BadElementException this should never happen
*/
public void addCell(Phrase content) throws BadElementException {
addCell(content, curPosition);
}
/**
* Adds a <CODE>Cell</CODE> to the <CODE>Table</CODE>.
* <P>
* This is a shortcut for <CODE>addCell(Cell cell, Point location)</CODE>.
* The <CODE>Phrase</CODE> will be converted to a <CODE>Cell</CODE>.
*
* @param content a <CODE>Phrase</CODE>
* @param location a <CODE>Point</CODE>
* @throws BadElementException this should never happen
*/
public void addCell(Phrase content, Point location) throws BadElementException {
Cell cell = new Cell(content);
cell.setBorder(defaultLayout.border());
cell.setBorderWidth(defaultLayout.borderWidth());
cell.setBorderColor(defaultLayout.borderColor());
cell.setBackgroundColor(defaultLayout.backgroundColor());
cell.setGrayFill(defaultLayout.grayFill());
cell.setHorizontalAlignment(defaultLayout.horizontalAlignment());
cell.setVerticalAlignment(defaultLayout.verticalAlignment());
cell.setColspan(defaultLayout.colspan());
cell.setRowspan(defaultLayout.rowspan());
addCell(cell, location);
}
/**
* Adds a <CODE>Cell</CODE> to the <CODE>Table</CODE>.
* <P>
* This is a shortcut for <CODE>addCell(Cell cell)</CODE>.
* The <CODE>String</CODE> will be converted to a <CODE>Cell</CODE>.
*
* @param content a <CODE>String</CODE>
* @throws BadElementException this should never happen
*/
public void addCell(String content) throws BadElementException {
addCell(new Phrase(content), curPosition);
}
/**
* Adds a <CODE>Cell</CODE> to the <CODE>Table</CODE>.
* <P>
* This is a shortcut for <CODE>addCell(Cell cell, Point location)</CODE>.
* The <CODE>String</CODE> will be converted to a <CODE>Cell</CODE>.
*
* @param content a <CODE>String</CODE>
* @param location a <CODE>Point</CODE>
* @throws BadElementException this should never happen
*/
public void addCell(String content, Point location) throws BadElementException {
addCell(new Phrase(content), location);
}
/**
* To put a table within the existing table at the current position
* generateTable will of course re-arrange the widths of the columns.
*
* @param aTable the table you want to insert
*/
public void insertTable(Table aTable) {
if (aTable == null) throw new NullPointerException("insertTable - table has null-value");
insertTable(aTable, curPosition);
}
/**
* To put a table within the existing table at the given position
* generateTable will of course re-arrange the widths of the columns.
*
* @param aTable The <CODE>Table</CODE> to add
* @param row The row where the <CODE>Cell</CODE> will be added
* @param column The column where the <CODE>Cell</CODE> will be added
*/
public void insertTable(Table aTable, int row, int column) {
if (aTable == null) throw new NullPointerException("insertTable - table has null-value");
insertTable(aTable, new Point(row, column));
}
/**
* To put a table within the existing table at the given position
* generateTable will of course re-arrange the widths of the columns.
*
* @param aTable the table you want to insert
* @param aLocation a <CODE>Point</CODE>
*/
public void insertTable(Table aTable, Point aLocation) {
if (aTable == null) throw new NullPointerException("insertTable - table has null-value");
if (aLocation == null) throw new NullPointerException("insertTable - point has null-value");
mTableInserted = true;
aTable.complete();
if (mDebug == true) {
if (aLocation.y > columns) System.err.println("insertTable -- wrong columnposition("+ aLocation.y + ") of location; max =" + columns);
}
int rowCount = aLocation.x + 1 - rows.size();
int i = 0;
if ( rowCount > 0 ) { //create new rows ?
for (; i < rowCount; i++) {
rows.add(new Row(columns));
}
}
((Row) rows.get(aLocation.x)).setElement(aTable,aLocation.y);
setCurrentLocationToNextValidPosition(aLocation);
}
/**
* Will fill empty cells with valid blank <CODE>Cell</CODE>s
*/
public void complete() {
if (mTableInserted == true) {
mergeInsertedTables(); // integrate tables in the table
mTableInserted = false;
}
if (mAutoFillEmptyCells == true) {
fillEmptyMatrixCells();
}
if (alternatingRowAttributes != null) {
Properties even = new Properties();
Properties odd = new Properties();
String name;
String[] value;
for (Iterator iterator = alternatingRowAttributes.keySet().iterator(); iterator.hasNext(); ) {
name = String.valueOf(iterator.next());
value = (String[])alternatingRowAttributes.get(name);
even.setProperty(name, value[0]);
odd.setProperty(name, value[1]);
}
Row row;
for (int i = lastHeaderRow + 1; i < rows.size(); i++) {
row = (Row)rows.get(i);
row.setMarkupAttributes(i % 2 == 0 ? even : odd);
}
}
}
/**
* Changes the border in the default layout of the <CODE>Cell</CODE>s
* added with method <CODE>addCell(String content)</CODE>.
*
* @param value the new border value
*/
public void setDefaultCellBorder(int value) {
defaultLayout.setBorder(value);
}
/**
* Changes the width of the borders in the default layout of the <CODE>Cell</CODE>s
* added with method <CODE>addCell(String content)</CODE>.
*
* @param value the new width
*/
public void setDefaultCellBorderWidth(float value) {
defaultLayout.setBorderWidth(value);
}
/**
* Changes the bordercolor in the default layout of the <CODE>Cell</CODE>s
* added with method <CODE>addCell(String content)</CODE>.
*
* @param color the new color
*/
public void setDefaultCellBorderColor(Color color) {
defaultLayout.setBorderColor(color);
}
/**
* Changes the backgroundcolor in the default layout of the <CODE>Cell</CODE>s
* added with method <CODE>addCell(String content)</CODE>.
*
* @param color the new color
*/
public void setDefaultCellBackgroundColor(Color color) {
defaultLayout.setBackgroundColor(color);
}
/**
* Changes the grayfill in the default layout of the <CODE>Cell</CODE>s
* added with method <CODE>addCell(String content)</CODE>.
*
* @param value the new value
*/
public void setDefaultCellGrayFill(float value) {
if (value >= 0 && value <= 1) {
defaultLayout.setGrayFill(value);
}
}
/**
* Changes the horizontalAlignment in the default layout of the <CODE>Cell</CODE>s
* added with method <CODE>addCell(String content)</CODE>.
*
* @param value the new alignment value
*/
public void setDefaultHorizontalAlignment(int value) {
defaultLayout.setHorizontalAlignment(value);
}
/**
* Changes the verticalAlignment in the default layout of the <CODE>Cell</CODE>s
* added with method <CODE>addCell(String content)</CODE>.
*
* @param value the new alignment value
*/
public void setDefaultVerticalAlignment(int value) {
defaultLayout.setVerticalAlignment(value);
}
/**
* Changes the rowspan in the default layout of the <CODE>Cell</CODE>s
* added with method <CODE>addCell(String content)</CODE>.
*
* @param value the new rowspan value
*/
public void setDefaultRowspan(int value) {
defaultLayout.setRowspan(value);
}
/**
* Changes the colspan in the default layout of the <CODE>Cell</CODE>s
* added with method <CODE>addCell(String content)</CODE>.
*
* @param value the new colspan value
*/
public void setDefaultColspan(int value) {
defaultLayout.setColspan(value);
}
// methods
/**
* Sets the unset cell properties to be the table defaults.
*
* @param aCell The cell to set to table defaults as necessary.
*/
private void assumeTableDefaults(Cell aCell) {
if (aCell.border() == Rectangle.UNDEFINED) {
aCell.setBorder(defaultLayout.border());
}
if (aCell.borderWidth() == Rectangle.UNDEFINED) {
aCell.setBorderWidth(defaultLayout.borderWidth());
}
if (aCell.borderColor() == null) {
aCell.setBorderColor(defaultLayout.borderColor());
}
if (aCell.backgroundColor() == null) {
aCell.setBackgroundColor(defaultLayout.backgroundColor());
}
if (aCell.grayFill() == Rectangle.UNDEFINED) {
aCell.setGrayFill(defaultLayout.grayFill());
}
if (aCell.horizontalAlignment() == Element.ALIGN_UNDEFINED) {
aCell.setHorizontalAlignment(defaultLayout.horizontalAlignment());
}
if (aCell.verticalAlignment() == Element.ALIGN_UNDEFINED) {
aCell.setVerticalAlignment(defaultLayout.verticalAlignment());
}
}
/**
* Deletes a column in this table.
*
* @param column the number of the column that has to be deleted
* @throws BadElementException
*/
public void deleteColumn(int column) throws BadElementException {
float newWidths[] = new float[--columns];
for (int i = 0; i < column; i++) {
newWidths[i] = widths[i];
}
for (int i = column; i < columns; i++) {
newWidths[i] = widths[i + 1];
}
setWidths(newWidths);
for (int i = 0; i < columns; i++) {
newWidths[i] = widths[i];
}
widths = newWidths;
Row row;
int size = rows.size();
for (int i = 0; i < size; i++) {
row = (Row) rows.get(i);
row.deleteColumn(column);
rows.set(i, row);
}
if (column == columns) {
curPosition.setLocation(curPosition.x+1, 0);
}
}
/**
* Deletes a row.
*
* @param row the number of the row to delete
* @return boolean <CODE>true</CODE> if the row was deleted; <CODE>false</CODE> if not
*/
public boolean deleteRow(int row) {
if (row < 0 || row >= rows.size()) {
return false;
}
rows.remove(row);
curPosition.setLocation(curPosition.x-1, curPosition.y);
return true;
}
/**
* Deletes all rows in this table.
* (contributed by dperezcar@fcc.es)
*/
public void deleteAllRows() {
rows.clear();
rows.add(new Row(columns));
curPosition.setLocation(0, 0);
lastHeaderRow = -1;
}
/**
* Deletes the last row in this table.
*
* @return boolean <CODE>true</CODE> if the row was deleted; <CODE>false</CODE> if not
*/
public boolean deleteLastRow() {
return deleteRow(rows.size() - 1);
}
/**
* Marks the last row of the table headers.
*
* @return the number of the last row of the table headers
*/
public int endHeaders() {
/* patch sep 8 2001 Francesco De Milato */
lastHeaderRow = curPosition.x - 1;
return lastHeaderRow;
}
// methods to set the membervariables
/**
* Sets the horizontal alignment.
*
* @param value the new value
*/
public void setLastHeaderRow(int value) {
lastHeaderRow = value;
}
/**
* Sets the horizontal alignment.
*
* @param value the new value
*/
public void setAlignment(int value) {
alignment = value;
}
/**
* Sets the alignment of this paragraph.
*
* @param alignment the new alignment as a <CODE>String</CODE>
*/
public void setAlignment(String alignment) {
if (ElementTags.ALIGN_LEFT.equalsIgnoreCase(alignment)) {
this.alignment = Element.ALIGN_LEFT;
return;
}
if (ElementTags.RIGHT.equalsIgnoreCase(alignment)) {
this.alignment = Element.ALIGN_RIGHT;
return;
}
this.alignment = Element.ALIGN_CENTER;
}
/**
* Sets the cellpadding.
*
* @param value the new value
*/
public void setSpaceInsideCell(float value) {
cellpadding = value;
}
/**
* Sets the cellspacing.
*
* @param value the new value
*/
public void setSpaceBetweenCells(float value) {
cellspacing = value;
}
/**
* Sets the cellpadding.
*
* @param value the new value
*/
public void setPadding(float value) {
cellpadding = value;
}
/**
* Sets the cellspacing.
*
* @param value the new value
*/
public void setSpacing(float value) {
cellspacing = value;
}
/**
* Sets the cellspacing (the meaning of cellpadding and cellspacing was inverted by mistake).
*
* @param value the new value
* @deprecated use setSpacing instead
*/
public void setCellpadding(float value) {
cellspacing = value;
}
/**
* Sets the cellpadding (the meaning of cellpadding and cellspacing was inverted by mistake).
*
* @param value the new value
* @deprecated use setPadding instead
*/
public void setCellspacing(float value) {
cellpadding = value;
}
/**
* Sets the width of this table (in percentage of the available space).
*
* @param width the width
*/
public void setWidth(float width) {
this.widthPercentage = width;
}
/**
* Sets the width of this table (in percentage of the available space).
*
* @param width the width
*/
public void setAbsWidth(String width) {
this.absWidth = width;
}
/**
* Sets the widths of the different columns (percentages).
* <P>
* You can give up relative values of borderwidths.
* The sum of these values will be considered 100%.
* The values will be recalculated as percentages of this sum.
* <P>
* example:
* <BLOCKQUOTE><PRE>
* float[] widths = {2, 1, 1};
* <STRONG>table.setWidths(widths)</STRONG>
* </PRE></BLOCKQUOTE>
* The widths will be: a width of 50% for the first column,
* 25% for the second and third column.
*
* @param widths an array with values
* @throws BadElementException
*/
public void setWidths(float[] widths) throws BadElementException {
if (widths.length != columns) {
throw new BadElementException("Wrong number of columns.");
}
// The sum of all values is 100%
float hundredPercent = 0;
for (int i = 0; i < columns; i++) {
hundredPercent += widths[i];
}
// The different percentages are calculated
float width;
this.widths[columns - 1] = 100;
for (int i = 0; i < columns - 1; i++) {
width = (100.0f * widths[i]) / hundredPercent;
this.widths[i] = width;
this.widths[columns - 1] -= width;
}
}
/**
* Sets the widths of the different columns (percentages).
* <P>
* You can give up relative values of borderwidths.
* The sum of these values will be considered 100%.
* The values will be recalculated as percentages of this sum.
*
* @param widths an array with values
* @throws DocumentException
*/
public void setWidths(int[] widths) throws DocumentException {
float tb[] = new float[widths.length];
for (int k = 0; k < widths.length; ++k)
tb[k] = widths[k];
setWidths(tb);
}
// methods to retrieve the membervariables
/**
* Gets the number of columns.
*
* @return a value
*/
public int columns() {
return columns;
}
/**
* Gets the number of rows in this <CODE>Table</CODE>.
*
* @return the number of rows in this <CODE>Table</CODE>
*/
public int size() {
return rows.size();
}
/**
* Gets the proportional widths of the columns in this <CODE>Table</CODE>.
*
* @return the proportional widths of the columns in this <CODE>Table</CODE>
*/
public float[] getProportionalWidths() {
return widths;
}
/**
* Gets an <CODE>Iterator</CODE> of all the <CODE>Row</CODE>s.
*
* @return an <CODE>Iterator</CODE>
*/
public Iterator iterator() {
return rows.iterator();
}
/**
* Gets the horizontal alignment.
*
* @return a value
*/
public int alignment() {
return alignment;
}
/**
* Gets the cellpadding.
*
* @return a value
*/
public float cellpadding() {
return cellpadding;
}
/**
* Gets the cellspacing.
*
* @return a value
*/
public float cellspacing() {
return cellspacing;
}
/**
* Gets the table width (a percentage).
*
* @return the table width
*/
public float widthPercentage() {
return widthPercentage;
}
/**
* Gets the table width (in pixels).
*
* @return the table width
*/
public String absWidth() {
return absWidth;
}
/**
* Gets the first number of the row that doesn't contain headers.
*
* @return a rownumber
*/
public int firstDataRow() {
return lastHeaderRow + 1;
}
/**
* Gets the last number of the rows that contain headers.
*
* @return a rownumber
*/
public int lastHeaderRow() {
return this.lastHeaderRow;
}
/**
* Gets the dimension of this table
*
* @return dimension
*/
public Dimension getDimension() {
return new Dimension(columns, rows.size());
}
/**
* returns the element at the position row, column
* (Cast to Cell or Table)
*
* @param row
* @param column
* @return dimension
*/
public Object getElement(int row, int column) {
return ((Row) rows.get(row)).getCell(column);
}
/**
* Integrates all added tables and recalculates column widths.
*/
private void mergeInsertedTables() {
int i=0, j=0;
float [] lNewWidths = null;
int [] lDummyWidths = new int[columns]; // to keep track in how many new cols this one will be split
float[][] lDummyColumnWidths = new float[columns][]; // bugfix Tony Copping
int [] lDummyHeights = new int[rows.size()]; // to keep track in how many new rows this one will be split
ArrayList newRows = null;
boolean isTable=false;
int lTotalRows = 0, lTotalColumns = 0;
int lNewMaxRows = 0, lNewMaxColumns = 0;
Table lDummyTable = null;
// first we'll add new columns when needed
// check one column at a time, find maximum needed nr of cols
// Search internal tables and find one with max columns
for (j=0; j < columns; j++) {
lNewMaxColumns = 1; // value to hold in how many columns the current one will be split
float [] tmpWidths = null;
for (i=0; i < rows.size(); i++) {
if ( Table.class.isInstance(((Row) rows.get(i)).getCell(j)) ) {
isTable=true;
lDummyTable = ((Table) ((Row) rows.get(i)).getCell(j));
if( tmpWidths == null) {
tmpWidths = lDummyTable.widths;
lNewMaxColumns=tmpWidths.length;
}
else {
int cols = lDummyTable.getDimension().width;
float [] tmpWidthsN = new float[ cols * tmpWidths.length];
float tpW=0, btW=0, totW=0;
int tpI=0, btI=0, totI=0;
tpW+=tmpWidths[0];
btW+=lDummyTable.widths[0];
while( tpI<tmpWidths.length && btI<cols) {
if( btW>tpW) {
tmpWidthsN[totI] = tpW-totW;
tpI++;
if(tpI<tmpWidths.length) {
tpW+=tmpWidths[tpI];
}
}
else {
tmpWidthsN[totI] = btW-totW;
btI++;
if(Math.abs(btW - tpW) < 0.0001) {
tpI++;
if(tpI<tmpWidths.length) {
tpW+=tmpWidths[tpI];
}
}
if(btI<cols) {
btW+=lDummyTable.widths[btI];
}
}
totW+=tmpWidthsN[totI];
totI++;
}
/*if( tpI<tmpWidths.length)
{
System.arraycopy(tmpWidths, tpI, tmpWidthsN, totI, tmpWidths.length-tpI);
totI +=tmpWidths.length-tpI;
}
else if(btI<cols)
{
System.arraycopy(lDummyTable.widths, btI, tmpWidthsN, totI, lDummyTable.widths.length-btI);
totI +=lDummyTable.widths.length-btI; }*/
tmpWidths = new float[totI];
System.arraycopy(tmpWidthsN, 0, tmpWidths, 0, totI);
lNewMaxColumns=totI;
}
/*if ( lDummyTable.getDimension().width > lNewMaxColumns )
{
lNewMaxColumns = lDummyTable.getDimension().width;
lDummyColumnWidths[j] = lDummyTable.widths; // bugfix Tony Copping
}*/
}
}
lDummyColumnWidths[j] = tmpWidths;
lTotalColumns += lNewMaxColumns;
lDummyWidths [j] = lNewMaxColumns;
}
// next we'll add new rows when needed
for (i=0; i < rows.size(); i++) {
lNewMaxRows = 1; // holds value in how many rows the current one will be split
for (j=0; j < columns; j++) {
if ( Table.class.isInstance(((Row) rows.get(i)).getCell(j)) ) {
isTable=true;
lDummyTable = (Table) ((Row) rows.get(i)).getCell(j);
if ( lDummyTable.getDimension().height > lNewMaxRows ) {
lNewMaxRows = lDummyTable.getDimension().height;
}
}
}
lTotalRows += lNewMaxRows;
lDummyHeights [i] = lNewMaxRows;
}
if ( (lTotalColumns != columns) || (lTotalRows != rows.size()) || isTable) // NO ADJUSTMENT
{
// ** WIDTH
// set correct width for new columns
// divide width over new nr of columns
// Take new max columns of internal table and work out widths for each col
lNewWidths = new float [lTotalColumns];
int lDummy = 0;
for (int tel=0; tel < widths.length;tel++) {
if ( lDummyWidths[tel] != 1) {
// divide
for (int tel2 = 0; tel2 < lDummyWidths[tel]; tel2++) {
// lNewWidths[lDummy] = widths[tel] / lDummyWidths[tel];
lNewWidths[lDummy] = widths[tel] * lDummyColumnWidths[tel][tel2] / 100f; // bugfix Tony Copping
lDummy++;
}
}
else {
lNewWidths[lDummy] = widths[tel];
lDummy++;
}
}
// ** FILL OUR NEW TABLE
// generate new table
// set new widths
// copy old values
newRows = new ArrayList(lTotalRows);
for (i = 0; i < lTotalRows; i++) {
newRows.add(new Row(lTotalColumns));
}
int lDummyRow = 0, lDummyColumn = 0; // to remember where we are in the new, larger table
Object lDummyElement = null;
for (i=0; i < rows.size(); i++) {
lDummyColumn = 0;
lNewMaxRows = 1;
for (j=0; j < columns; j++) {
if ( Table.class.isInstance(((Row) rows.get(i)).getCell(j)) ) // copy values from embedded table
{
lDummyTable = (Table) ((Row) rows.get(i)).getCell(j);
// Work out where columns in table table correspond to columns in current table
int colMap[] = new int[lDummyTable.widths.length+1];
int cb=0, ct=0;
for( ; cb<lDummyTable.widths.length;cb++) {
colMap[cb] = lDummyColumn+ct;
float wb;
wb = lDummyTable.widths[cb];
float wt=0;
while( ct<lDummyWidths[j]) {
wt+=lDummyColumnWidths[j][ct++];
if(Math.abs(convertWidth(wb) - convertWidth(wt)) < 0.0001) break;
}
}
colMap[cb] = lDummyColumn+ct;
// need to change this to work out how many cols to span
for (int k=0; k < lDummyTable.getDimension().height; k++) {
for (int l=0; l < lDummyTable.getDimension().width; l++) {
int yy=l;
lDummyElement = lDummyTable.getElement(k,l);
if (lDummyElement != null) {
int col=lDummyColumn+l;
if ( Cell.class.isInstance(lDummyElement) ) {
Cell lDummyC = (Cell)lDummyElement;
// Find col to add cell in and set col span
col = colMap[l];
int ot = colMap[l+lDummyC.colspan()];
lDummyC.setColspan(ot-col);
}
((Row) newRows.get(k + lDummyRow)).addElement(lDummyElement,col); // use addElement to set reserved status ok in row
}
}
}
}
else // copy others values
{
Object aElement = getElement(i,j);
if ( Cell.class.isInstance(aElement) ) {
// adjust spans for cell
((Cell) aElement).setRowspan(((Cell) ((Row) rows.get(i)).getCell(j)).rowspan() + lDummyHeights[i] - 1);
((Cell) aElement).setColspan(((Cell) ((Row) rows.get(i)).getCell(j)).colspan() + lDummyWidths[j] - 1);
// most likely this cell covers a larger area because of the row/cols splits : define not-to-be-filled cells
placeCell(newRows,((Cell) aElement), new Point(lDummyRow,lDummyColumn));
}
}
lDummyColumn += lDummyWidths[j];
}
lDummyRow += lDummyHeights[i];
}
// Set our new matrix
columns = lTotalColumns;
rows = newRows;
this.widths = lNewWidths;
}
}
/**
* adds new<CODE>Cell</CODE>'s to empty/null spaces.
*/
private void fillEmptyMatrixCells() {
try {
for (int i=0; i < rows.size(); i++) {
for (int j=0; j < columns; j++) {
if ( ((Row) rows.get(i)).isReserved(j) == false) {
addCell(defaultLayout, new Point(i, j));
}
}
}
}
catch(BadElementException bee) {
throw new ExceptionConverter(bee);
}
}
/**
* check if <CODE>Cell</CODE> 'fits' the table.
* <P>
* <UL><LI>rowspan/colspan not beyond borders
* <LI>spanned cell don't overlap existing cells</UL>
*
* @param aCell the cell that has to be checked
* @param aLocation the location where the cell has to be placed
* @return true if the location was valid
*/
private boolean isValidLocation(Cell aCell, Point aLocation) {
// rowspan not beyond last column
if ( aLocation.x < rows.size() ) // if false : new location is already at new, not-yet-created area so no check
{
if ((aLocation.y + aCell.colspan()) > columns) {
return false;
}
int difx = ((rows.size() - aLocation.x) > aCell.rowspan()) ? aCell.rowspan() : rows.size() - aLocation.x;
int dify = ((columns - aLocation.y) > aCell.colspan()) ? aCell.colspan() : columns - aLocation.y;
// no other content at cells targetted by rowspan/colspan
for (int i=aLocation.x; i < (aLocation.x + difx); i++) {
for (int j=aLocation.y; j < (aLocation.y + dify); j++) {
if ( ((Row) rows.get(i)).isReserved(j) == true ) {
return false;
}
}
}
}
else {
if ((aLocation.y + aCell.colspan()) > columns) {
return false;
}
}
return true;
}
/**
* Inserts a Cell in a cell-array and reserves cells defined by row-/colspan.
*
* @param someRows some rows
* @param aCell the cell that has to be inserted
* @param aPosition the position where the cell has to be placed
*/
private void placeCell(ArrayList someRows, Cell aCell, Point aPosition) {
int i;
Row row = null;
int lColumns = ((Row) someRows.get(0)).columns();
int rowCount = aPosition.x + aCell.rowspan() - someRows.size();
assumeTableDefaults(aCell);
if ( (aPosition.x + aCell.rowspan()) > someRows.size() ) //create new rows ?
{
for (i = 0; i < rowCount; i++) {
row = new Row(lColumns);
someRows.add(row);
}
}
// reserve cell in rows below
for (i = aPosition.x + 1; i < (aPosition.x + aCell.rowspan()); i++) {
if ( !((Row) someRows.get(i)).reserve(aPosition.y, aCell.colspan())) {
// should be impossible to come here :-)
throw new RuntimeException("addCell - error in reserve");
}
}
row = (Row) someRows.get(aPosition.x);
row.addElement(aCell, aPosition.y);
}
/**
* Gives you the posibility to add columns.
*
* @param aColumns the number of columns to add
*/
public void addColumns(int aColumns) {
ArrayList newRows = new ArrayList(rows.size());
int newColumns = columns + aColumns;
Row row;
for (int i = 0; i < rows.size(); i++) {
row = new Row(newColumns);
for (int j = 0; j < columns; j++) {
row.setElement(((Row) rows.get(i)).getCell(j) ,j);
}
for (int j = columns; j < newColumns && i < curPosition.x; j++) {
row.setElement(defaultLayout, j);
}
newRows.add(row);
}
// applied 1 column-fix; last column needs to have a width of 0
float [] newWidths = new float[newColumns];
for (int j = 0; j < columns; j++) {
newWidths[j] = widths[j];
}
for (int j = columns; j < newColumns ; j++) {
newWidths[j] = 0;
}
columns = newColumns;
widths = newWidths;
rows = newRows;
}
/**
* Gets an array with the positions of the borders between every column.
* <P>
* This method translates the widths expressed in percentages into the
* x-coordinate of the borders of the columns on a real document.
*
* @param left this is the position of the first border at the left (cellpadding not included)
* @param totalWidth this is the space between the first border at the left
* and the last border at the right (cellpadding not included)
* @return an array with borderpositions
*/
public float[] getWidths(float left, float totalWidth) {
// for x columns, there are x+1 borders
float[] w = new float[columns + 1];
// the border at the left is calculated
switch(alignment) {
case Element.ALIGN_LEFT:
w[0] = left;
break;
case Element.ALIGN_RIGHT:
w[0] = left + (totalWidth * (100 - widthPercentage)) / 100;
break;
case Element.ALIGN_CENTER:
default:
w[0] = left + (totalWidth * (100 - widthPercentage)) / 200;
}
// the total available width is changed
totalWidth = (totalWidth * widthPercentage) / 100;
// the inner borders are calculated
for (int i = 1; i < columns; i++) {
w[i] = w[i - 1] + (widths[i - 1] * totalWidth / 100);
}
// the border at the right is calculated
w[columns] = w[0] + totalWidth;
return w;
}
/**
* Sets current col/row to valid(empty) pos after addCell/Table
* @param aLocation a location in the Table
*/
private void setCurrentLocationToNextValidPosition(Point aLocation) {
// set latest location to next valid position
int i, j;
i = aLocation.x;
j = aLocation.y;
do {
if ( (j + 1) == columns ) { // goto next row
i++;
j = 0;
}
else {
j++;
}
}
while (
(i < rows.size()) && (j < columns) && (((Row) rows.get(i)).isReserved(j) == true)
);
curPosition = new Point(i, j);
}
/**
* Checks if a given tag corresponds with this object.
*
* @param tag the given tag
* @return true if the tag corresponds
*/
public static boolean isTag(String tag) {
return ElementTags.TABLE.equals(tag);
}
/**
* Allows clients to set up alternating attributes for each Row in the Table.
* <P>
* This code was contributed by Matt Benson.
*
* @param name the name of the attribute
* @param value0 the value of the attribute for even rows
* @param value1 the value of the attribute for odd rows
*/
public void setAlternatingRowAttribute(String name, String value0, String value1) {
if (value0 == null || value1 == null) {
throw new NullPointerException("MarkupTable#setAlternatingRowAttribute(): null values are not permitted.");
}
if (alternatingRowAttributes == null) alternatingRowAttributes = new Hashtable();
// we could always use new Arrays but this is big enough
String[] value = (String[])(alternatingRowAttributes.get(name));
if (value == null) value = new String[2];
value[0] = value0;
value[1] = value1;
alternatingRowAttributes.put(name, value);
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @return NA
*/
public float top() {
throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @return NA
*/
public float bottom() {
throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @return NA
*/
public float left() {
throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @return NA
*/
public float right() {
throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @param margin NA
* @return NA
*/
public float top(int margin) {
throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @param margin NA
* @return NA
*/
public float bottom(int margin) {
throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @param margin NA
* @return NA
*/
public float left(int margin) {
throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @param margin NA
* @return NA
*/
public float right(int margin) {
throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @param value NA
*/
public void setTop(int value) {
throw new UnsupportedOperationException("Dimensions of a Table are attributed automagically. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @param value NA
*/
public void setBottom(int value) {
throw new UnsupportedOperationException("Dimensions of a Table are attributed automagically. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @param value NA
*/
public void setLeft(int value) {
throw new UnsupportedOperationException("Dimensions of a Table are attributed automagically. See the FAQ.");
}
/**
* This method throws an <CODE>UnsupportedOperationException</CODE>.
* @param value NA
*/
public void setRight(int value) {
throw new UnsupportedOperationException("Dimensions of a Table are attributed automagically. See the FAQ.");
}
/**
* Returns the next row 0-based index where a new cell would be added.
* (contributed by dperezcar@fcc.es)
* @return x coordinate for the next row
*/
public int getNextRow() {
return curPosition.x;
}
/**
* Returns the next column 0-based index where a new cell would be added.
* (contributed by dperezcar@fcc.es)
* @return y coordinate for the next row
*/
public int getNextColumn() {
return curPosition.y;
}
private static final double convertWidth( double val) {
if( val == 0) {
return 0;
}
try {
String tmp = widthFormat.format( val);
Number result = widthFormat.parse( tmp);
return result.doubleValue();
}
catch( java.text.ParseException pe) {
throw new RuntimeException( "Could not convert double to width for val:" + val);
}
}
private static DecimalFormat widthFormat = new DecimalFormat( "0.00");
/**
* Create a PdfPTable based on this Table object.
* @return a PdfPTable object
* @throws BadElementException
*/
public PdfPTable createPdfPTable() throws BadElementException {
if (!convert2pdfptable) {
throw new BadElementException("No error, just an old style table");
}
setAutoFillEmptyCells(true);
complete();
PdfPTable pdfptable = new PdfPTable(widths);
pdfptable.setTableEvent(SimpleTable.getDimensionlessInstance(this, cellspacing));
pdfptable.setHeaderRows(lastHeaderRow + 1);
pdfptable.setSplitLate(cellsFitPage);
if (!Float.isNaN(offset)) {
pdfptable.setSpacingBefore(offset);
}
pdfptable.setHorizontalAlignment(alignment);
if (absWidth.length() > 0) {
try {
pdfptable.setTotalWidth(Float.parseFloat(absWidth));
}
catch(Exception e1) {
try {
pdfptable.setTotalWidth((float)Integer.parseInt(absWidth));
}
catch(Exception e2) {
pdfptable.setWidthPercentage(widthPercentage);
}
}
}
else {
pdfptable.setWidthPercentage(widthPercentage);
}
Row row;
for (Iterator iterator = iterator(); iterator.hasNext(); ) {
row = (Row) iterator.next();
Element cell;
PdfPCell pcell;
for (int i = 0; i < row.columns(); i++) {
if ((cell = (Element)row.getCell(i)) != null) {
if (cell instanceof Table) {
pcell = new PdfPCell(((Table)cell).createPdfPTable());
}
else if (cell instanceof Cell) {
pcell = ((Cell)cell).createPdfPCell();
pcell.setPadding(cellpadding + cellspacing / 2f);
pcell.setCellEvent(SimpleCell.getDimensionlessInstance((Cell)cell, cellspacing));
}
else {
pcell = new PdfPCell();
}
pdfptable.addCell(pcell);
}
}
}
return pdfptable;
}
/**
* Method to check if the Table should be converted to a PdfPTable or not.
* @return false if the table should be handled the oldfashioned way.
*/
public boolean isConvert2pdfptable() {
return convert2pdfptable;
}
/**
* If set to true, iText will try to convert the Table to a PdfPTable.
* @param convert2pdfptable true if you want iText to try to convert the Table to a PdfPTable
*/
public void setConvert2pdfptable(boolean convert2pdfptable) {
this.convert2pdfptable = convert2pdfptable;
}
}
|