Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 81bf243ca34e0967425d65eefdf382fc79706687 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.dialogs;

import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.e4.core.commands.ECommandService;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.bindings.EBindingService;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.commands.MParameter;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar;
import org.eclipse.e4.ui.model.application.ui.basic.MTrimElement;
import org.eclipse.e4.ui.model.application.ui.menu.MHandledItem;
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
import org.eclipse.e4.ui.model.application.ui.menu.MItem;
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
import org.eclipse.e4.ui.model.application.ui.menu.MOpaqueMenuItem;
import org.eclipse.e4.ui.model.application.ui.menu.MOpaqueToolItem;
import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
import org.eclipse.e4.ui.model.application.ui.menu.MToolBarElement;
import org.eclipse.e4.ui.model.application.ui.menu.MToolBarSeparator;
import org.eclipse.e4.ui.workbench.IResourceUtilities;
import org.eclipse.e4.ui.workbench.renderers.swt.MenuManagerRenderer;
import org.eclipse.e4.ui.workbench.renderers.swt.ToolBarManagerRenderer;
import org.eclipse.e4.ui.workbench.swt.util.ISWTResourceUtilities;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.CoolBarManager;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IContributionManager;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.SubContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.bindings.Binding;
import org.eclipse.jface.bindings.BindingManager;
import org.eclipse.jface.bindings.TriggerSequence;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.internal.provisional.action.IToolBarContributionItem;
import org.eclipse.jface.internal.provisional.action.ToolBarContributionItem2;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ICheckStateProvider;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.window.ToolTip;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Resource;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.IActionBars2;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.OpenPerspectiveAction;
import org.eclipse.ui.activities.WorkbenchActivityHelper;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.internal.ActionSetActionBars;
import org.eclipse.ui.internal.ActionSetContributionItem;
import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
import org.eclipse.ui.internal.Perspective;
import org.eclipse.ui.internal.PluginActionCoolBarContributionItem;
import org.eclipse.ui.internal.PluginActionSet;
import org.eclipse.ui.internal.PluginActionSetBuilder;
import org.eclipse.ui.internal.WorkbenchMessages;
import org.eclipse.ui.internal.WorkbenchPage;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.internal.actions.NewWizardShortcutAction;
import org.eclipse.ui.internal.dialogs.TreeManager.CheckListener;
import org.eclipse.ui.internal.dialogs.TreeManager.TreeItem;
import org.eclipse.ui.internal.e4.compatibility.ModeledPageLayout;
import org.eclipse.ui.internal.intro.IIntroConstants;
import org.eclipse.ui.internal.keys.BindingService;
import org.eclipse.ui.internal.provisional.application.IActionBarConfigurer2;
import org.eclipse.ui.internal.registry.ActionSetDescriptor;
import org.eclipse.ui.internal.registry.ActionSetRegistry;
import org.eclipse.ui.internal.registry.IActionSetDescriptor;
import org.eclipse.ui.internal.util.BundleUtility;
import org.eclipse.ui.internal.util.Util;
import org.eclipse.ui.keys.IBindingService;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.model.WorkbenchViewerComparator;
import org.eclipse.ui.part.PageBook;
import org.eclipse.ui.services.IServiceLocator;
import org.eclipse.ui.views.IViewCategory;
import org.eclipse.ui.views.IViewDescriptor;
import org.eclipse.ui.views.IViewRegistry;
import org.eclipse.ui.wizards.IWizardCategory;
import org.eclipse.ui.wizards.IWizardDescriptor;

/**
 * Dialog to allow users the ability to customize the perspective. This includes
 * customizing menus and toolbars by adding, removing, or re-arranging commands
 * or groups of commands.
 * 
 */
public class CustomizePerspectiveDialog extends TrayDialog {

	private static final String TOOLBAR_ICON = "$nl$/icons/full/obj16/toolbar.gif"; //$NON-NLS-1$
	private static final String SUBMENU_ICON = "$nl$/icons/full/obj16/submenu.gif"; //$NON-NLS-1$
	private static final String MENU_ICON = "$nl$/icons/full/obj16/menu.gif"; //$NON-NLS-1$
	private static final String WARNING_ICON = "$nl$/icons/full/obj16/warn_tsk.gif"; //$NON-NLS-1$

	private static final String SHORTCUT_CONTRIBUTION_ITEM_ID_OPEN_PERSPECTIVE = "openPerspective"; //$NON-NLS-1$
	private static final String SHORTCUT_CONTRIBUTION_ITEM_ID_SHOW_VIEW = "showView"; //$NON-NLS-1$

	private static final String KEYS_PREFERENCE_PAGE_ID = "org.eclipse.ui.preferencePages.Keys"; //$NON-NLS-1$

	private static final String NEW_LINE = System.getProperty("line.separator"); //$NON-NLS-1$

	private static final int MIN_TOOLTIP_WIDTH = 160;

	private WorkbenchWindow window;

	private Perspective perspective;

	private TabFolder tabFolder;

	private final static int TAB_WIDTH_IN_DLUS = 490;

	private final static int TAB_HEIGHT_IN_DLUS = 230;

	private final String shortcutMenuColumnHeaders[] = {
			WorkbenchMessages.ActionSetSelection_menuColumnHeader,
			WorkbenchMessages.ActionSetSelection_descriptionColumnHeader };

	private int[] shortcutMenuColumnWidths = { 125, 300 };

	ImageDescriptor menuImageDescriptor = null;

	ImageDescriptor submenuImageDescriptor = null;

	ImageDescriptor toolbarImageDescriptor = null;

	ImageDescriptor warningImageDescriptor = null;

	private TreeManager treeManager;

	private DisplayItem menuItems;

	private DisplayItem toolBarItems;

	private Category shortcuts;

	private DisplayItem wizards;

	private DisplayItem perspectives;

	private DisplayItem views;

	private Map idToActionSet = new HashMap();

	private final List actionSets = new ArrayList();

	private IWorkbenchWindowConfigurer configurer;

	private TabItem actionSetTab;

	private CheckboxTableViewer actionSetAvailabilityTable;

	private CheckboxTreeViewer menuStructureViewer1;

	private CheckboxTreeViewer menuStructureViewer2;

	private CheckboxTreeViewer toolbarStructureViewer1;

	private CheckboxTreeViewer toolbarStructureViewer2;

	private Set toDispose;

	private CustomizeActionBars customizeActionBars;

	private Font tooltipHeading;
	MApplication application;
	private MenuManagerRenderer menuMngrRenderer;
	ToolBarManagerRenderer toolbarMngrRenderer;

	private ISWTResourceUtilities resUtils;
	private IEclipseContext context;

	/**
	 * A Listener for a list of command groups, that updates the viewer and
	 * filter who are dependent on the action set selection.
	 * 
	 * @since 3.5
	 */
	private static final class ActionSetSelectionChangedListener implements
			ISelectionChangedListener {
		private final TreeViewer filterViewer;
		private final ActionSetFilter filter;

		public ActionSetSelectionChangedListener(TreeViewer viewer,
				ActionSetFilter menuStructureFilterByActionSet) {
			this.filterViewer = viewer;
			this.filter = menuStructureFilterByActionSet;
		}

		public void selectionChanged(SelectionChangedEvent event) {
			Object element = ((IStructuredSelection) event.getSelection())
					.getFirstElement();
			filter.setActionSet((ActionSet) element);
			filterViewer.refresh();
			filterViewer.expandAll();
		}
	}

	/**
	 * A filter which will only show action sets which contribute items in the
	 * given tree structure.
	 * 
	 * @since 3.5
	 */
	private static final class ShowUsedActionSetsFilter extends ViewerFilter {
		private DisplayItem rootItem;

		public ShowUsedActionSetsFilter(DisplayItem rootItem) {
			this.rootItem = rootItem;
		}

		public boolean select(Viewer viewer, Object parentElement,
				Object element) {
			return (includeInSetStructure(rootItem, (ActionSet) element));
		}
	}

	/**
	 * Represents a menu item or a tool bar item.
	 * 
	 * @since 3.5
	 */
	private class DisplayItem extends TreeItem {
		/** The logic item represented */
		private IContributionItem item;

		/** The action set this item belongs to (optional) */
		private ActionSet actionSet;

		public DisplayItem(String label, IContributionItem item) {
			treeManager.super(label == null ? null : DialogUtil
					.removeAccel(removeShortcut(label)));
			this.item = item;
		}

		public void setActionSet(ActionSet actionSet) {
			this.actionSet = actionSet;
			if (actionSet != null)
				actionSet.addItem(this);
		}

		public ActionSet getActionSet() {
			return actionSet;
		}

		public IContributionItem getIContributionItem() {
			return item;
		}
	}

	/**
	 * Represents a menu item whose content is dynamic. Contains a list of the
	 * current items being displayed.
	 * 
	 * @since 3.5
	 */
	private class DynamicContributionItem extends DisplayItem {
		private List preview;

		public DynamicContributionItem(IContributionItem item) {
			super(WorkbenchMessages.HideItems_dynamicItemName, item);
			preview = new ArrayList();
		}

		public void addCurrentItem(MenuItem item) {
			preview.add(item);
		}

		public List getCurrentItems() {
			return preview;
		}
	}

	/**
	 * @param descriptor
	 * @param window
	 * @return the appropriate {@link IContributionItem} for the given wizard
	 */
	private static ActionContributionItem getIContributionItem(
			IWizardDescriptor descriptor, IWorkbenchWindow window) {
		IAction action = new NewWizardShortcutAction(window, descriptor);
		return new ActionContributionItem(action);
	}

	/**
	 * @param descriptor
	 * @param window
	 * @return the appropriate {@link IContributionItem} for the given
	 *         perspective
	 */
	private static ActionContributionItem getIContributionItem(
			IPerspectiveDescriptor descriptor, IWorkbenchWindow window) {
		IAction action = new OpenPerspectiveAction(window, descriptor, null);
		return new ActionContributionItem(action);
	}

	/**
	 * @param window
	 * @return the appropriate {@link IContributionItem} for showing views
	 */
	private static ActionContributionItem getIContributionItem(
			IWorkbenchWindow window) {
		IAction action = ActionFactory.SHOW_VIEW_MENU.create(window);
		return new ActionContributionItem(action);
	}

	/**
	 * Represents a menu item which needs to be shown in the Shortcuts tab.
	 * 
	 * @since 3.5
	 */
	private class ShortcutItem extends DisplayItem {
		/** The description to show in the table */
		private String description;

		/** The category this shortcut is in (should be set) */
		private Category category;

		private Object descriptor;

		public ShortcutItem(String label, IWizardDescriptor descriptor) {
			super(label, CustomizePerspectiveDialog.getIContributionItem(
					descriptor, window));
			this.descriptor = descriptor;
		}

		public ShortcutItem(String label, IPerspectiveDescriptor descriptor) {
			super(label, CustomizePerspectiveDialog.getIContributionItem(
					descriptor, window));
			this.descriptor = descriptor;
		}

		public ShortcutItem(String label, IViewDescriptor descriptor) {
			super(label, CustomizePerspectiveDialog
					.getIContributionItem(window));
			this.descriptor = descriptor;
		}

		public Object getDescriptor() {
			return descriptor;
		}

		public void setDescription(String description) {
			this.description = description;
		}

		public String getDescription() {
			return description;
		}

		public void setCategory(Category category) {
			this.category = category;
		}

		public Category getCategory() {
			return category;
		}
	}

	/**
	 * Represents a category in the shortcuts menu. Since categories can have a
	 * tree-structure, the functionality provided by the TreeManager and
	 * TreeItem classes is used, however the logic for visibility changes and
	 * gray states is more sophisticated.
	 * 
	 * @since 3.5
	 */
	private class Category extends TreeItem {

		/** ShortcutItems which are contributed in this Category */
		private List contributionItems;

		public Category(String label) {
			treeManager.super(label == null ? null : DialogUtil
					.removeAccel(removeShortcut(label)));
			this.contributionItems = new ArrayList();
		}

		public List getContributionItems() {
			return contributionItems;
		}

		/**
		 * Adds another ShortcutItem to this Category's list of ShortcutItems
		 * and creates a pseudo-child/parent relationship.
		 * 
		 * @param item
		 *            the item to add
		 */
		public void addShortcutItem(ShortcutItem item) {
			contributionItems.add(item);
			item.setCategory(this);
		}

		/**
		 * While the child/parent state in the Category hierarchy is
		 * automatically maintained, the pseudo-child/parent relationship must
		 * be explicitly updated. This method will update Categories if their
		 * states need to change as a result of their ShortcutItems.
		 */
		public void update() {
			for (Iterator i = contributionItems.iterator(); i.hasNext();) {
				DisplayItem item = (DisplayItem) i.next();
				if (item.getState()) {
					this.setCheckState(true);
					return;
				}
			}

			this.setCheckState(false);
		}

		/**
		 * Changes the state of all pseudo-descendant ShortcutItems, causing the
		 * effective state of this Category and all its sub-Categories to match.
		 * 
		 * @param state
		 *            The state to set this branch to.
		 */
		public void setItemsState(boolean state) {
			for (Iterator i = contributionItems.iterator(); i.hasNext();) {
				DisplayItem item = (DisplayItem) i.next();
				item.setCheckState(state);
			}
			for (Iterator i = getChildren().iterator(); i.hasNext();) {
				Category category = (Category) i.next();
				category.setItemsState(state);
			}
		}
	}

	/**
	 * Represents an action set, under which ContributionItems exist. There is
	 * no inherent hierarchy in action sets - they exist independent of one
	 * another, simply contribution menu items and tool bar items.
	 * 
	 * @since 3.5
	 */
	private class ActionSet {
		/** The descriptor which describes the action set represented */
		private ActionSetDescriptor descriptor;

		/** ContributionItems contributed by this action set */
		private List contributionItems;

		private boolean active;
		
		private boolean wasChanged = false;

		public ActionSet(ActionSetDescriptor descriptor, boolean active) {
			this.descriptor = descriptor;
			this.active = active;
			this.contributionItems = new ArrayList();
		}

		public void addItem(DisplayItem item) {
			contributionItems.add(item);
		}

		public String toString() {
			return descriptor.getLabel();
		}

		public boolean isActive() {
			return active;
		}

		public boolean wasChanged() {
			return wasChanged;
		}
		
		public void setActive(boolean active) {
			boolean wasActive = this.active;
			this.active = active;
			if (!active) {
				for (Iterator i = contributionItems.iterator(); i.hasNext();) {
					DisplayItem item = (DisplayItem) i.next();
					item.setCheckState(false);
				}
			}
			if (wasActive != active) {
				actionSetAvailabilityChanged();
			}
			
			wasChanged = true;
		}
	}

	/**
	 * A label provider to include the description field of ShortcutItems in the
	 * table.
	 * 
	 * @since 3.5
	 */
	private class ShortcutLabelProvider extends
			TreeManager.TreeItemLabelProvider implements ITableLabelProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			if (columnIndex == 0)
				return this.getImage(element);
			return null;
		}

		public String getColumnText(Object element, int columnIndex) {
			if (columnIndex == 1)
				return ((ShortcutItem) element).getDescription();
			return this.getText(element);
		}

		public void addListener(ILabelProviderListener listener) {
		}

		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		public void removeListener(ILabelProviderListener listener) {
		}
	}

	/**
	 * Provides the check logic for the categories viewer in the shortcuts tab.
	 * Categories have a dual concept of children - their proper children
	 * (sub-Categories, as in the wizards), and the actual elements they
	 * contribute to the menu system. The check state must take this into
	 * account.
	 * 
	 * @since 3.5
	 */
	private static class CategoryCheckProvider implements ICheckStateProvider {
		public boolean isChecked(Object element) {
			Category category = (Category) element;

			if (category.getChildren().isEmpty()
					&& category.getContributionItems().isEmpty())
				return false;

			// To be checked, any sub-Category can be checked.
			for (Iterator i = category.getChildren().iterator(); i.hasNext();) {
				Category child = (Category) i.next();
				if (isChecked(child))
					return true;
			}

			// To be checked, any ShortcutItem can be checked.
			for (Iterator i = category.getContributionItems().iterator(); i
					.hasNext();) {
				DisplayItem item = (DisplayItem) i.next();
				if (item.getState())
					return true;
			}

			return false;
		}

		public boolean isGrayed(Object element) {
			boolean hasChecked = false;
			boolean hasUnchecked = false;
			Category category = (Category) element;

			// Search in sub-Categories and ShortcutItems for one that is
			// checked and one that is unchecked.

			for (Iterator i = category.getChildren().iterator(); i.hasNext();) {
				Category child = (Category) i.next();
				if (isGrayed(child))
					return true;
				if (isChecked(child))
					hasChecked = true;
				else
					hasUnchecked = true;
				if (hasChecked && hasUnchecked)
					return true;
			}

			for (Iterator i = category.getContributionItems().iterator(); i
					.hasNext();) {
				DisplayItem item = (DisplayItem) i.next();
				if (item.getState())
					hasChecked = true;
				else
					hasUnchecked = true;
				if (hasChecked && hasUnchecked)
					return true;
			}

			return false;
		}
	}

	/**
	 * A tooltip which, given a model element, will display its icon (if there
	 * is one), name, and a description (if there is one).
	 * 
	 * @since 3.5
	 */
	private abstract class NameAndDescriptionToolTip extends ToolTip {
		public NameAndDescriptionToolTip(Control control, int style) {
			super(control, style, false);
		}

		protected abstract Object getModelElement(Event event);

		/**
		 * Adds logic to only show a tooltip if a meaningful item is under the
		 * cursor.
		 */
		protected boolean shouldCreateToolTip(Event event) {
			return super.shouldCreateToolTip(event)
					&& getModelElement(event) != null;
		}

		protected Composite createToolTipContentArea(Event event,
				Composite parent) {
			Object modelElement = getModelElement(event);

			Image iconImage = null;
			String nameString = null;

			if (modelElement instanceof DisplayItem) {
				iconImage = ((DisplayItem) modelElement).getImage();
				nameString = ((DisplayItem) modelElement).getLabel();
			} else if (modelElement instanceof ActionSet) {
				nameString = ((ActionSet) modelElement).descriptor.getLabel();
			}

			// Create the content area
			Composite composite = new Composite(parent, SWT.NONE);
			composite.setBackground(parent.getDisplay().getSystemColor(
					SWT.COLOR_INFO_BACKGROUND));
			composite.setLayout(new GridLayout(2, false));

			// The title area with the icon (if there is one) and label.
			Label title = createEntry(composite, iconImage, nameString);
			title.setFont(tooltipHeading);
			GridDataFactory.createFrom((GridData)title.getLayoutData())
				.hint(SWT.DEFAULT, SWT.DEFAULT)
				.minSize(MIN_TOOLTIP_WIDTH, 1)
				.applyTo(title);

			// The description (if there is one)
			String descriptionString = getDescription(modelElement);
			if (descriptionString != null) {
				createEntry(composite, null, descriptionString);
			}

			// Other Content to add
			addContent(composite, modelElement);

			return composite;
		}

		/**
		 * Adds a line of information to <code>parent</code>. If
		 * <code>icon</code> is not <code>null</code>, an icon is placed on the
		 * left, and then a label with <code>text</code>.
		 * 
		 * @param parent
		 *            the composite to add the entry to
		 * @param icon
		 *            the icon to place next to the text. <code>null</code> for
		 *            none.
		 * @param text
		 *            the text to display
		 * @return the created label
		 */
		protected Label createEntry(Composite parent, Image icon, String text) {
			Color fg = parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND);
			Color bg = parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
			if (icon != null) {
				Label iconLabel = new Label(parent, SWT.NONE);
				iconLabel.setImage(icon);
				iconLabel.setForeground(fg);
				iconLabel.setBackground(bg);
				iconLabel.setData(new GridData());
			}

			Label textLabel = new Label(parent, SWT.WRAP);
			
			if(icon == null) {
				GridDataFactory.generate(textLabel, 2, 1);
			} else {
				GridDataFactory.generate(textLabel, 1, 1);
			}
			
			textLabel.setText(text);
			textLabel.setForeground(fg);
			textLabel.setBackground(bg);
			return textLabel;
		}

		/**
		 * Adds a line of information to <code>parent</code>. If
		 * <code>icon</code> is not <code>null</code>, an icon is placed on the
		 * left, and then a label with <code>text</code>, which supports using
		 * anchor tags to creates links
		 * 
		 * @param parent
		 *            the composite to add the entry to
		 * @param icon
		 *            the icon to place next to the text. <code>null</code> for
		 *            none.
		 * @param text
		 *            the text to display
		 * @return the created link
		 */
		protected Link createEntryWithLink(Composite parent, Image icon,
				String text) {
			Color fg = parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND);
			Color bg = parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
			if (icon != null) {
				Label iconLabel = new Label(parent, SWT.NONE);
				iconLabel.setImage(icon);
				iconLabel.setForeground(fg);
				iconLabel.setBackground(bg);
				iconLabel.setData(new GridData());
			}
			
			Link textLink = new Link(parent, SWT.WRAP);

			if(icon == null) {
				GridDataFactory.generate(textLink, 2, 1);
			}
			
			textLink.setText(text);
			textLink.setForeground(fg);
			textLink.setBackground(bg);
			return textLink;
		}

		protected void addContent(Composite destination, Object modelElement) {
		}
	}

	/**
	 * A tooltip with useful information based on the type of ContributionItem
	 * the cursor hovers over in a Table.
	 * 
	 * @since 3.5
	 */
	private class TableToolTip extends NameAndDescriptionToolTip {
		private Table table;

		public TableToolTip(Table table) {
			super(table, RECREATE);
			this.table = table;
		}

		protected Object getModelElement(Event event) {
			TableItem tableItem = table.getItem(new Point(event.x, event.y));
			if (tableItem == null)
				return null;
			return tableItem.getData();
		}
	}

	/**
	 * A tooltip with useful information based on the type of ContributionItem
	 * the cursor hovers over in a Tree. In addition to the content provided by
	 * the {@link NameAndDescriptionToolTip} this includes action set
	 * information and key binding data.
	 * 
	 * @since 3.5
	 */
	private class ItemDetailToolTip extends NameAndDescriptionToolTip {
		private Tree tree;
		private boolean showActionSet;
		private boolean showKeyBindings;
		private ViewerFilter filter;
		private TreeViewer v;
		
		/**
		 * @param tree
		 *            The tree for the tooltip to hover over
		 */
		private ItemDetailToolTip(TreeViewer v, Tree tree, boolean showActionSet,
				boolean showKeyBindings, ViewerFilter filter) {
			super(tree,NO_RECREATE);
			this.tree = tree;
			this.v = v;
			this.showActionSet = showActionSet;
			this.showKeyBindings = showKeyBindings;
			this.filter = filter;
			this.setHideOnMouseDown(false);
		}
		
		public Point getLocation(Point tipSize, Event event) {
			// try to position the tooltip at the bottom of the cell
			ViewerCell cell = v.getCell(new Point(event.x, event.y));
			
			if( cell != null ) {
				return tree.toDisplay(event.x,cell.getBounds().y+cell.getBounds().height);
			}
			
			return super.getLocation(tipSize, event);
		}

		protected Object getToolTipArea(Event event) {
			// Ensure that the tooltip is hidden when the cell is left
			return v.getCell(new Point(event.x, event.y));
		}
		
		protected void addContent(Composite destination, Object modelElement) {
			final DisplayItem item = (DisplayItem) modelElement;

			// Show any relevant action set info
			if (showActionSet) {
				String text = null;
				Image image = null;
				
				if(isEffectivelyAvailable(item, filter)) {
					if(item.actionSet != null) {
						//give information on which command group the item is in
						
						final String actionSetName = item.getActionSet().descriptor
								.getLabel();
						
						text = NLS.bind(
								WorkbenchMessages.HideItems_itemInActionSet,
								actionSetName);
					}
				} else {
					//give feedback on why item is unavailable
					
					image = warningImageDescriptor.createImage();
					
					if (item.getChildren().isEmpty() && item.getActionSet() != null) {
						//i.e. is a leaf
						
						final String actionSetName = item.getActionSet().
								descriptor.getLabel();
						
						text = NLS.bind(
								WorkbenchMessages.HideItems_itemInUnavailableActionSet,
								actionSetName);
						
					} else {
						//i.e. has children

						Set actionGroup = new LinkedHashSet();
						collectDescendantCommandGroups(actionGroup, item,
								filter);
						
						if (actionGroup.size() == 1) {
							//i.e. only one child
							ActionSet actionSet = (ActionSet) actionGroup.
									iterator().next();
							text = NLS.bind(
									WorkbenchMessages.HideItems_unavailableChildCommandGroup,
									actionSet.descriptor.getId(),
									actionSet.descriptor.getLabel());
						} else {
							//i.e. multiple children
							String commandGroupList = null;
		
							for (Iterator i = actionGroup.iterator(); i.hasNext();) {
								ActionSet actionSet = (ActionSet) i.next();
		
								// For each action set, make a link for it, set
								// the href to its id
								String commandGroupLink = MessageFormat.format(
										"<a href=\"{0}\">{1}</a>", //$NON-NLS-1$
										new Object[] { actionSet.descriptor.getId(),
												actionSet.descriptor.getLabel() });
		
								if (commandGroupList == null)
									commandGroupList = commandGroupLink;
								else
									commandGroupList = Util.createList(
											commandGroupList, commandGroupLink);
							}
							
							commandGroupList = NLS.bind(
									"{0}{1}", new Object[] { NEW_LINE, commandGroupList }); //$NON-NLS-1$
							text = NLS.bind(
									WorkbenchMessages.HideItems_unavailableChildCommandGroups,
									commandGroupList);
						}
					}
				}
				
				if(text != null) {
					Link link = createEntryWithLink(destination, image, text);
					link.addSelectionListener(new SelectionListener() {
						public void widgetDefaultSelected(SelectionEvent e) {
							widgetSelected(e);
						}
	
						public void widgetSelected(SelectionEvent e) {
							ActionSet actionSet = (ActionSet) idToActionSet
									.get(e.text);
							if (actionSet == null) {
								hide();
								viewActionSet(item);
							} else {
								hide();
								viewActionSet(actionSet);
							}
						}
					});
				}
			}

			// Show key binding info
			if (showKeyBindings && getCommandID(item) != null) {
				// See if there is a command associated with the command id
				ICommandService commandService = (ICommandService) window
						.getService(ICommandService.class);
				Command command = commandService.getCommand(getCommandID(item));

				if (command != null && command.isDefined()) {
					// Find the bindings and list them as a string
					Binding[] bindings = getKeyBindings(item);
					String keybindings = keyBindingsAsString(bindings);

					String text = null;

					// Is it possible for this item to be visible?
					final boolean available = (item.getActionSet() == null)
							|| (item.getActionSet().isActive());

					if (bindings.length > 0) {
						if (available)
							text = NLS.bind(
									WorkbenchMessages.HideItems_keyBindings,
									keybindings);
						else
							text = NLS
									.bind(
											WorkbenchMessages.HideItems_keyBindingsActionSetUnavailable,
											keybindings);
					} else {
						if (available)
							text = WorkbenchMessages.HideItems_noKeyBindings;
						else
							text = WorkbenchMessages.HideItems_noKeyBindingsActionSetUnavailable;
					}

					// Construct link to go to the preferences page for key
					// bindings
					final Object highlight;
					if (bindings.length == 0) {
						Map parameters = new HashMap();

						// If item is a shortcut, need to add a parameter to go
						// to
						// the correct item
						if (item instanceof ShortcutItem) {
							if (isNewWizard(item)) {
								parameters.put(
												IWorkbenchCommandConstants.FILE_NEW_PARM_WIZARDID,
										getParamID(item));
							} else if (isShowPerspective(item)) {
								parameters
										.put(
												IWorkbenchCommandConstants.PERSPECTIVES_SHOW_PERSPECTIVE_PARM_ID,
												getParamID(item));
							} else if (isShowView(item)) {
								parameters.put(
										IWorkbenchCommandConstants.VIEWS_SHOW_VIEW_PARM_ID,
										getParamID(item));
							}
						}

						ParameterizedCommand pc = ParameterizedCommand
								.generateCommand(command, parameters);
						highlight = pc;
					} else {
						highlight = bindings[0];
					}

					Link bindingLink = createEntryWithLink(destination, null,
							text);

					bindingLink.addSelectionListener(new SelectionListener() {
						public void widgetDefaultSelected(SelectionEvent e) {
							widgetDefaultSelected(e);
						}

						public void widgetSelected(SelectionEvent e) {
							PreferenceDialog dialog = PreferencesUtil
									.createPreferenceDialogOn(getShell(),
											KEYS_PREFERENCE_PAGE_ID,
											new String[0], highlight);
							hide();
							dialog.open();
						}
					});
				}
			}

			// Show dynamic menu item info
			if (item instanceof DynamicContributionItem) {
				DynamicContributionItem dynamic = ((DynamicContributionItem) item);
				StringBuffer text = new StringBuffer();
				final List currentItems = dynamic.getCurrentItems();

				if (currentItems.size() > 0) {
					// Create a list of the currently displayed items
					text.append(WorkbenchMessages.HideItems_dynamicItemList);
					for (Iterator i = currentItems.iterator(); i.hasNext();) {
						MenuItem menuItem = (MenuItem) i.next();
						text.append(NEW_LINE).append("- ") //$NON-NLS-1$
								.append(menuItem.getText());
					}
				} else {
					text
							.append(WorkbenchMessages.HideItems_dynamicItemEmptyList);
				}
				createEntry(destination, null, text.toString());
			}
		}

		protected Object getModelElement(Event event) {
			org.eclipse.swt.widgets.TreeItem treeItem = tree.getItem(new Point(
					event.x, event.y));
			if (treeItem == null)
				return null;
			return treeItem.getData();
		}
	}

	/**
	 * Filters out contribution items which are not in a given action set.
	 * 
	 * @since 3.5
	 */
	private static class ActionSetFilter extends ViewerFilter {
		private ActionSet actionSet;

		public void setActionSet(ActionSet actionSet) {
			this.actionSet = actionSet;
		}

		public boolean select(Viewer viewer, Object parentElement,
				Object element) {
			if (!(element instanceof DisplayItem))
				return false;
			if (actionSet == null)
				return false;
			return includeInSetStructure((DisplayItem) element, actionSet);
		}
	}

	/**
	 * A check provider which calculates checked state based on leaf states in
	 * the tree (as opposed to children in a model).
	 * 
	 * @since 3.5
	 */
	private static class FilteredTreeCheckProvider implements
			ICheckStateProvider {
		private ITreeContentProvider contentProvider;
		private ViewerFilter filter;

		public FilteredTreeCheckProvider(ITreeContentProvider contentProvider,
				ViewerFilter filter) {
			this.contentProvider = contentProvider;
			this.filter = filter;
		}

		public boolean isChecked(Object element) {
			TreeItem treeItem = (TreeItem) element;
			return getLeafStates(treeItem, contentProvider, filter) != TreeManager.CHECKSTATE_UNCHECKED;
		}

		public boolean isGrayed(Object element) {
			TreeItem treeItem = (TreeItem) element;
			return getLeafStates(treeItem, contentProvider, filter) == TreeManager.CHECKSTATE_GRAY;
		}
	}

	/**
	 * A check listener to bring about the expected change in a model based on a
	 * check event in a filtered viewer. Since the checked state of a parent in
	 * a filtered viewer is not based on its model state, but rather its leafs'
	 * states, when a non-leaf element's check state changes, its model state
	 * does not necessarily change, but its leafs' model states do.
	 * 
	 * @since 3.5
	 */
	private static class FilteredViewerCheckListener implements
			ICheckStateListener {
		private ITreeContentProvider contentProvider;
		private ViewerFilter filter;

		public FilteredViewerCheckListener(
				ITreeContentProvider contentProvider, ViewerFilter filter) {
			this.contentProvider = contentProvider;
			this.filter = filter;
		}

		public void checkStateChanged(CheckStateChangedEvent event) {
			setAllLeafs((DisplayItem) event.getElement(), event
					.getChecked(), contentProvider, filter);
		}
	}

	/**
	 * On a model change, update a filtered listener. While the check listener
	 * provided by the model will take care of the elements which change, since
	 * we simulate our own check state of parents, the parents may need to be
	 * updated.
	 * 
	 * @since 3.5
	 */
	private final class FilteredModelCheckListener implements CheckListener {
		private final ActionSetFilter filter;
		private final StructuredViewer viewer;

		private FilteredModelCheckListener(ActionSetFilter filter,
				StructuredViewer viewer) {
			this.filter = filter;
			this.viewer = viewer;
		}

		public void checkChanged(TreeItem changedItem) {
			TreeItem item = changedItem;
			boolean update = false;

			// Force an update on all parents.
			while (item != null) {
				update = update || filter.select(null, null, item);
				if (update) {
					viewer.update(item, null);
				}
				item = item.getParent();
			}
		}
	}

	/**
	 * A check listener which, upon changing the check state of a contribution
	 * item, checks if that item is eligible to be checked (i.e. it is in an
	 * available action set), and if not, informs the user of the illegal
	 * operation. If the operation is legal, the event is forwarded to the check
	 * listener to actually perform a useful action.
	 * 
	 * @since 3.5
	 */
	private class UnavailableContributionItemCheckListener implements
			ICheckStateListener {
		private CheckboxTreeViewer viewer;
		private ICheckStateListener originalListener;

		/**
		 * @param viewer
		 *            the viewer being listened to
		 * @param originalListener
		 *            the listener to invoke upon a legal action
		 */
		public UnavailableContributionItemCheckListener(
				CheckboxTreeViewer viewer, ICheckStateListener originalListener) {
			this.viewer = viewer;
			this.originalListener = originalListener;
		}

		public void checkStateChanged(CheckStateChangedEvent event) {
			DisplayItem item = (DisplayItem) event.getElement();
			ViewerFilter[] filters = viewer.getFilters();
			boolean isEffectivelyAvailable = isEffectivelyAvailable(item, filters.length > 0 ? filters[0] : null);

			if (isEffectivelyAvailable) {
				// legal action - invoke the listener which will do actual work
				originalListener.checkStateChanged(event);
				return;
			}

			boolean isAvailable = isAvailable(item);
			viewer.update(event.getElement(), null);

			if (isAvailable) {
				// the case where this item is unavailable because of its
				// children
				if (viewer.getExpandedState(item)) {
					MessageBox mb = new MessageBox(viewer.getControl()
							.getShell(), SWT.OK | SWT.ICON_WARNING | SWT.SHEET);
					mb
							.setText(WorkbenchMessages.HideItemsCannotMakeVisible_dialogTitle);
					mb
							.setMessage(NLS
									.bind(
											WorkbenchMessages.HideItemsCannotMakeVisible_unavailableChildrenText,
											item.getLabel()));
					mb.open();
				} else {
					MessageBox mb = new MessageBox(viewer.getControl()
							.getShell(), SWT.OK | SWT.ICON_WARNING | SWT.SHEET);
					mb
							.setText(WorkbenchMessages.HideItemsCannotMakeVisible_dialogTitle);
					mb
							.setMessage(NLS
									.bind(
											WorkbenchMessages.HideItemsCannotMakeVisible_unavailableChildrenText,
											item.getLabel()));
					mb.open();
				}
			} else {
				// the case where this item is unavailable because it belongs to
				// an unavailable action set
				MessageBox mb = new MessageBox(viewer.getControl().getShell(),
						SWT.YES | SWT.NO | SWT.ICON_WARNING | SWT.SHEET);
				mb
						.setText(WorkbenchMessages.HideItemsCannotMakeVisible_dialogTitle);
				final String errorExplanation = NLS
						.bind(
								WorkbenchMessages.HideItemsCannotMakeVisible_unavailableCommandGroupText,
								item.getLabel(), item.getActionSet());
				final String message = NLS
						.bind(
								"{0}{1}{1}{2}", new Object[] { errorExplanation, NEW_LINE, WorkbenchMessages.HideItemsCannotMakeVisible_switchToCommandGroupTab }); //$NON-NLS-1$
				mb.setMessage(message);
				if (mb.open() == SWT.YES) {
					viewActionSet(item);
				}
			}
		}
	}

	/**
	 * A label provider which takes the default label provider in the
	 * TreeManager, and adds on functionality to gray out text and icons of
	 * contribution items whose action sets are unavailable.
	 * 
	 * @since 3.5
	 * 
	 */
	private class GrayOutUnavailableLabelProvider extends
			TreeManager.TreeItemLabelProvider implements IColorProvider {
		private Display display;
		private ViewerFilter filter;

		public GrayOutUnavailableLabelProvider(Display display, ViewerFilter filter) {
			this.display = display;
			this.filter = filter;
		}

		public Color getBackground(Object element) {
			return null;
		}

		public Color getForeground(Object element) {
			if (!isEffectivelyAvailable((DisplayItem) element, filter)) {
				return display.getSystemColor(SWT.COLOR_GRAY);
			}
			return null;
		}

		public Image getImage(Object element) {
			Image actual = super.getImage(element);

			if (element instanceof DisplayItem && actual != null) {
				DisplayItem item = (DisplayItem) element;
				if (!isEffectivelyAvailable(item, filter)) {
					ImageDescriptor original = ImageDescriptor
							.createFromImage(actual);
					ImageDescriptor disable = ImageDescriptor.createWithFlags(
							original, SWT.IMAGE_DISABLE);
					Image newImage = disable.createImage();
					toDispose.add(newImage);
					return newImage;
				}
			}

			return actual;
		}
	}

	/**
	 * The proxy IActionBarConfigurer that gets passed to the application's
	 * ActionBarAdvisor. This is used to construct a representation of the
	 * window's hardwired menus and toolbars in order to display their structure
	 * properly in the preview panes.
	 * 
	 * @since 3.5
	 */
	public class CustomizeActionBars implements IActionBarConfigurer2,
			IActionBars2 {

		IWorkbenchWindowConfigurer configurer;

		/**
		 * Fake action bars to use to build the menus and toolbar contributions
		 * for the workbench. We cannot use the actual workbench action bars,
		 * since doing so would make the action set items visible.
		 */
		private MenuManager menuManager = new MenuManager();
		private CoolBarManager coolBarManager = new CoolBarManager();
		private StatusLineManager statusLineManager = new StatusLineManager();

		/**
		 * Create a new instance of this class.
		 * 
		 * @param configurer
		 *            the configurer
		 */
		public CustomizeActionBars(IWorkbenchWindowConfigurer configurer) {
			this.configurer = configurer;
		}

		public IWorkbenchWindowConfigurer getWindowConfigurer() {
			return configurer;
		}

		public IMenuManager getMenuManager() {
			return menuManager;
		}

		public IStatusLineManager getStatusLineManager() {
			return statusLineManager;
		}

		public ICoolBarManager getCoolBarManager() {
			return coolBarManager;
		}

		public IToolBarManager getToolBarManager() {
			return null;
		}

		public void setGlobalActionHandler(String actionID, IAction handler) {
		}

		public void updateActionBars() {
		}

		public void clearGlobalActionHandlers() {
		}

		public IAction getGlobalActionHandler(String actionId) {
			return null;
		}

		public void registerGlobalAction(IAction action) {
		}

		/**
		 * Clean up the action bars.
		 */
		public void dispose() {
			coolBarManager.dispose();
			menuManager.dispose();
			statusLineManager.dispose();
		}

		public final IServiceLocator getServiceLocator() {
			return configurer.getWindow();
		}

		public IToolBarContributionItem createToolBarContributionItem(
				IToolBarManager toolBarManager, String id) {
			return new ToolBarContributionItem2(toolBarManager, id);
		}

		public IToolBarManager createToolBarManager() {
			return new ToolBarManager();
		}
	}

	/**
	 * Create an instance of this Dialog.
	 * 
	 * @param configurer
	 *            the configurer
	 * @param persp
	 *            the perspective
	 * @param context
	 *            The runtime context for this window
	 */
	public CustomizePerspectiveDialog(IWorkbenchWindowConfigurer configurer, Perspective persp,
			IEclipseContext context) {
		super(configurer.getWindow().getShell());
		this.treeManager = new TreeManager();
		this.configurer = configurer;
		this.context = context;
		perspective = persp;
		window = (WorkbenchWindow) configurer.getWindow();
		application = context.get(MApplication.class);
		menuMngrRenderer = context.get(MenuManagerRenderer.class);
		toolbarMngrRenderer = context.get(ToolBarManagerRenderer.class);
		resUtils = (ISWTResourceUtilities) context.get(IResourceUtilities.class);

		toDispose = new HashSet();

		initializeIcons();

		initializeActionSetInput();
		loadMenuAndToolbarStructure();
	}

	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		String title = perspective.getDesc().getLabel();

		title = NLS.bind(WorkbenchMessages.ActionSetSelection_customize, title);
		shell.setText(title);
		window.getWorkbench().getHelpSystem().setHelp(shell,
				IWorkbenchHelpContextIds.ACTION_SET_SELECTION_DIALOG);
	}

	protected Control createDialogArea(Composite parent) {
		// Create a font for titles in the tooltips
		FontData[] defaultFont = JFaceResources.getDefaultFont().getFontData();
		FontData boldFontData = new FontData(defaultFont[0].getName(),
				defaultFont[0].getHeight(), SWT.BOLD);
		tooltipHeading = new Font(parent.getDisplay(), boldFontData);

		Composite composite = (Composite) super.createDialogArea(parent);

		// tab folder
		tabFolder = new TabFolder(composite, SWT.NONE);

		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd.widthHint = convertHorizontalDLUsToPixels(TAB_WIDTH_IN_DLUS);
		gd.heightHint = convertVerticalDLUsToPixels(TAB_HEIGHT_IN_DLUS);
		tabFolder.setLayoutData(gd);

		// Tool Bar Item Hiding Page
		TabItem tab = new TabItem(tabFolder, SWT.NONE);
		tab.setText(WorkbenchMessages.HideToolBarItems_toolBarItemsTab);
		tab.setControl(createToolBarVisibilityPage(tabFolder));

		// Menu Item Hiding Page
		tab = new TabItem(tabFolder, SWT.NONE);
		tab.setControl(createMenuVisibilityPage(tabFolder));
		tab.setText(WorkbenchMessages.HideMenuItems_menuItemsTab);

		// Action Set Availability Page
		actionSetTab = new TabItem(tabFolder, SWT.NONE);
		actionSetTab
				.setText(WorkbenchMessages.ActionSetSelection_actionSetsTab);
		actionSetTab.setControl(createActionSetAvailabilityPage(tabFolder));

		// Shortcuts Page
		if (showShortcutTab()) {
			TabItem item1 = new TabItem(tabFolder, SWT.NONE);
			item1.setText(WorkbenchMessages.Shortcuts_shortcutTab);
			item1.setControl(createShortCutsPage(tabFolder));
		}

		applyDialogFont(tabFolder);

		return composite;
	}

	private Composite createShortCutsPage(Composite parent) {
		GridData data;

		Composite menusComposite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		menusComposite.setLayout(layout);

		// Select... label
		Label label = new Label(menusComposite, SWT.WRAP);
		label.setText(NLS.bind(
				WorkbenchMessages.Shortcuts_selectShortcutsLabel, perspective
						.getDesc().getLabel()));
		data = new GridData(SWT.FILL, SWT.CENTER, true, false);
		label.setLayoutData(data);

		Label sep = new Label(menusComposite, SWT.HORIZONTAL | SWT.SEPARATOR);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		SashForm sashComposite = new SashForm(menusComposite, SWT.HORIZONTAL);
		data = new GridData(SWT.FILL, SWT.FILL, true, true);
		sashComposite.setLayoutData(data);

		// Menus List
		Composite menusGroup = new Composite(sashComposite, SWT.NONE);
		layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		menusGroup.setLayout(layout);
		menusGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		label = new Label(menusGroup, SWT.WRAP);
		label.setText(WorkbenchMessages.Shortcuts_availableMenus);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		Combo menusCombo = new Combo(menusGroup, SWT.READ_ONLY);
		menusCombo
				.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		ComboViewer menusViewer = new ComboViewer(menusCombo);
		menusViewer.setContentProvider(TreeManager.getTreeContentProvider());
		menusViewer.setLabelProvider(TreeManager.getLabelProvider());

		// Categories Tree
		label = new Label(menusGroup, SWT.WRAP);
		label.setText(WorkbenchMessages.Shortcuts_availableCategories);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		final CheckboxTreeViewer menuCategoriesViewer = new CheckboxTreeViewer(
				menusGroup);
		menuCategoriesViewer.getControl().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true));
		menuCategoriesViewer.setLabelProvider(TreeManager.getLabelProvider());
		menuCategoriesViewer.setContentProvider(TreeManager
				.getTreeContentProvider());
		menuCategoriesViewer.setComparator(new WorkbenchViewerComparator());
		menuCategoriesViewer.setCheckStateProvider(new CategoryCheckProvider());
		menuCategoriesViewer.addCheckStateListener(new ICheckStateListener() {
			public void checkStateChanged(CheckStateChangedEvent event) {
				Category category = (Category) event.getElement();
				category.setItemsState(event.getChecked());
				updateCategoryAndParents(menuCategoriesViewer, category);
			}
		});

		treeManager.addListener(new CheckListener() {
			public void checkChanged(TreeItem changedItem) {
				if (changedItem instanceof Category) {
					menuCategoriesViewer.update(changedItem, null);
				} else if (changedItem instanceof ShortcutItem) {
					ShortcutItem item = (ShortcutItem) changedItem;
					if (item.getCategory() != null) {
						item.getCategory().update();
						updateCategoryAndParents(menuCategoriesViewer, item
								.getCategory());
					}
				}
			}
		});

		// Menu items list
		Composite menuItemsGroup = new Composite(sashComposite, SWT.NONE);
		layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		menuItemsGroup.setLayout(layout);
		menuItemsGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));

		label = new Label(menuItemsGroup, SWT.WRAP);
		label.setText(WorkbenchMessages.Shortcuts_allShortcuts);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		final CheckboxTableViewer menuItemsViewer = CheckboxTableViewer
				.newCheckList(menuItemsGroup, SWT.BORDER | SWT.H_SCROLL
						| SWT.V_SCROLL);
		Table menuTable = menuItemsViewer.getTable();
		menuTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		menuItemsViewer.setLabelProvider(new ShortcutLabelProvider());
		menuItemsViewer.setCheckStateProvider(TreeManager
				.getCheckStateProvider());
		menuItemsViewer.addCheckStateListener(treeManager
				.getViewerCheckStateListener());
		treeManager.getCheckListener(menuItemsViewer);

		menuItemsViewer
				.setContentProvider(new TreeManager.TreeItemContentProvider() {
					public Object[] getChildren(Object parentElement) {
						if (parentElement instanceof Category)
							return ((Category) parentElement)
									.getContributionItems().toArray();
						return super.getChildren(parentElement);
					}
				});
		menuItemsViewer.setComparator(new WorkbenchViewerComparator());

		// update menuCategoriesViewer, and menuItemsViewer on a change to
		// menusViewer
		menusViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					public void selectionChanged(SelectionChangedEvent event) {
						Category category = (Category) ((IStructuredSelection) event
								.getSelection()).getFirstElement();
						menuCategoriesViewer.setInput(category);
						menuItemsViewer.setInput(category);
						if (category.getChildren().size() != 0) {
							setSelectionOn(menuCategoriesViewer, category
									.getChildren().get(0));
						}
					}
				});

		// update menuItemsViewer on a change to menuCategoriesViewer
		menuCategoriesViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					public void selectionChanged(SelectionChangedEvent event) {
						Category category = (Category) ((IStructuredSelection) event
								.getSelection()).getFirstElement();
						menuItemsViewer.setInput(category);
					}
				});

		menuTable.setHeaderVisible(true);
		int[] columnWidths = new int[shortcutMenuColumnWidths.length];
		for (int i = 0; i < shortcutMenuColumnWidths.length; i++) {
			columnWidths[i] = convertHorizontalDLUsToPixels(shortcutMenuColumnWidths[i]);
		}
		for (int i = 0; i < shortcutMenuColumnHeaders.length; i++) {
			TableColumn tc = new TableColumn(menuTable, SWT.NONE, i);
			tc.setResizable(true);
			tc.setText(shortcutMenuColumnHeaders[i]);
			tc.setWidth(columnWidths[i]);
		}
		sashComposite.setWeights(new int[] { 30, 70 });

		menusViewer.setInput(shortcuts);

		if (shortcuts.getChildren().size() > 0) {
			setSelectionOn(menusViewer, shortcuts.getChildren().get(0));
		}

		return menusComposite;
	}

	private Composite createActionSetAvailabilityPage(Composite parent) {
		GridData data;

		Composite actionSetsComposite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		actionSetsComposite.setLayout(layout);

		// Select... label
		Label label = new Label(actionSetsComposite, SWT.WRAP);
		label.setText(NLS.bind(
				WorkbenchMessages.ActionSetSelection_selectActionSetsLabel,
				perspective.getDesc().getLabel()));
		data = new GridData(SWT.FILL, SWT.CENTER, true, false);
		label.setLayoutData(data);

		Label sep = new Label(actionSetsComposite, SWT.HORIZONTAL
				| SWT.SEPARATOR);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		SashForm sashComposite = new SashForm(actionSetsComposite,
				SWT.HORIZONTAL);
		data = new GridData(SWT.FILL, SWT.FILL, true, true);
		sashComposite.setLayoutData(data);

		// Action Set List Composite
		Composite actionSetGroup = new Composite(sashComposite, SWT.NONE);
		layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		actionSetGroup.setLayout(layout);
		actionSetGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));

		label = new Label(actionSetGroup, SWT.WRAP);
		label.setText(WorkbenchMessages.ActionSetSelection_availableActionSets);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		final CheckboxTableViewer actionSetsViewer = CheckboxTableViewer
				.newCheckList(actionSetGroup, SWT.BORDER | SWT.H_SCROLL
						| SWT.V_SCROLL);
		actionSetAvailabilityTable = actionSetsViewer;
		actionSetsViewer.getTable().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true));
		actionSetsViewer.setLabelProvider(new LabelProvider());
		actionSetsViewer.setContentProvider(new ArrayContentProvider());
		actionSetsViewer.setComparator(new WorkbenchViewerComparator());
		actionSetsViewer.setCheckStateProvider(new ICheckStateProvider() {
			public boolean isChecked(Object element) {
				return ((ActionSet) element).isActive();
			}

			public boolean isGrayed(Object element) {
				return false;
			}
		});
		actionSetsViewer.setInput(actionSets.toArray());

		Table table = actionSetsViewer.getTable();
		new TableToolTip(table);

		final ActionSet[] selectedActionSet = { null };

		// Filter to show only branches necessary for the selected action set.
		final ViewerFilter setFilter = new ViewerFilter() {
			public boolean select(Viewer viewer, Object parentElement,
					Object element) {
				if (selectedActionSet[0] == null)
					return false;
				return includeInSetStructure((DisplayItem) element,
						selectedActionSet[0]);
			}
		};

		// Updates the check state of action sets
		actionSetsViewer.addCheckStateListener(new ICheckStateListener() {
			public void checkStateChanged(CheckStateChangedEvent event) {
				final ActionSet actionSet = (ActionSet) event.getElement();
				if (event.getChecked()) {
					actionSet.setActive(true);
					for (Iterator i = actionSet.contributionItems.iterator(); i
							.hasNext();) {
						DisplayItem item = (DisplayItem) i.next();
						item.setCheckState(true);
					}
				} else {
					actionSet.setActive(false);
				}
			}
		});

		// Menu and toolbar composite
		Composite actionGroup = new Composite(sashComposite, SWT.NONE);
		layout = new GridLayout();
		layout.numColumns = 2;
		layout.makeColumnsEqualWidth = true;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.horizontalSpacing = 0;
		actionGroup.setLayout(layout);
		actionGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		Composite menubarGroup = new Composite(actionGroup, SWT.NONE);
		layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		menubarGroup.setLayout(layout);
		menubarGroup
				.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		label = new Label(menubarGroup, SWT.WRAP);
		label.setText(WorkbenchMessages.ActionSetSelection_menubarActions);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		final TreeViewer actionSetMenuViewer = new TreeViewer(menubarGroup);
		actionSetMenuViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
		actionSetMenuViewer.getControl().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true));
		actionSetMenuViewer.setUseHashlookup(true);
		actionSetMenuViewer.setContentProvider(TreeManager
				.getTreeContentProvider());
		actionSetMenuViewer.setLabelProvider(TreeManager.getLabelProvider());
		actionSetMenuViewer.addFilter(setFilter);
		actionSetMenuViewer.setInput(menuItems);

		Tree tree = actionSetMenuViewer.getTree();
		new ItemDetailToolTip(actionSetMenuViewer, tree, false, true, setFilter);

		Composite toolbarGroup = new Composite(actionGroup, SWT.NONE);
		layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		toolbarGroup.setLayout(layout);
		toolbarGroup
				.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		label = new Label(toolbarGroup, SWT.WRAP);
		label.setText(WorkbenchMessages.ActionSetSelection_toolbarActions);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		final TreeViewer actionSetToolbarViewer = new TreeViewer(toolbarGroup);
		actionSetToolbarViewer
				.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
		actionSetToolbarViewer.getControl().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true));
		actionSetToolbarViewer.setContentProvider(TreeManager
				.getTreeContentProvider());
		actionSetToolbarViewer.setLabelProvider(TreeManager.getLabelProvider());
		actionSetToolbarViewer.addFilter(setFilter);
		actionSetToolbarViewer.setInput(toolBarItems);

		tree = actionSetToolbarViewer.getTree();
		new ItemDetailToolTip(actionSetToolbarViewer, tree, false, true, setFilter);

		// Updates the menu item and toolbar items tree viewers when the
		// selection changes
		actionSetsViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					public void selectionChanged(SelectionChangedEvent event) {
						selectedActionSet[0] = (ActionSet) ((IStructuredSelection) event
								.getSelection()).getFirstElement();
						actionSetMenuViewer.setInput(menuItems);
						actionSetToolbarViewer.setInput(toolBarItems);
					}
				});

		sashComposite.setWeights(new int[] { 30, 70 });

		return actionSetsComposite;
	}

	/**
	 * Creates the page used to allow users to choose menu items to hide.
	 */
	private Composite createMenuVisibilityPage(Composite parent) {
		GridData data;

		Composite hideMenuItemsComposite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		hideMenuItemsComposite.setLayout(layout);

		// Label for entire tab
		Label label = new Label(hideMenuItemsComposite, SWT.WRAP);
		label.setText(WorkbenchMessages.HideMenuItems_chooseMenuItemsLabel);
		data = new GridData(SWT.FILL, SWT.CENTER, true, false);
		label.setLayoutData(data);

		Label sep = new Label(hideMenuItemsComposite, SWT.HORIZONTAL
				| SWT.SEPARATOR);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		// Main contents of tab
		final PageBook book = new PageBook(hideMenuItemsComposite, SWT.NONE);
		data = new GridData(GridData.FILL_BOTH);
		book.setLayoutData(data);

		// Simple view: just the menu structure
		final Composite simpleComposite = createItemStructureGroup(book,
				WorkbenchMessages.HideMenuItems_menuStructure);
		menuStructureViewer1 = initStructureViewer(simpleComposite,
				new TreeManager.ViewerCheckStateListener(), null);

		// Update the viewer when the model changes
		treeManager.getCheckListener(menuStructureViewer1); // To update ctv on
															// model changes

		// Simply grab the checkstate out of the model
		menuStructureViewer1.setCheckStateProvider(TreeManager
				.getCheckStateProvider());

		// Init with input
		menuStructureViewer1.setInput(menuItems);

		// Advanced view: action set with filtered menu structure
		final SashForm advancedComposite = new SashForm(book, SWT.HORIZONTAL);
		data = new GridData(SWT.FILL, SWT.FILL, true, true);
		advancedComposite.setLayoutData(data);

		// Action set list
		final TableViewer actionSetViewer = initActionSetViewer(createActionSetGroup(advancedComposite));

		// Filter to only show action sets which have useful menu items
		actionSetViewer.addFilter(new ShowUsedActionSetsFilter(menuItems));

		// Init with input
		actionSetViewer.setInput(actionSets.toArray());

		// Filter to only show items in the current action set
		final ActionSetFilter menuStructureFilterByActionSet = new ActionSetFilter();

		final Composite menuStructureComposite = createItemStructureGroup(
				advancedComposite,
				WorkbenchMessages.HideMenuItems_menuStructure);
		final ICheckStateListener menuStructureFilter = new FilteredViewerCheckListener(
				TreeManager.getTreeContentProvider(),
				menuStructureFilterByActionSet);
		menuStructureViewer2 = initStructureViewer(menuStructureComposite,
				menuStructureFilter, menuStructureFilterByActionSet);

		treeManager.addListener(new FilteredModelCheckListener(
				menuStructureFilterByActionSet, menuStructureViewer2));

		menuStructureViewer2.addFilter(menuStructureFilterByActionSet);

		// Update filter when a new action set is selected
		actionSetViewer
				.addSelectionChangedListener(new ActionSetSelectionChangedListener(
						menuStructureViewer2, menuStructureFilterByActionSet));

		// Check state provider to emulate standard SWT
		// behaviour on visual tree
		menuStructureViewer2
				.setCheckStateProvider(new FilteredTreeCheckProvider(
						TreeManager.getTreeContentProvider(),
						menuStructureFilterByActionSet));

		// Init input
		menuStructureViewer2.setInput(menuItems);

		// Override any attempts to set an item to visible
		// which exists in an unavailable action set
		treeManager.addListener(new CheckListener() {
			public void checkChanged(TreeItem changedItem) {
				if (!(changedItem instanceof DisplayItem))
					return;
				if (!changedItem.getState())
					return;
				if (isAvailable((DisplayItem) changedItem))
					return;
				changedItem.setCheckState(false);
			}
		});

		final Button showCommandGroupFilterButton = new Button(
				hideMenuItemsComposite, SWT.CHECK);
		showCommandGroupFilterButton
				.setText(WorkbenchMessages.HideItems_turnOnActionSets);
		showCommandGroupFilterButton
				.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						if (showCommandGroupFilterButton.getSelection()) {
							Object o = ((StructuredSelection) menuStructureViewer1
									.getSelection()).getFirstElement();
							ActionSet initSelectAS = null;
							DisplayItem initSelectCI = null;
							if (o instanceof DisplayItem) {
								initSelectCI = ((DisplayItem) o);
								initSelectAS = initSelectCI.getActionSet();
							}
							if (initSelectAS == null) {
								initSelectAS = (ActionSet) actionSetViewer
										.getElementAt(0);
							}
							setSelectionOn(actionSetViewer, initSelectAS);
							actionSetViewer.reveal(initSelectAS);
							if (initSelectCI != null) {
								setSelectionOn(menuStructureViewer2,
										initSelectCI);
								menuStructureViewer2.reveal(initSelectCI);
							}
							book.showPage(advancedComposite);
						} else {
							book.showPage(simpleComposite);
						}
					}
				});

		book.showPage(simpleComposite);
		advancedComposite.setWeights(new int[] { 30, 70 });

		return hideMenuItemsComposite;
	}

	/**
	 * Creates the page used to allow users to choose menu items to hide.
	 */
	private Composite createToolBarVisibilityPage(Composite parent) {
		GridData data;

		Composite hideToolbarItemsComposite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		hideToolbarItemsComposite.setLayout(layout);

		// Label for entire tab
		Label label = new Label(hideToolbarItemsComposite, SWT.WRAP);
		label
				.setText(WorkbenchMessages.HideToolBarItems_chooseToolBarItemsLabel);
		data = new GridData(SWT.FILL, SWT.CENTER, true, false);
		label.setLayoutData(data);

		Label sep = new Label(hideToolbarItemsComposite, SWT.HORIZONTAL
				| SWT.SEPARATOR);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		// Main contents of tab
		final PageBook book = new PageBook(hideToolbarItemsComposite, SWT.NONE);
		data = new GridData(GridData.FILL_BOTH);
		book.setLayoutData(data);

		// Simple view: just the toolbar structure
		final Composite simpleComposite = createItemStructureGroup(book,
				WorkbenchMessages.HideToolBarItems_toolBarStructure);
		toolbarStructureViewer1 = initStructureViewer(simpleComposite,
				new TreeManager.ViewerCheckStateListener(), null);

		// Update the viewer when the model changes
		treeManager.getCheckListener(toolbarStructureViewer1); // To update ctv
																// on model
																// changes

		// Simply grab the check state out of the model
		toolbarStructureViewer1.setCheckStateProvider(TreeManager
				.getCheckStateProvider());

		// Init with input
		toolbarStructureViewer1.setInput(toolBarItems);

		// Advanced view: action set with filtered toolbar structure
		final SashForm advancedComposite = new SashForm(book, SWT.HORIZONTAL);
		data = new GridData(SWT.FILL, SWT.FILL, true, true);
		advancedComposite.setLayoutData(data);

		// Action set list
		final TableViewer actionSetViewer = initActionSetViewer(createActionSetGroup(advancedComposite));

		// Filter to only show action sets which have useful toolbar items
		actionSetViewer.addFilter(new ShowUsedActionSetsFilter(toolBarItems));

		// Init with input
		actionSetViewer.setInput(actionSets.toArray());

		// Filter to only show items in the current action set
		final ActionSetFilter toolbarStructureFilterByActionSet = new ActionSetFilter();

		final Composite toolbarStructureComposite = createItemStructureGroup(
				advancedComposite,
				WorkbenchMessages.HideToolBarItems_toolBarStructure);
		final ICheckStateListener toolbarStructureFilter = new FilteredViewerCheckListener(
				TreeManager.getTreeContentProvider(),
				toolbarStructureFilterByActionSet);
		toolbarStructureViewer2 = initStructureViewer(
				toolbarStructureComposite, toolbarStructureFilter,
				toolbarStructureFilterByActionSet);

		toolbarStructureViewer2.addFilter(toolbarStructureFilterByActionSet);

		treeManager.addListener(new FilteredModelCheckListener(
				toolbarStructureFilterByActionSet, toolbarStructureViewer2));

		// Update filter when a new action set is selected
		actionSetViewer
				.addSelectionChangedListener(new ActionSetSelectionChangedListener(
						toolbarStructureViewer2,
						toolbarStructureFilterByActionSet));

		// Check state provider to emulate standard SWT
		// behaviour on visual tree
		toolbarStructureViewer2
				.setCheckStateProvider(new FilteredTreeCheckProvider(
						TreeManager.getTreeContentProvider(),
						toolbarStructureFilterByActionSet));

		// Init input
		toolbarStructureViewer2.setInput(toolBarItems);

		// Override any attempts to set an item to visible
		// which exists in an unavailable action set
		treeManager.addListener(new CheckListener() {
			public void checkChanged(TreeItem changedItem) {
				if (!(changedItem instanceof DisplayItem))
					return;
				if (!changedItem.getState())
					return;
				if (isAvailable((DisplayItem) changedItem))
					return;
				changedItem.setCheckState(false);
			}
		});

		final Button showCommandGroupFilterButton = new Button(
				hideToolbarItemsComposite, SWT.CHECK);
		showCommandGroupFilterButton
				.setText(WorkbenchMessages.HideItems_turnOnActionSets);
		showCommandGroupFilterButton
				.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						if (showCommandGroupFilterButton.getSelection()) {
							Object o = ((StructuredSelection) toolbarStructureViewer1
									.getSelection()).getFirstElement();
							ActionSet initSelectAS = null;
							DisplayItem initSelectCI = null;
							if (o instanceof DisplayItem) {
								initSelectCI = ((DisplayItem) o);
								initSelectAS = initSelectCI.getActionSet();
							}
							if (initSelectAS == null) {
								initSelectAS = (ActionSet) actionSetViewer
										.getElementAt(0);
							}
							setSelectionOn(actionSetViewer, initSelectAS);
							actionSetViewer.reveal(initSelectAS);
							if (initSelectCI != null) {
								setSelectionOn(toolbarStructureViewer2,
										initSelectCI);
								toolbarStructureViewer2.reveal(initSelectCI);
							}
							book.showPage(advancedComposite);
						} else {
							book.showPage(simpleComposite);
						}
					}
				});

		book.showPage(simpleComposite);
		advancedComposite.setWeights(new int[] { 30, 70 });

		return hideToolbarItemsComposite;
	}

	/**
	 * Creates a table to display action sets.
	 * 
	 * @param parent
	 * @return a viewer to display action sets
	 */
	private TableViewer initActionSetViewer(Composite parent) {
		// List of categories
		final TableViewer actionSetViewer = new TableViewer(parent, SWT.BORDER
				| SWT.H_SCROLL | SWT.V_SCROLL);
		actionSetViewer.getTable().setLayoutData(
				new GridData(GridData.FILL_BOTH));
		actionSetViewer.setLabelProvider(new LabelProvider());
		actionSetViewer.setComparator(new WorkbenchViewerComparator());
		actionSetViewer.setContentProvider(new ArrayContentProvider());

		// Tooltip on tree items
		Table table = actionSetViewer.getTable();
		new TableToolTip(table);
		return actionSetViewer;
	}

	/**
	 * Creates a CheckboxTreeViewer to display menu or toolbar structure.
	 * 
	 * @param parent
	 * @param checkStateListener
	 *            the listener which listens to the viewer for check changes
	 * @param filter the filter used in the viewer (null for none)
	 * @return A viewer within <code>parent</code> which will show menu or
	 *         toolbar structure. It comes setup, only missing a
	 *         CheckStateProvider and its input.
	 */
	private CheckboxTreeViewer initStructureViewer(Composite parent,
			ICheckStateListener checkStateListener, ViewerFilter filter) {
		CheckboxTreeViewer ctv = new CheckboxTreeViewer(parent, SWT.BORDER
				| SWT.H_SCROLL | SWT.V_SCROLL);
		ctv.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
		ctv.setUseHashlookup(true);
		ctv.setContentProvider(TreeManager.getTreeContentProvider());
		// use an UnavailableContributionItemCheckListener to filter check
		// events: if it is legal, forward it to the actual checkStateListener,
		// if not, inform the user
		ctv.addCheckStateListener(new UnavailableContributionItemCheckListener(
				ctv, checkStateListener));
		ctv.setLabelProvider(new GrayOutUnavailableLabelProvider(parent
				.getDisplay(), filter));
		new ItemDetailToolTip(ctv, ctv.getTree(), true, true, filter);
		return ctv;
	}

	/**
	 * Creates a composite to put a tree viewer in to display menu or toolbar
	 * items.
	 */
	private static Composite createItemStructureGroup(
			final Composite composite, String labelText) {
		GridLayout layout;
		Label label;
		layout = new GridLayout();
		Composite menubarGroup = new Composite(composite, SWT.NONE);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		menubarGroup.setLayout(layout);
		menubarGroup
				.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		label = new Label(menubarGroup, SWT.WRAP);
		label.setText(labelText);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		return menubarGroup;
	}

	/**
	 * Creates a composite to put a viewer in to display action sets.
	 */
	private static Composite createActionSetGroup(final Composite composite) {
		GridLayout layout;
		Label label;
		Composite actionSetGroup = new Composite(composite, SWT.NONE);
		layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		actionSetGroup.setLayout(layout);
		actionSetGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));

		label = new Label(actionSetGroup, SWT.WRAP);
		label.setText(WorkbenchMessages.HideItems_commandGroupTitle);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		return actionSetGroup;
	}

	/**
	 * Set the selection on a structured viewer.
	 * 
	 * @param viewer
	 * @param selected
	 */
	private void setSelectionOn(Viewer viewer, final Object selected) {
		viewer.setSelection(new StructuredSelection(selected), true);
	}

	/**
	 * Searches deeply to see if <code>item</code> is a node in a branch
	 * containing a ContributionItem contributed by <code>set</code>.
	 * 
	 * @param item
	 *            the item in question
	 * @param set
	 *            the action set to look for
	 * @return true iff <code>item</code> is required in build a tree including
	 *         elements in <code>set</code>
	 */
	private static boolean includeInSetStructure(DisplayItem item,
			ActionSet set) {
		if (item.actionSet != null && item.actionSet.equals(set))
			return true;
		for (Iterator i = item.getChildren().iterator(); i.hasNext();) {
			DisplayItem child = (DisplayItem) i.next();
			if (includeInSetStructure(child, set))
				return true;
		}
		return false;
	}

	/**
	 * @param item
	 * @return true iff the item is available - i.e. if it belongs to an action
	 *         set, that that action set is available
	 */
	private static boolean isAvailable(DisplayItem item) {
		if (item.getActionSet() != null && item.getActionSet().isActive())
			return true;
		for (Iterator i = item.getChildren().iterator(); i.hasNext();) {
			DisplayItem child = (DisplayItem) i.next();
			if (isAvailable(child))
				return true;
		}
		return item.getIContributionItem() != null && item.getIContributionItem().isVisible();
	}

	/**
	 * @param item
	 * @return true iff the item will show up in a menu or tool bar structure -
	 *         i.e. it is available, or has a child which is available thus must
	 *         be displayed in order to display the child
	 */
	private static boolean isEffectivelyAvailable(DisplayItem item, ViewerFilter filter) {
		if (!isAvailable(item))
			return false;
		final List children = item.getChildren();
		if (children.isEmpty())
			return true;
		for (Iterator i = children.iterator(); i.hasNext();) {
			DisplayItem child = (DisplayItem) i.next();
			if(filter != null && !filter.select(null, null, child))
				continue;
			if (isAvailable(child)) {
				return true;
			}
		}
		for (Iterator i = children.iterator(); i.hasNext();) {
			DisplayItem child = (DisplayItem) i.next();
			if(filter != null && !filter.select(null, null, child))
				continue;
			if (isEffectivelyAvailable(child, filter)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @param collection
	 *            a collection, into which all command groups (action sets)
	 *            which contribute <code>item</code> or its descendants will be
	 *            placed
	 * @param item	the item to collect descendants of
	 * @param filter the filter currently being used
	 * @param item
	 */
	private static void collectDescendantCommandGroups(Collection collection,
			DisplayItem item, ViewerFilter filter) {
		List children = item.getChildren();
		for (Iterator i = children.iterator(); i.hasNext();) {
			DisplayItem child = (DisplayItem) i.next();
			if ((filter == null || filter.select(null, null, child))
					&& child.getActionSet() != null) {
				collection.add(child.getActionSet());
			}
			collectDescendantCommandGroups(collection, child, filter);
		}
	}

	/**
	 * Gets the keybindings associated with a ContributionItem.
	 */
	private Binding[] getKeyBindings(DisplayItem item) {
		IBindingService bindingService = (IBindingService) window
				.getService(IBindingService.class);

		if (!(bindingService instanceof BindingService))
			return new Binding[0];

		String id = getCommandID(item);
		String param = getParamID(item);

		BindingManager bindingManager = ((BindingService) bindingService)
				.getBindingManager();

		Collection allBindings = bindingManager
				.getActiveBindingsDisregardingContextFlat();

		List foundBindings = new ArrayList(2);

		for (Iterator i = allBindings.iterator(); i.hasNext();) {
			Binding binding = (Binding) i.next();
			if (binding.getParameterizedCommand() == null)
				continue;
			if (binding.getParameterizedCommand().getId() == null)
				continue;
			if (binding.getParameterizedCommand().getId().equals(id)) {
				if (param == null) {
					// We found it!
					foundBindings.add(binding);
				} else {
					// command parameters are only used in the shortcuts
					Map m = binding.getParameterizedCommand().getParameterMap();
					String key = null;
					if (isNewWizard(item)) {
						key = IWorkbenchCommandConstants.FILE_NEW_PARM_WIZARDID;
					} else if (isShowView(item)) {
						key = IWorkbenchCommandConstants.VIEWS_SHOW_VIEW_PARM_ID;
					} else if (isShowPerspective(item)) {
						key = IWorkbenchCommandConstants.PERSPECTIVES_SHOW_PERSPECTIVE_PARM_ID;
					}

					if (key != null) {
						if (param.equals(m.get(key))) {
							foundBindings.add(binding);
						}
					}
				}
			}
		}

		Binding[] bindings = (Binding[]) foundBindings
				.toArray(new Binding[foundBindings.size()]);

		return bindings;
	}

	/**
	 * @param bindings
	 * @return a String representing the key bindings in <code>bindings</code>
	 */
	private String keyBindingsAsString(Binding[] bindings) {
		String keybindings = null;
		for (int i = 0; i < bindings.length; i++) {
			// Unfortunately, bindings may be reported more than once:
			// check to see if this one has already been recorded.
			boolean alreadyRecorded = false;
			for (int j = 0; j < i && !alreadyRecorded; j++) {
				if (bindings[i].getTriggerSequence().equals(
						bindings[j].getTriggerSequence())) {
					alreadyRecorded = true;
				}
			}
			if (!alreadyRecorded) {
				String keybinding = bindings[i].getTriggerSequence().format();
				if (i == 0) {
					keybindings = keybinding;
				} else {
					keybindings = Util.createList(keybindings, keybinding);
				}
			}
		}
		return keybindings;
	}

	/**
	 * On a change to availability, updates the appropriate widgets.
	 */
	private void actionSetAvailabilityChanged() {
		menuStructureViewer1.refresh();
		menuStructureViewer2.refresh();
		toolbarStructureViewer1.refresh();
		toolbarStructureViewer2.refresh();
	}

	private void initializeActionSetInput() {
		// Just get the action sets at this point. Do not load the action set
		// until it is actually selected in the dialog.
		ActionSetRegistry reg = WorkbenchPlugin.getDefault()
				.getActionSetRegistry();
		IActionSetDescriptor[] sets = reg.getActionSets();
		IActionSetDescriptor[] actionSetDescriptors = ((WorkbenchPage) window
				.getActivePage()).getActionSets();
		List initiallyAvailableActionSets = Arrays.asList(actionSetDescriptors);

		for (int i = 0; i < sets.length; i++) {
			ActionSetDescriptor actionSetDesc = (ActionSetDescriptor) sets[i];
			if (WorkbenchActivityHelper.filterItem(actionSetDesc)) {
				continue;
			}
			ActionSet actionSet = new ActionSet(actionSetDesc,
					initiallyAvailableActionSets.contains(actionSetDesc));
			idToActionSet.put(actionSetDesc.getId(), actionSet);
			actionSets.add(actionSet);
		}
	}

	public String getToolbarLabel(String actionSetId) {
		if (actionSetId == null)
			return ""; //$NON-NLS-1$
		ActionSetRegistry registry = WorkbenchPlugin.getDefault().getActionSetRegistry();
		IActionSetDescriptor findActionSet = registry.findActionSet(actionSetId);
		if (findActionSet != null) {
			return findActionSet.getLabel();
		}

		if (IWorkbenchActionConstants.TOOLBAR_FILE.equalsIgnoreCase(actionSetId)) {
			return WorkbenchMessages.WorkbenchWindow_FileToolbar;
		}
		if (IWorkbenchActionConstants.TOOLBAR_NAVIGATE.equalsIgnoreCase(actionSetId)) {
			return WorkbenchMessages.WorkbenchWindow_NavigateToolbar;
		}
		// Nothing is available. Let's smartly guess the name then.
		String[] nameParts = actionSetId.split("\\."); //$NON-NLS-1$
		return nameParts[nameParts.length - 1];
	}

	private void initializeIcons() {
		String iconPath = MENU_ICON;
		URL url = BundleUtility.find(PlatformUI.PLUGIN_ID, iconPath);
		menuImageDescriptor = ImageDescriptor.createFromURL(url);

		iconPath = SUBMENU_ICON;
		url = BundleUtility.find(PlatformUI.PLUGIN_ID, iconPath);
		submenuImageDescriptor = ImageDescriptor.createFromURL(url);

		iconPath = TOOLBAR_ICON;
		url = BundleUtility.find(PlatformUI.PLUGIN_ID, iconPath);
		toolbarImageDescriptor = ImageDescriptor.createFromURL(url);

		iconPath = WARNING_ICON;
		url = BundleUtility.find(PlatformUI.PLUGIN_ID, iconPath);
		warningImageDescriptor = ImageDescriptor.createFromURL(url);
	}

	private void initializeNewWizardsMenu(DisplayItem menu,
			Category parentCategory, IWizardCategory element, List activeIds) {
		Category category = new Category(element.getLabel());
		parentCategory.addChild(category);

		Object[] wizards = element.getWizards();
		for (int i = 0; i < wizards.length; i++) {
			WorkbenchWizardElement wizard = (WorkbenchWizardElement) wizards[i];

			ShortcutItem item = new ShortcutItem(wizard.getLabel(), wizard);
			item.setLabel(wizard.getLabel());
			item.setDescription(wizard.getDescription());
			if (wizard.getImageDescriptor() != null) {
				item.setImageDescriptor(wizard.getImageDescriptor());
			}
			item.setCheckState(activeIds.contains(wizard.getId()));
			menu.addChild(item);
			category.addShortcutItem(item);
		}
		// @issue should not pass in null
		IWizardCategory[] children = element.getCategories();
		for (int i = 0; i < children.length; i++) {
			initializeNewWizardsMenu(menu, category, children[i], activeIds);
		}
	}

	private void initializeNewWizardsMenu(DisplayItem menu) {
		Category rootForNewWizards = new Category(
				WorkbenchMessages.ActionSetDialogInput_wizardCategory);
		shortcuts.addChild(rootForNewWizards);

		IWizardCategory wizardCollection = WorkbenchPlugin.getDefault()
				.getNewWizardRegistry().getRootCategory();
		IWizardCategory[] wizardCategories = wizardCollection.getCategories();
		List activeIDs = Arrays.asList(perspective.getNewWizardShortcuts());

		for (int i = 0; i < wizardCategories.length; i++) {
			IWizardCategory element = wizardCategories[i];
			if (WorkbenchActivityHelper.filterItem(element)) {
				continue;
			}

			initializeNewWizardsMenu(menu, rootForNewWizards, element,
					activeIDs);
		}
	}

	private void initializePerspectivesMenu(DisplayItem menu) {
		Category rootForPerspectives = new Category(
				WorkbenchMessages.ActionSetDialogInput_perspectiveCategory);
		shortcuts.addChild(rootForPerspectives);

		IPerspectiveRegistry perspReg = WorkbenchPlugin.getDefault()
				.getPerspectiveRegistry();
		IPerspectiveDescriptor[] persps = perspReg.getPerspectives();

		List activeIds = Arrays.asList(perspective.getPerspectiveShortcuts());

		for (int i = 0; i < persps.length; i++) {
			IPerspectiveDescriptor perspective = persps[i];
			if (WorkbenchActivityHelper.filterItem(perspective)) {
				continue;
			}

			ShortcutItem child = new ShortcutItem(perspective.getLabel(),
					perspective);
			child.setImageDescriptor(perspective.getImageDescriptor());
			child.setDescription(perspective.getDescription());
			child.setCheckState(activeIds.contains(perspective.getId()));
			menu.addChild(child);

			rootForPerspectives.addShortcutItem(child);
		}
	}

	private void initializeViewsMenu(DisplayItem menu) {
		Category rootForViews = new Category(
				WorkbenchMessages.ActionSetDialogInput_viewCategory);

		shortcuts.addChild(rootForViews);

		IViewRegistry viewReg = WorkbenchPlugin.getDefault().getViewRegistry();
		IViewCategory[] categories = viewReg.getCategories();

		List activeIds = Arrays.asList(perspective.getShowViewShortcuts());

		for (int i = 0; i < categories.length; i++) {
			IViewCategory category = categories[i];
			if (WorkbenchActivityHelper.filterItem(category)) {
				continue;
			}

			Category viewCategory = new Category(category.getLabel());
			rootForViews.addChild(viewCategory);

			IViewDescriptor[] views = category.getViews();

			if (views != null) {
				for (int j = 0; j < views.length; j++) {
					IViewDescriptor view = views[j];
					if (view.getId().equals(IIntroConstants.INTRO_VIEW_ID)) {
						continue;
					}
					if (WorkbenchActivityHelper.filterItem(view)) {
						continue;
					}

					ShortcutItem child = new ShortcutItem(view.getLabel(), view);
					child.setImageDescriptor(view.getImageDescriptor());
					child.setDescription(view.getDescription());
					child.setCheckState(activeIds.contains(view.getId()));
					menu.addChild(child);
					viewCategory.addShortcutItem(child);
				}
			}
		}
	}

	/**
	 * Loads the current perspective's menu structure and also loads which menu
	 * items are visible and not.
	 */
	private void loadMenuAndToolbarStructure() {
		WorkbenchWindow workbenchWindow = (WorkbenchWindow) PlatformUI
				.getWorkbench().getActiveWorkbenchWindow();

		customizeActionBars = new CustomizeActionBars(configurer);

		// Fill fake action bars with static menu information.
		window.fillActionBars(customizeActionBars, ActionBarAdvisor.FILL_PROXY
				| ActionBarAdvisor.FILL_MENU_BAR
				| ActionBarAdvisor.FILL_COOL_BAR);

		// 3.3 start
		// final IMenuService menuService = (IMenuService) window
		// .getService(IMenuService.class);
		// menuService.populateContributionManager(
		// (ContributionManager) customizeActionBars.getMenuManager(),
		// MenuUtil.MAIN_MENU);
		// ICoolBarManager coolbar = customizeActionBars.getCoolBarManager();
		// if (coolbar != null) {
		// menuService.populateContributionManager(
		// (ContributionManager) coolbar, MenuUtil.MAIN_TOOLBAR);
		// }
		// 3.3 end

		// Populate the action bars with the action sets' data
		for (Iterator i = actionSets.iterator(); i.hasNext();) {
			ActionSet actionSet = (ActionSet) i.next();
			ActionSetDescriptor descriptor = actionSet.descriptor;
			PluginActionSet pluginActionSet = buildMenusAndToolbarsFor(
					customizeActionBars, descriptor);

			if (pluginActionSet != null) {
				pluginActionSet.dispose();
			}
		}

		// Make all menu items visible so they are included in the list.
		customizeActionBars.menuManager.setVisible(true);

		makeAllContributionsVisible(customizeActionBars.menuManager);

		// Get the menu from the action bars
		customizeActionBars.menuManager
				.createMenuBar((Decorations) workbenchWindow.getShell());

		CoolBar cb = customizeActionBars.coolBarManager
				.createControl(workbenchWindow.getShell());
		cb.equals(cb);

		// Ensure the menu is completely built by updating the menu manager.
		// (This method call requires a menu already be created)
		customizeActionBars.menuManager.updateAll(true);
		customizeActionBars.coolBarManager.update(true);

		shortcuts = new Category(""); //$NON-NLS-1$
		toolBarItems = createToolBarStructure(window.getTopTrim());
		menuItems = createMenuStructure(window.getModel().getMainMenu());
	}

	private PluginActionSet buildMenusAndToolbarsFor(
			CustomizeActionBars customizeActionBars,
			ActionSetDescriptor actionSetDesc) {
		String id = actionSetDesc.getId();
		ActionSetActionBars bars = new ActionSetActionBars(customizeActionBars,
				window, customizeActionBars, id);
		bars.getMenuManager().setVisible(true);
		PluginActionSetBuilder builder = new PluginActionSetBuilder();
		PluginActionSet actionSet = null;
		try {
			actionSet = (PluginActionSet) actionSetDesc.createActionSet();
			actionSet.init(null, bars);
		} catch (CoreException ex) {
			WorkbenchPlugin.log(
					"Unable to create action set " + actionSetDesc.getId(), ex); //$NON-NLS-1$
			return null;
		}
		builder.buildMenuAndToolBarStructure(actionSet, window);
		return actionSet;
	}

	private static String getCommandID(DisplayItem item) {
		Object object = item.getIContributionItem();

		if (item instanceof ShortcutItem && isShowView(item)) {
			return IWorkbenchCommandConstants.VIEWS_SHOW_VIEW;
		}

		return getIDFromIContributionItem(object);
	}

	/**
	 * Given an object, tries to find an id which will uniquely identify it.
	 * 
	 * @param object
	 *            an instance of {@link IContributionItem},
	 *            {@link IPerspectiveDescriptor}, {@link IViewDescriptor} or
	 *            {@link WorkbenchWizardElement}.
	 * @return an id
	 * @throws IllegalArgumentException
	 *             if object is not one of the listed types
	 */
	public static String getIDFromIContributionItem(Object object) {
		if (object instanceof ActionContributionItem) {
			ActionContributionItem item = (ActionContributionItem) object;
			IAction action = item.getAction();
			if (action == null)
				return null;
			if (action instanceof NewWizardShortcutAction) {
				return IWorkbenchCommandConstants.FILE_NEW;
			}
			if (action instanceof OpenPerspectiveAction) {
				return IWorkbenchCommandConstants.PERSPECTIVES_SHOW_PERSPECTIVE;
			}
			String id = action.getActionDefinitionId();
			if (id != null) {
				return id;
			}
			return action.getId();
		}
		if (object instanceof ActionSetContributionItem) {
			ActionSetContributionItem item = (ActionSetContributionItem) object;
			IContributionItem subitem = item.getInnerItem();
			return getIDFromIContributionItem(subitem);
		}
		if (object instanceof CommandContributionItem) {
			CommandContributionItem item = (CommandContributionItem) object;
			ParameterizedCommand command = item.getCommand();
			if (command == null) {
				return null;
			}
			return command.getId();
		}
		if (object instanceof IPerspectiveDescriptor) {
			return ((IPerspectiveDescriptor) object).getId();
		}
		if (object instanceof IViewDescriptor) {
			return ((IViewDescriptor) object).getId();
		}
		if (object instanceof WorkbenchWizardElement) {
			return ((WorkbenchWizardElement) object).getLocalId();
		}
		if (object instanceof IContributionItem) {
			String id = ((IContributionItem) object).getId();
			if (id != null)
				return id;
			return object.getClass().getName();
		}
		return null;	//couldn't determine the id
	}

	private static String getDescription(Object object) {
		if (object instanceof DisplayItem) {
			DisplayItem item = (DisplayItem) object;

			if (isNewWizard(item)) {
				ShortcutItem shortcut = (ShortcutItem) item;
				IWizardDescriptor descriptor = (IWizardDescriptor) shortcut
						.getDescriptor();
				return descriptor.getDescription();
			}

			if (isShowPerspective(item)) {
				ShortcutItem shortcut = (ShortcutItem) item;
				IPerspectiveDescriptor descriptor = (IPerspectiveDescriptor) shortcut
						.getDescriptor();
				return descriptor.getDescription();
			}

			if (isShowView(item)) {
				ShortcutItem shortcut = (ShortcutItem) item;
				IViewDescriptor descriptor = (IViewDescriptor) shortcut
						.getDescriptor();
				return descriptor.getDescription();
			}

			if (item instanceof DynamicContributionItem) {
				return WorkbenchMessages.HideItems_dynamicItemDescription;
			}

			IContributionItem contrib = item.getIContributionItem();
			return getDescription(contrib);
		}

		if (object instanceof ActionSet) {
			ActionSet actionSet = (ActionSet) object;
			return actionSet.descriptor.getDescription();
		}

		return null;
	}

	private static String getDescription(IContributionItem item) {
		if (item instanceof ActionContributionItem) {
			ActionContributionItem aci = (ActionContributionItem) item;
			IAction action = aci.getAction();
			if (action == null)
				return null;
			return action.getDescription();
		}
		if (item instanceof ActionSetContributionItem) {
			ActionSetContributionItem asci = (ActionSetContributionItem) item;
			IContributionItem subitem = asci.getInnerItem();
			return getDescription(subitem);
		}
		return null;
	}

	private static String getParamID(DisplayItem object) {
		if (object instanceof ShortcutItem) {
			ShortcutItem shortcutItem = (ShortcutItem) object;

			if (isNewWizard(shortcutItem)) {
				ActionContributionItem item = (ActionContributionItem) object
						.getIContributionItem();
				NewWizardShortcutAction nwsa = (NewWizardShortcutAction) item
						.getAction();
				return nwsa.getLocalId();
			}

			if (isShowPerspective(shortcutItem)) {
				ActionContributionItem item = (ActionContributionItem) object
						.getIContributionItem();
				OpenPerspectiveAction opa = (OpenPerspectiveAction) item
						.getAction();
				return opa.getLocalId();
			}

			if (isShowView(shortcutItem)) {
				IViewDescriptor descriptor = (IViewDescriptor) shortcutItem
						.getDescriptor();
				return descriptor.getId();
			}
		}

		return null;
	}

	private static boolean isNewWizard(DisplayItem item) {
		if (!(item instanceof ShortcutItem))
			return false;
		return ((ShortcutItem) item).getDescriptor() instanceof IWizardDescriptor;
	}

	private static boolean isShowPerspective(DisplayItem item) {
		if (!(item instanceof ShortcutItem))
			return false;
		return ((ShortcutItem) item).getDescriptor() instanceof IPerspectiveDescriptor;
	}

	private static boolean isShowView(DisplayItem item) {
		if (!(item instanceof ShortcutItem))
			return false;
		return ((ShortcutItem) item).getDescriptor() instanceof IViewDescriptor;
	}

	private static String getActionSetID(IContributionItem item) {
		if (item instanceof ActionSetContributionItem) {
			ActionSetContributionItem asci = (ActionSetContributionItem) item;
			return asci.getActionSetId();
		}
		if (item instanceof PluginActionCoolBarContributionItem) {
			PluginActionCoolBarContributionItem pacbci = (PluginActionCoolBarContributionItem) item;
			return pacbci.getActionSetId();
		}
		return null;
	}

	private static String getActionSetID(MUIElement item) {
		return (String) item.getTransientData().get("ActionSet"); //$NON-NLS-1$
	}

	/**
	 * Causes all items under the manager to be visible, so they can be read.
	 * 
	 * @param manager
	 */
	private static void makeAllContributionsVisible(IContributionManager manager) {
		IContributionItem[] items = manager.getItems();

		for (int i = 0; i < items.length; i++) {
			makeContributionVisible(items[i]);
		}
	}

	/**
	 * Makes all items under the item to be visible, so they can be read.
	 * 
	 * @param item
	 */
	private static void makeContributionVisible(IContributionItem item) {
		item.setVisible(true);

		if (item instanceof IContributionManager) {
			makeAllContributionsVisible((IContributionManager) item);
		}
		if (item instanceof SubContributionItem) {
			makeContributionVisible(((SubContributionItem) item).getInnerItem());
		}
	}

	private DisplayItem createMenuStructure(MMenu menu) {
		DisplayItem root = new DisplayItem("", null); //$NON-NLS-1$
		createMenuEntries(menu, root, true);
		return root;
	}

	private void createMenuEntries(MMenu menu, DisplayItem parent,
			boolean trackDynamics) {
		Map<IContributionItem, IContributionItem> findDynamics = new HashMap<IContributionItem, IContributionItem>();
		DynamicContributionItem dynamicEntry = null;

		if (trackDynamics && menu.getParent() != null) {
			// Search for any dynamic menu entries which will be handled later
			Object data = menuMngrRenderer.getManager(menu);
			if (data instanceof IContributionManager) {
				IContributionManager manager = (IContributionManager) data;
				IContributionItem[] items = manager.getItems();
				for (int i = 0; i < items.length; i++) {
					if (items[i].isDynamic()) {
						findDynamics.put(i > 0 ? items[i - 1] : null, items[i]);
					}
				}
				// If there is an item with no preceeding item, set it up to be
				// added first.
				if (findDynamics.containsKey(null)) {
					IContributionItem item = findDynamics.get(null);
					dynamicEntry = new DynamicContributionItem(item);
					parent.addChild(dynamicEntry);
				}
			}
		}

		for (MMenuElement menuItem : menu.getChildren()) {
			if ((menuItem.getLabel() != null && menuItem.getLabel().length() != 0)
					|| (menuItem.getLocalizedLabel() != null && menuItem.getLocalizedLabel()
							.length() != 0) || (menuItem instanceof MHandledMenuItem)
					|| menuItem.getWidget() != null) {
				IContributionItem contributionItem;
				if (menuItem instanceof MMenu) {
					contributionItem = menuMngrRenderer.getManager((MMenu) menuItem);
				} else {
					contributionItem = menuMngrRenderer.getContribution(menuItem);
				}
				if (dynamicEntry != null
						&& contributionItem.equals(dynamicEntry.getIContributionItem())) {
					// If the last item added is the item meant to go before the
					// given dynamic entry, add the dynamic entry so it is in
					// the
					// correct order.
					dynamicEntry.addCurrentItem((MenuItem) menuItem.getWidget());
					// TODO: might not work
				} else {
					String text = menuItem.getLocalizedLabel();
					if (text == null || text.length() == 0) {
						text = menuItem.getLabel();
					}
					ImageDescriptor iconDescriptor = null;
					String iconURI = menuItem.getIconURI();
					if (iconURI != null && iconURI.length() > 0) {
						iconDescriptor = resUtils.imageDescriptorFromURI(URI.createURI(iconURI));
					}

					if (menuItem.getWidget() instanceof MenuItem) {
						MenuItem item = (MenuItem) menuItem.getWidget();
						if (text == null) {
							if ("".equals(item.getText())) { //$NON-NLS-1$
								continue;
							}
							text = item.getText();
						}
						if (iconDescriptor == null) {
							Image image = item.getImage();
							if (image != null) {
								iconDescriptor = ImageDescriptor.createFromImage(image);
							}
						}
					} else if (menuItem instanceof MHandledMenuItem) {
						MHandledMenuItem hmi = (MHandledMenuItem) menuItem;
						final String i18nLabel = hmi.getLocalizedLabel();
						if (i18nLabel != null) {
							text = i18nLabel;
						} else if (hmi.getWbCommand() != null) {
							try {
								text = hmi.getWbCommand().getName();
							} catch (NotDefinedException e) {
								// we'll just ignore a failure
							}
						}
					}
					DisplayItem menuEntry = new DisplayItem(text, contributionItem);

					if (iconDescriptor != null) {
						menuEntry.setImageDescriptor(iconDescriptor);
					}
					menuEntry.setActionSet((ActionSet) idToActionSet.get(getActionSetID(menuItem)));
					parent.addChild(menuEntry);

					if (ActionFactory.NEW.getId().equals(contributionItem.getId())) {
						initializeNewWizardsMenu(menuEntry);
						wizards = menuEntry;
					} else if (SHORTCUT_CONTRIBUTION_ITEM_ID_OPEN_PERSPECTIVE
							.equals(contributionItem.getId())) {
						initializePerspectivesMenu(menuEntry);
						perspectives = menuEntry;
					} else if (SHORTCUT_CONTRIBUTION_ITEM_ID_SHOW_VIEW.equals(contributionItem
							.getId())) {
						initializeViewsMenu(menuEntry);
						views = menuEntry;
					} else {
						if (menuItem instanceof MMenu) {// TODO:menuItem any
														// other instance
							createMenuEntries((MMenu) menuItem, menuEntry, trackDynamics);
						}
					}

					if (menuEntry.getChildren().isEmpty()) {
						menuEntry.setCheckState(getMenuItemIsVisible(menuEntry));
					}

					if (iconDescriptor == null) {
						if (parent != null && parent.getParent() == null) {
							menuEntry.setImageDescriptor(menuImageDescriptor);
						} else if (menuEntry.getChildren().size() > 0) {
							menuEntry.setImageDescriptor(submenuImageDescriptor);
						}
					}
				}
				if (trackDynamics && findDynamics.containsKey(contributionItem)) {
					IContributionItem item = findDynamics.get(contributionItem);
					dynamicEntry = new DynamicContributionItem(item);
					dynamicEntry.setCheckState(getMenuItemIsVisible(dynamicEntry));
					parent.addChild(dynamicEntry);
				}
			} else if (menuItem instanceof MOpaqueMenuItem) {
				IContributionItem contributionItem = menuMngrRenderer.getContribution(menuItem);
				if (contributionItem instanceof ActionContributionItem) {
					final IAction action = ((ActionContributionItem) contributionItem).getAction();
					DisplayItem menuEntry = new DisplayItem(action.getText(), contributionItem);
					menuEntry.setImageDescriptor(action.getImageDescriptor());
					menuEntry.setActionSet((ActionSet) idToActionSet
							.get(getActionSetID(contributionItem)));
					parent.addChild(menuEntry);
					if (menuEntry.getChildren().isEmpty()) {
						menuEntry.setCheckState(getMenuItemIsVisible(menuEntry));
					}
				}
			}
		}
	}

	private boolean getMenuItemIsVisible(DisplayItem item) {
		return getItemIsVisible(item, ModeledPageLayout.HIDDEN_MENU_PREFIX);
	}

	private boolean getToolbarItemIsVisible(DisplayItem item) {
		return getItemIsVisible(item, ModeledPageLayout.HIDDEN_TOOLBAR_PREFIX);
	}

	private boolean getItemIsVisible(DisplayItem item, String prefix) {
		return isAvailable(item)
				&& !(((WorkbenchPage) window.getActivePage()).getHiddenItems().contains(prefix
						+ getCommandID(item) + ",")); //$NON-NLS-1$
	}

	/**
	 * Causes a viewer to update the state of a category and all its ancestors.
	 * 
	 * @param viewer
	 * @param category
	 */
	private void updateCategoryAndParents(StructuredViewer viewer,
			Category category) {
		while (category.getParent() != shortcuts) {
			viewer.update(category, null);
			category = (Category) category.getParent();
		}
	}

	private DisplayItem createToolBarStructure(MTrimBar toolbar) {
		DisplayItem root = new DisplayItem(null, null); // Create a
		// root
		createToolbarEntries(toolbar, root);
		return root;
	}

	private boolean hasVisibleItems(MToolBar toolBar) {
		for (MToolBarElement e : toolBar.getChildren()) {
			if (!(e instanceof MToolBarSeparator)) {
				return true;
			}
		}
		return false;
	}

	private void createToolbarEntries(MTrimBar toolbar, DisplayItem parent) {
		if (toolbar == null)
			return;
		for (MTrimElement trimElement : toolbar.getChildren()) {
			if (trimElement instanceof MToolBar) {
				MToolBar toolBar = (MToolBar) trimElement;
				String text;
				ToolBarManager manager = toolbarMngrRenderer.getManager(toolBar);
				if (manager != null && hasVisibleItems(toolBar)) {
					IContributionItem contributionItem = (IContributionItem) trimElement
							.getTransientData().get("coolbar.object"); //$NON-NLS-1$
					Object name = trimElement.getTransientData().get("Name"); //$NON-NLS-1$
					if (name != null) {// && ((String) name).length() != 0
						text = (String) name;
					} else {
						text = getToolbarLabel(trimElement.getElementId());
					}
					DisplayItem toolBarEntry = new DisplayItem(text, contributionItem);
					toolBarEntry.setImageDescriptor(toolbarImageDescriptor);
					toolBarEntry.setActionSet((ActionSet) idToActionSet
							.get(getActionSetID(trimElement)));
					parent.addChild(toolBarEntry);
					toolBarEntry.setCheckState(getToolbarItemIsVisible(toolBarEntry));
					createToolbarEntries((MToolBar) trimElement, toolBarEntry);
				}
			}
		}
	}

	private void createToolbarEntries(MToolBar toolbar, DisplayItem parent) {
		if (toolbar == null)
			return;
		for (MToolBarElement element : toolbar.getChildren()) {
			IContributionItem contributionItem = toolbarMngrRenderer.getContribution(element);
			if (element instanceof MToolBarSeparator
					|| (contributionItem == null || contributionItem.isGroupMarker() || contributionItem
							.isSeparator())) {
				continue;
			}

			if (element instanceof MOpaqueToolItem) {
				if (contributionItem instanceof ActionContributionItem) {
					final IAction action = ((ActionContributionItem) contributionItem).getAction();
					DisplayItem toolbarEntry = new DisplayItem(action.getText(), contributionItem);
					toolbarEntry.setImageDescriptor(action.getImageDescriptor());
					toolbarEntry.setActionSet((ActionSet) idToActionSet
							.get(getActionSetID(contributionItem)));
					if (toolbarEntry.getChildren().isEmpty()) {
						toolbarEntry.setCheckState(contributionItem.isVisible());
					}
					parent.addChild(toolbarEntry);
				}
			} else {
				String text = null;
				if (element instanceof MItem) {
					text = getToolTipText((MItem) element);
				}
				ImageDescriptor iconDescriptor = null;
				String iconURI = element instanceof MItem ? ((MItem) element).getIconURI() : null;
				if (iconURI != null && iconURI.length() > 0) {
					iconDescriptor = resUtils.imageDescriptorFromURI(URI.createURI(iconURI));
				}
				if (element.getWidget() instanceof ToolItem) {
					ToolItem item = (ToolItem) element.getWidget();
					if (text == null) {
						text = item.getToolTipText();
					}
					if (iconDescriptor == null) {
						Image image = item.getImage();
						if (image != null) {
							iconDescriptor = ImageDescriptor.createFromImage(image);
						}
					}
				}
				if (text == null) {
					Object name = element.getTransientData().get("Name"); //$NON-NLS-1$
					if (name != null) { // && ((String) name).length() != 0
						text = (String) name;
					} else {
						text = getToolbarLabel(element.getElementId());
					}
				}

				DisplayItem toolBarEntry = new DisplayItem(text, contributionItem);

				if (iconDescriptor != null) {
					toolBarEntry.setImageDescriptor(iconDescriptor);
				}
				toolBarEntry.setActionSet((ActionSet) idToActionSet.get(getActionSetID(element)));
				if (toolBarEntry.getChildren().isEmpty()) {
					toolBarEntry.setCheckState(getToolbarItemIsVisible(toolBarEntry));
				}
				parent.addChild(toolBarEntry);
			}
		}
	}

	private ParameterizedCommand generateParameterizedCommand(final MHandledItem item,
			final IEclipseContext lclContext) {
		ECommandService cmdService = (ECommandService) lclContext.get(ECommandService.class
				.getName());
		Map<String, Object> parameters = null;
		List<MParameter> modelParms = item.getParameters();
		if (modelParms != null && !modelParms.isEmpty()) {
			parameters = new HashMap<String, Object>();
			for (MParameter mParm : modelParms) {
				parameters.put(mParm.getName(), mParm.getValue());
			}
		}
		ParameterizedCommand cmd = cmdService.createCommand(item.getCommand().getElementId(),
				parameters);
		item.setWbCommand(cmd);
		return cmd;
	}

	public String getToolTipText(MItem item) {
		String text = item.getLocalizedTooltip();
		if (item instanceof MHandledItem) {
			MHandledItem handledItem = (MHandledItem) item;
			EBindingService bs = (EBindingService) context.get(EBindingService.class.getName());
			ParameterizedCommand cmd = handledItem.getWbCommand();
			if (cmd == null) {
				cmd = generateParameterizedCommand(handledItem, context);
			}
			TriggerSequence sequence = bs.getBestSequenceFor(handledItem.getWbCommand());
			if (sequence != null) {
				if (text == null) {
					try {
						text = cmd.getName();
					} catch (NotDefinedException e) {
						return null;
					}
				}
				text = text + " (" + sequence.format() + ')'; //$NON-NLS-1$
			}
			return text;
		} else if (item instanceof MOpaqueMenuItem) {
			Object opaque = ((MOpaqueMenuItem) item).getOpaqueItem();
			if (opaque instanceof ActionContributionItem) {
				return ((ActionContributionItem) opaque).getAction().getText();
			}
		} else if (item instanceof MOpaqueToolItem) {
			Object opaque = ((MOpaqueToolItem) item).getOpaqueItem();
			if (opaque instanceof ActionContributionItem) {
				return ((ActionContributionItem) opaque).getAction().getToolTipText();
			}
		}
		return text;
	}

	/**
	 * Returns whether the shortcut tab should be shown.
	 * 
	 * @return <code>true</code> if the shortcut tab should be shown, and
	 *         <code>false</code> otherwise
	 * @since 3.0
	 */
	private boolean showShortcutTab() {
		return window.containsSubmenu(WorkbenchWindow.NEW_WIZARD_SUBMENU)
				|| window
						.containsSubmenu(WorkbenchWindow.OPEN_PERSPECTIVE_SUBMENU)
				|| window.containsSubmenu(WorkbenchWindow.SHOW_VIEW_SUBMENU);
	}

	private ArrayList getVisibleIDs(TreeItem root) {
		if (root == null) {
			return new ArrayList();
		}
		ArrayList ids = new ArrayList(root.getChildren().size());
		for (Iterator i = root.getChildren().iterator(); i.hasNext();) {
			DisplayItem object = (DisplayItem) i.next();
			if (object instanceof ShortcutItem && object.getState()) {
				ids.add(getParamID(object));
			}
		}
		return ids;
	}

	private void getChangedIds(DisplayItem item, List invisible, List visible) {
		if (item instanceof ShortcutItem)
			return;

		if (item == wizards || item == perspectives || item == views) {
			// We always want the top-level wizard/perspective/view shortcuts to
			// be visible, see bug 293448
			return;
		} else if (item.getChildren().size() > 0) {
			if (item.isChangedByUser()) {
				String id = getCommandID(item);
				if (item.getState())
					visible.add(id);
				else
					invisible.add(id);
			}
			for (Iterator i = item.getChildren().iterator(); i.hasNext();) {
				getChangedIds((DisplayItem) i.next(), invisible, visible);
			}
		} else if (item.isChangedByUser()) {
			String id = getCommandID(item);
			if (item.getState())
				visible.add(id);
			else
				invisible.add(id);
		}
	}

	private boolean updateHiddenElements(DisplayItem items, String currentHidden, String prefix) {
		boolean hasChanges = false;

		List<String> changedAndVisible = new ArrayList<String>();
		List<String> changedAndInvisible = new ArrayList<String>();
		getChangedIds(items, changedAndInvisible, changedAndVisible);

		// Remove explicitly 'visible' elements from the current list
		for (Iterator<String> iterator = changedAndVisible.iterator(); iterator.hasNext();) {
			String id = iterator.next();
			if (id != null && currentHidden.contains(prefix + id)) {
				hasChanges = true;
				((WorkbenchPage) window.getActivePage()).removeHiddenItems(prefix + id);
			}
		}

		// Add explicitly 'hidden' elements to the current list
		for (Iterator<String> iterator = changedAndInvisible.iterator(); iterator.hasNext();) {
			String id = iterator.next();
			if (id != null && !currentHidden.contains(prefix + id)) {
				hasChanges = true;
				((WorkbenchPage) window.getActivePage()).addHiddenItems(prefix + id);
			}
		}

		return hasChanges;
	}
	
	protected void okPressed() {
		// Shortcuts
		if (showShortcutTab()) {
			WorkbenchPage wPage = (WorkbenchPage) window.getActivePage();
			wPage.setNewShortcuts(getVisibleIDs(wizards), ModeledPageLayout.NEW_WIZARD_TAG);
			wPage.setNewShortcuts(getVisibleIDs(perspectives), ModeledPageLayout.PERSP_SHORTCUT_TAG);
			wPage.setNewShortcuts(getVisibleIDs(views), ModeledPageLayout.SHOW_VIEW_TAG);
		}

		// Determine if anything has changed and, if so, update the menu & tb's
		boolean requiresUpdate = false;
		
		// Action Sets
		ArrayList toAdd = new ArrayList();
		ArrayList toRemove = new ArrayList();

		for (Iterator i = actionSets.iterator(); i.hasNext();) {
			ActionSet actionSet = (ActionSet) i.next();
			if (!actionSet.wasChanged())
				continue;
			
			// Something has changed
			requiresUpdate = true;
			
			if (actionSet.isActive()) {
				toAdd.add(actionSet.descriptor);
			} else {
				toRemove.add(actionSet.descriptor);
			}
		}

		perspective.turnOnActionSets((IActionSetDescriptor[]) toAdd
				.toArray(new IActionSetDescriptor[toAdd.size()]));
		perspective.turnOffActionSets((IActionSetDescriptor[]) toRemove
				.toArray(new IActionSetDescriptor[toRemove.size()]));

		// Menu and Toolbar Items
		requiresUpdate |= updateHiddenElements(menuItems,
				((WorkbenchPage) window.getActivePage()).getHiddenItems(),
				ModeledPageLayout.HIDDEN_MENU_PREFIX);
		requiresUpdate |= updateHiddenElements(toolBarItems,
				((WorkbenchPage) window.getActivePage()).getHiddenItems(),
				ModeledPageLayout.HIDDEN_TOOLBAR_PREFIX);
		
		if (requiresUpdate) {
			perspective.updateActionBars();
		}
		
		super.okPressed();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.dialogs.TrayDialog#close()
	 */
	public boolean close() {
		tooltipHeading.dispose();

		for (Iterator i = toDispose.iterator(); i.hasNext();) {
			Resource resource = (Resource) i.next();
			resource.dispose();
		}

		treeManager.dispose();
		customizeActionBars.dispose();

		return super.close();
	}

	private String removeShortcut(String label) {
		if (label == null) {
			return label;
		}
		int end = label.lastIndexOf('@');
		if (end >= 0) {
			label = label.substring(0, end);
		}

		end = label.lastIndexOf('\t');
		if (end >= 0) {
			label = label.substring(0, end);
		}

		return label;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.dialogs.Dialog#applyDialogFont()
	 */
	protected boolean applyDialogFont() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
	 */
	protected boolean isResizable() {
		return true;
	}

	private void viewActionSet(final DisplayItem item) {
		if (item.getActionSet() != null) {
			viewActionSet(item.getActionSet());
		}
	}

	/**
	 * @param item
	 */
	private void viewActionSet(final ActionSet actionSet) {
		tabFolder.setSelection(actionSetTab);
		actionSetAvailabilityTable.reveal(actionSet);
		setSelectionOn(actionSetAvailabilityTable, actionSet);
		actionSetAvailabilityTable.getControl().setFocus();
	}

	/**
	 * Determines the state <code>item</code> should be (checked, gray or
	 * unchecked) based only on the leafs underneath it (unless it is indeed a
	 * leaf).
	 * 
	 * @param item
	 *            the item to find the state of
	 * @param provider
	 *            the content provider which will provide <code>item</code>'s
	 *            children
	 * @param filter
	 *            the filter that will only select elements in the currently
	 *            chosen action set
	 * @return {@link TreeManager#CHECKSTATE_CHECKED},
	 *         {@link TreeManager#CHECKSTATE_GRAY} or
	 *         {@link TreeManager#CHECKSTATE_UNCHECKED}
	 */
	private static int getLeafStates(TreeItem item,
			ITreeContentProvider provider, ViewerFilter filter) {
		Object[] children = provider.getChildren(item);

		boolean checkedFound = false;
		boolean uncheckedFound = false;

		for (int i = 0; i < children.length; i++) {
			if (filter.select(null, null, children[i])) {
				TreeItem child = (TreeItem) children[i];
				switch (getLeafStates(child, provider, filter)) {
				case TreeManager.CHECKSTATE_CHECKED: {
					checkedFound = true;
					break;
				}
				case TreeManager.CHECKSTATE_GRAY: {
					checkedFound = uncheckedFound = true;
					break;
				}
				case TreeManager.CHECKSTATE_UNCHECKED: {
					uncheckedFound = true;
					break;
				}
				}
				if (checkedFound && uncheckedFound) {
					return TreeManager.CHECKSTATE_GRAY;
				}
			}
		}

		if (!checkedFound && !uncheckedFound)
			return item.getState() ? TreeManager.CHECKSTATE_CHECKED
					: TreeManager.CHECKSTATE_UNCHECKED;
		return checkedFound ? TreeManager.CHECKSTATE_CHECKED
				: TreeManager.CHECKSTATE_UNCHECKED;
	}

	/**
	 * Sets all leafs under a {@link DisplayItem} to either visible or
	 * invisible. This is for use with the action set trees, where the only
	 * state used is that of leafs, and the rest is rolled up to the parents.
	 * Thus, this method effectively sets the state of the entire branch.
	 * 
	 * @param item
	 *            the item whose leafs underneath (or itself, if it is a leaf)
	 *            to <code>value</code>
	 * @param value
	 *            <code>true</code>for visible, <code>false</code> for invisible
	 * @param provider
	 *            the content provider which will provide <code>item</code>'s
	 *            children
	 * @param filter
	 *            the filter that will only select elements in the currently
	 *            chosen action set
	 */
	private static void setAllLeafs(DisplayItem item, boolean value,
			ITreeContentProvider provider, ViewerFilter filter) {
		Object[] children = provider.getChildren(item);
		boolean isLeaf = true;

		for (int i = 0; i < children.length; i++) {
			isLeaf = false;
			if (filter.select(null, null, children[i])) {
				DisplayItem child = (DisplayItem) children[i];
				setAllLeafs(child, value, provider, filter);
			}
		}

		if (isLeaf) {
			item.setCheckState(value);
		}
	}
}

Back to the top