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 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
|
/*
* ====================================================================
*
* The Clearthought Software License, Version 2.0
*
* Copyright (c) 2001 Daniel Barbalace. All rights reserved.
*
* Project maintained at https://tablelayout.dev.java.net
*
* I. Terms for redistribution of original source and binaries
*
* Redistribution and use of unmodified source and/or binaries are
* permitted provided that the following condition is met:
*
* 1. Redistributions of original source code must retain the above
* copyright notice and license. You are not required to redistribute
* the original source; you may choose to redistribute only the
* binaries.
*
* Basically, if you distribute unmodified source, you meet
* automatically comply with the license with no additional effort on
* your part.
*
* II. Terms for distribution of derived works via subclassing and/or
* composition.
*
* You may generate derived works by means of subclassing and/or
* composition (e.g., the Adaptor Pattern), provided that the following
* conditions are met:
*
* 1. Redistributions of original source code must retain the above
* copyright notice and license. You are not required to redistribute
* the original source; you may choose to redistribute only the
* binaries.
*
* 2. The original software is not altered.
*
* 3. Derived works are not contained in the info.clearthought
* namespace/package or any subpackage of info.clearthought.
*
* 4. Derived works do not use the class or interface names from the
* info.clearthought... packages
*
* For example, you may define a class with the following full name:
* org.nameOfMyOrganization.layouts.RowMajorTableLayout
*
* However, you may not define a class with the either of the
* following full names:
* info.clearthought.layout.RowMajorTableLayout
* org.nameOfMyOrganization.layouts.TableLayout
*
* III. Terms for redistribution of source modified via patch files.
*
* You may generate derived works by means of patch files provided
* that the following conditions are met:
*
* 1. Redistributions of original source code must retain the above
* copyright notice and license. You are not required to
* redistribute the original source; you may choose to redistribute
* only the binaries resulting from the patch files.
*
* 2. The original source files are not altered. All alteration is
* done in patch files.
*
* 3. Derived works are not contained in the info.clearthought
* namespace/package or any subpackage of info.clearthought. This
* means that your patch files must change the namespace/package
* for the derived work. See section II for examples.
*
* 4. Derived works do not use the class or interface names from the
* info.clearthought... packages. This means your patch files
* must change the names of the interfaces and classes they alter.
* See section II for examples.
*
* 5. Derived works must include the following disclaimer.
* "This work is derived from Clearthought's TableLayout,
* https://tablelayout.dev.java.net, by means of patch files
* rather than subclassing or composition. Therefore, this work
* might not contain the latest fixes and features of TableLayout."
*
* IV. Terms for repackaging, transcoding, and compiling of binaries.
*
* You may do any of the following with the binaries of the
* original software.
*
* 1. You may move binaries (.class files) from the original .jar file
* to your own .jar file.
*
* 2. You may move binaries from the original .jar file to other
* resource containing files, including but not limited to .zip,
* .gz, .tar, .dll, .exe files.
*
* 3. You may backend compile the binaries to any platform, including
* but not limited to Win32, Win64, MAC OS, Linux, Palm OS, any
* handheld or embedded platform.
*
* 4. You may transcribe the binaries to other virtual machine byte
* code protocols, including but not limited to .NET.
*
* V. License Disclaimer.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, AFFILATED BUSINESSES,
* OR ANYONE ELSE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*/
package org.debian.tablelayout;
import java.awt.*;
import java.util.*;
import java.lang.reflect.*;
/**
* <p>TableLayout is a layout manager that is more powerful than GridBagLayout
* yet much easier to use.</p>
*
* <b>Background</b>
*
* <p>TableLayout is a layout manager that arranges components in rows and
* columns like a spreadsheet. TableLayout allows each row or column to be a
* different size. A row or column can be given an absolute size in pixels, a
* percentage of the available space, or it can grow and shrink to fill the
* remaining space after other rows and columns have been resized. </p>
*
* <p>Using spreadsheet terminology, a cell is the intersection of a row and
* column. Cells have finite, non-negative sizes measured in pixels. The
* dimensions of a cell depend solely upon the dimensions of its row and column.
* </p>
*
* <p>A component occupies a rectangular group of one or more cells. The
* component can be aligned within those cells using four vertical and six
* horizontal justifications. The vertical justifications are left, center,
* right, and full. The horizontal justifications are left, center, right,
* full, leading, and trailing. With full justification the component is
* stretched either vertically or horizontally to fit the cell or group of
* cells.<p>
*
* <b>Justification</b>
*
* <p>Leading and trailing justification are used to support languages that
* are read from right to left. See the
* <code>java.awt.ComponentOrientation</code> class for details and
* http://java.sun.com/products/jfc/tsc/articles/bidi for an introduction to
* component orientation and bidirectional text support. The leading
* justification will align the component along the leading edge of the
* container and the trailing justification will align the component along the
* trailing edge. There is no leading or trailing justification along the
* vertical axis since all modern languages are read from top to bottom and
* no bottom-to-top orientation is defined in
* <code>java.awt.ComponentOrientation.</code></p>
*
* <p>For components using the <code>ComponentOrientation.LEFT_TO_RIGHT</code>
* orientation, the leading edge is the left edge and the trailing edge is the
* right one. For components using the <code>ComponentOrientation.RIGHT_TO_LEFT
* </code> orientation, the opposite is true. For components that are using
* <code>ComponentOrientation.UNKNOWN</code> and for Java runtime environments
* that do not support component orientation, left-to-right orientation is
* assumed for backwards compatibility.</p>
*
* <b>Gaps</b>
*
* <p>Horizontal and vertical gaps can be placed between rows and columns in two
* ways. If uniformed gaps are desired, the <code>setHGap</code> and <code>
* setVGap</code> methods may be used. To vary the size of gaps, simply use
* empty rows and columns with absolute sizes. Similarly, to make a border
* around a container that does not have insets, use empty rows and columns
* along the edges of the container.</p>
*
* <b>Constraints</b>
*
* <p>Using TableLayout is a simple two step process. First, create a grid for
* your container by specifying row and column sizes using either a TableLayout
* constructor or the <code>insertRow</code> and <code>insertColumn</code>
* methods. Second, add components to the cells formed by the rows and
* columns.</p>
*
* <p>When adding a component to a container that uses TableLayout, you
* specify the component's constraints that state which cells the component
* will occupy and how the component will be aligned. The constraints
* can be specified into two ways. The <code>TableLayoutConstraints</code>
* class can be used to systematically specify the constraints. This is
* useful to dynamic code, bean builders, and rapid application development
* software.</p>
*
* <p>For manual coding, a quicker and easier way to specify constraints is with
* a short string in the form "x1, y1, x2, y2, hAlign, vAlign" where (x1, y1)
* identifies the top left cell (column x1, row y1) for the component and
* (x2, y2) identifies the bottom right cell. x2 and y2 are optional. If they
* are not specified, the component will occupy only one cell, (x1, y1).
* hAlign and vAlign are also optional with default values of full
* justification. Alignments may be spelt fully as in "LEFT" or abbreviated as
* in "L". The text is not case sensitive, but it is recommended that uppercase
* is used for two reasons. First, these text values are in essence constants.
* Second, some fonts use the same glyphs for representing a lowercase L and
* the number one. Ex., "l" vs. "1". Even fonts that do not will often use
* similar glyphs so using uppercase avoids confusion.</p>
*
* <b>Dynamically altering the layout</b>
*
* <p>Rows and columns can be dynamically created, resized, and removed at any
* time, even if the container is visible. Components will be shifted
* appropriately as rows and columns are inserted or removed, just as cells
* are shifted in a spreadsheet.</p>
*
* <p>Rows and columns can be made "hidden" or effectively invisible by setting
* their size to zero. They can be shown again by setting their size back to
* a non-zero value. This is very useful for toggle form elements without
* having to remove individual components.</p>
*
* <b>Preferred sizes</b>
*
* <p>Often it is desirable to make a row or column just large enough to ensure
* that all components contained partially or wholly in that row or column are
* their preferred size. To make this easy, there is a constant called
* <code>PREFERRED</code> that can be used to specify row or column sizes.
* There is another constant called <code>MINIMUM</code> that does a similar
* task using components' minimum sizes instead of their preferred sizes.</p>
*
* <p>There is no corresponding <code>MAXIMUM</code> constant for several
* reasons. First, it is mathematically impossible to honor both the minimum
* and maximum sizes of more than one component when conflicts arise. For
* example, say components a and b are in the same row. If a's maximum height
* is less than b's minimum height, then one of these constraints must be
* violated. Since TableLayout is a complete, general Cartesian layout manager,
* it would be possible to specify conflicting constraints if a <code>MAXIMUM
* </code> constant existed.<p>
*
* <p>Second, the ability to make a component grow up to a maximum size is
* primarily of interest to layout managers like <code>SpringLayout</code> that
* have to balance the sizes of components because the presence of one component
* affects the size of another. Other than the effect of preferred and minimum
* size rows/columns, which are essentially convenient ways of specifying
* absolute sizes, the existence and constraints of one component does not
* affect any other components when using TableLayout. This is accomplished
* because rows and columns are explicit in TableLayout.</p>
*
* <p>Third, the ability to constrain a component to its maximum size is
* subsumed by the ability to constrain it to its preferred size, which is
* precisely what happens when a component is aligned using anything but
* full justification. In the case of full justification, the component's
* maximum size is by definition unbounded.</p>
*
* <b>Example</b>
*
* <pre>
* import java.awt.*;
* import javax.swing.*;
* import info.clearthought.layout.TableLayout;
* <spc>
* public class Preferred extends JFrame
* {
* <spc>
* public static void main (String args[])
* {
* new Preferred();
* }
* <spc>
* public Preferred ()
* {
* super("The Power of Preferred Sizes");
* setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
* Container pane = getContentPane();
* <spc>
* // b - border
* // f - FILL
* // p - PREFERRED
* // vs - vertical space between labels and text fields
* // vg - vertical gap between form elements
* // hg - horizontal gap between form elements
* <spc>
* double b = 10;
* double f = TableLayout.FILL;
* double p = TableLayout.PREFERRED;
* double vs = 5;
* double vg = 10;
* double hg = 10;
* <spc>
* double size[][] =
* {{b, f, hg, p, hg, p, b},
* {b, p, vs, p, vg, p, vs, p, vg, p, vs, p, vg, p, b}};
* <spc>
* TableLayout layout = new TableLayout(size);
* pane.setLayout (layout);
* <spc>
* // Create all controls
* JLabel labelName = new JLabel("Name");
* JLabel labelAddress = new JLabel("Address");
* JLabel labelCity = new JLabel("City");
* JLabel labelState = new JLabel("State");
* JLabel labelZip = new JLabel("Zip");
* <spc>
* JTextField textfieldName = new JTextField(10);
* JTextField textfieldAddress = new JTextField(20);
* JTextField textfieldCity = new JTextField(10);
* JTextField textfieldState = new JTextField(2);
* JTextField textfieldZip = new JTextField(5);
* <spc>
* JButton buttonOk = new JButton("OK");
* JButton buttonCancel = new JButton("Cancel");
* JPanel panelButton = new JPanel();
* panelButton.add(buttonOk);
* panelButton.add(buttonCancel);
* <spc>
* // Add all controls
* pane.add(labelName, "1, 1, 5, 1");
* pane.add(textfieldName, "1, 3, 5, 3");
* pane.add(labelAddress, "1, 5, 5, 5");
* pane.add(textfieldAddress, "1, 7, 5, 7");
* pane.add(labelCity, "1, 9");
* pane.add(textfieldCity, "1, 11");
* pane.add(labelState, "3, 9");
* pane.add(textfieldState, "3, 11");
* pane.add(labelZip, "5, 9");
* pane.add(textfieldZip, "5, 11");
* pane.add(panelButton, "1, 13, 5, 13");
* <spc>
* pack();
* setResizable(false);
* show();
* }
* }
* </pre>
*
* @version 4.2 August 26, 2009
* @author Daniel E. Barbalace
*/
public class TableLayout implements
java.awt.LayoutManager2,
java.io.Serializable,
org.debian.tablelayout.TableLayoutConstants
{
/*
Note: In this file, a cr refers to either a column or a row. cr[C] always
means column and cr[R] always means row. A cr size is either a column
width or a row Height. TableLayout views columns and rows as being
conceptually symmetric. Therefore, much of the code applies to both
columns and rows, and the use of the cr terminology eliminates redundancy.
Also, for ease of reading, z always indicates a parameter whose value is
either C or R.
*/
/** Default row/column size */
protected static final double defaultSize[][] = {{}, {}};
/** Indicates a column */
protected static final int C = 0;
/** Indicates a row */
protected static final int R = 1;
/** Used to minimize reflection calls */
protected static boolean checkForComponentOrientationSupport = true;
/** Method used to get component orientation while preserving compatibility
with earlier versions of java.awt.Container. Necessary for supporting
older JDKs and MicroEdition versions of Java. */
protected static Method methodGetComponentOrientation;
/** Sizes of crs expressed in absolute and relative terms */
protected double crSpec[][] = {null, null};
/** Sizes of crs in pixels */
protected int crSize[][] = {null, null};
/** Offsets of crs in pixels. The left boarder of column n is at
crOffset[C][n] and the right boarder is at cr[C][n + 1] for all
columns including the last one. crOffset[C].length = crSize[C].length + 1 */
protected int crOffset[][] = {null, null};
/** List of components and their sizes */
protected LinkedList list;
/** Indicates whether or not the size of the cells are known for the last known
size of the container. If dirty is true or the container has been resized,
the cell sizes must be recalculated using calculateSize. */
protected boolean dirty;
/** Previous known width of the container */
protected int oldWidth;
/** Previous known height of the container */
protected int oldHeight;
/** Horizontal gap between columns */
protected int hGap;
/** Vertical gap between rows */
protected int vGap;
//******************************************************************************
//** Constructors ***
//******************************************************************************
/**
* Constructs an instance of TableLayout. This TableLayout will have no columns
* or rows. This constructor is most useful for bean-oriented programming and
* dynamically adding columns and rows.
*/
public TableLayout ()
{
init(defaultSize[C], defaultSize[R]);
}
/**
* Constructs an instance of TableLayout.
*
* @param size widths of columns and heights of rows in the format,
* {{col0, col1, col2, ..., colN}, {row0, row1, row2, ..., rowM}}
* If this parameter is invalid, the TableLayout will have
* exactly one row and one column.
*/
public TableLayout (double [][] size)
{
// Make sure columns and rows and nothing else is specified
if ((size != null) && (size.length == 2))
init(size[C], size[R]);
else
throw new IllegalArgumentException
("Parameter size should be an array, a[2], where a[0] is the " +
"is an array of column widths and a[1] is an array or row " +
"heights.");
}
/**
* Constructs an instance of TableLayout.
*
* @param col widths of columns in the format, {{col0, col1, col2, ..., colN}
* @param row heights of rows in the format, {{row0, row1, row2, ..., rowN}
*/
public TableLayout (double [] col, double [] row)
{
init(col, row);
}
/**
* Initializes the TableLayout for all constructors.
*
* @param col widths of columns in the format, {{col0, col1, col2, ..., colN}
* @param row heights of rows in the format, {{row0, row1, row2, ..., rowN}
*/
protected void init (double [] col, double [] row)
{
// Check parameters
if (col == null)
throw new IllegalArgumentException("Parameter col cannot be null");
if (row == null)
throw new IllegalArgumentException("Parameter row cannot be null");
// Create new rows and columns
crSpec[C] = new double[col.length];
crSpec[R] = new double[row.length];
// Copy rows and columns
System.arraycopy(col, 0, crSpec[C], 0, crSpec[C].length);
System.arraycopy(row, 0, crSpec[R], 0, crSpec[R].length);
// Make sure rows and columns are valid
for (int counter = 0; counter < crSpec[C].length; counter++)
if ((crSpec[C][counter] < 0.0) &&
(crSpec[C][counter] != FILL) &&
(crSpec[C][counter] != PREFERRED) &&
(crSpec[C][counter] != MINIMUM))
{
crSpec[C][counter] = 0.0;
}
for (int counter = 0; counter < crSpec[R].length; counter++)
if ((crSpec[R][counter] < 0.0) &&
(crSpec[R][counter] != FILL) &&
(crSpec[R][counter] != PREFERRED) &&
(crSpec[R][counter] != MINIMUM))
{
crSpec[R][counter] = 0.0;
}
// Create an empty list of components
list = new LinkedList();
// Indicate that the cell sizes are not known
dirty = true;
}
//******************************************************************************
//** Get/Set methods ***
//******************************************************************************
/**
* Gets the constraints of a given component.
*
* @param component desired component
*
* @return If the given component is found, the constraints associated with
* that component. If the given component is null or is not found,
* null is returned.
*/
public TableLayoutConstraints getConstraints (Component component)
{
ListIterator iterator = list.listIterator(0);
while (iterator.hasNext())
{
Entry entry = (Entry) iterator.next();
if (entry.component == component)
return new TableLayoutConstraints
(entry.cr1[C], entry.cr1[R], entry.cr2[C], entry.cr2[R],
entry.alignment[C], entry.alignment[R]);
}
return null;
}
/**
* Sets the constraints of a given component.
*
* @param component desired component. This parameter cannot be null.
* @param constraint new set of constraints. This parameter cannot be null.
*/
public void setConstraints
(Component component, TableLayoutConstraints constraint)
{
// Check parameters
if (component == null)
throw new IllegalArgumentException
("Parameter component cannot be null.");
else if (constraint == null)
throw new IllegalArgumentException
("Parameter constraint cannot be null.");
// Find and update constraints for the given component
ListIterator iterator = list.listIterator(0);
while (iterator.hasNext())
{
Entry entry = (Entry) iterator.next();
if (entry.component == component)
iterator.set(new Entry(component, constraint));
}
}
/**
* Adjusts the number and sizes of rows in this layout. After calling this
* method, the caller should request this layout manager to perform the
* layout. This can be done with the following code:
*
* <pre>
* layout.layoutContainer(container);
* container.repaint();
* </pre>
*
* or
*
* <pre>
* window.pack()
* </pre>
*
* If this is not done, the changes in the layout will not be seen until the
* container is resized.
*
* @param column widths of each of the columns
*
* @see #getColumn
*/
public void setColumn (double column[])
{
setCr(C, column);
}
/**
* Adjusts the number and sizes of rows in this layout. After calling this
* method, the caller should request this layout manager to perform the
* layout. This can be done with the following code:
*
* <code>
* layout.layoutContainer(container);
* container.repaint();
* </code>
*
* or
*
* <pre>
* window.pack()
* </pre>
*
* If this is not done, the changes in the layout will not be seen until the
* container is resized.
*
* @param row heights of each of the rows. This parameter cannot be null.
*
* @see #getRow
*/
public void setRow (double row[])
{
setCr(R, row);
}
/**
* Sets the sizes of rows or columns for the methods setRow or setColumn.
*
* @param z indicates row or column
* @param size new cr size
*/
protected void setCr (int z, double size[])
{
// Copy crs
crSpec[z] = new double[size.length];
System.arraycopy(size, 0, crSpec[z], 0, crSpec[z].length);
// Make sure rows are valid
for (int counter = 0; counter < crSpec[z].length; counter++)
if ((crSpec[z][counter] < 0.0) &&
(crSpec[z][counter] != FILL) &&
(crSpec[z][counter] != PREFERRED) &&
(crSpec[z][counter] != MINIMUM))
{
crSpec[z][counter] = 0.0;
}
// Indicate that the cell sizes are not known
dirty = true;
}
/**
* Adjusts the width of a single column in this layout. After calling this
* method, the caller should request this layout manager to perform the
* layout. This can be done with the following code:
*
* <code>
* layout.layoutContainer(container);
* container.repaint();
* </code>
*
* or
*
* <pre>
* window.pack()
* </pre>
*
* If this is not done, the changes in the layout will not be seen until the
* container is resized.
*
* @param i zero-based index of column to set. If this parameter is not
* valid, an ArrayOutOfBoundsException will be thrown.
* @param size width of the column. This parameter cannot be null.
*
* @see #getColumn
*/
public void setColumn (int i, double size)
{
setCr(C, i, size);
}
/**
* Adjusts the height of a single row in this layout. After calling this
* method, the caller should request this layout manager to perform the
* layout. This can be done with the following code:
*
* <code>
* layout.layoutContainer(container);
* container.repaint();
* </code>
*
* or
*
* <pre>
* window.pack()
* </pre>
*
* If this is not done, the changes in the layout will not be seen until the
* container is resized.
*
* @param i zero-based index of row to set. If this parameter is not
* valid, an ArrayOutOfBoundsException will be thrown.
* @param size height of the row. This parameter cannot be null.
*
* @see #getRow
*/
public void setRow (int i, double size)
{
setCr(R, i, size);
}
/**
* Sets the sizes of rows or columns for the methods setRow or setColumn.
*
* @param z indicates row or column
* @param i indicates which cr to resize
* @param size new cr size
*/
protected void setCr (int z, int i, double size)
{
// Make sure size is valid
if ((size < 0.0) &&
(size != FILL) &&
(size != PREFERRED) &&
(size != MINIMUM))
{
size = 0.0;
}
// Copy new size
crSpec[z][i] = size;
// Indicate that the cell sizes are not known
dirty = true;
}
/**
* Gets the sizes of columns in this layout.
*
* @return widths of each of the columns
*
* @see #setColumn
*/
public double [] getColumn ()
{
// Copy columns
double column[] = new double[crSpec[C].length];
System.arraycopy(crSpec[C], 0, column, 0, column.length);
return column;
}
/**
* Gets the height of a single row in this layout.
*
* @return height of the requested row
*
* @see #setRow
*/
public double [] getRow ()
{
// Copy rows
double row[] = new double[crSpec[R].length];
System.arraycopy(crSpec[R], 0, row, 0, row.length);
return row;
}
/**
* Gets the width of a single column in this layout.
*
* @param i zero-based index of row to get. If this parameter is not valid,
* an ArrayOutOfBoundsException will be thrown.
*
* @return width of the requested column
*
* @see #setRow
*/
public double getColumn (int i)
{
return crSpec[C][i];
}
/**
* Gets the sizes of a row in this layout.
*
* @param i zero-based index of row to get. If this parameter is not valid,
* an ArrayOutOfBoundsException will be thrown.
*
* @return height of each of the requested row
*
* @see #setRow
*/
public double getRow (int i)
{
return crSpec[R][i];
}
/**
* Gets the number of columns in this layout.
*
* @return the number of columns
*/
public int getNumColumn ()
{
return crSpec[C].length;
}
/**
* Gets the number of rows in this layout.
*
* @return the number of rows
*/
public int getNumRow ()
{
return crSpec[R].length;
}
/**
* Gets the horizontal gap between colunns.
*
* @return the horizontal gap in pixels
*/
public int getHGap ()
{
return hGap;
}
/**
* Gets the vertical gap between rows.
*
* @return the vertical gap in pixels
*/
public int getVGap ()
{
return vGap;
}
/**
* Sets the horizontal gap between colunns.
*
* @param hGap the horizontal gap in pixels
*/
public void setHGap (int hGap)
{
if (hGap >= 0)
this.hGap = hGap;
else
throw new IllegalArgumentException
("Parameter hGap must be non-negative.");
}
/**
* Sets the vertical gap between rows.
*
* @param vGap the horizontal gap in pixels
*/
public void setVGap (int vGap)
{
if (vGap >= 0)
this.vGap = vGap;
else
throw new IllegalArgumentException
("Parameter vGap must be non-negative.");
}
//******************************************************************************
//** Insertion/Deletion methods ***
//******************************************************************************
/**
* Inserts a column in this layout. All components to the right of the
* insertion point are moved right one column. The container will need to
* be laid out after this method returns. See <code>setColumn</code>.
*
* @param i zero-based index at which to insert the column
* @param size size of the column to be inserted
*
* @see #setColumn
* @see #deleteColumn
*/
public void insertColumn (int i, double size)
{
insertCr(C, i, size);
}
/**
* Inserts a row in this layout. All components below the insertion point
* are moved down one row. The container will need to be laid out after this
* method returns. See <code>setRow</code>.
*
* @param i zero-based index at which to insert the row
* @param size size of the row to be inserted
*
* @see #setRow
* @see #deleteRow
*/
public void insertRow (int i, double size)
{
insertCr(R, i, size);
}
/**
* Inserts a cr for the methods insertRow or insertColumn.
*
* @param z indicates row or column
* @param i zero-based index at which to insert the cr
* @param size size of cr being inserted
*/
protected void insertCr (int z, int i, double size)
{
// Make sure position is valid
if ((i < 0) || (i > crSpec[z].length))
throw new IllegalArgumentException
("Parameter i is invalid. i = " + i + ". Valid range is [0, " +
crSpec[z].length + "].");
// Make sure row size is valid
if ((size < 0.0) &&
(size != FILL) &&
(size != PREFERRED) &&
(size != MINIMUM))
{
size = 0.0;
}
// Copy crs
double cr[] = new double[crSpec[z].length + 1];
System.arraycopy(crSpec[z], 0, cr, 0, i);
System.arraycopy(crSpec[z], i, cr, i + 1, crSpec[z].length - i);
// Insert cr
cr[i] = size;
crSpec[z] = cr;
// Move all components that are below the new cr
ListIterator iterator = list.listIterator(0);
while (iterator.hasNext())
{
// Get next entry
Entry entry = (Entry) iterator.next();
// Is the first cr below the new cr
if (entry.cr1[z] >= i)
// Move first cr
entry.cr1[z]++;
// Is the second cr below the new cr
if (entry.cr2[z] >= i)
// Move second cr
entry.cr2[z]++;
}
// Indicate that the cell sizes are not known
dirty = true;
}
/**
* Deletes a column in this layout. All components to the right of the
* deletion point are moved left one column. The container will need to
* be laid out after this method returns. See <code>setColumn</code>.
*
* @param i zero-based index of column to delete
*
* @see #setColumn
* @see #deleteColumn
*/
public void deleteColumn (int i)
{
deleteCr(C, i);
}
/**
* Deletes a row in this layout. All components below the deletion point are
* moved up one row. The container will need to be laid out after this method
* returns. See <code>setRow</code>. There must be at least two rows in order
* to delete a row.
*
* @param i zero-based index of row to delete
*
* @see #setRow
* @see #deleteRow
*/
public void deleteRow (int i)
{
deleteCr(R, i);
}
/**
* Deletes a cr for the methods deleteRow or deleteColumn.
*
* @param z indicates row or column
* @param i zero-based index of cr to delete
*/
protected void deleteCr (int z, int i)
{
// Make sure position is valid
if ((i < 0) || (i >= crSpec[z].length))
throw new IllegalArgumentException
("Parameter i is invalid. i = " + i + ". Valid range is [0, " +
(crSpec[z].length - 1) + "].");
// Copy rows
double cr[] = new double[crSpec[z].length - 1];
System.arraycopy(crSpec[z], 0, cr, 0, i);
System.arraycopy(crSpec[z], i + 1, cr, i, crSpec[z].length - i - 1);
// Delete row
crSpec[z] = cr;
// Move all components that are to below the row deleted
ListIterator iterator = list.listIterator(0);
while (iterator.hasNext())
{
// Get next entry
Entry entry = (Entry) iterator.next();
// Is the first row below the new row
if (entry.cr1[z] > i)
// Move first row
entry.cr1[z]--;
// Is the second row below the new row
if (entry.cr2[z] > i)
// Move second row
entry.cr2[z]--;
}
// Indicate that the cell sizes are not known
dirty = true;
}
//******************************************************************************
//** Misc methods ***
//******************************************************************************
/**
* Converts this TableLayout to a string.
*
* @return a string representing the columns and row sizes in the form
* "{{col0, col1, col2, ..., colN}, {row0, row1, row2, ..., rowM}}"
*/
public String toString ()
{
int counter;
String value = "TableLayout {{";
if (crSpec[C].length > 0)
{
for (counter = 0; counter < crSpec[C].length - 1; counter++)
value += crSpec[C][counter] + ", ";
value += crSpec[C][crSpec[C].length - 1] + "}, {";
}
else
value += "}, {";
if (crSpec[R].length > 0)
{
for (counter = 0; counter < crSpec[R].length - 1; counter++)
value += crSpec[R][counter] + ", ";
value += crSpec[R][crSpec[R].length - 1] + "}}";
}
else
value += "}}";
return value;
}
/**
* Determines whether or not there are any components with invalid constraints.
* An invalid constraint is one that references a non-existing row or column.
* For example, on a table with five rows, row -1 and row 5 are both invalid.
* Valid rows are 0 through 4, inclusively. This method is useful for
* debugging.
*
* @return a list of TableLayout.Entry instances referring to the invalid
* constraints and corresponding components
*
* @see #getOverlappingEntry
*/
public java.util.List getInvalidEntry ()
{
LinkedList listInvalid = new LinkedList();
try
{
ListIterator iterator = list.listIterator(0);
while (iterator.hasNext())
{
Entry entry = (Entry) iterator.next();
if ((entry.cr1[R] < 0) || (entry.cr1[C] < 0) ||
(entry.cr2[R] >= crSpec[R].length) ||
(entry.cr2[C] >= crSpec[C].length))
{
listInvalid.add(entry.copy());
}
}
}
catch (CloneNotSupportedException error)
{
throw new RuntimeException("Unexpected CloneNotSupportedException");
}
return listInvalid;
}
/**
* Gets a list of overlapping components and their constraints. Two
* components overlap if they cover at least one common cell. This method is
* useful for debugging.
*
* @return a list of zero or more TableLayout.Entry instances
*
* @see #getInvalidEntry
*/
public java.util.List getOverlappingEntry ()
{
LinkedList listOverlapping = new LinkedList();
try
{
// Count constraints
int numEntry = list.size();
// If there are no components, they can't be overlapping
if (numEntry == 0)
return listOverlapping;
// Put entries in an array
Entry entry[] = (Entry []) list.toArray(new Entry[numEntry]);
// Check all components
for (int knowUnique = 1; knowUnique < numEntry; knowUnique++)
for (int checking = knowUnique - 1; checking >= 0; checking--)
if
(
(
(entry[checking].cr1[C] >= entry[knowUnique].cr1[C]) &&
(entry[checking].cr1[C] <= entry[knowUnique].cr2[C]) &&
(entry[checking].cr1[R] >= entry[knowUnique].cr1[R]) &&
(entry[checking].cr1[R] <= entry[knowUnique].cr2[R])
)
||
(
(entry[checking].cr2[C] >= entry[knowUnique].cr1[C]) &&
(entry[checking].cr2[C] <= entry[knowUnique].cr2[C]) &&
(entry[checking].cr2[R] >= entry[knowUnique].cr1[R]) &&
(entry[checking].cr2[R] <= entry[knowUnique].cr2[R])
)
)
{
listOverlapping.add(entry[checking].copy());
}
}
catch (CloneNotSupportedException error)
{
throw new RuntimeException("Unexpected CloneNotSupportedException");
}
return listOverlapping;
}
//******************************************************************************
//** Calculation methods ***
//******************************************************************************
/**
* Calculates the sizes of the rows and columns based on the absolute and
* relative sizes specified in <code>crSpec[R]</code> and <code>crSpec[C]</code>
* and the size of the container. The result is stored in <code>crSize[R]</code>
* and <code>crSize[C]</code>.
*
* @param container container using this TableLayout
*/
protected void calculateSize (Container container)
{
// Get the container's insets
Insets inset = container.getInsets();
// Get the size of the container's available space
Dimension d = container.getSize();
int availableWidth = d.width - inset.left - inset.right;
int availableHeight = d.height - inset.top - inset.bottom;
// Compensate for horiztonal and vertical gaps
if (crSpec[C].length > 0)
availableWidth -= hGap * (crSpec[C].length - 1);
if (crSpec[R].length > 0)
availableHeight -= vGap * (crSpec[R].length - 1);
// Create array to hold actual sizes in pixels
crSize[C] = new int[crSpec[C].length];
crSize[R] = new int[crSpec[R].length];
// Assign absolute sizes (must be done before assignPrefMinSize)
availableWidth = assignAbsoluteSize(C, availableWidth);
availableHeight = assignAbsoluteSize(R, availableHeight);
// Assign preferred and minimum sizes (must be done after assignAbsoluteSize)
availableWidth = assignPrefMinSize(C, availableWidth, MINIMUM);
availableWidth = assignPrefMinSize(C, availableWidth, PREFERRED);
availableHeight = assignPrefMinSize(R, availableHeight, MINIMUM);
availableHeight = assignPrefMinSize(R, availableHeight, PREFERRED);
// Assign relative sizes
availableWidth = assignRelativeSize(C, availableWidth);
availableHeight = assignRelativeSize(R, availableHeight);
// Assign fill sizes
assignFillSize(C, availableWidth);
assignFillSize(R, availableHeight);
// Calculate cr offsets for efficiency
calculateOffset(C, inset);
calculateOffset(R, inset);
// Indicate that the size of the cells are known for the container's
// current size
dirty = false;
oldWidth = d.width;
oldHeight = d.height;
}
/**
* Assigns absolute sizes.
*
* @param z indicates row or column
* @param availableSize amount of space available in the container
*
* @return the amount of space available after absolute crs have been assigned
* sizes
*/
protected int assignAbsoluteSize (int z, int availableSize)
{
int numCr = crSpec[z].length;
for (int counter = 0; counter < numCr; counter++)
if ((crSpec[z][counter] >= 1.0) || (crSpec[z][counter] == 0.0))
{
crSize[z][counter] = (int) (crSpec[z][counter] + 0.5);
availableSize -= crSize[z][counter];
}
return availableSize;
}
/**
* Assigns relative sizes.
*
* @param z indicates row or column
* @param availableSize amount of space available in the container
*
* @return the amount of space available after relative crs have been assigned
* sizes
*/
protected int assignRelativeSize (int z, int availableSize)
{
int relativeSize = (availableSize < 0) ? 0 : availableSize;
int numCr = crSpec[z].length;
for (int counter = 0; counter < numCr; counter++)
if ((crSpec[z][counter] > 0.0) && (crSpec[z][counter] < 1.0))
{
crSize[z][counter] =
(int) (crSpec[z][counter] * relativeSize + 0.5);
availableSize -= crSize[z][counter];
}
return availableSize;
}
/**
* Assigns FILL sizes.
*
* @param z indicates row or column
* @param availableSize amount of space available in the container
*/
protected void assignFillSize (int z, int availableSize)
{
// Skip if there is no more space to allocate
if (availableSize <= 0)
return;
// Count the number of "fill" cells
int numFillSize = 0;
int numCr = crSpec[z].length;
for (int counter = 0; counter < numCr; counter++)
if (crSpec[z][counter] == FILL)
numFillSize++;
// If numFillSize is zero, the if statement below will always evaluate to
// false and the division will not occur.
// If there are more than one "fill" cell, slack may occur due to rounding
// errors
int slackSize = availableSize;
// Assign "fill" cells equal amounts of the remaining space
for (int counter = 0; counter < numCr; counter++)
if (crSpec[z][counter] == FILL)
{
crSize[z][counter] = availableSize / numFillSize;
slackSize -= crSize[z][counter];
}
// Assign one pixel of slack to each FILL cr, starting at the last one,
// until all slack has been consumed
for (int counter = numCr - 1; (counter >= 0) && (slackSize > 0); counter--)
{
if (crSpec[z][counter] == FILL)
{
crSize[z][counter]++;
slackSize--;
}
}
}
/**
* Calculates the offset of each cr.
*
* @param z indicates row or column
*/
protected void calculateOffset (int z, Insets inset)
{
int numCr = crSpec[z].length;
crOffset[z] = new int[numCr + 1];
crOffset[z][0] = (z == C) ? inset.left : inset.top;
for (int counter = 0; counter < numCr; counter++)
crOffset[z][counter + 1] =
crOffset[z][counter] + crSize[z][counter];
}
/**
* Assigned sizes to preferred and minimum size columns and rows. This
* reduces the available width and height. Minimum widths/heights must be
* calculated first because they affect preferred widths/heights, but not vice
* versa. The end result is that any component contained wholly or partly in
* a column/row of minimum/preferred width or height will get at least its
* minimum/preferred width or height, respectively.
*
* @param z indicates row or column
* @param availableSize amount of space available in the container
* @param typeOfSize indicates preferred or minimum
*
* @return the amount of space available after absolute crs have been assigned
* sizes
*/
protected int assignPrefMinSize
(int z, int availableSize, double typeOfSize)
{
// Get variables referring to columns or rows (crs)
int numCr = crSpec[z].length;
// Address every cr
for (int counter = 0; counter < numCr; counter++)
// Is the current cr a preferred/minimum (based on typeOfSize) size
if (crSpec[z][counter] == typeOfSize)
{
// Assume a maximum width of zero
int maxSize = 0;
// Find maximum preferred/min width of all components completely
// or partially contained within this cr
ListIterator iterator = list.listIterator(0);
nextComponent:
while (iterator.hasNext())
{
Entry entry = (Entry) iterator.next();
// Skip invalid entries
if ((entry.cr1[z] < 0) || (entry.cr2[z] >= numCr))
continue nextComponent;
// Find the maximum desired size of this cr based on all crs
// the current component occupies
if ((entry.cr1[z] <= counter) && (entry.cr2[z] >= counter))
{
// Setup size and number of adjustable crs
Dimension p = (typeOfSize == PREFERRED) ?
entry.component.getPreferredSize() :
entry.component.getMinimumSize();
int size = (p == null) ? 0 :
((z == C) ? p.width : p.height);
int numAdjustable = 0;
// Calculate for preferred size
if (typeOfSize == PREFERRED)
// Consider all crs this component occupies
for (int entryCr = entry.cr1[z];
entryCr <= entry.cr2[z]; entryCr++)
{
// Subtract absolute, relative, and minumum cr
// sizes, which have already been calculated
if ((crSpec[z][entryCr] >= 0.0) ||
(crSpec[z][entryCr] == MINIMUM))
{
size -= crSize[z][entryCr];
}
// Count preferred/min width columns
else if (crSpec[z][entryCr] == PREFERRED)
numAdjustable++;
// Skip any component that occupies a fill cr
// because the fill should fulfill the size
// requirements
else if (crSpec[z][entryCr] == FILL)
continue nextComponent;
}
// Calculate for minimum size
else
// Consider all crs this component occupies
for (int entryCr = entry.cr1[z];
entryCr <= entry.cr2[z]; entryCr++)
{
// Subtract absolute and relative cr sizes, which
// have already been calculated
if (crSpec[z][entryCr] >= 0.0)
size -= crSize[z][entryCr];
// Count preferred/min width columns
else if ((crSpec[z][entryCr] == PREFERRED) ||
(crSpec[z][entryCr] == MINIMUM))
{
numAdjustable++;
}
// Skip any component that occupies a fill cr
// because the fill should fulfill the size
// requirements
else if (crSpec[z][entryCr] == FILL)
continue nextComponent;
}
// Adjust remaining size by eliminating space for gaps between the crs this component occupies
int numCrOccupiedByThisComponent = entry.cr2[z] - entry.cr1[z] + 1;
if (numCrOccupiedByThisComponent > 1)
{
int gap = (z == 0) ? hGap : vGap;
size -= (numCrOccupiedByThisComponent - 1) * gap;
}
// Divide the size evenly among the adjustable crs
size = (int) Math.ceil(size / (double) numAdjustable);
// Take the maximumn size
if (maxSize < size)
maxSize = size;
}
}
// Assign preferred size
crSize[z][counter] = maxSize;
// Reduce available size
availableSize -= maxSize;
}
return availableSize;
}
//******************************************************************************
//** java.awt.event.LayoutManager methods ***
//******************************************************************************
/**
* To lay out the specified container using this layout. This method reshapes
* the components in the specified target container in order to satisfy the
* constraints of all components.
*
* <p>User code should not have to call this method directly.</p>
*
* @param container container being served by this layout manager
*/
public void layoutContainer (Container container)
{
// Calculate sizes if container has changed size or components were added
Dimension d = container.getSize();
if (dirty || (d.width != oldWidth) || (d.height != oldHeight))
calculateSize(container);
// Get component orientation and insets
ComponentOrientation co = getComponentOrientation(container);
boolean isRightToLeft = (co != null) && !co.isLeftToRight();
Insets insets = container.getInsets();
// Get components
Component component[] = container.getComponents();
// Layout components
for (int counter = 0; counter < component.length; counter++)
{
try
{
// Get the entry for the next component
ListIterator iterator = list.listIterator(0);
Entry entry = null;
while (iterator.hasNext())
{
entry = (Entry) iterator.next();
if (entry.component == component[counter])
break;
else
entry = null;
}
// Skip any components that have not been place in a specific cell,
// setting the skip component's bounds to zero
if (entry == null)
{
component[counter].setBounds(0, 0, 0, 0);
continue;
}
// The following block of code has been optimized so that the
// preferred size of the component is only obtained if it is
// needed. There are components in which the getPreferredSize
// method is extremely expensive, such as data driven controls
// with a large amount of data.
// Get the preferred size of the component
int preferredWidth = 0;
int preferredHeight = 0;
if ((entry.alignment[C] != FULL) || (entry.alignment[R] != FULL))
{
Dimension preferredSize =
component[counter].getPreferredSize();
preferredWidth = preferredSize.width;
preferredHeight = preferredSize.height;
}
// Calculate the coordinates and size of the component
int value[] = calculateSizeAndOffset(entry, preferredWidth, true);
int x = value[0];
int w = value[1];
value = calculateSizeAndOffset(entry, preferredHeight, false);
int y = value[0];
int h = value[1];
// Compensate for component orientation.
if (isRightToLeft)
x = d.width - x - w + insets.left - insets.right;
// Move and resize component
component[counter].setBounds(x, y, w, h);
}
catch (Exception error)
{
// If any error occurs, set the bounds of this component to zero
// and continue
component[counter].setBounds(0, 0, 0, 0);
continue;
}
}
}
/**
* Gets the container's component orientation. If a JDK that does not support
* component orientation is being used, then null is returned.
*
* @param container Container whose orientation is being queried
*
* @return the container's orientation or null if no orientation is supported
*/
protected ComponentOrientation getComponentOrientation(Container container)
{
// This method is implemented to only get the class and method objects
// once so as to reduce expensive reflection operations. If the reflection
// fails, then component orientation is not supported.
ComponentOrientation co = null;
try
{
if (checkForComponentOrientationSupport)
{
methodGetComponentOrientation =
Class.forName("java.awt.Container").getMethod
("getComponentOrientation", new Class[0]);
checkForComponentOrientationSupport = false;
}
if (methodGetComponentOrientation != null)
{
co = (ComponentOrientation)
methodGetComponentOrientation.invoke(container, new Object[0]);
}
}
catch (Exception e) {}
return co;
}
/**
* Calculates the vertical/horizontal offset and size of a component.
*
* @param entry entry containing component and contraints
* @param preferredSize previously calculated preferred width/height of
* component
* @param isColumn if true, this method is being called to calculate
* the offset/size of a column. if false,... of a row.
*
* @return an array, a, of two integers such that a[0] is the offset and
* a[1] is the size
*/
protected int [] calculateSizeAndOffset
(Entry entry, int preferredSize, boolean isColumn)
{
// Get references to cr properties
int crOffset[] = isColumn ? this.crOffset[C] : this.crOffset[R];
int entryAlignment = isColumn ? entry.alignment[C] : entry.alignment[R];
// Determine cell set size
int cellSetSize = isColumn ?
crOffset[entry.cr2[C] + 1] - crOffset[entry.cr1[C]] :
crOffset[entry.cr2[R] + 1] - crOffset[entry.cr1[R]];
// Determine the size of the component
int size;
if ((entryAlignment == FULL) || (cellSetSize < preferredSize))
size = cellSetSize;
else
size = preferredSize;
// Since the component orientation is adjusted for in the layoutContainer
// method, we can treat leading justification as left justification and
// trailing justification as right justification.
if (isColumn && (entryAlignment == LEADING))
entryAlignment = LEFT;
if (isColumn && (entryAlignment == TRAILING))
entryAlignment = RIGHT;
// Determine offset
int offset;
switch (entryAlignment)
{
case LEFT : // Align left/top side along left edge of cell
offset = crOffset[isColumn ? entry.cr1[C] : entry.cr1[R]];
break;
case RIGHT : // Align right/bottom side along right edge of cell
offset =
crOffset[(isColumn ? entry.cr2[C] : entry.cr2[R]) + 1] - size;
break;
case CENTER : // Center justify component
offset = crOffset[isColumn ? entry.cr1[C] : entry.cr1[R]] +
((cellSetSize - size) >> 1);
break;
case FULL : // Align left/top side along left/top edge of cell
offset = crOffset[isColumn ? entry.cr1[C] : entry.cr1[R]];
break;
default : // This is a never should happen case, but just in case
offset = 0;
}
// Compensate for gaps
if (isColumn)
{
offset += hGap * entry.cr1[C];
int cumlativeGap = hGap * (entry.cr2[C] - entry.cr1[C]);
switch (entryAlignment)
{
case RIGHT :
offset += cumlativeGap;
break;
case CENTER :
offset += cumlativeGap >> 1;
break;
case FULL :
size += cumlativeGap;
break;
}
}
else
{
offset += vGap * entry.cr1[R];
int cumlativeGap = vGap * (entry.cr2[R] - entry.cr1[R]);
switch (entryAlignment)
{
case BOTTOM :
offset += cumlativeGap;
break;
case CENTER :
offset += cumlativeGap >> 1;
break;
case FULL :
size += cumlativeGap;
break;
}
}
// Package return values
int value[] = {offset, size};
return value;
}
/**
* Determines the preferred size of the container argument using this layout.
* The preferred size is the smallest size that, if used for the container's
* size, will ensure that all components are at least as large as their
* preferred size. This method cannot guarantee that all components will be
* their preferred size. For example, if component A and component B are each
* allocate half of the container's width and component A wants to be 10 pixels
* wide while component B wants to be 100 pixels wide, they cannot both be
* accommodated. Since in general components rather be larger than their
* preferred size instead of smaller, component B's request will be fulfilled.
* The preferred size of the container would be 200 pixels.
*
* @param container container being served by this layout manager
*
* @return a dimension indicating the container's preferred size
*/
public Dimension preferredLayoutSize (Container container)
{
return calculateLayoutSize(container, PREFERRED);
}
/**
* Determines the minimum size of the container argument using this layout.
* The minimum size is the smallest size that, if used for the container's
* size, will ensure that all components are at least as large as their
* minimum size. This method cannot guarantee that all components will be
* their minimum size. For example, if component A and component B are each
* allocate half of the container's width and component A wants to be 10 pixels
* wide while component B wants to be 100 pixels wide, they cannot both be
* accommodated. Since in general components rather be larger than their
* minimum size instead of smaller, component B's request will be fulfilled.
* The minimum size of the container would be 200 pixels.
*
* @param container container being served by this layout manager
*
* @return a dimension indicating the container's minimum size
*/
public Dimension minimumLayoutSize (Container container)
{
return calculateLayoutSize(container, MINIMUM);
}
/**
* Calculates the preferred or minimum size for the methods preferredLayoutSize
* and minimumLayoutSize.
*
* @param container container whose size is being calculated
* @param typeOfSize indicates preferred or minimum
*
* @return a dimension indicating the container's preferred or minimum size
*/
protected Dimension calculateLayoutSize (Container container, double typeOfSize)
{
// Get preferred/minimum sizes
Entry entryList[] = (Entry []) list.toArray(new Entry[list.size()]);
int numEntry = entryList.length;
Dimension prefMinSize[] = new Dimension[numEntry];
for (int i = 0; i < numEntry; i++)
prefMinSize[i] = (typeOfSize == PREFERRED) ?
entryList[i].component.getPreferredSize() :
entryList[i].component.getMinimumSize();
// Calculate sizes
int width =
calculateLayoutSize(container, C, typeOfSize, entryList, prefMinSize);
int height =
calculateLayoutSize(container, R, typeOfSize, entryList, prefMinSize);
// Compensate for container's insets
Insets inset = container.getInsets();
width += inset.left + inset.right;
height += inset.top + inset.bottom;
return new Dimension(width, height);
}
/**
* Calculates the preferred or minimum size for the method
* calculateLayoutSize(Container container, double typeOfSize). This method
* is passed the preferred/minimum sizes of the components so that the
* potentially expensive methods getPreferredSize()/getMinimumSize() are not
* called twice for the same component.
*
* @param container container whose size is being calculated
* @param z indicates row or column
* @param typeOfSize indicates preferred or minimum
* @param entryList list of Entry objects
* @param prefMinSize list of preferred or minimum sizes
*
* @return a dimension indicating the container's preferred or minimum size
*/
protected int calculateLayoutSize
(Container container, int z, double typeOfSize, Entry entryList[],
Dimension prefMinSize[])
{
Dimension size; // Preferred/minimum size of current component
int scaledSize = 0; // Preferred/minimum size of scaled components
int temp; // Temporary variable used to compare sizes
int counter; // Counting variable
// Get number of crs
int numCr = crSpec[z].length;
// Determine percentage of space allocated to fill components. This is
// one minus the sum of all scalable components.
double fillSizeRatio = 1.0;
int numFillSize = 0;
for (counter = 0; counter < numCr; counter++)
if ((crSpec[z][counter] > 0.0) && (crSpec[z][counter] < 1.0))
fillSizeRatio -= crSpec[z][counter];
else if (crSpec[z][counter] == FILL)
numFillSize++;
// Adjust fill ratios to reflect number of fill rows/columns
if (numFillSize > 1)
fillSizeRatio /= numFillSize;
// Cap fill ratio bottoms to 0.0
if (fillSizeRatio < 0.0)
fillSizeRatio = 0.0;
// Create array to hold actual sizes in pixels
crSize[z] = new int[numCr];
// Assign absolute sizes (must be done before assignPrefMinSize)
// This is done to calculate absolute cr sizes
assignAbsoluteSize(z, 0);
// Assign preferred and minimum sizes (must be done after assignAbsoluteSize)
// This is done to calculate preferred/minimum cr sizes
assignPrefMinSize(z, 0, MINIMUM);
assignPrefMinSize(z, 0, PREFERRED);
int crPrefMin[] = new int[numCr];
for (counter = 0; counter < numCr; counter++)
if ((crSpec[z][counter] == PREFERRED) ||
(crSpec[z][counter] == MINIMUM))
{
crPrefMin[counter] = crSize[z][counter];
}
// Find maximum preferred/minimum size of all scaled components
int numColumn = crSpec[C].length;
int numRow = crSpec[R].length;
int numEntry = entryList.length;
for (int entryCounter = 0; entryCounter < numEntry; entryCounter++)
{
// Get next entry
Entry entry = entryList[entryCounter];
// Make sure entry is in valid rows and columns
if ((entry.cr1[C] < 0) || (entry.cr1[C] >= numColumn) ||
(entry.cr2[C] >= numColumn) ||
(entry.cr1[R] < 0) || (entry.cr1[R] >= numRow) ||
(entry.cr2[R] >= numRow))
{
// Skip the bad component
continue;
}
// Get preferred/minimum size of current component
size = prefMinSize[entryCounter];
//----------------------------------------------------------------------
// Calculate portion of component that is not absolutely sized
int scalableSize = (z == C) ? size.width : size.height;
for (counter = entry.cr1[z]; counter <= entry.cr2[z]; counter++)
if (crSpec[z][counter] >= 1.0)
scalableSize -= crSpec[z][counter];
else if ((crSpec[z][counter] == PREFERRED) ||
(crSpec[z][counter] == MINIMUM))
{
scalableSize -= crPrefMin[counter];
}
//----------------------------------------------------------------------
// Determine total percentage of scalable space that the component
// occupies by adding the relative columns and the fill columns
double relativeSize = 0.0;
for (counter = entry.cr1[z]; counter <= entry.cr2[z]; counter++)
{
// Cr is scaled
if ((crSpec[z][counter] > 0.0) && (crSpec[z][counter] < 1.0))
// Add scaled size to relativeWidth
relativeSize += crSpec[z][counter];
// Cr is fill
else if ((crSpec[z][counter] == FILL) && (fillSizeRatio != 0.0))
// Add fill size to relativeWidth
relativeSize += fillSizeRatio;
}
// Determine the total scaled size as estimated by this component
if (relativeSize == 0)
temp = 0;
else
temp = (int) (scalableSize / relativeSize + 0.5);
//----------------------------------------------------------------------
// If the container needs to be bigger, make it so
if (scaledSize < temp)
scaledSize = temp;
}
// totalSize is the scaledSize plus the sum of all absolute sizes and all
// preferred sizes
int totalSize = scaledSize;
for (counter = 0; counter < numCr; counter++)
// Is the current cr an absolute size
if (crSpec[z][counter] >= 1.0)
totalSize += (int) (crSpec[z][counter] + 0.5);
// Is the current cr a preferred/minimum size
else if ((crSpec[z][counter] == PREFERRED) ||
(crSpec[z][counter] == MINIMUM))
{
// Add preferred/minimum width
totalSize += crPrefMin[counter];
}
// Compensate for horizontal and vertical gap
if (numCr > 0)
totalSize += ((z == C) ? hGap : vGap) * (numCr - 1);
return totalSize;
}
/**
* Adds the specified component with the specified name to the layout.
*
* @param name indicates entry's position and anchor
* @param component component to add
*/
public void addLayoutComponent (String name, Component component)
{
addLayoutComponent(component, name);
}
//******************************************************************************
//** java.awt.event.LayoutManager2 methods ***
//******************************************************************************
/**
* Adds the specified component with the specified name to the layout.
*
* @param component component to add
* @param constraint indicates entry's position and alignment
*/
public void addLayoutComponent (Component component, Object constraint)
{
if (constraint instanceof String)
{
// Create an entry to associate component with its constraints
constraint = new TableLayoutConstraints((String) constraint);
// Add component and constraints to the list
list.add(new Entry(component, (TableLayoutConstraints) constraint));
// Indicate that the cell sizes are not known
dirty = true;
}
else if (constraint instanceof TableLayoutConstraints)
{
// Add component and constraints to the list
list.add(new Entry(component, (TableLayoutConstraints) constraint));
// Indicate that the cell sizes are not known
dirty = true;
}
else if (constraint == null)
throw new IllegalArgumentException("No constraint for the component");
else
throw new IllegalArgumentException
("Cannot accept a constraint of class " + constraint.getClass());
}
/**
* Removes the specified component from the layout.
*
* @param component component being removed
*/
public void removeLayoutComponent (Component component)
{
// Remove the component
ListIterator iterator = list.listIterator(0);
while (iterator.hasNext())
{
Entry entry = (Entry) iterator.next();
if (entry.component == component)
iterator.remove();
}
// Indicate that the cell sizes are not known since
dirty = true;
}
/**
* Returns the maximum dimensions for this layout given the components in the
* specified target container.
*
* @param target the component which needs to be laid out
*
* @return unconditionally, a Dimension of Integer.MAX_VALUE by
* Integer.MAX_VALUE since TableLayout does not limit the
* maximum size of a container
*/
public Dimension maximumLayoutSize (Container target)
{
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
/**
* Returns the alignment along the x axis. This specifies how the component
* would like to be aligned relative to other components. The value should be
* a number between 0 and 1 where 0 represents alignment along the origin, 1 is
* aligned the furthest away from the origin, 0.5 is centered, etc.
*
* @return unconditionally, 0.5
*/
public float getLayoutAlignmentX (Container parent)
{
return 0.5f;
}
/**
* Returns the alignment along the y axis. This specifies how the component
* would like to be aligned relative to other components. The value should be
* a number between 0 and 1 where 0 represents alignment along the origin, 1 is
* aligned the furthest away from the origin, 0.5 is centered, etc.
*
* @return unconditionally, 0.5
*/
public float getLayoutAlignmentY (Container parent)
{
return 0.5f;
}
/**
* Invalidates the layout, indicating that if the layout manager has cached
* information it should be discarded.
*/
public void invalidateLayout (Container target)
{
dirty = true;
}
//******************************************************************************
//*** Inner Class ***
//******************************************************************************
// The following inner class is used to bind components to their constraints
public static class Entry implements Cloneable
{
/** Component bound by the constraints */
public Component component;
/** Cell in which the upper-left corner of the component lies */
public int cr1[];
/** Cell in which the lower-right corner of the component lies */
public int cr2[];
/** Horizontal and vertical alignment */
public int alignment[];
/**
* Constructs an Entry that binds a component to a set of constraints.
*
* @param component component being bound
* @param constraint constraints being applied
*/
public Entry (Component component, TableLayoutConstraints constraint)
{
int cr1[] = {constraint.col1, constraint.row1};
int cr2[] = {constraint.col2, constraint.row2};
int alignment[] = {constraint.hAlign, constraint.vAlign};
this.cr1 = cr1;
this.cr2 = cr2;
this.alignment = alignment;
this.component = component;
}
/**
* Copies this Entry.
*/
public Object copy () throws CloneNotSupportedException
{
return clone();
}
/**
* Gets the string representation of this Entry.
*
* @return a string in the form
* "(col1, row1, col2, row2, vAlign, hAlign) component"
*/
public String toString ()
{
TableLayoutConstraints c = new TableLayoutConstraints
(cr1[C], cr1[R], cr2[C], cr2[R], alignment[C], alignment[R]);
return "(" + c + ") " + component;
}
}
}
|