summaryrefslogtreecommitdiff
path: root/javax/swing/plaf/basic/BasicTreeUI.java
blob: ef46efdd25be4c16eee49f29860239caf89cd8f8 (plain)
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
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
/* BasicTreeUI.java --
 Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.

 This file is part of GNU Classpath.

 GNU Classpath is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.

 GNU Classpath 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
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GNU Classpath; see the file COPYING.  If not, write to the
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 02110-1301 USA.

 Linking this library statically or dynamically with other modules is
 making a combined work based on this library.  Thus, the terms and
 conditions of the GNU General Public License cover the whole
 combination.

 As a special exception, the copyright holders of this library give you
 permission to link this library with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */


package javax.swing.plaf.basic;

import gnu.javax.swing.tree.GnuPath;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.CellRendererPane;
import javax.swing.Icon;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.MouseInputListener;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.ActionMapUIResource;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.TreeUI;
import javax.swing.tree.AbstractLayoutCache;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.tree.VariableHeightLayoutCache;

/**
 * A delegate providing the user interface for <code>JTree</code> according to
 * the Basic look and feel.
 * 
 * @see javax.swing.JTree
 * @author Lillian Angel (langel@redhat.com)
 * @author Sascha Brawer (brawer@dandelis.ch)
 * @author Audrius Meskauskas (audriusa@bioinformatics.org)
 */
public class BasicTreeUI
    extends TreeUI
{
  /**
   * The tree cell editing may be started by the single mouse click on the
   * selected cell. To separate it from the double mouse click, the editing
   * session starts after this time (in ms) after that single click, and only no
   * other clicks were performed during that time.
   */
  static int WAIT_TILL_EDITING = 900;

  /** Collapse Icon for the tree. */
  protected transient Icon collapsedIcon;

  /** Expanded Icon for the tree. */
  protected transient Icon expandedIcon;

  /** Distance between left margin and where vertical dashes will be drawn. */
  protected int leftChildIndent;

  /**
   * Distance between leftChildIndent and where cell contents will be drawn.
   */
  protected int rightChildIndent;

  /**
   * Total fistance that will be indented. The sum of leftChildIndent and
   * rightChildIndent .
   */
  protected int totalChildIndent;

  /** Index of the row that was last selected. */
  protected int lastSelectedRow;

  /** Component that we're going to be drawing onto. */
  protected JTree tree;

  /** Renderer that is being used to do the actual cell drawing. */
  protected transient TreeCellRenderer currentCellRenderer;

  /**
   * Set to true if the renderer that is currently in the tree was created by
   * this instance.
   */
  protected boolean createdRenderer;

  /** Editor for the tree. */
  protected transient TreeCellEditor cellEditor;

  /**
   * Set to true if editor that is currently in the tree was created by this
   * instance.
   */
  protected boolean createdCellEditor;

  /**
   * Set to false when editing and shouldSelectCall() returns true meaning the
   * node should be selected before editing, used in completeEditing.
   * GNU Classpath editing is implemented differently, so this value is not
   * actually read anywhere. However it is always set correctly to maintain 
   * interoperability with the derived classes that read this field.
   */
  protected boolean stopEditingInCompleteEditing;

  /** Used to paint the TreeCellRenderer. */
  protected CellRendererPane rendererPane;

  /** Size needed to completely display all the nodes. */
  protected Dimension preferredSize;

  /** Minimum size needed to completely display all the nodes. */
  protected Dimension preferredMinSize;

  /** Is the preferredSize valid? */
  protected boolean validCachedPreferredSize;

  /** Object responsible for handling sizing and expanded issues. */
  protected AbstractLayoutCache treeState;

  /** Used for minimizing the drawing of vertical lines. */
  protected Hashtable<TreePath, Boolean> drawingCache;

  /**
   * True if doing optimizations for a largeModel. Subclasses that don't support
   * this may wish to override createLayoutCache to not return a
   * FixedHeightLayoutCache instance.
   */
  protected boolean largeModel;

  /** Responsible for telling the TreeState the size needed for a node. */
  protected AbstractLayoutCache.NodeDimensions nodeDimensions;

  /** Used to determine what to display. */
  protected TreeModel treeModel;

  /** Model maintaining the selection. */
  protected TreeSelectionModel treeSelectionModel;

  /**
   * How much the depth should be offset to properly calculate x locations. This
   * is based on whether or not the root is visible, and if the root handles are
   * visible.
   */
  protected int depthOffset;

  /**
   * When editing, this will be the Component that is doing the actual editing.
   */
  protected Component editingComponent;

  /** Path that is being edited. */
  protected TreePath editingPath;

  /**
   * Row that is being edited. Should only be referenced if editingComponent is
   * null.
   */
  protected int editingRow;

  /** Set to true if the editor has a different size than the renderer. */
  protected boolean editorHasDifferentSize;

  /** Boolean to keep track of editing. */
  boolean isEditing;

  /** The current path of the visible nodes in the tree. */
  TreePath currentVisiblePath;

  /** The gap between the icon and text. */
  int gap = 4;

  /** The max height of the nodes in the tree. */
  int maxHeight;
  
  /** The hash color. */
  Color hashColor;

  /** Listeners */
  PropertyChangeListener propertyChangeListener;

  FocusListener focusListener;

  TreeSelectionListener treeSelectionListener;

  MouseListener mouseListener;

  KeyListener keyListener;

  PropertyChangeListener selectionModelPropertyChangeListener;

  ComponentListener componentListener;

  CellEditorListener cellEditorListener;

  TreeExpansionListener treeExpansionListener;

  TreeModelListener treeModelListener;

  /**
   * The zero size icon, used for expand controls, if they are not visible.
   */
  static Icon nullIcon;

  /**
   * The special value of the mouse event is sent indicating that this is not
   * just the mouse click, but the mouse click on the selected node. Sending
   * such event forces to start the cell editing session.
   */
  static final MouseEvent EDIT = new MouseEvent(new Label(), 7, 7, 7, 7, 7, 7,
                                                false);

  /**
   * Creates a new BasicTreeUI object.
   */
  public BasicTreeUI()
  {
    validCachedPreferredSize = false;
    drawingCache = new Hashtable();
    nodeDimensions = createNodeDimensions();
    configureLayoutCache();

    editingRow = - 1;
    lastSelectedRow = - 1;
  }

  /**
   * Returns an instance of the UI delegate for the specified component.
   * 
   * @param c the <code>JComponent</code> for which we need a UI delegate for.
   * @return the <code>ComponentUI</code> for c.
   */
  public static ComponentUI createUI(JComponent c)
  {
    return new BasicTreeUI();
  }

  /**
   * Returns the Hash color.
   * 
   * @return the <code>Color</code> of the Hash.
   */
  protected Color getHashColor()
  {
    return hashColor;
  }

  /**
   * Sets the Hash color.
   * 
   * @param color the <code>Color</code> to set the Hash to.
   */
  protected void setHashColor(Color color)
  {
    hashColor = color;
  }

  /**
   * Sets the left child's indent value.
   * 
   * @param newAmount is the new indent value for the left child.
   */
  public void setLeftChildIndent(int newAmount)
  {
    leftChildIndent = newAmount;
  }

  /**
   * Returns the indent value for the left child.
   * 
   * @return the indent value for the left child.
   */
  public int getLeftChildIndent()
  {
    return leftChildIndent;
  }

  /**
   * Sets the right child's indent value.
   * 
   * @param newAmount is the new indent value for the right child.
   */
  public void setRightChildIndent(int newAmount)
  {
    rightChildIndent = newAmount;
  }

  /**
   * Returns the indent value for the right child.
   * 
   * @return the indent value for the right child.
   */
  public int getRightChildIndent()
  {
    return rightChildIndent;
  }

  /**
   * Sets the expanded icon.
   * 
   * @param newG is the new expanded icon.
   */
  public void setExpandedIcon(Icon newG)
  {
    expandedIcon = newG;
  }

  /**
   * Returns the current expanded icon.
   * 
   * @return the current expanded icon.
   */
  public Icon getExpandedIcon()
  {
    return expandedIcon;
  }

  /**
   * Sets the collapsed icon.
   * 
   * @param newG is the new collapsed icon.
   */
  public void setCollapsedIcon(Icon newG)
  {
    collapsedIcon = newG;
  }

  /**
   * Returns the current collapsed icon.
   * 
   * @return the current collapsed icon.
   */
  public Icon getCollapsedIcon()
  {
    return collapsedIcon;
  }

  /**
   * Updates the componentListener, if necessary.
   * 
   * @param largeModel sets this.largeModel to it.
   */
  protected void setLargeModel(boolean largeModel)
  {
    if (largeModel != this.largeModel)
      {
        completeEditing();
        tree.removeComponentListener(componentListener);
        this.largeModel = largeModel;
        tree.addComponentListener(componentListener);
      }
  }

  /**
   * Returns true if largeModel is set
   * 
   * @return true if largeModel is set, otherwise false.
   */
  protected boolean isLargeModel()
  {
    return largeModel;
  }

  /**
   * Sets the row height.
   * 
   * @param rowHeight is the height to set this.rowHeight to.
   */
  protected void setRowHeight(int rowHeight)
  {
    completeEditing();
    if (rowHeight == 0)
      rowHeight = getMaxHeight(tree);
    treeState.setRowHeight(rowHeight);
  }

  /**
   * Returns the current row height.
   * 
   * @return current row height.
   */
  protected int getRowHeight()
  {
    return tree.getRowHeight();
  }

  /**
   * Sets the TreeCellRenderer to <code>tcr</code>. This invokes
   * <code>updateRenderer</code>.
   * 
   * @param tcr is the new TreeCellRenderer.
   */
  protected void setCellRenderer(TreeCellRenderer tcr)
  {
    // Finish editing before changing the renderer.
    completeEditing();

    // The renderer is set in updateRenderer.
    updateRenderer();

    // Refresh the layout if necessary.
    if (treeState != null)
      {
	treeState.invalidateSizes();
	updateSize();
      }
  }

  /**
   * Return currentCellRenderer, which will either be the trees renderer, or
   * defaultCellRenderer, which ever was not null.
   * 
   * @return the current Cell Renderer
   */
  protected TreeCellRenderer getCellRenderer()
  {
    if (currentCellRenderer != null)
      return currentCellRenderer;

    return createDefaultCellRenderer();
  }

  /**
   * Sets the tree's model.
   * 
   * @param model to set the treeModel to.
   */
  protected void setModel(TreeModel model)
  {
    completeEditing();

    if (treeModel != null && treeModelListener != null)
      treeModel.removeTreeModelListener(treeModelListener);

    treeModel = tree.getModel();

    if (treeModel != null && treeModelListener != null)
      treeModel.addTreeModelListener(treeModelListener);

    if (treeState != null)
      {
        treeState.setModel(treeModel);
        updateLayoutCacheExpandedNodes();
        updateSize();
      }
  }

  /**
   * Returns the tree's model
   * 
   * @return treeModel
   */
  protected TreeModel getModel()
  {
    return treeModel;
  }

  /**
   * Sets the root to being visible.
   * 
   * @param newValue sets the visibility of the root
   */
  protected void setRootVisible(boolean newValue)
  {
    completeEditing();
    tree.setRootVisible(newValue);
  }

  /**
   * Returns true if the root is visible.
   * 
   * @return true if the root is visible.
   */
  protected boolean isRootVisible()
  {
    return tree.isRootVisible();
  }

  /**
   * Determines whether the node handles are to be displayed.
   * 
   * @param newValue sets whether or not node handles should be displayed.
   */
  protected void setShowsRootHandles(boolean newValue)
  {
    completeEditing();
    updateDepthOffset();
    if (treeState != null)
      {
        treeState.invalidateSizes();
        updateSize();
      }
  }

  /**
   * Returns true if the node handles are to be displayed.
   * 
   * @return true if the node handles are to be displayed.
   */
  protected boolean getShowsRootHandles()
  {
    return tree.getShowsRootHandles();
  }

  /**
   * Sets the cell editor.
   * 
   * @param editor to set the cellEditor to.
   */
  protected void setCellEditor(TreeCellEditor editor)
  {
    updateCellEditor();
  }

  /**
   * Returns the <code>TreeCellEditor</code> for this tree.
   * 
   * @return the cellEditor for this tree.
   */
  protected TreeCellEditor getCellEditor()
  {
    return cellEditor;
  }

  /**
   * Configures the receiver to allow, or not allow, editing.
   * 
   * @param newValue sets the receiver to allow editing if true.
   */
  protected void setEditable(boolean newValue)
  {
    updateCellEditor();
  }

  /**
   * Returns true if the receiver allows editing.
   * 
   * @return true if the receiver allows editing.
   */
  protected boolean isEditable()
  {
    return tree.isEditable();
  }

  /**
   * Resets the selection model. The appropriate listeners are installed on the
   * model.
   * 
   * @param newLSM resets the selection model.
   */
  protected void setSelectionModel(TreeSelectionModel newLSM)
  {
    completeEditing();
    if (newLSM != null)
      {
        treeSelectionModel = newLSM;
        tree.setSelectionModel(treeSelectionModel);
      }
  }

  /**
   * Returns the current selection model.
   * 
   * @return the current selection model.
   */
  protected TreeSelectionModel getSelectionModel()
  {
    return treeSelectionModel;
  }

  /**
   * Returns the Rectangle enclosing the label portion that the last item in
   * path will be drawn to. Will return null if any component in path is
   * currently valid.
   * 
   * @param tree is the current tree the path will be drawn to.
   * @param path is the current path the tree to draw to.
   * @return the Rectangle enclosing the label portion that the last item in the
   *         path will be drawn to.
   */
  public Rectangle getPathBounds(JTree tree, TreePath path)
  {
    return treeState.getBounds(path, new Rectangle());
  }

  /**
   * Returns the max height of all the nodes in the tree.
   * 
   * @param tree - the current tree
   * @return the max height.
   */
  int getMaxHeight(JTree tree)
  {
    if (maxHeight != 0)
      return maxHeight;

    Icon e = UIManager.getIcon("Tree.openIcon");
    Icon c = UIManager.getIcon("Tree.closedIcon");
    Icon l = UIManager.getIcon("Tree.leafIcon");
    int rc = getRowCount(tree);
    int iconHeight = 0;

    for (int row = 0; row < rc; row++)
      {
        if (isLeaf(row))
          iconHeight = l.getIconHeight();
        else if (tree.isExpanded(row))
          iconHeight = e.getIconHeight();
        else
          iconHeight = c.getIconHeight();

        maxHeight = Math.max(maxHeight, iconHeight + gap);
      }
     
    treeState.setRowHeight(maxHeight);
    return maxHeight;
  }
  
  /**
   * Get the tree node icon.
   */
  Icon getNodeIcon(TreePath path)
  {
    Object node = path.getLastPathComponent();
    if (treeModel.isLeaf(node))
      return UIManager.getIcon("Tree.leafIcon");
    else if (treeState.getExpandedState(path))
      return UIManager.getIcon("Tree.openIcon");
    else
      return UIManager.getIcon("Tree.closedIcon");
  }

  /**
   * Returns the path for passed in row. If row is not visible null is returned.
   * 
   * @param tree is the current tree to return path for.
   * @param row is the row number of the row to return.
   * @return the path for passed in row. If row is not visible null is returned.
   */
  public TreePath getPathForRow(JTree tree, int row)
  {
    return treeState.getPathForRow(row);
  }

  /**
   * Returns the row that the last item identified in path is visible at. Will
   * return -1 if any of the elments in the path are not currently visible.
   * 
   * @param tree is the current tree to return the row for.
   * @param path is the path used to find the row.
   * @return the row that the last item identified in path is visible at. Will
   *         return -1 if any of the elments in the path are not currently
   *         visible.
   */
  public int getRowForPath(JTree tree, TreePath path)
  {
    return treeState.getRowForPath(path);
  }

  /**
   * Returns the number of rows that are being displayed.
   * 
   * @param tree is the current tree to return the number of rows for.
   * @return the number of rows being displayed.
   */
  public int getRowCount(JTree tree)
  {
    return treeState.getRowCount();
  }

  /**
   * Returns the path to the node that is closest to x,y. If there is nothing
   * currently visible this will return null, otherwise it'll always return a
   * valid path. If you need to test if the returned object is exactly at x,y
   * you should get the bounds for the returned path and test x,y against that.
   * 
   * @param tree the tree to search for the closest path
   * @param x is the x coordinate of the location to search
   * @param y is the y coordinate of the location to search
   * @return the tree path closes to x,y.
   */
  public TreePath getClosestPathForLocation(JTree tree, int x, int y)
  {
    return treeState.getPathClosestTo(x, y);
  }

  /**
   * Returns true if the tree is being edited. The item that is being edited can
   * be returned by getEditingPath().
   * 
   * @param tree is the tree to check for editing.
   * @return true if the tree is being edited.
   */
  public boolean isEditing(JTree tree)
  {
    return isEditing;
  }

  /**
   * Stops the current editing session. This has no effect if the tree is not
   * being edited. Returns true if the editor allows the editing session to
   * stop.
   * 
   * @param tree is the tree to stop the editing on
   * @return true if the editor allows the editing session to stop.
   */
  public boolean stopEditing(JTree tree)
  {
    boolean ret = false;
    if (editingComponent != null && cellEditor.stopCellEditing())
      {
        completeEditing(false, false, true);
        ret = true;
      }
    return ret;
  }

  /**
   * Cancels the current editing session.
   * 
   * @param tree is the tree to cancel the editing session on.
   */
  public void cancelEditing(JTree tree)
  {
    // There is no need to send the cancel message to the editor,
    // as the cancellation event itself arrives from it. This would
    // only be necessary when cancelling the editing programatically.
    if (editingComponent != null)
      completeEditing(false, true, false);
  }

  /**
   * Selects the last item in path and tries to edit it. Editing will fail if
   * the CellEditor won't allow it for the selected item.
   * 
   * @param tree is the tree to edit on.
   * @param path is the path in tree to edit on.
   */
  public void startEditingAtPath(JTree tree, TreePath path)
  {
    tree.scrollPathToVisible(path);
    if (path != null && tree.isVisible(path))
      startEditing(path, null);
  }

  /**
   * Returns the path to the element that is being editted.
   * 
   * @param tree is the tree to get the editing path from.
   * @return the path that is being edited.
   */
  public TreePath getEditingPath(JTree tree)
  {
    return editingPath;
  }

  /**
   * Invoked after the tree instance variable has been set, but before any
   * default/listeners have been installed.
   */
  protected void prepareForUIInstall()
  {
    lastSelectedRow = -1;
    preferredSize = new Dimension();
    largeModel = tree.isLargeModel();
    preferredSize = new Dimension();
    stopEditingInCompleteEditing = true;
    setModel(tree.getModel());
  }

  /**
   * Invoked from installUI after all the defaults/listeners have been
   * installed.
   */
  protected void completeUIInstall()
  {
    setShowsRootHandles(tree.getShowsRootHandles());
    updateRenderer();
    updateDepthOffset();
    setSelectionModel(tree.getSelectionModel());
    configureLayoutCache();
    treeState.setRootVisible(tree.isRootVisible()); 
    treeSelectionModel.setRowMapper(treeState);
    updateSize();
  }

  /**
   * Invoked from uninstallUI after all the defaults/listeners have been
   * uninstalled.
   */
  protected void completeUIUninstall()
  {
    tree = null;
  }

  /**
   * Installs the subcomponents of the tree, which is the renderer pane.
   */
  protected void installComponents()
  {
    currentCellRenderer = createDefaultCellRenderer();
    rendererPane = createCellRendererPane();
    createdRenderer = true;
    setCellRenderer(currentCellRenderer);
  }

  /**
   * Creates an instance of NodeDimensions that is able to determine the size of
   * a given node in the tree. The node dimensions must be created before
   * configuring the layout cache.
   * 
   * @return the NodeDimensions of a given node in the tree
   */
  protected AbstractLayoutCache.NodeDimensions createNodeDimensions()
  {
    return new NodeDimensionsHandler();
  }

  /**
   * Creates a listener that is reponsible for the updates the UI based on how
   * the tree changes.
   * 
   * @return the PropertyChangeListener that is reposnsible for the updates
   */
  protected PropertyChangeListener createPropertyChangeListener()
  {
    return new PropertyChangeHandler();
  }

  /**
   * Creates the listener responsible for updating the selection based on mouse
   * events.
   * 
   * @return the MouseListener responsible for updating.
   */
  protected MouseListener createMouseListener()
  {
    return new MouseHandler();
  }

  /**
   * Creates the listener that is responsible for updating the display when
   * focus is lost/grained.
   * 
   * @return the FocusListener responsible for updating.
   */
  protected FocusListener createFocusListener()
  {
    return new FocusHandler();
  }

  /**
   * Creates the listener reponsible for getting key events from the tree.
   * 
   * @return the KeyListener responsible for getting key events.
   */
  protected KeyListener createKeyListener()
  {
    return new KeyHandler();
  }

  /**
   * Creates the listener responsible for getting property change events from
   * the selection model.
   * 
   * @returns the PropertyChangeListener reponsible for getting property change
   *          events from the selection model.
   */
  protected PropertyChangeListener createSelectionModelPropertyChangeListener()
  {
    return new SelectionModelPropertyChangeHandler();
  }

  /**
   * Creates the listener that updates the display based on selection change
   * methods.
   * 
   * @return the TreeSelectionListener responsible for updating.
   */
  protected TreeSelectionListener createTreeSelectionListener()
  {
    return new TreeSelectionHandler();
  }

  /**
   * Creates a listener to handle events from the current editor
   * 
   * @return the CellEditorListener that handles events from the current editor
   */
  protected CellEditorListener createCellEditorListener()
  {
    return new CellEditorHandler();
  }

  /**
   * Creates and returns a new ComponentHandler. This is used for the large
   * model to mark the validCachedPreferredSize as invalid when the component
   * moves.
   * 
   * @return a new ComponentHandler.
   */
  protected ComponentListener createComponentListener()
  {
    return new ComponentHandler();
  }

  /**
   * Creates and returns the object responsible for updating the treestate when
   * a nodes expanded state changes.
   * 
   * @return the TreeExpansionListener responsible for updating the treestate
   */
  protected TreeExpansionListener createTreeExpansionListener()
  {
    return new TreeExpansionHandler();
  }

  /**
   * Creates the object responsible for managing what is expanded, as well as
   * the size of nodes.
   * 
   * @return the object responsible for managing what is expanded.
   */
  protected AbstractLayoutCache createLayoutCache()
  {
    return new VariableHeightLayoutCache();
  }

  /**
   * Returns the renderer pane that renderer components are placed in.
   * 
   * @return the rendererpane that render components are placed in.
   */
  protected CellRendererPane createCellRendererPane()
  {
    return new CellRendererPane();
  }

  /**
   * Creates a default cell editor.
   * 
   * @return the default cell editor.
   */
  protected TreeCellEditor createDefaultCellEditor()
  {
    DefaultTreeCellEditor ed;
    if (currentCellRenderer != null
        && currentCellRenderer instanceof DefaultTreeCellRenderer)
      ed = new DefaultTreeCellEditor(tree,
                                (DefaultTreeCellRenderer) currentCellRenderer);
    else
      ed = new DefaultTreeCellEditor(tree, null);
    return ed;
  }

  /**
   * Returns the default cell renderer that is used to do the stamping of each
   * node.
   * 
   * @return the default cell renderer that is used to do the stamping of each
   *         node.
   */
  protected TreeCellRenderer createDefaultCellRenderer()
  {
    return new DefaultTreeCellRenderer();
  }

  /**
   * Returns a listener that can update the tree when the model changes.
   * 
   * @return a listener that can update the tree when the model changes.
   */
  protected TreeModelListener createTreeModelListener()
  {
    return new TreeModelHandler();
  }

  /**
   * Uninstall all registered listeners
   */
  protected void uninstallListeners()
  {
    tree.removePropertyChangeListener(propertyChangeListener);
    tree.removeFocusListener(focusListener);
    tree.removeTreeSelectionListener(treeSelectionListener);
    tree.removeMouseListener(mouseListener);
    tree.removeKeyListener(keyListener);
    tree.removePropertyChangeListener(selectionModelPropertyChangeListener);
    tree.removeComponentListener(componentListener);
    tree.removeTreeExpansionListener(treeExpansionListener);

    TreeCellEditor tce = tree.getCellEditor();
    if (tce != null)
      tce.removeCellEditorListener(cellEditorListener);
    if (treeModel != null)
      treeModel.removeTreeModelListener(treeModelListener);
  }

  /**
   * Uninstall all keyboard actions.
   */
  protected void uninstallKeyboardActions()
  {
    tree.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).setParent(
                                                                              null);
    tree.getActionMap().setParent(null);
  }

  /**
   * Uninstall the rendererPane.
   */
  protected void uninstallComponents()
  {
    currentCellRenderer = null;
    rendererPane = null;
    createdRenderer = false;
    setCellRenderer(currentCellRenderer);
  }

  /**
   * The vertical element of legs between nodes starts at the bottom of the
   * parent node by default. This method makes the leg start below that.
   * 
   * @return the vertical leg buffer
   */
  protected int getVerticalLegBuffer()
  {
    return getRowHeight() / 2;
  }

  /**
   * The horizontal element of legs between nodes starts at the right of the
   * left-hand side of the child node by default. This method makes the leg end
   * before that.
   * 
   * @return the horizontal leg buffer
   */
  protected int getHorizontalLegBuffer()
  {
    return rightChildIndent / 2;
  }

  /**
   * Make all the nodes that are expanded in JTree expanded in LayoutCache. This
   * invokes updateExpandedDescendants with the root path.
   */
  protected void updateLayoutCacheExpandedNodes()
  {
    if (treeModel != null && treeModel.getRoot() != null)
      updateExpandedDescendants(new TreePath(treeModel.getRoot()));
  }

  /**
   * Updates the expanded state of all the descendants of the <code>path</code>
   * by getting the expanded descendants from the tree and forwarding to the
   * tree state.
   * 
   * @param path the path used to update the expanded states
   */
  protected void updateExpandedDescendants(TreePath path)
  {
    completeEditing();
    Enumeration expanded = tree.getExpandedDescendants(path);
    while (expanded.hasMoreElements())
      treeState.setExpandedState((TreePath) expanded.nextElement(), true);
  }

  /**
   * Returns a path to the last child of <code>parent</code>
   * 
   * @param parent is the topmost path to specified
   * @return a path to the last child of parent
   */
  protected TreePath getLastChildPath(TreePath parent)
  {
    return (TreePath) parent.getLastPathComponent();
  }

  /**
   * Updates how much each depth should be offset by.
   */
  protected void updateDepthOffset()
  {
    depthOffset += getVerticalLegBuffer();
  }

  /**
   * Updates the cellEditor based on editability of the JTree that we're
   * contained in. If the tree is editable but doesn't have a cellEditor, a
   * basic one will be used.
   */
  protected void updateCellEditor()
  {
    completeEditing();
    TreeCellEditor newEd = null;
    if (tree != null && tree.isEditable())
      {
        newEd = tree.getCellEditor();
        if (newEd == null)
          {
            newEd = createDefaultCellEditor();
            if (newEd != null)
              {
                tree.setCellEditor(newEd);
                createdCellEditor = true;
              }
          }
      }
    // Update listeners.
    if (newEd != cellEditor)
      {
        if (cellEditor != null && cellEditorListener != null)
          cellEditor.removeCellEditorListener(cellEditorListener);
        cellEditor = newEd;
        if (cellEditorListener == null)
          cellEditorListener = createCellEditorListener();
        if (cellEditor != null && cellEditorListener != null)
          cellEditor.addCellEditorListener(cellEditorListener);
        createdCellEditor = false;
      }
  }

  /**
   * Messaged from the tree we're in when the renderer has changed.
   */
  protected void updateRenderer()
  {
    if (tree != null)
      {
	TreeCellRenderer rend = tree.getCellRenderer();
	if (rend != null)
	  {
	    createdRenderer = false;
	    currentCellRenderer = rend;
	    if (createdCellEditor)
	      tree.setCellEditor(null);
	  }
	else
	  {
	    tree.setCellRenderer(createDefaultCellRenderer());
	    createdRenderer = true;
	  }
      }
    else
      {
	currentCellRenderer = null;
	createdRenderer = false;
      }

    updateCellEditor();
  }

  /**
   * Resets the treeState instance based on the tree we're providing the look
   * and feel for. The node dimensions handler is required and must be created
   * in advance.
   */
  protected void configureLayoutCache()
  {
    treeState = createLayoutCache();
    treeState.setNodeDimensions(nodeDimensions);
  }

  /**
   * Marks the cached size as being invalid, and messages the tree with
   * <code>treeDidChange</code>.
   */
  protected void updateSize()
  {
    preferredSize = null;
    updateCachedPreferredSize();
    tree.treeDidChange();
  }

  /**
   * Updates the <code>preferredSize</code> instance variable, which is
   * returned from <code>getPreferredSize()</code>.
   */
  protected void updateCachedPreferredSize()
  {
    validCachedPreferredSize = false;
  }

  /**
   * Messaged from the VisibleTreeNode after it has been expanded.
   * 
   * @param path is the path that has been expanded.
   */
  protected void pathWasExpanded(TreePath path)
  {
    validCachedPreferredSize = false;
    treeState.setExpandedState(path, true);
    tree.repaint();
  }

  /**
   * Messaged from the VisibleTreeNode after it has collapsed
   */
  protected void pathWasCollapsed(TreePath path)
  {
    validCachedPreferredSize = false;
    treeState.setExpandedState(path, false);
    tree.repaint();
  }

  /**
   * Install all defaults for the tree.
   */
  protected void installDefaults()
  {
    LookAndFeel.installColorsAndFont(tree, "Tree.background",
                                     "Tree.foreground", "Tree.font");
    
    hashColor = UIManager.getColor("Tree.hash");
    if (hashColor == null)
      hashColor = Color.black;
    
    tree.setOpaque(true);

    rightChildIndent = UIManager.getInt("Tree.rightChildIndent");
    leftChildIndent = UIManager.getInt("Tree.leftChildIndent");
    totalChildIndent = rightChildIndent + leftChildIndent;
    setRowHeight(UIManager.getInt("Tree.rowHeight"));
    tree.setRowHeight(getRowHeight());
    tree.setScrollsOnExpand(UIManager.getBoolean("Tree.scrollsOnExpand"));
    setExpandedIcon(UIManager.getIcon("Tree.expandedIcon"));
    setCollapsedIcon(UIManager.getIcon("Tree.collapsedIcon"));
  }

  /**
   * Install all keyboard actions for this
   */
  protected void installKeyboardActions()
  {
    InputMap focusInputMap =
      (InputMap) SharedUIDefaults.get("Tree.focusInputMap");
    SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED,
                                     focusInputMap);
    InputMap ancestorInputMap =
      (InputMap) SharedUIDefaults.get("Tree.ancestorInputMap");
    SwingUtilities.replaceUIInputMap(tree,
                                 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
                                 ancestorInputMap);

    SwingUtilities.replaceUIActionMap(tree, getActionMap());
  }

  /**
   * Creates and returns the shared action map for JTrees.
   *
   * @return the shared action map for JTrees
   */
  private ActionMap getActionMap()
  {
    ActionMap am = (ActionMap) UIManager.get("Tree.actionMap");
    if (am == null)
      {
        am = createDefaultActions();
        UIManager.getLookAndFeelDefaults().put("Tree.actionMap", am);
      }
    return am;
  }

  /**
   * Creates the default actions when there are none specified by the L&F.
   *
   * @return the default actions
   */
  private ActionMap createDefaultActions()
  {
    ActionMapUIResource am = new ActionMapUIResource();
    Action action;

    // TreeHomeAction.
    action = new TreeHomeAction(-1, "selectFirst");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeHomeAction(-1, "selectFirstChangeLead");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeHomeAction(-1, "selectFirstExtendSelection");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeHomeAction(1, "selectLast");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeHomeAction(1, "selectLastChangeLead");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeHomeAction(1, "selectLastExtendSelection");
    am.put(action.getValue(Action.NAME), action);

    // TreeIncrementAction.
    action = new TreeIncrementAction(-1, "selectPrevious");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeIncrementAction(-1, "selectPreviousExtendSelection");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeIncrementAction(-1, "selectPreviousChangeLead");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeIncrementAction(1, "selectNext");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeIncrementAction(1, "selectNextExtendSelection");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeIncrementAction(1, "selectNextChangeLead");
    am.put(action.getValue(Action.NAME), action);

    // TreeTraverseAction.
    action = new TreeTraverseAction(-1, "selectParent");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeTraverseAction(1, "selectChild");
    am.put(action.getValue(Action.NAME), action);
    
    // TreeToggleAction.
    action = new TreeToggleAction("toggleAndAnchor");
    am.put(action.getValue(Action.NAME), action);

    // TreePageAction.
    action = new TreePageAction(-1, "scrollUpChangeSelection");
    am.put(action.getValue(Action.NAME), action);
    action = new TreePageAction(-1, "scrollUpExtendSelection");
    am.put(action.getValue(Action.NAME), action);
    action = new TreePageAction(-1, "scrollUpChangeLead");
    am.put(action.getValue(Action.NAME), action);
    action = new TreePageAction(1, "scrollDownChangeSelection");
    am.put(action.getValue(Action.NAME), action);
    action = new TreePageAction(1, "scrollDownExtendSelection");
    am.put(action.getValue(Action.NAME), action);
    action = new TreePageAction(1, "scrollDownChangeLead");
    am.put(action.getValue(Action.NAME), action);
    
    // Tree editing actions
    action = new TreeStartEditingAction("startEditing");
    am.put(action.getValue(Action.NAME), action);
    action = new TreeCancelEditingAction("cancel");
    am.put(action.getValue(Action.NAME), action);
    

    return am;
  }

  /**
   * Converts the modifiers.
   * 
   * @param mod - modifier to convert
   * @returns the new modifier
   */
  private int convertModifiers(int mod)
  {
    if ((mod & KeyEvent.SHIFT_DOWN_MASK) != 0)
      {
        mod |= KeyEvent.SHIFT_MASK;
        mod &= ~ KeyEvent.SHIFT_DOWN_MASK;
      }
    if ((mod & KeyEvent.CTRL_DOWN_MASK) != 0)
      {
        mod |= KeyEvent.CTRL_MASK;
        mod &= ~ KeyEvent.CTRL_DOWN_MASK;
      }
    if ((mod & KeyEvent.META_DOWN_MASK) != 0)
      {
        mod |= KeyEvent.META_MASK;
        mod &= ~ KeyEvent.META_DOWN_MASK;
      }
    if ((mod & KeyEvent.ALT_DOWN_MASK) != 0)
      {
        mod |= KeyEvent.ALT_MASK;
        mod &= ~ KeyEvent.ALT_DOWN_MASK;
      }
    if ((mod & KeyEvent.ALT_GRAPH_DOWN_MASK) != 0)
      {
        mod |= KeyEvent.ALT_GRAPH_MASK;
        mod &= ~ KeyEvent.ALT_GRAPH_DOWN_MASK;
      }
    return mod;
  }

  /**
   * Install all listeners for this
   */
  protected void installListeners()
  {
    propertyChangeListener = createPropertyChangeListener();
    tree.addPropertyChangeListener(propertyChangeListener);

    focusListener = createFocusListener();
    tree.addFocusListener(focusListener);

    treeSelectionListener = createTreeSelectionListener();
    tree.addTreeSelectionListener(treeSelectionListener);

    mouseListener = createMouseListener();
    tree.addMouseListener(mouseListener);

    keyListener = createKeyListener();
    tree.addKeyListener(keyListener);

    selectionModelPropertyChangeListener =
      createSelectionModelPropertyChangeListener();
    if (treeSelectionModel != null
        && selectionModelPropertyChangeListener != null)
      {
        treeSelectionModel.addPropertyChangeListener(
            selectionModelPropertyChangeListener);
      }

    componentListener = createComponentListener();
    tree.addComponentListener(componentListener);

    treeExpansionListener = createTreeExpansionListener();
    tree.addTreeExpansionListener(treeExpansionListener);

    treeModelListener = createTreeModelListener();
    if (treeModel != null)
      treeModel.addTreeModelListener(treeModelListener);

    cellEditorListener = createCellEditorListener();
  }

  /**
   * Install the UI for the component
   * 
   * @param c the component to install UI for
   */
  public void installUI(JComponent c)
  {
    tree = (JTree) c;

    prepareForUIInstall();
    installDefaults();
    installComponents();
    installKeyboardActions();
    installListeners();
    completeUIInstall();
  }
  
  /**
   * Uninstall the defaults for the tree
   */
  protected void uninstallDefaults()
  {
    tree.setFont(null);
    tree.setForeground(null);
    tree.setBackground(null);
  }

  /**
   * Uninstall the UI for the component
   * 
   * @param c the component to uninstall UI for
   */
  public void uninstallUI(JComponent c)
  {
    completeEditing();

    prepareForUIUninstall();
    uninstallDefaults();
    uninstallKeyboardActions();
    uninstallListeners();
    uninstallComponents();
    completeUIUninstall();
  }

  /**
   * Paints the specified component appropriate for the look and feel. This
   * method is invoked from the ComponentUI.update method when the specified
   * component is being painted. Subclasses should override this method and use
   * the specified Graphics object to render the content of the component.
   * 
   * @param g the Graphics context in which to paint
   * @param c the component being painted; this argument is often ignored, but
   *          might be used if the UI object is stateless and shared by multiple
   *          components
   */
  public void paint(Graphics g, JComponent c)
  {
    JTree tree = (JTree) c;
    
    int rows = treeState.getRowCount();
    
    if (rows == 0)
      // There is nothing to do if the tree is empty.
      return;

    Rectangle clip = g.getClipBounds();

    Insets insets = tree.getInsets();

    if (clip != null && treeModel != null)
      {
        int startIndex = tree.getClosestRowForLocation(clip.x, clip.y);
        int endIndex = tree.getClosestRowForLocation(clip.x + clip.width,
                                                     clip.y + clip.height);

        // Also paint dashes to the invisible nodes below.
        // These should be painted first, otherwise they may cover
        // the control icons.
        if (endIndex < rows)
          for (int i = endIndex + 1; i < rows; i++)
            {
              TreePath path = treeState.getPathForRow(i);
              if (isLastChild(path))
                paintVerticalPartOfLeg(g, clip, insets, path);
            }

        // The two loops are required to ensure that the lines are not
        // painted over the other tree components.

        int n = endIndex - startIndex + 1;
        Rectangle[] bounds = new Rectangle[n];
        boolean[] isLeaf = new boolean[n];
        boolean[] isExpanded = new boolean[n];
        TreePath[] path = new TreePath[n];
        int k;

        k = 0;
        for (int i = startIndex; i <= endIndex; i++, k++)
          {
            path[k] = treeState.getPathForRow(i);
            isLeaf[k] = treeModel.isLeaf(path[k].getLastPathComponent());
            isExpanded[k] = tree.isExpanded(path[k]);
            bounds[k] = getPathBounds(tree, path[k]);

            paintHorizontalPartOfLeg(g, clip, insets, bounds[k], path[k], i,
                                     isExpanded[k], false, isLeaf[k]);
            if (isLastChild(path[k]))
              paintVerticalPartOfLeg(g, clip, insets, path[k]);
          }

        k = 0;
        for (int i = startIndex; i <= endIndex; i++, k++)
          {
            paintRow(g, clip, insets, bounds[k], path[k], i, isExpanded[k],
                     false, isLeaf[k]);
          }
      }
  }

  /**
   * Check if the path is referring to the last child of some parent.
   */
  private boolean isLastChild(TreePath path)
  {
    if (path instanceof GnuPath)
      {
        // Except the seldom case when the layout cache is changed, this
        // optimized code will be executed.
        return ((GnuPath) path).isLastChild;
      }
    else
      {
        // Non optimized general case.
        TreePath parent = path.getParentPath();
        if (parent == null)
          return false;
        int childCount = treeState.getVisibleChildCount(parent);
        int p = treeModel.getIndexOfChild(parent, path.getLastPathComponent());
        return p == childCount - 1;
      }
  }

  /**
   * Ensures that the rows identified by beginRow through endRow are visible.
   * 
   * @param beginRow is the first row
   * @param endRow is the last row
   */
  protected void ensureRowsAreVisible(int beginRow, int endRow)
  {
    if (beginRow < endRow)
      {
        int temp = endRow;
        endRow = beginRow;
        beginRow = temp;
      }

    for (int i = beginRow; i < endRow; i++)
      {
        TreePath path = getPathForRow(tree, i);
        if (! tree.isVisible(path))
          tree.makeVisible(path);
      }
  }

  /**
   * Sets the preferred minimum size.
   * 
   * @param newSize is the new preferred minimum size.
   */
  public void setPreferredMinSize(Dimension newSize)
  {
    preferredMinSize = newSize;
  }

  /**
   * Gets the preferred minimum size.
   * 
   * @returns the preferred minimum size.
   */
  public Dimension getPreferredMinSize()
  {
    if (preferredMinSize == null)
      return getPreferredSize(tree);
    else
      return preferredMinSize;
  }

  /**
   * Returns the preferred size to properly display the tree, this is a cover
   * method for getPreferredSize(c, false).
   * 
   * @param c the component whose preferred size is being queried; this argument
   *          is often ignored but might be used if the UI object is stateless
   *          and shared by multiple components
   * @return the preferred size
   */
  public Dimension getPreferredSize(JComponent c)
  {
    return getPreferredSize(c, false);
  }

  /**
   * Returns the preferred size to represent the tree in c. If checkConsistancy
   * is true, checkConsistancy is messaged first.
   * 
   * @param c the component whose preferred size is being queried.
   * @param checkConsistancy if true must check consistancy
   * @return the preferred size
   */
  public Dimension getPreferredSize(JComponent c, boolean checkConsistancy)
  {
    if (! validCachedPreferredSize)
      {
        Rectangle size = tree.getBounds();
        // Add the scrollbar dimensions to the preferred size.
        preferredSize = new Dimension(treeState.getPreferredWidth(size),
                                      treeState.getPreferredHeight());
        validCachedPreferredSize = true;
      }
    return preferredSize;
  }

  /**
   * Returns the minimum size for this component. Which will be the min
   * preferred size or (0,0).
   * 
   * @param c the component whose min size is being queried.
   * @returns the preferred size or null
   */
  public Dimension getMinimumSize(JComponent c)
  {
    return preferredMinSize = getPreferredSize(c);
  }

  /**
   * Returns the maximum size for the component, which will be the preferred
   * size if the instance is currently in JTree or (0,0).
   * 
   * @param c the component whose preferred size is being queried
   * @return the max size or null
   */
  public Dimension getMaximumSize(JComponent c)
  {
    return getPreferredSize(c);
  }

  /**
   * Messages to stop the editing session. If the UI the receiver is providing
   * the look and feel for returns true from
   * <code>getInvokesStopCellEditing</code>, stopCellEditing will be invoked
   * on the current editor. Then completeEditing will be messaged with false,
   * true, false to cancel any lingering editing.
   */
  protected void completeEditing()
  {
    if (tree.getInvokesStopCellEditing() && stopEditingInCompleteEditing
        && editingComponent != null)
      cellEditor.stopCellEditing();

    completeEditing(false, true, false);
  }

  /**
   * Stops the editing session. If messageStop is true, the editor is messaged
   * with stopEditing, if messageCancel is true the editor is messaged with
   * cancelEditing. If messageTree is true, the treeModel is messaged with
   * valueForPathChanged.
   * 
   * @param messageStop message to stop editing
   * @param messageCancel message to cancel editing
   * @param messageTree message to treeModel
   */
  protected void completeEditing(boolean messageStop, boolean messageCancel,
                                 boolean messageTree)
  {
    // Make no attempt to complete the non existing editing session.
    if (stopEditingInCompleteEditing && editingComponent != null)
      {
        Component comp = editingComponent;
        TreePath p = editingPath;
        editingComponent = null;
        editingPath = null;
        if (messageStop)
          cellEditor.stopCellEditing();
        else if (messageCancel)
          cellEditor.cancelCellEditing();

        tree.remove(comp);

        if (editorHasDifferentSize)
          {
            treeState.invalidatePathBounds(p);
            updateSize();
          }
        else
          {
            // Need to refresh the tree.
            Rectangle b = getPathBounds(tree, p);
            tree.repaint(0, b.y, tree.getWidth(), b.height);
          }

        if (messageTree)
          {
            Object value = cellEditor.getCellEditorValue();
            treeModel.valueForPathChanged(p, value);
          }
      }
  }

  /**
   * Will start editing for node if there is a cellEditor and shouldSelectCall
   * returns true. This assumes that path is valid and visible.
   * 
   * @param path is the path to start editing
   * @param event is the MouseEvent performed on the path
   * @return true if successful
   */
  protected boolean startEditing(TreePath path, MouseEvent event)
  {
    // Maybe cancel editing.
    if (isEditing(tree) && tree.getInvokesStopCellEditing()
        && ! stopEditing(tree))
      return false;

    completeEditing();
    TreeCellEditor ed = cellEditor;
    if (ed != null && tree.isPathEditable(path))
      {
        if (ed.isCellEditable(event))
          {
            editingRow = getRowForPath(tree, path); 
            Object value = path.getLastPathComponent();
            boolean isSelected = tree.isPathSelected(path);
            boolean isExpanded = tree.isExpanded(editingPath);
            boolean isLeaf = treeModel.isLeaf(value);
            editingComponent = ed.getTreeCellEditorComponent(tree, value,
                                                             isSelected,
                                                             isExpanded,
                                                             isLeaf,
                                                             editingRow);

            Rectangle bounds = getPathBounds(tree, path);

            Dimension size = editingComponent.getPreferredSize();
            int rowHeight = getRowHeight();
            if (size.height != bounds.height && rowHeight > 0)
              size.height = rowHeight;

            if (size.width != bounds.width || size.height != bounds.height)
              {
                editorHasDifferentSize = true;
                treeState.invalidatePathBounds(path);
                updateSize();
              }
            else
              editorHasDifferentSize = false;
            
            // The editing component must be added to its container. We add the
            // container, not the editing component itself.
            tree.add(editingComponent);
            editingComponent.setBounds(bounds.x, bounds.y, size.width,
                                       size.height);
            editingComponent.validate();
            editingPath = path;

            if (ed.shouldSelectCell(event))
              {
                stopEditingInCompleteEditing = false;
                tree.setSelectionRow(editingRow);
                stopEditingInCompleteEditing = true;
              }

            editorRequestFocus(editingComponent);
            // Register MouseInputHandler to redispatch initial mouse events
            // correctly.
            if (event instanceof MouseEvent)
              {
                Point p = SwingUtilities.convertPoint(tree, event.getX(), event.getY(),
                                                      editingComponent);
                Component active =
                  SwingUtilities.getDeepestComponentAt(editingComponent, p.x, p.y);
                if (active != null)
                  {
                    MouseInputHandler ih = new MouseInputHandler(tree, active, event);
                    
                  }
              }

            return true;
          }
        else
          editingComponent = null;
      }
    return false;
  }

  /**
   * Requests focus on the editor. The method is necessary since the
   * DefaultTreeCellEditor returns a container that contains the
   * actual editor, and we want to request focus on the editor, not the
   * container.
   */
  private void editorRequestFocus(Component c)
  {
    if (c instanceof Container)
      {
        // TODO: Maybe do something more reasonable here, like queriying the
        // FocusTraversalPolicy.
        Container cont = (Container) c;
        if (cont.getComponentCount() > 0)
          cont.getComponent(0).requestFocus();
      }
    else if (c.isFocusable())
      c.requestFocus();
      
  }

  /**
   * If the <code>mouseX</code> and <code>mouseY</code> are in the expand or
   * collapse region of the row, this will toggle the row.
   * 
   * @param path the path we are concerned with
   * @param mouseX is the cursor's x position
   * @param mouseY is the cursor's y position
   */
  protected void checkForClickInExpandControl(TreePath path, int mouseX,
                                              int mouseY)
  {
    if (isLocationInExpandControl(path, mouseX, mouseY))
      handleExpandControlClick(path, mouseX, mouseY);
  }

  /**
   * Returns true if the <code>mouseX</code> and <code>mouseY</code> fall in
   * the area of row that is used to expand/collpse the node and the node at row
   * does not represent a leaf.
   * 
   * @param path the path we are concerned with
   * @param mouseX is the cursor's x position
   * @param mouseY is the cursor's y position
   * @return true if the <code>mouseX</code> and <code>mouseY</code> fall in
   *         the area of row that is used to expand/collpse the node and the
   *         node at row does not represent a leaf.
   */
  protected boolean isLocationInExpandControl(TreePath path, int mouseX,
                                              int mouseY)
  {
    boolean cntlClick = false;
    if (! treeModel.isLeaf(path.getLastPathComponent()))
      {
        int width;
        Icon expandedIcon = getExpandedIcon();
        if (expandedIcon != null)
          width = expandedIcon.getIconWidth();
        else
          // Only guessing. This is the width of
          // the tree control icon in Metal L&F.
          width = 18;

        Insets i = tree.getInsets();
        
        int depth;
        if (isRootVisible())
          depth = path.getPathCount()-1;
        else
          depth = path.getPathCount()-2;
        
        int left = getRowX(tree.getRowForPath(path), depth)
                   - width + i.left;
        cntlClick = mouseX >= left && mouseX <= left + width;
      }
    return cntlClick;
  }

  /**
   * Messaged when the user clicks the particular row, this invokes
   * toggleExpandState.
   * 
   * @param path the path we are concerned with
   * @param mouseX is the cursor's x position
   * @param mouseY is the cursor's y position
   */
  protected void handleExpandControlClick(TreePath path, int mouseX, int mouseY)
  {
    toggleExpandState(path);
  }

  /**
   * Expands path if it is not expanded, or collapses row if it is expanded. If
   * expanding a path and JTree scroll on expand, ensureRowsAreVisible is
   * invoked to scroll as many of the children to visible as possible (tries to
   * scroll to last visible descendant of path).
   * 
   * @param path the path we are concerned with
   */
  protected void toggleExpandState(TreePath path)
  {
    // tree.isExpanded(path) would do the same, but treeState knows faster.
    if (treeState.isExpanded(path))
      tree.collapsePath(path);
    else
      tree.expandPath(path);
  }

  /**
   * Returning true signifies a mouse event on the node should toggle the
   * selection of only the row under the mouse. The BasisTreeUI treats the
   * event as "toggle selection event" if the CTRL button was pressed while
   * clicking. The event is not counted as toggle event if the associated
   * tree does not support the multiple selection.
   * 
   * @param event is the MouseEvent performed on the row.
   * @return true signifies a mouse event on the node should toggle the
   *         selection of only the row under the mouse.
   */
  protected boolean isToggleSelectionEvent(MouseEvent event)
  {
    return 
      (tree.getSelectionModel().getSelectionMode() != 
        TreeSelectionModel.SINGLE_TREE_SELECTION) &&
      ((event.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0);  
  }

  /**
   * Returning true signifies a mouse event on the node should select from the
   * anchor point. The BasisTreeUI treats the event as "multiple selection
   * event" if the SHIFT button was pressed while clicking. The event is not
   * counted as multiple selection event if the associated tree does not support
   * the multiple selection.
   * 
   * @param event is the MouseEvent performed on the node.
   * @return true signifies a mouse event on the node should select from the
   *         anchor point.
   */
  protected boolean isMultiSelectEvent(MouseEvent event)
  {
    return 
      (tree.getSelectionModel().getSelectionMode() != 
        TreeSelectionModel.SINGLE_TREE_SELECTION) &&
      ((event.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK) != 0);  
  }

  /**
   * Returning true indicates the row under the mouse should be toggled based on
   * the event. This is invoked after checkForClickInExpandControl, implying the
   * location is not in the expand (toggle) control.
   * 
   * @param event is the MouseEvent performed on the row.
   * @return true indicates the row under the mouse should be toggled based on
   *         the event.
   */
  protected boolean isToggleEvent(MouseEvent event)
  {
    boolean toggle = false;
    if (SwingUtilities.isLeftMouseButton(event))
      {
        int clickCount = tree.getToggleClickCount();
        if (clickCount > 0 && event.getClickCount() == clickCount)
          toggle = true;
      }
    return toggle;
  }

  /**
   * Messaged to update the selection based on a MouseEvent over a particular
   * row. If the even is a toggle selection event, the row is either selected,
   * or deselected. If the event identifies a multi selection event, the
   * selection is updated from the anchor point. Otherwise, the row is selected,
   * and the previous selection is cleared.</p>
   * 
   * @param path is the path selected for an event
   * @param event is the MouseEvent performed on the path.
   * 
   * @see #isToggleSelectionEvent(MouseEvent)
   * @see #isMultiSelectEvent(MouseEvent)
   */
  protected void selectPathForEvent(TreePath path, MouseEvent event)
  {
    if (isToggleSelectionEvent(event))
      {
        // The event selects or unselects the clicked row.
        if (tree.isPathSelected(path))
          tree.removeSelectionPath(path);
        else
          {
            tree.addSelectionPath(path);
            tree.setAnchorSelectionPath(path);
          }
      }
    else if (isMultiSelectEvent(event))
      {
        // The event extends selection form anchor till the clicked row.
        TreePath anchor = tree.getAnchorSelectionPath();
        if (anchor != null)
          {
            int aRow = getRowForPath(tree, anchor);
            tree.addSelectionInterval(aRow, getRowForPath(tree, path));
          }
        else
          tree.addSelectionPath(path);
      }
    else
      {
        // This is an ordinary event that just selects the clicked row.
        tree.setSelectionPath(path);
        if (isToggleEvent(event))
          toggleExpandState(path);
      }
  }

  /**
   * Returns true if the node at <code>row</code> is a leaf.
   * 
   * @param row is the row we are concerned with.
   * @return true if the node at <code>row</code> is a leaf.
   */
  protected boolean isLeaf(int row)
  {
    TreePath pathForRow = getPathForRow(tree, row);
    if (pathForRow == null)
      return true;

    Object node = pathForRow.getLastPathComponent();
    return treeModel.isLeaf(node);
  }
  
  /**
   * The action to start editing at the current lead selection path.
   */
  class TreeStartEditingAction
      extends AbstractAction
  {
    /**
     * Creates the new tree cancel editing action.
     * 
     * @param name the name of the action (used in toString).
     */
    public TreeStartEditingAction(String name)
    {
      super(name);
    }    
    
    /**
     * Start editing at the current lead selection path.
     * 
     * @param e the ActionEvent that caused this action.
     */
    public void actionPerformed(ActionEvent e)
    {
      TreePath lead = tree.getLeadSelectionPath();
      if (!tree.isEditing()) 
        tree.startEditingAtPath(lead);
    }
  }  

  /**
   * Updates the preferred size when scrolling, if necessary.
   */
  public class ComponentHandler
      extends ComponentAdapter
      implements ActionListener
  {
    /**
     * Timer used when inside a scrollpane and the scrollbar is adjusting
     */
    protected Timer timer;

    /** ScrollBar that is being adjusted */
    protected JScrollBar scrollBar;

    /**
     * Constructor
     */
    public ComponentHandler()
    {
      // Nothing to do here.
    }

    /**
     * Invoked when the component's position changes.
     * 
     * @param e the event that occurs when moving the component
     */
    public void componentMoved(ComponentEvent e)
    {
      if (timer == null)
        {
          JScrollPane scrollPane = getScrollPane();
          if (scrollPane == null)
            updateSize();
          else
            {
              // Determine the scrollbar that is adjusting, if any, and
              // start the timer for that. If no scrollbar is adjusting,
              // we simply call updateSize().
              scrollBar = scrollPane.getVerticalScrollBar();
              if (scrollBar == null || !scrollBar.getValueIsAdjusting())
                {
                  // It's not the vertical scrollbar, try the horizontal one.
                  scrollBar = scrollPane.getHorizontalScrollBar();
                  if (scrollBar != null && scrollBar.getValueIsAdjusting())
                    startTimer();
                  else
                    updateSize();
                }
              else
                {
                  startTimer();
                }
            }
        }
    }

    /**
     * Creates, if necessary, and starts a Timer to check if needed to resize
     * the bounds
     */
    protected void startTimer()
    {
      if (timer == null)
        {
          timer = new Timer(200, this);
          timer.setRepeats(true);
        }
      timer.start();
    }

    /**
     * Returns the JScrollPane housing the JTree, or null if one isn't found.
     * 
     * @return JScrollPane housing the JTree, or null if one isn't found.
     */
    protected JScrollPane getScrollPane()
    {
      JScrollPane found = null;
      Component p = tree.getParent();
      while (p != null && !(p instanceof JScrollPane))
        p = p.getParent();
      if (p instanceof JScrollPane)
        found = (JScrollPane) p;
      return found;
    }

    /**
     * Public as a result of Timer. If the scrollBar is null, or not adjusting,
     * this stops the timer and updates the sizing.
     * 
     * @param ae is the action performed
     */
    public void actionPerformed(ActionEvent ae)
    {
      if (scrollBar == null || !scrollBar.getValueIsAdjusting())
        {
          if (timer != null)
            timer.stop();
          updateSize();
          timer = null;
          scrollBar = null;
        }
    }
  }

  /**
   * Listener responsible for getting cell editing events and updating the tree
   * accordingly.
   */
  public class CellEditorHandler
      implements CellEditorListener
  {
    /**
     * Constructor
     */
    public CellEditorHandler()
    {
      // Nothing to do here.
    }

    /**
     * Messaged when editing has stopped in the tree. Tells the listeners
     * editing has stopped.
     * 
     * @param e is the notification event
     */
    public void editingStopped(ChangeEvent e)
    {
      completeEditing(false, false, true);
    }

    /**
     * Messaged when editing has been canceled in the tree. This tells the
     * listeners the editor has canceled editing.
     * 
     * @param e is the notification event
     */
    public void editingCanceled(ChangeEvent e)
    {
      completeEditing(false, false, false);
    }
  } // CellEditorHandler

  /**
   * Repaints the lead selection row when focus is lost/grained.
   */
  public class FocusHandler
      implements FocusListener
  {
    /**
     * Constructor
     */
    public FocusHandler()
    {
      // Nothing to do here.
    }

    /**
     * Invoked when focus is activated on the tree we're in, redraws the lead
     * row. Invoked when a component gains the keyboard focus. The method
     * repaints the lead row that is shown differently when the tree is in
     * focus.
     * 
     * @param e is the focus event that is activated
     */
    public void focusGained(FocusEvent e)
    {
      repaintLeadRow();
    }

    /**
     * Invoked when focus is deactivated on the tree we're in, redraws the lead
     * row. Invoked when a component loses the keyboard focus. The method
     * repaints the lead row that is shown differently when the tree is in
     * focus.
     * 
     * @param e is the focus event that is deactivated
     */
    public void focusLost(FocusEvent e)
    {
      repaintLeadRow();
    }

    /**
     * Repaint the lead row.
     */
    void repaintLeadRow()
    {
      TreePath lead = tree.getLeadSelectionPath();
      if (lead != null)
        tree.repaint(tree.getPathBounds(lead));
    }
  }

  /**
   * This is used to get multiple key down events to appropriately genereate
   * events.
   */
  public class KeyHandler
      extends KeyAdapter
  {
    /** Key code that is being generated for. */
    protected Action repeatKeyAction;

    /** Set to true while keyPressed is active */
    protected boolean isKeyDown;

    /**
     * Constructor
     */
    public KeyHandler()
    {
      // Nothing to do here.
    }

    /**
     * Invoked when a key has been typed. Moves the keyboard focus to the first
     * element whose first letter matches the alphanumeric key pressed by the
     * user. Subsequent same key presses move the keyboard focus to the next
     * object that starts with the same letter.
     * 
     * @param e the key typed
     */
    public void keyTyped(KeyEvent e)
    {
      char typed = Character.toLowerCase(e.getKeyChar());
      for (int row = tree.getLeadSelectionRow() + 1;
        row < tree.getRowCount(); row++)
        {
           if (checkMatch(row, typed))
             {
               tree.setSelectionRow(row);
               tree.scrollRowToVisible(row);
               return;
             }
        }
      
      // Not found below, search above:
      for (int row = 0; row < tree.getLeadSelectionRow(); row++)
        {
           if (checkMatch(row, typed))
             {
               tree.setSelectionRow(row);
               tree.scrollRowToVisible(row);               
               return;
             }
        }
    }
    
    /**
     * Check if the given tree row starts with this character
     * 
     * @param row the tree row
     * @param typed the typed char, must be converted to lowercase
     * @return true if the given tree row starts with this character
     */
    boolean checkMatch(int row, char typed)
    {
      TreePath path = treeState.getPathForRow(row);
      String node = path.getLastPathComponent().toString();
      if (node.length() > 0)
        {
          char x = node.charAt(0);
          if (typed == Character.toLowerCase(x))
            return true;
        }
      return false;
    }

    /**
     * Invoked when a key has been pressed.
     * 
     * @param e the key pressed
     */
    public void keyPressed(KeyEvent e)
    {
      // Nothing to do here.
    }

    /**
     * Invoked when a key has been released
     * 
     * @param e the key released
     */
    public void keyReleased(KeyEvent e)
    {
      // Nothing to do here.
    }
  }

  /**
   * MouseListener is responsible for updating the selection based on mouse
   * events.
   */
  public class MouseHandler
    extends MouseAdapter
    implements MouseMotionListener
  {
    
    /**
     * If the cell has been selected on mouse press.
     */
    private boolean selectedOnPress;

    /**
     * Constructor
     */
    public MouseHandler()
    {
      // Nothing to do here.
    }

    /**
     * Invoked when a mouse button has been pressed on a component.
     * 
     * @param e is the mouse event that occured
     */
    public void mousePressed(MouseEvent e)
    {
      if (! e.isConsumed())
        {
          handleEvent(e);
          selectedOnPress = true;
        }
      else
        {
          selectedOnPress = false;
        }
    }

    /**
     * Invoked when a mouse button is pressed on a component and then dragged.
     * MOUSE_DRAGGED events will continue to be delivered to the component where
     * the drag originated until the mouse button is released (regardless of
     * whether the mouse position is within the bounds of the component).
     * 
     * @param e is the mouse event that occured
     */
    public void mouseDragged(MouseEvent e)
    {
      // Nothing to do here.
    }

    /**
     * Invoked when the mouse button has been moved on a component (with no
     * buttons no down).
     * 
     * @param e the mouse event that occured
     */
    public void mouseMoved(MouseEvent e)
    {
      // Nothing to do here.
    }

    /**
     * Invoked when a mouse button has been released on a component.
     * 
     * @param e is the mouse event that occured
     */
    public void mouseReleased(MouseEvent e)
    {
      if (! e.isConsumed() && ! selectedOnPress)
        handleEvent(e);
    }

    /**
     * Handles press and release events.
     *
     * @param e the mouse event
     */
    private void handleEvent(MouseEvent e)
    {
      if (tree != null && tree.isEnabled())
        {
          // Maybe stop editing.
          if (isEditing(tree) && tree.getInvokesStopCellEditing()
              && ! stopEditing(tree))
            return;

          // Explicitly request focus.
          tree.requestFocusInWindow();

          int x = e.getX();
          int y = e.getY();
          TreePath path = getClosestPathForLocation(tree, x, y);
          if (path != null)
            {
              Rectangle b = getPathBounds(tree, path);
              if (y <= b.y + b.height)
                {
                  if (SwingUtilities.isLeftMouseButton(e))
                    checkForClickInExpandControl(path, x, y);
                  if (x > b.x && x <= b.x + b.width)
                    {
                      if (! startEditing(path, e))
                        selectPathForEvent(path, e);
                    }
                }
            }
        }
    }
  }

  /**
   * MouseInputHandler handles passing all mouse events, including mouse motion
   * events, until the mouse is released to the destination it is constructed
   * with.
   */
  public class MouseInputHandler
      implements MouseInputListener
  {
    /** Source that events are coming from */
    protected Component source;

    /** Destination that receives all events. */
    protected Component destination;

    /**
     * Constructor
     * 
     * @param source that events are coming from
     * @param destination that receives all events
     * @param e is the event received
     */
    public MouseInputHandler(Component source, Component destination,
                             MouseEvent e)
    {
      this.source = source;
      this.destination = destination;
      source.addMouseListener(this);
      source.addMouseMotionListener(this);
      dispatch(e);
    }

    /**
     * Invoked when the mouse button has been clicked (pressed and released) on
     * a component.
     * 
     * @param e mouse event that occured
     */
    public void mouseClicked(MouseEvent e)
    {
      dispatch(e);
    }

    /**
     * Invoked when a mouse button has been pressed on a component.
     * 
     * @param e mouse event that occured
     */
    public void mousePressed(MouseEvent e)
    {
      // Nothing to do here.
    }

    /**
     * Invoked when a mouse button has been released on a component.
     * 
     * @param e mouse event that occured
     */
    public void mouseReleased(MouseEvent e)
    {
      dispatch(e);
      removeFromSource();
    }

    /**
     * Invoked when the mouse enters a component.
     * 
     * @param e mouse event that occured
     */
    public void mouseEntered(MouseEvent e)
    {
      if (! SwingUtilities.isLeftMouseButton(e))
        removeFromSource();
    }

    /**
     * Invoked when the mouse exits a component.
     * 
     * @param e mouse event that occured
     */
    public void mouseExited(MouseEvent e)
    {
      if (! SwingUtilities.isLeftMouseButton(e))
        removeFromSource();
    }

    /**
     * Invoked when a mouse button is pressed on a component and then dragged.
     * MOUSE_DRAGGED events will continue to be delivered to the component where
     * the drag originated until the mouse button is released (regardless of
     * whether the mouse position is within the bounds of the component).
     * 
     * @param e mouse event that occured
     */
    public void mouseDragged(MouseEvent e)
    {
      dispatch(e);
    }

    /**
     * Invoked when the mouse cursor has been moved onto a component but no
     * buttons have been pushed.
     * 
     * @param e mouse event that occured
     */
    public void mouseMoved(MouseEvent e)
    {
      removeFromSource();
    }

    /**
     * Removes event from the source
     */
    protected void removeFromSource()
    {
      if (source != null)
        {
          source.removeMouseListener(this);
          source.removeMouseMotionListener(this);
        }
      source = null;
      destination = null;
    }

    /**
     * Redispatches mouse events to the destination.
     *
     * @param e the mouse event to redispatch
     */
    private void dispatch(MouseEvent e)
    {
      if (destination != null)
        {
          MouseEvent e2 = SwingUtilities.convertMouseEvent(source, e,
                                                           destination);
          destination.dispatchEvent(e2);
        }
    }
  }

  /**
   * Class responsible for getting size of node, method is forwarded to
   * BasicTreeUI method. X location does not include insets, that is handled in
   * getPathBounds.
   */
  public class NodeDimensionsHandler
      extends AbstractLayoutCache.NodeDimensions
  {
    /**
     * Constructor
     */
    public NodeDimensionsHandler()
    {
      // Nothing to do here.
    }

    /**
     * Returns, by reference in bounds, the size and x origin to place value at.
     * The calling method is responsible for determining the Y location. If
     * bounds is null, a newly created Rectangle should be returned, otherwise
     * the value should be placed in bounds and returned.
     * 
     * @param cell the value to be represented
     * @param row row being queried
     * @param depth the depth of the row
     * @param expanded true if row is expanded
     * @param size a Rectangle containing the size needed to represent value
     * @return containing the node dimensions, or null if node has no dimension
     */
    public Rectangle getNodeDimensions(Object cell, int row, int depth,
                                       boolean expanded, Rectangle size)
    {
      Dimension prefSize;
      if (editingComponent != null && editingRow == row)
        {
          // Editing, ask editor for preferred size.
          prefSize = editingComponent.getPreferredSize();
          int rowHeight = getRowHeight();
          if (rowHeight > 0 && rowHeight != prefSize.height)
            prefSize.height = rowHeight;
        }
      else
        {
          // Not editing, ask renderer for preferred size.
          Component rend =
            currentCellRenderer.getTreeCellRendererComponent(tree, cell,
                                                       tree.isRowSelected(row),
                                                       expanded,
                                                       treeModel.isLeaf(cell),
                                                       row, false);
          // Make sure the layout is valid.
          rendererPane.add(rend);
          rend.validate();
          prefSize = rend.getPreferredSize();
        }
      if (size != null)
        {
          size.x = getRowX(row, depth);
          // FIXME: This should be handled by the layout cache.
          size.y = prefSize.height * row;
          size.width =  prefSize.width;
          size.height = prefSize.height;
        }
      else
        // FIXME: The y should be handled by the layout cache.
        size = new Rectangle(getRowX(row, depth), prefSize.height * row, prefSize.width,
                             prefSize.height);
      
      return size;
    }

    /**
     * Returns the amount to indent the given row
     * 
     * @return amount to indent the given row.
     */
    protected int getRowX(int row, int depth)
    {
      return BasicTreeUI.this.getRowX(row, depth);
    }
  } // NodeDimensionsHandler

  /**
   * PropertyChangeListener for the tree. Updates the appropriate variable, or
   * TreeState, based on what changes.
   */
  public class PropertyChangeHandler
      implements PropertyChangeListener
  {

    /**
     * Constructor
     */
    public PropertyChangeHandler()
    {
      // Nothing to do here.
    }

    /**
     * This method gets called when a bound property is changed.
     * 
     * @param event A PropertyChangeEvent object describing the event source and
     *          the property that has changed.
     */
    public void propertyChange(PropertyChangeEvent event)
    {
      String property = event.getPropertyName();
      if (property.equals(JTree.ROOT_VISIBLE_PROPERTY))
        {
          validCachedPreferredSize = false;
          treeState.setRootVisible(tree.isRootVisible());
          tree.repaint();
        }
      else if (property.equals(JTree.SELECTION_MODEL_PROPERTY))
        {
          treeSelectionModel = tree.getSelectionModel();
          treeSelectionModel.setRowMapper(treeState);
        }
      else if (property.equals(JTree.TREE_MODEL_PROPERTY))
        {
          setModel(tree.getModel());
        }
      else if (property.equals(JTree.CELL_RENDERER_PROPERTY))
        {
          setCellRenderer(tree.getCellRenderer());
          // Update layout.
          if (treeState != null)
            treeState.invalidateSizes();
        }
      else if (property.equals(JTree.EDITABLE_PROPERTY))
        setEditable(((Boolean) event.getNewValue()).booleanValue());
        
    }
  }

  /**
   * Listener on the TreeSelectionModel, resets the row selection if any of the
   * properties of the model change.
   */
  public class SelectionModelPropertyChangeHandler
    implements PropertyChangeListener
  {

    /**
     * Constructor
     */
    public SelectionModelPropertyChangeHandler()
    {
      // Nothing to do here.
    }

    /**
     * This method gets called when a bound property is changed.
     * 
     * @param event A PropertyChangeEvent object describing the event source and
     *          the property that has changed.
     */
    public void propertyChange(PropertyChangeEvent event)
    {
      treeSelectionModel.resetRowSelection();
    }
  }

  /**
   * The action to cancel editing on this tree.
   */
  public class TreeCancelEditingAction
      extends AbstractAction
  {
    /**
     * Creates the new tree cancel editing action.
     * 
     * @param name the name of the action (used in toString).
     */
    public TreeCancelEditingAction(String name)
    {
      super(name);
    }

    /**
     * Invoked when an action occurs, cancels the cell editing (if the
     * tree cell is being edited). 
     * 
     * @param e event that occured
     */
    public void actionPerformed(ActionEvent e)
    {
      if (isEnabled() && tree.isEditing())
        tree.cancelEditing();
    }
  }

  /**
   * Updates the TreeState in response to nodes expanding/collapsing.
   */
  public class TreeExpansionHandler
      implements TreeExpansionListener
  {

    /**
     * Constructor
     */
    public TreeExpansionHandler()
    {
      // Nothing to do here.
    }

    /**
     * Called whenever an item in the tree has been expanded.
     * 
     * @param event is the event that occured
     */
    public void treeExpanded(TreeExpansionEvent event)
    {
      validCachedPreferredSize = false;
      treeState.setExpandedState(event.getPath(), true);
      // The maximal cell height may change
      maxHeight = 0;
      tree.revalidate();
      tree.repaint();
    }

    /**
     * Called whenever an item in the tree has been collapsed.
     * 
     * @param event is the event that occured
     */
    public void treeCollapsed(TreeExpansionEvent event)
    {
      completeEditing();
      validCachedPreferredSize = false;
      treeState.setExpandedState(event.getPath(), false);
      // The maximal cell height may change
      maxHeight = 0;
      tree.revalidate();
      tree.repaint();
    }
  } // TreeExpansionHandler

  /**
   * TreeHomeAction is used to handle end/home actions. Scrolls either the first
   * or last cell to be visible based on direction.
   */
  public class TreeHomeAction
      extends AbstractAction
  {

    /** The direction, either home or end */
    protected int direction;

    /**
     * Creates a new TreeHomeAction instance.
     * 
     * @param dir the direction to go to, <code>-1</code> for home,
     *        <code>1</code> for end
     * @param name the name of the action
     */
    public TreeHomeAction(int dir, String name)
    {
      direction = dir;
      putValue(Action.NAME, name);
    }

    /**
     * Invoked when an action occurs.
     * 
     * @param e is the event that occured
     */
    public void actionPerformed(ActionEvent e)
    {
      if (tree != null)
        {
          String command = (String) getValue(Action.NAME);
          if (command.equals("selectFirst"))
            {
              ensureRowsAreVisible(0, 0);
              tree.setSelectionInterval(0, 0);
            }
          if (command.equals("selectFirstChangeLead"))
            {
              ensureRowsAreVisible(0, 0);
              tree.setLeadSelectionPath(getPathForRow(tree, 0));
            }
          if (command.equals("selectFirstExtendSelection"))
            {
              ensureRowsAreVisible(0, 0);
              TreePath anchorPath = tree.getAnchorSelectionPath();
              if (anchorPath == null)
                tree.setSelectionInterval(0, 0);
              else
                {
                  int anchorRow = getRowForPath(tree, anchorPath);
                  tree.setSelectionInterval(0, anchorRow);
                  tree.setAnchorSelectionPath(anchorPath);
                  tree.setLeadSelectionPath(getPathForRow(tree, 0));
                }
            }
          else if (command.equals("selectLast"))
            {
              int end = getRowCount(tree) - 1;
              ensureRowsAreVisible(end, end);
              tree.setSelectionInterval(end, end);
            }
          else if (command.equals("selectLastChangeLead"))
            {
              int end = getRowCount(tree) - 1;
              ensureRowsAreVisible(end, end);
              tree.setLeadSelectionPath(getPathForRow(tree, end));
            }
          else if (command.equals("selectLastExtendSelection"))
            {
              int end = getRowCount(tree) - 1;
              ensureRowsAreVisible(end, end);
              TreePath anchorPath = tree.getAnchorSelectionPath();
              if (anchorPath == null)
                tree.setSelectionInterval(end, end);
              else
                {
                  int anchorRow = getRowForPath(tree, anchorPath);
                  tree.setSelectionInterval(end, anchorRow);
                  tree.setAnchorSelectionPath(anchorPath);
                  tree.setLeadSelectionPath(getPathForRow(tree, end));
                }
            }
        }

      // Ensure that the lead path is visible after the increment action.
      tree.scrollPathToVisible(tree.getLeadSelectionPath());
    }

    /**
     * Returns true if the action is enabled.
     * 
     * @return true if the action is enabled.
     */
    public boolean isEnabled()
    {
      return (tree != null) && tree.isEnabled();
    }
  }

  /**
   * TreeIncrementAction is used to handle up/down actions. Selection is moved
   * up or down based on direction.
   */
  public class TreeIncrementAction
    extends AbstractAction
  {

    /**
     * Specifies the direction to adjust the selection by.
     */
    protected int direction;

    /**
     * Creates a new TreeIncrementAction.
     * 
     * @param dir up or down, <code>-1</code> for up, <code>1</code> for down
     * @param name is the name of the direction
     */
    public TreeIncrementAction(int dir, String name)
    {
      direction = dir;
      putValue(Action.NAME, name);
    }

    /**
     * Invoked when an action occurs.
     * 
     * @param e is the event that occured
     */
    public void actionPerformed(ActionEvent e)
    {
      TreePath currentPath = tree.getLeadSelectionPath();
      int currentRow;

      if (currentPath != null)
        currentRow = treeState.getRowForPath(currentPath);
      else
        currentRow = 0;

      int rows = treeState.getRowCount();

      int nextRow = currentRow + 1;
      int prevRow = currentRow - 1;
      boolean hasNext = nextRow < rows;
      boolean hasPrev = prevRow >= 0 && rows > 0;
      TreePath newPath;
      String command = (String) getValue(Action.NAME);

      if (command.equals("selectPreviousChangeLead") && hasPrev)
        {
          newPath = treeState.getPathForRow(prevRow);
          tree.setSelectionPath(newPath);
          tree.setAnchorSelectionPath(newPath);
          tree.setLeadSelectionPath(newPath);
        }
      else if (command.equals("selectPreviousExtendSelection") && hasPrev)
        {
          newPath = treeState.getPathForRow(prevRow);

          // If the new path is already selected, the selection shrinks,
          // unselecting the previously current path.
          if (tree.isPathSelected(newPath))
            tree.getSelectionModel().removeSelectionPath(currentPath);

          // This must be called in any case because it updates the model
          // lead selection index.
          tree.addSelectionPath(newPath);
          tree.setLeadSelectionPath(newPath);
        }
      else if (command.equals("selectPrevious") && hasPrev)
        {
          newPath = treeState.getPathForRow(prevRow);
          tree.setSelectionPath(newPath);
        }
      else if (command.equals("selectNext") && hasNext)
        {
          newPath = treeState.getPathForRow(nextRow);
          tree.setSelectionPath(newPath);
        }
      else if (command.equals("selectNextExtendSelection") && hasNext)
        {
          newPath = treeState.getPathForRow(nextRow);

          // If the new path is already selected, the selection shrinks,
          // unselecting the previously current path.
          if (tree.isPathSelected(newPath))
            tree.getSelectionModel().removeSelectionPath(currentPath);

          // This must be called in any case because it updates the model
          // lead selection index.
          tree.addSelectionPath(newPath);

          tree.setLeadSelectionPath(newPath);
        }
      else if (command.equals("selectNextChangeLead") && hasNext)
        {
          newPath = treeState.getPathForRow(nextRow);
          tree.setSelectionPath(newPath);
          tree.setAnchorSelectionPath(newPath);
          tree.setLeadSelectionPath(newPath);
        }
      
      // Ensure that the lead path is visible after the increment action.
      tree.scrollPathToVisible(tree.getLeadSelectionPath());
    }

    /**
     * Returns true if the action is enabled.
     * 
     * @return true if the action is enabled.
     */
    public boolean isEnabled()
    {
      return (tree != null) && tree.isEnabled();
    }
  }

  /**
   * Forwards all TreeModel events to the TreeState.
   */
  public class TreeModelHandler
      implements TreeModelListener
  {
    /**
     * Constructor
     */
    public TreeModelHandler()
    {
      // Nothing to do here.
    }

    /**
     * Invoked after a node (or a set of siblings) has changed in some way. The
     * node(s) have not changed locations in the tree or altered their children
     * arrays, but other attributes have changed and may affect presentation.
     * Example: the name of a file has changed, but it is in the same location
     * in the file system. To indicate the root has changed, childIndices and
     * children will be null. Use e.getPath() to get the parent of the changed
     * node(s). e.getChildIndices() returns the index(es) of the changed
     * node(s).
     * 
     * @param e is the event that occured
     */
    public void treeNodesChanged(TreeModelEvent e)
    {
      validCachedPreferredSize = false;
      treeState.treeNodesChanged(e);
      tree.repaint();
    }

    /**
     * Invoked after nodes have been inserted into the tree. Use e.getPath() to
     * get the parent of the new node(s). e.getChildIndices() returns the
     * index(es) of the new node(s) in ascending order.
     * 
     * @param e is the event that occured
     */
    public void treeNodesInserted(TreeModelEvent e)
    {
      validCachedPreferredSize = false;
      treeState.treeNodesInserted(e);
      tree.repaint();
    }

    /**
     * Invoked after nodes have been removed from the tree. Note that if a
     * subtree is removed from the tree, this method may only be invoked once
     * for the root of the removed subtree, not once for each individual set of
     * siblings removed. Use e.getPath() to get the former parent of the deleted
     * node(s). e.getChildIndices() returns, in ascending order, the index(es)
     * the node(s) had before being deleted.
     * 
     * @param e is the event that occured
     */
    public void treeNodesRemoved(TreeModelEvent e)
    {
      validCachedPreferredSize = false;
      treeState.treeNodesRemoved(e);
      tree.repaint();
    }

    /**
     * Invoked after the tree has drastically changed structure from a given
     * node down. If the path returned by e.getPath() is of length one and the
     * first element does not identify the current root node the first element
     * should become the new root of the tree. Use e.getPath() to get the path
     * to the node. e.getChildIndices() returns null.
     * 
     * @param e is the event that occured
     */
    public void treeStructureChanged(TreeModelEvent e)
    {
      if (e.getPath().length == 1
          && ! e.getPath()[0].equals(treeModel.getRoot()))
        tree.expandPath(new TreePath(treeModel.getRoot()));
      validCachedPreferredSize = false;
      treeState.treeStructureChanged(e);
      tree.repaint();
    }
  } // TreeModelHandler

  /**
   * TreePageAction handles page up and page down events.
   */
  public class TreePageAction
      extends AbstractAction
  {
    /** Specifies the direction to adjust the selection by. */
    protected int direction;

    /**
     * Constructor
     * 
     * @param direction up or down
     * @param name is the name of the direction
     */
    public TreePageAction(int direction, String name)
    {
      this.direction = direction;
      putValue(Action.NAME, name);
    }

    /**
     * Invoked when an action occurs.
     * 
     * @param e is the event that occured
     */
    public void actionPerformed(ActionEvent e)
    {
      String command = (String) getValue(Action.NAME);
      boolean extendSelection = command.equals("scrollUpExtendSelection")
                                || command.equals("scrollDownExtendSelection");
      boolean changeSelection = command.equals("scrollUpChangeSelection")
                                || command.equals("scrollDownChangeSelection");

      // Disable change lead, unless we are in discontinuous mode.
      if (!extendSelection && !changeSelection
          && tree.getSelectionModel().getSelectionMode() !=
            TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION)
        {
          changeSelection = true;
        }

      int rowCount = getRowCount(tree);
      if (rowCount > 0 && treeSelectionModel != null)
        {
          Dimension maxSize = tree.getSize();
          TreePath lead = tree.getLeadSelectionPath();
          TreePath newPath = null;
          Rectangle visible = tree.getVisibleRect();
          if (direction == -1) // The RI handles -1 as up.
            {
              newPath = getClosestPathForLocation(tree, visible.x, visible.y);
              if (newPath.equals(lead)) // Corner case, adjust one page up.
                {
                  visible.y = Math.max(0, visible.y - visible.height);
                  newPath = getClosestPathForLocation(tree, visible.x,
                                                      visible.y);
                }
            }
          else // +1 is down.
            {
              visible.y = Math.min(maxSize.height,
                                   visible.y + visible.height - 1);
              newPath = getClosestPathForLocation(tree, visible.x, visible.y);
              if (newPath.equals(lead)) // Corner case, adjust one page down.
                {
                  visible.y = Math.min(maxSize.height,
                                       visible.y + visible.height - 1);
                  newPath = getClosestPathForLocation(tree, visible.x,
                                                      visible.y);
                }
            }

          // Determine new visible rect.
          Rectangle newVisible = getPathBounds(tree, newPath);
          newVisible.x = visible.x;
          newVisible.width = visible.width;
          if (direction == -1)
            {
              newVisible.height = visible.height;
            }
          else
            {
              newVisible.y -= visible.height - newVisible.height;
              newVisible.height = visible.height;
            }

          if (extendSelection)
            {
              // Extend selection.
              TreePath anchorPath = tree.getAnchorSelectionPath();
              if (anchorPath == null)
                {
                  tree.setSelectionPath(newPath);
                }
              else
                {
                  int newIndex = getRowForPath(tree, newPath);
                  int anchorIndex = getRowForPath(tree, anchorPath);
                  tree.setSelectionInterval(Math.min(anchorIndex, newIndex),
                                            Math.max(anchorIndex, newIndex));
                  tree.setAnchorSelectionPath(anchorPath);
                  tree.setLeadSelectionPath(newPath);
                }
            }
          else if (changeSelection)
            {
              tree.setSelectionPath(newPath);
            }
          else // Change lead.
            {
              tree.setLeadSelectionPath(newPath);
            }

          tree.scrollRectToVisible(newVisible);
        }
    }

    /**
     * Returns true if the action is enabled.
     * 
     * @return true if the action is enabled.
     */
    public boolean isEnabled()
    {
      return (tree != null) && tree.isEnabled();
    }
  } // TreePageAction

  /**
   * Listens for changes in the selection model and updates the display
   * accordingly.
   */
  public class TreeSelectionHandler
      implements TreeSelectionListener
  {
    /**
     * Constructor
     */
    public TreeSelectionHandler()
    {
      // Nothing to do here.
    }

    /**
     * Messaged when the selection changes in the tree we're displaying for.
     * Stops editing, messages super and displays the changed paths.
     * 
     * @param event the event that characterizes the change.
     */
    public void valueChanged(TreeSelectionEvent event)
    {
      completeEditing();

      TreePath op = event.getOldLeadSelectionPath();
      TreePath np = event.getNewLeadSelectionPath();
      
      // Repaint of the changed lead selection path.
      if (op != np)
        {
          Rectangle o = treeState.getBounds(event.getOldLeadSelectionPath(), 
                                           new Rectangle());
          Rectangle n = treeState.getBounds(event.getNewLeadSelectionPath(), 
                                           new Rectangle());
          
          if (o != null)
            tree.repaint(o);
          if (n != null)
            tree.repaint(n);
        }
    }
  } // TreeSelectionHandler

  /**
   * For the first selected row expandedness will be toggled.
   */
  public class TreeToggleAction
      extends AbstractAction
  {
    /**
     * Creates a new TreeToggleAction.
     * 
     * @param name is the name of <code>Action</code> field
     */
    public TreeToggleAction(String name)
    {
      putValue(Action.NAME, name);
    }

    /**
     * Invoked when an action occurs.
     * 
     * @param e the event that occured
     */
    public void actionPerformed(ActionEvent e)
    {
      int selected = tree.getLeadSelectionRow();
      if (selected != -1 && isLeaf(selected))
        {
          TreePath anchorPath = tree.getAnchorSelectionPath();
          TreePath leadPath = tree.getLeadSelectionPath();
          toggleExpandState(getPathForRow(tree, selected));
          // Need to do this, so that the toggling doesn't mess up the lead
          // and anchor.
          tree.setLeadSelectionPath(leadPath);
          tree.setAnchorSelectionPath(anchorPath);

          // Ensure that the lead path is visible after the increment action.
          tree.scrollPathToVisible(tree.getLeadSelectionPath());
        }
    }

    /**
     * Returns true if the action is enabled.
     * 
     * @return true if the action is enabled, false otherwise
     */
    public boolean isEnabled()
    {
      return (tree != null) && tree.isEnabled();
    }
  } // TreeToggleAction

  /**
   * TreeTraverseAction is the action used for left/right keys. Will toggle the
   * expandedness of a node, as well as potentially incrementing the selection.
   */
  public class TreeTraverseAction
      extends AbstractAction
  {
    /**
     * Determines direction to traverse, 1 means expand, -1 means collapse.
     */
    protected int direction;

    /**
     * Constructor
     * 
     * @param direction to traverse
     * @param name is the name of the direction
     */
    public TreeTraverseAction(int direction, String name)
    {
      this.direction = direction;
      putValue(Action.NAME, name);
    }

    /**
     * Invoked when an action occurs.
     * 
     * @param e the event that occured
     */
    public void actionPerformed(ActionEvent e)
    {
      TreePath current = tree.getLeadSelectionPath();
      if (current == null)
        return;

      String command = (String) getValue(Action.NAME);
      if (command.equals("selectParent"))
        {
          if (current == null)
            return;

          if (tree.isExpanded(current))
            {
              tree.collapsePath(current);
            }
          else
            {
              // If the node is not expanded (also, if it is a leaf node),
              // we just select the parent. We do not select the root if it
              // is not visible.
              TreePath parent = current.getParentPath();
              if (parent != null && 
                  ! (parent.getPathCount() == 1 && ! tree.isRootVisible()))
                tree.setSelectionPath(parent);
            }
        }
      else if (command.equals("selectChild"))
        {
          Object node = current.getLastPathComponent();
          int nc = treeModel.getChildCount(node);
          if (nc == 0 || treeState.isExpanded(current))
            {
              // If the node is leaf or it is already expanded,
              // we just select the next row.
              int nextRow = tree.getLeadSelectionRow() + 1;
              if (nextRow <= tree.getRowCount())
                tree.setSelectionRow(nextRow);
            }
          else
            {
              tree.expandPath(current);
            }
        }
      
      // Ensure that the lead path is visible after the increment action.
      tree.scrollPathToVisible(tree.getLeadSelectionPath());
    }

    /**
     * Returns true if the action is enabled.
     * 
     * @return true if the action is enabled, false otherwise
     */
    public boolean isEnabled()
    {
      return (tree != null) && tree.isEnabled();
    }
  }

  /**
   * Returns true if the LookAndFeel implements the control icons. Package
   * private for use in inner classes.
   * 
   * @returns true if there are control icons
   */
  boolean hasControlIcons()
  {
    if (expandedIcon != null || collapsedIcon != null)
      return true;
    return false;
  }

  /**
   * Returns control icon. It is null if the LookAndFeel does not implements the
   * control icons. Package private for use in inner classes.
   * 
   * @return control icon if it exists.
   */
  Icon getCurrentControlIcon(TreePath path)
  {
    if (hasControlIcons())
      {
        if (tree.isExpanded(path))
          return expandedIcon;
        else
          return collapsedIcon;
      }
    else
      {
        if (nullIcon == null)
          nullIcon = new Icon()
          {
            public int getIconHeight()
            {
              return 0;
            }

            public int getIconWidth()
            {
              return 0;
            }

            public void paintIcon(Component c, Graphics g, int x, int y)
            {
              // No action here.
            }
          };
        return nullIcon;
      }
  }

  /**
   * Returns the parent of the current node
   * 
   * @param root is the root of the tree
   * @param node is the current node
   * @return is the parent of the current node
   */
  Object getParent(Object root, Object node)
  {
    if (root == null || node == null || root.equals(node))
      return null;

    if (node instanceof TreeNode)
      return ((TreeNode) node).getParent();
    return findNode(root, node);
  }

  /**
   * Recursively checks the tree for the specified node, starting at the root.
   * 
   * @param root is starting node to start searching at.
   * @param node is the node to search for
   * @return the parent node of node
   */
  private Object findNode(Object root, Object node)
  {
    if (! treeModel.isLeaf(root) && ! root.equals(node))
      {
        int size = treeModel.getChildCount(root);
        for (int j = 0; j < size; j++)
          {
            Object child = treeModel.getChild(root, j);
            if (node.equals(child))
              return root;

            Object n = findNode(child, node);
            if (n != null)
              return n;
          }
      }
    return null;
  }

  /**
   * Selects the specified path in the tree depending on modes. Package private
   * for use in inner classes.
   * 
   * @param tree is the tree we are selecting the path in
   * @param path is the path we are selecting
   */
  void selectPath(JTree tree, TreePath path)
  {
    if (path != null)
      {
        tree.setSelectionPath(path);
        tree.setLeadSelectionPath(path);        
        tree.makeVisible(path);
        tree.scrollPathToVisible(path);
      }
  }

  /**
   * Returns the path from node to the root. Package private for use in inner
   * classes.
   * 
   * @param node the node to get the path to
   * @param depth the depth of the tree to return a path for
   * @return an array of tree nodes that represent the path to node.
   */
  Object[] getPathToRoot(Object node, int depth)
  {
    if (node == null)
      {
        if (depth == 0)
          return null;

        return new Object[depth];
      }

    Object[] path = getPathToRoot(getParent(treeModel.getRoot(), node),
                                  depth + 1);
    path[path.length - depth - 1] = node;
    return path;
  }

  /**
   * Draws a vertical line using the given graphic context
   * 
   * @param g is the graphic context
   * @param c is the component the new line will belong to
   * @param x is the horizonal position
   * @param top specifies the top of the line
   * @param bottom specifies the bottom of the line
   */
  protected void paintVerticalLine(Graphics g, JComponent c, int x, int top,
                                   int bottom)
  {
    // FIXME: Check if drawing a dashed line or not.
    g.setColor(getHashColor());
    g.drawLine(x, top, x, bottom);
  }

  /**
   * Draws a horizontal line using the given graphic context
   * 
   * @param g is the graphic context
   * @param c is the component the new line will belong to
   * @param y is the vertical position
   * @param left specifies the left point of the line
   * @param right specifies the right point of the line
   */
  protected void paintHorizontalLine(Graphics g, JComponent c, int y, int left,
                                     int right)
  {
    // FIXME: Check if drawing a dashed line or not.
    g.setColor(getHashColor());
    g.drawLine(left, y, right, y);
  }

  /**
   * Draws an icon at around a specific position
   * 
   * @param c is the component the new line will belong to
   * @param g is the graphic context
   * @param icon is the icon which will be drawn
   * @param x is the center position in x-direction
   * @param y is the center position in y-direction
   */
  protected void drawCentered(Component c, Graphics g, Icon icon, int x, int y)
  {
    x -= icon.getIconWidth() / 2;
    y -= icon.getIconHeight() / 2;

    if (x < 0)
      x = 0;
    if (y < 0)
      y = 0;

    icon.paintIcon(c, g, x, y);
  }

  /**
   * Draws a dashed horizontal line.
   * 
   * @param g - the graphics configuration.
   * @param y - the y location to start drawing at
   * @param x1 - the x location to start drawing at
   * @param x2 - the x location to finish drawing at
   */
  protected void drawDashedHorizontalLine(Graphics g, int y, int x1, int x2)
  {
    g.setColor(getHashColor());
    for (int i = x1; i < x2; i += 2)
      g.drawLine(i, y, i + 1, y);
  }

  /**
   * Draws a dashed vertical line.
   * 
   * @param g - the graphics configuration.
   * @param x - the x location to start drawing at
   * @param y1 - the y location to start drawing at
   * @param y2 - the y location to finish drawing at
   */
  protected void drawDashedVerticalLine(Graphics g, int x, int y1, int y2)
  {
    g.setColor(getHashColor());
    for (int i = y1; i < y2; i += 2)
      g.drawLine(x, i, x, i + 1);
  }

  /**
   * Paints the expand (toggle) part of a row. The receiver should NOT modify
   * clipBounds, or insets.
   * 
   * @param g - the graphics configuration
   * @param clipBounds -
   * @param insets -
   * @param bounds - bounds of expand control
   * @param path - path to draw control for
   * @param row - row to draw control for
   * @param isExpanded - is the row expanded
   * @param hasBeenExpanded - has the row already been expanded
   * @param isLeaf - is the path a leaf
   */
  protected void paintExpandControl(Graphics g, Rectangle clipBounds,
                                    Insets insets, Rectangle bounds,
                                    TreePath path, int row, boolean isExpanded,
                                    boolean hasBeenExpanded, boolean isLeaf)
  {
    if (shouldPaintExpandControl(path, row, isExpanded, hasBeenExpanded, isLeaf))
      {
        Icon icon = getCurrentControlIcon(path);
        int iconW = icon.getIconWidth();
        int x = bounds.x - iconW - gap;
        icon.paintIcon(tree, g, x, bounds.y + bounds.height / 2
                                   - icon.getIconHeight() / 2);
      }
  }

  /**
   * Paints the horizontal part of the leg. The receiver should NOT modify
   * clipBounds, or insets. NOTE: parentRow can be -1 if the root is not
   * visible.
   * 
   * @param g - the graphics configuration
   * @param clipBounds -
   * @param insets -
   * @param bounds - bounds of the cell
   * @param path - path to draw leg for
   * @param row - row to start drawing at
   * @param isExpanded - is the row expanded
   * @param hasBeenExpanded - has the row already been expanded
   * @param isLeaf - is the path a leaf
   */
  protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds,
                                          Insets insets, Rectangle bounds,
                                          TreePath path, int row,
                                          boolean isExpanded,
                                          boolean hasBeenExpanded,
                                          boolean isLeaf)
  {
    if (row != 0)
      {
        paintHorizontalLine(g, tree, bounds.y + bounds.height / 2,
                            bounds.x - leftChildIndent - gap, bounds.x - gap);
      }
  }

  /**
   * Paints the vertical part of the leg. The receiver should NOT modify
   * clipBounds, insets.
   * 
   * @param g - the graphics configuration.
   * @param clipBounds -
   * @param insets -
   * @param path - the path to draw the vertical part for.
   */
  protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds,
                                        Insets insets, TreePath path)
  {
    Rectangle bounds = getPathBounds(tree, path);
    TreePath parent = path.getParentPath();
    
    boolean paintLine;
    if (isRootVisible())
      paintLine = parent != null;
    else
      paintLine = parent != null && parent.getPathCount() > 1;
    if (paintLine)
      {
        Rectangle parentBounds = getPathBounds(tree, parent);
        paintVerticalLine(g, tree, parentBounds.x + 2 * gap, 
                          parentBounds.y + parentBounds.height / 2,
                          bounds.y + bounds.height / 2);
      }
  }

  /**
   * Paints the renderer part of a row. The receiver should NOT modify
   * clipBounds, or insets.
   * 
   * @param g - the graphics configuration
   * @param clipBounds -
   * @param insets -
   * @param bounds - bounds of expand control
   * @param path - path to draw control for
   * @param row - row to draw control for
   * @param isExpanded - is the row expanded
   * @param hasBeenExpanded - has the row already been expanded
   * @param isLeaf - is the path a leaf
   */
  protected void paintRow(Graphics g, Rectangle clipBounds, Insets insets,
                          Rectangle bounds, TreePath path, int row,
                          boolean isExpanded, boolean hasBeenExpanded,
                          boolean isLeaf)
  {
    boolean selected = tree.isPathSelected(path);
    boolean hasIcons = false;
    Object node = path.getLastPathComponent();

    paintExpandControl(g, clipBounds, insets, bounds, path, row, isExpanded,
                       hasBeenExpanded, isLeaf);

    TreeCellRenderer dtcr = currentCellRenderer;

    boolean focused = false;
    if (treeSelectionModel != null)
      focused = treeSelectionModel.getLeadSelectionRow() == row
                && tree.isFocusOwner();

    Component c = dtcr.getTreeCellRendererComponent(tree, node, selected,
                                                    isExpanded, isLeaf, row,
                                                    focused);

    rendererPane.paintComponent(g, c, c.getParent(), bounds);
  }

  /**
   * Prepares for the UI to uninstall.
   */
  protected void prepareForUIUninstall()
  {
    // Nothing to do here yet.
  }

  /**
   * Returns true if the expand (toggle) control should be drawn for the
   * specified row.
   * 
   * @param path - current path to check for.
   * @param row - current row to check for.
   * @param isExpanded - true if the path is expanded
   * @param hasBeenExpanded - true if the path has been expanded already
   * @param isLeaf - true if the row is a lead
   */
  protected boolean shouldPaintExpandControl(TreePath path, int row,
                                             boolean isExpanded,
                                             boolean hasBeenExpanded,
                                             boolean isLeaf)
  {
    Object node = path.getLastPathComponent();
    return ! isLeaf && hasControlIcons();
  }

  /**
   * Returns the amount to indent the given row
   * 
   * @return amount to indent the given row.
   */
  protected int getRowX(int row, int depth)
  {
    return depth * totalChildIndent;
  }
} // BasicTreeUI