Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5670f3830dcc1e308aecb65bd0ec9bcd9452a621 (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
/*****************************************************************************
 * Copyright (c) 2009 CEA LIST.
 * 
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.customization.palette.dialog;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteStack;
import org.eclipse.gef.palette.PaletteToolbar;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.ui.palette.PaletteCustomizer;
import org.eclipse.gmf.runtime.common.core.service.ProviderPriority;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditorWithFlyOutPalette;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.papyrus.customization.palette.proxies.XMLPaletteDefinitionProxyFactory;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.Messages;
import org.eclipse.papyrus.uml.diagram.common.part.PaletteUtil;
import org.eclipse.papyrus.uml.diagram.common.part.PapyrusPalettePreferences;
import org.eclipse.papyrus.uml.diagram.common.service.AspectCreationEntry;
import org.eclipse.papyrus.uml.diagram.common.service.IPapyrusPaletteConstant;
import org.eclipse.papyrus.uml.diagram.common.service.PapyrusPaletteService;
import org.eclipse.papyrus.uml.diagram.common.service.XMLPaletteDefinitionWalker;
import org.eclipse.papyrus.uml.diagram.common.service.palette.IAspectAction;
import org.eclipse.papyrus.uml.diagram.common.service.palette.StereotypeAspectActionProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TreeDropTargetEffect;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
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.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IEditorPart;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.UMLPackage;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 * Wizard page for information about the new local palette definition
 */
public class LocalPaletteContentPage extends WizardPage implements Listener {

	/** editor part in which the palette is created */
	protected IEditorPart editorPart;

	/** available tools viewer */
	protected TreeViewer availableToolsViewer;

	/** label provider for the tree viewer */
	protected PaletteLabelProvider paletteLabelProvider;

	/** icon path when tools are hidden */
	protected static final String HIDDEN_TOOLS_ICON = "/icons/tools_hidden.gif";

	/** icon path when tools are shown */
	protected static final String SHOWN_TOOLS_ICON = "/icons/tools_shown.gif";

	/** path to the icon */
	protected static final String WIZARD_ICON = "/icons/local_desc_wiz.png";

	/** icon path when drawers are hidden */
	protected static final String SHOWN_DRAWERS_ICON = "/icons/drawers_shown.gif";

	/** icon path when drawers are shown */
	protected static final String HIDDEN_DRAWERS_ICON = "/icons/drawers_hidden.gif";

	/** icon path for the add button */
	protected static final String ADD_ICON = "/icons/arrow_right.gif";

	/** icon path for the remove button */
	protected static final String REMOVE_ICON = "/icons/arrow_left.gif";

	/** icon path for the delete button */
	protected static final String DELETE_ICON = "/icons/delete.gif";

	/** icon path for the create drawer button */
	protected static final String CREATE_DRAWERS_ICON = "/icons/new_drawer.gif";

	/** icon path for the create separator button */
	protected String CREATE_SEPARATOR_ICON = "/icons/separator.gif";

	/** icon path for the create stack button */
	protected String CREATE_STACK_ICON = "/icons/stack.gif";

	/** icon path for the delete drawer button */
	protected static final String DELETE_DRAWERS_ICON = "/icons/delete.gif";

	/** icon for the content provider switch button */
	protected String SWITCH_CONTENT_PROVIDER_ICON = "/icons/switch_provider.gif";

	/** label for the standard tools */
	protected static final String UML_TOOLS_LABEL = "UML tools";

	/** icon path for the edit drawer button */
	protected static final String EDIT_ICON = "/icons/obj16/file.gif";

	/** instance of the filter used to show/hide drawers */
	protected final ViewerFilter drawerFilter = new DrawerFilter();

	/** instance of the filter used to show/hide tools */
	protected final ViewerFilter toolFilter = new ToolFilter();

	/** stored preferences */
	protected List<String> storedPreferences;

	/** add button */
	protected Button addButton;

	/** remove button */
	protected Button removeButton;

	/** tree viewer for the new palette */
	protected TreeViewer paletteTreeViewer;

	/** document for element creation */
	protected Document document;

	/** content node for the palette viewer */
	protected PaletteContainerProxy contentNode;

	/** combo to select which profile tools should be visible */
	protected Combo profileCombo;

	/** list of profiles that can provide tools */
	protected List<String> profileComboList = new ArrayList<String>();

	/** tool item in charge of toggling content providers in the available tool viewer */
	protected ToolItem toggleContentProvider;

	/** required profile by this palette */
	protected Set<String> requiredProfiles;

	/** palette customizer used for the palette */
	protected PaletteCustomizer customizer;

	/** current selected entry proxy */
	protected PaletteEntryProxy selectedEntryProxy;

	/** class in charge of the information composite */
	protected PaletteEntryProxyInformationComposite informationComposite = new PaletteEntryProxyInformationComposite();

	/** class in charge of the aspect tool information composite */
	protected AspectActionsInformationComposite aspectActionComposite = new AspectActionsInformationComposite();

	protected ToolBar toolbar;

	/** validator key for toolbar items */
	protected final static String VALIDATOR = "validator";

	/** priority of the current edited palette */
	protected ProviderPriority priority;


	/**
	 * Creates a new wizard page with the given name, title, and image.
	 * 
	 * @param part
	 *        the editor part in which the wizard was created
	 */
	public LocalPaletteContentPage(IEditorPart part, PaletteCustomizer customizer) {
		super(Messages.Local_Palette_ContentPage_Name, Messages.Local_Palette_ContentPage_Title, Activator.getImageDescriptor(WIZARD_ICON));
		this.editorPart = part;
		this.customizer = customizer;
	}

	/**
	 * {@inheritDoc}
	 */
	public void createControl(Composite parent) {

		// initialize dialog units
		initializeDialogUnits(parent);

		// Create a new composite as there is the title bar seperator
		// to deal with
		Composite control = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(4, false);
		control.setLayout(layout);
		control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		setControl(control);

		// create Available Tools Group
		createAvailableToolsGroup();

		// create add/remove buttons
		createAddRemoveButtons();

		// create Palette Group
		createPalettePreviewGroup();

		// create tool description group (custom name, description, and perhaphs icon.... Just under this group, another one with aspect actions)
		createToolDescriptionGroup();

		// just under, creates a new line of composites...
		createAspectActionComposite();



		// add listeners inter-groups
		ISelectionChangedListener listener = createToolsViewerSelectionChangeListener();
		availableToolsViewer.addSelectionChangedListener(listener);
		paletteTreeViewer.addSelectionChangedListener(listener);

		// end of the control creation
		Dialog.applyDialogFont(control);

		validatePage();
		// Show description on opening
		setErrorMessage(null);
		setMessage(null);
		setControl(control);
	}

	/**
	 * Creates the composite group that presents information about ascpect actions for aspect tools
	 */
	protected void createAspectActionComposite() {
		aspectActionComposite.createComposite((Composite)getControl(), getAllAppliedProfiles());
	}

	/**
	 * Creates the composite group that presents information about current selected tool
	 */
	protected void createToolDescriptionGroup() {
		informationComposite.createComposite((Composite)getControl(), getAllAppliedProfiles());
	}

	/**
	 * update the preferences to have all tools accessible
	 */
	protected void updatePreferences() {
		// change => set to no hidden palettes
		storedPreferences = PapyrusPalettePreferences.getHiddenPalettes(editorPart);

		// remove all, but should only remove visible palettes
		for(String id : storedPreferences) {
			PapyrusPalettePreferences.changePaletteVisibility(id, editorPart, true);
		}
	}

	/**
	 * Restore preferences
	 */
	public void restorePreferences() {
		// remove all, but should only remove visible palettes
		for(String id : storedPreferences) {
			PapyrusPalettePreferences.changePaletteVisibility(id, editorPart, false);
		}
	}

	/**
	 * creates the palette preview group
	 */
	protected void createPalettePreviewGroup() {
		Composite parent = (Composite)getControl();
		Composite paletteComposite = new Composite(parent, SWT.NONE);

		GridLayout layout = new GridLayout(2, true);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		paletteComposite.setLayout(layout);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		paletteComposite.setLayoutData(data);

		Label label = new Label(paletteComposite, SWT.NONE);
		label.setText(Messages.Local_Palette_Palette_Preview);
		data = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		label.setLayoutData(data);

		toolbar = new ToolBar(paletteComposite, SWT.HORIZONTAL);
		data = new GridData(SWT.RIGHT, SWT.FILL, false, false);
		toolbar.setLayoutData(data);
		populatePalettePreviewToolBar(toolbar);

		Tree tree = new Tree(paletteComposite, SWT.SINGLE | SWT.BORDER);
		data = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
		data.widthHint = 185;
		// Make the tree this tall even when there is nothing in it. This will keep the
		// dialog from shrinking to an unusually small size.
		data.heightHint = 200;
		tree.setLayoutData(data);
		paletteTreeViewer = new TreeViewer(tree);
		paletteTreeViewer.setContentProvider(new PaletteContentProvider(paletteTreeViewer));
		paletteTreeViewer.setLabelProvider(new PaletteProxyLabelProvider());
		paletteTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

			public void selectionChanged(SelectionChangedEvent event) {
				handlePalettePreviewSelectionChanged(event);
			}
		});

		addPalettePreviewDropSupport();
		addPalettePreviewDragSupport();
		addPalettePreviewEditSupport();

		paletteTreeViewer.setInput(contentNode);
	}

	/**
	 * handle the selection change event for the palette preview
	 * 
	 * @param event
	 *        the event that is thrown by the palette viewer
	 */
	protected void handlePalettePreviewSelectionChanged(SelectionChangedEvent event) {
		// retrieve current selection
		ITreeSelection selection = (TreeSelection)event.getSelection();
		Object firstSelected = selection.getFirstElement();
		if(firstSelected instanceof PaletteEntryProxy) {
			// update the current selected palette entry proxy
			selectedEntryProxy = ((PaletteEntryProxy)firstSelected);
			informationComposite.setSelectedEntryProxy(selectedEntryProxy);
			aspectActionComposite.setSelectedEntryProxy(selectedEntryProxy);
		}

		// update toolbar
		if(toolbar != null && !toolbar.isDisposed()) {
			for(int i = 0; i < toolbar.getItemCount(); i++) {
				ToolItem item = toolbar.getItem(i);
				Object validator = item.getData(VALIDATOR);
				if(validator instanceof ToolBarItemValidator) {
					item.setEnabled(((ToolBarItemValidator)validator).isEnable());
				}
			}
		}

	}

	/**
	 * @{inheritDoc
	 */
	@Override
	public void dispose() {
		super.dispose();
	}

	/**
	 * Adds the behavior for the double click strategy
	 */
	protected void addPalettePreviewEditSupport() {
		paletteTreeViewer.addDoubleClickListener(new IDoubleClickListener() {

			/**
			 * {@inheritDoc}
			 */
			public void doubleClick(DoubleClickEvent event) {
				// retrieve current item double clicked...
				ITreeSelection selection = (TreeSelection)event.getSelection();
				Object firstSelected = selection.getFirstElement();
				if(firstSelected instanceof PaletteLocalDrawerProxy) {
					UpdateLocalDrawerWizard wizard = new UpdateLocalDrawerWizard(((PaletteLocalDrawerProxy)firstSelected).getParent(), (PaletteLocalDrawerProxy)firstSelected);
					WizardDialog dialog = new WizardDialog(getShell(), wizard);
					dialog.open();
					//paletteTreeViewer.refresh();
				}
			}
		});

	}

	/**
	 * Add drop behavior for the palette preview
	 */
	protected void addPalettePreviewDropSupport() {
		// transfer types
		Transfer[] transfers = new Transfer[]{ LocalSelectionTransfer.getTransfer() };

		// drag listener
		DropTargetListener listener = new TreeDropTargetEffect(paletteTreeViewer.getTree()) {

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void drop(DropTargetEvent event) {
				super.drop(event);

				// create proxy and adds it to its target parent
				PaletteEntryProxy target = (PaletteEntryProxy)((TreeItem)event.item).getData();
				if(target == null) {
					target = (PaletteContainerProxy)paletteTreeViewer.getInput();
				}

				// get the elements from the drag listener (either a palette entry or a palette
				// entry proxy)
				IStructuredSelection transferedSelection = (IStructuredSelection)LocalSelectionTransfer.getTransfer().nativeToJava(event.currentDataType);
				Object entry = transferedSelection.getFirstElement();

				// creates the proxy for the element to be dropped
				PaletteEntryProxy entryProxy = createNodeFromEntry(entry);

				if(entryProxy == null) {
					return;
				}
				if(target instanceof PaletteContainerProxy) {
					// tries to remove from its parent if possible
					if(entryProxy.getParent() != null) {
						entryProxy.getParent().removeChild(entryProxy);
					}
					((PaletteContainerProxy)target).addChild(entryProxy);
					paletteTreeViewer.expandToLevel(target, 1);
				} else if(target instanceof PaletteEntryProxy) {
					// tries to remove from its parent if possible
					if(entryProxy.getParent() != null) {
						entryProxy.getParent().removeChild(entryProxy);
					}
					target.getParent().addChild(entryProxy, target);
					paletteTreeViewer.expandToLevel(target.getParent(), 1);
				} else {
					// add to parent...
					target.getParent().addChild(entryProxy);
					paletteTreeViewer.expandToLevel(target.getParent(), TreeViewer.ALL_LEVELS);
				}
				setPageComplete(validatePage());
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void dragOver(DropTargetEvent event) {
				super.dragOver(event);

				IStructuredSelection transferedSelection = (IStructuredSelection)LocalSelectionTransfer.getTransfer().nativeToJava(event.currentDataType);
				// check selection is compatible for drop target

				TreeItem item = paletteTreeViewer.getTree().getItem(paletteTreeViewer.getTree().toControl(new Point(event.x, event.y)));

				checkSelectionForDrop(transferedSelection, item, event);
			}
		};

		paletteTreeViewer.addDropSupport(DND.DROP_LINK | DND.DROP_MOVE, transfers, listener);
	}

	/**
	 * Adds drag ability to the palette preview composite
	 */
	protected void addPalettePreviewDragSupport() {

		// transfer types
		Transfer[] transfers = new Transfer[]{ LocalSelectionTransfer.getTransfer() };

		// drag listener
		DragSourceListener listener = new DragSourceAdapter() {

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void dragStart(DragSourceEvent event) {
				super.dragStart(event);
				event.data = paletteTreeViewer.getSelection();
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void dragSetData(DragSourceEvent event) {
				super.dragSetData(event);
				LocalSelectionTransfer.getTransfer().setSelection(paletteTreeViewer.getSelection());
			}

		};

		paletteTreeViewer.addDragSupport(DND.DROP_MOVE, transfers, listener);
	}

	/**
	 * Checks if the selection can be added to the target widget
	 * 
	 * @param transferedSelection
	 *        the selection to be dropped
	 * @param widget
	 *        the widget where to drop
	 * @return <code>true</code> if element can be dropped
	 */
	protected void checkSelectionForDrop(IStructuredSelection transferedSelection, TreeItem item, DropTargetEvent event) {
		event.detail = DND.DROP_NONE;
		Object entry = transferedSelection.getFirstElement();
		// handle only first selected element
		if(item == null) {
			// adding to the root, should only be a drawer
			if(entry instanceof PaletteDrawer) {
				event.detail = DND.DROP_LINK;
			}
		} else {
			PaletteEntryProxy targetProxy = (PaletteEntryProxy)item.getData();
			switch(targetProxy.getType()) {
			case DRAWER:
				if(entry instanceof ToolEntry) {
					event.detail = DND.DROP_LINK;
				} else if(entry instanceof PaletteEntryProxy) {
					event.detail = DND.DROP_MOVE;
				}
				break;
			case STACK:
				if(entry instanceof ToolEntry) {
					event.detail = DND.DROP_LINK;
				} else if(entry instanceof PaletteEntryProxy && !(entry instanceof PaletteLocalStackProxy)) {
					event.detail = DND.DROP_MOVE;
				}
				break;
			case TOOL:
				if(entry instanceof ToolEntry) {
					event.detail = DND.DROP_LINK; // add the selected tool before the destination
					// tool
				} else if(entry instanceof PaletteEntryProxy) {
					event.detail = DND.DROP_MOVE; // moves the element before the entry
				}
				break;
			case SEPARATOR:
				if(entry instanceof PaletteEntryProxy) {
					event.detail = DND.DROP_MOVE;
				}
				break;
			default:
				break;
			}
		}
	}

	/**
	 * Sets an empty content for the palette preview
	 */
	public void initializeContent() {
		contentNode = new PaletteContainerProxy(null);

		// adds a default local drawer
		PaletteLocalDrawerProxy proxy = new PaletteLocalDrawerProxy("Default", generateID("Drawer_"), "/icons/drawer.gif", "Default Drawer");
		contentNode.addChild(proxy);

		setPageComplete(false);
	}

	/**
	 * Sets the initial content for the palette preview
	 */
	public void initializeContent(PapyrusPaletteService.LocalProviderDescriptor descriptor) {
		// retrieve the xml definition file
		String xmlPath = PapyrusPalettePreferences.getPalettePathFromID(descriptor.getContributionID());

		// parse the content file
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		documentBuilderFactory.setNamespaceAware(true);
		try {
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

			// Bundle bundle = Platform.getBundle(pluginID);
			// URL url = bundle.getEntry(path);

			File file = Activator.getDefault().getStateLocation().append(xmlPath).toFile();
			if(!file.exists()) {
				Activator.log.error("Impossible to load file: " + file, null);
			} else {
				Document document = documentBuilder.parse(file);
				Map<String, PaletteEntry> entries = PaletteUtil.getAvailableEntriesSet(editorPart, ProviderPriority.HIGHEST);
				
				XMLPaletteDefinitionProxyFactory factory = new XMLPaletteDefinitionProxyFactory(entries);
				XMLPaletteDefinitionWalker walker = new XMLPaletteDefinitionWalker(factory);
				
				for(int i = 0; i < document.getChildNodes().getLength(); i++) {
					Node node = document.getChildNodes().item(i);
					if(IPapyrusPaletteConstant.PALETTE_DEFINITION.equals(node.getNodeName())) {
						walker.walk(node);
					}
				}
				contentNode = factory.getRootProxy();

				// tells that the page can be closed directly without modifying the palette
				setPageComplete(true);
				return;
			}
		} catch (ParserConfigurationException e) {
			Activator.log.error(e);
		} catch (IOException e) {
			Activator.log.error(e);
		} catch (SAXException e) {
			Activator.log.error(e);
		}

		// paletteTreeViewer.setInput(contentNode);
		contentNode = new PaletteContainerProxy(null);
	}

	/**
	 * Saves the xml document into file
	 * 
	 * @param document
	 *        the document to save
	 * @param path
	 *        name of the file
	 * @return the file created or updated
	 */
	protected File loadDocument(Document document, String path) {
		File file = null;
		try {
			// create the file that stores the XML configuration
			file = Activator.getDefault().getStateLocation().append(path).toFile();
			Transformer aTransformer = TransformerFactory.newInstance().newTransformer();

			Source src = new DOMSource(document);
			Result dest = new StreamResult(file);
			aTransformer.transform(src, dest);
		} catch (TransformerConfigurationException e) {
			Activator.log.error(e);
		} catch (TransformerException e) {
			Activator.log.error(e);
		}
		return file;

	}

	/**
	 * populates the preview palette toolbar
	 * 
	 * @param toolbar
	 *        the toolbar to populate
	 */
	protected void populatePalettePreviewToolBar(ToolBar toolbar) {
		PaletteEntryProxySelectedValidator validator = new PaletteEntryProxySelectedValidator();
		createToolBarItem(toolbar, DELETE_ICON, Messages.PapyrusPaletteCustomizerDialog_RemoveButtonTooltip, createRemoveElementListener(), validator);
		createToolBarItem(toolbar, EDIT_ICON, Messages.PapyrusPaletteCustomizerDialog_EditButtonTooltip, createEditElementListener(), new EditElementToolBarItemValidator());
		createToolBarItem(toolbar, CREATE_DRAWERS_ICON, Messages.Local_Palette_Create_Drawer_Tooltip, createNewDrawerListener(), null);
		createToolBarItem(toolbar, CREATE_SEPARATOR_ICON, Messages.Local_Palette_Create_Separator_Tooltip, createNewSeparatorListener(), validator);
		createToolBarItem(toolbar, CREATE_STACK_ICON, Messages.Local_Palette_Create_Stack_Tooltip, createNewStackListener(), validator);
	}

	/**
	 * Edits the current selected elements. This works for drawers, should work on more elements
	 * 
	 * @return the listener for the edit button
	 */
	protected Listener createEditElementListener() {
		return new Listener() {

			/**
			 * {@inheritDoc}
			 */
			public void handleEvent(Event event) {
				IStructuredSelection selection = (IStructuredSelection)paletteTreeViewer.getSelection();
				if(selection == null || selection.size() < 1) {
					return;
				}

				Object selected = selection.getFirstElement();
				if(selected instanceof PaletteLocalDrawerProxy) {
					UpdateLocalDrawerWizard wizard = new UpdateLocalDrawerWizard(((PaletteLocalDrawerProxy)selected).getParent(), (PaletteLocalDrawerProxy)selected);
					WizardDialog dialog = new WizardDialog(getShell(), wizard);
					dialog.open();
				}

				// paletteTreeViewer.refresh();
			}
		};
	}

	/**
	 * Creates the listener for the remove item(s) button
	 * 
	 * @return the listener for the remove button
	 */
	protected Listener createRemoveElementListener() {
		return new Listener() {

			/**
			 * {@inheritDoc}
			 */
			public void handleEvent(Event event) {
				IStructuredSelection selection = (IStructuredSelection)paletteTreeViewer.getSelection();
				if(selection == null || selection.size() < 1) {
					return;
				}

				Iterator<Object> it = selection.iterator();
				while(it.hasNext()) {
					Object o = it.next();
					if(o instanceof PaletteEntryProxy) {
						PaletteEntryProxy proxyToDelete = (PaletteEntryProxy)o;
						// create a new entry in the document
						// get container of the proxy to be deleted
						PaletteContainerProxy parentProxy = proxyToDelete.getParent();
						if(parentProxy != null) {
							parentProxy.removeChild(proxyToDelete);
						}
					}
				}
				// paletteTreeViewer.refresh();
			}
		};
	}

	/**
	 * Creates the listener for the new drawer tool item
	 * 
	 * @return the listener created
	 */
	protected Listener createNewDrawerListener() {
		return new Listener() {

			/**
			 * {@inheritDoc}
			 */
			public void handleEvent(Event event) {
				// retrieve selected container
				PaletteContainerProxy containerProxy;
				containerProxy = (PaletteContainerProxy)paletteTreeViewer.getInput();
				NewDrawerWizard wizard = new NewDrawerWizard(containerProxy);
				WizardDialog wizardDialog = new WizardDialog(new Shell(), wizard);
				wizardDialog.open();
				// paletteTreeViewer.refresh();
				setPageComplete(validatePage());
			}
		};
	}

	/**
	 * Creates the listener for the new stack tool item
	 * 
	 * @return the listener created
	 */
	protected Listener createNewStackListener() {
		return new Listener() {

			/**
			 * {@inheritDoc}
			 */
			public void handleEvent(Event event) {
				// retrieve selected element
				Object object = ((IStructuredSelection)paletteTreeViewer.getSelection()).getFirstElement();

				// if element = drawer => the new stack must be created at the end of the drawer's
				// children list
				// if element = tool => must be placed before this tool
				// else : nothinng to do
				if(object instanceof PaletteLocalDrawerProxy) {
					String id = generateID("Stack");
					PaletteLocalStackProxy proxy = new PaletteLocalStackProxy(id);
					((PaletteLocalDrawerProxy)object).addChild(proxy);
				} else if(object instanceof PaletteEntryProxy) {
					String id = generateID("Stack");
					PaletteLocalStackProxy proxy = new PaletteLocalStackProxy(id);
					// retrieve parent
					PaletteEntryProxy childProxy = (PaletteEntryProxy)object;
					PaletteContainerProxy parentProxy = childProxy.getParent();
					parentProxy.addChild(proxy, childProxy);
				}

				// paletteTreeViewer.refresh();
				setPageComplete(validatePage());
			}
		};
	}

	/**
	 * Generates the ID for a local element
	 * 
	 * @param base
	 *        the begining of the id
	 * @return the separator id
	 */
	protected String generateID(String base) {
		StringBuffer id = new StringBuffer();
		id.append(base);
		id.append("_");
		id.append(System.currentTimeMillis());

		return id.toString();
	}

	/**
	 * Creates the listener for the new separator tool item
	 * 
	 * @return the listener created
	 */
	protected Listener createNewSeparatorListener() {
		return new Listener() {

			/**
			 * {@inheritDoc}
			 */
			public void handleEvent(Event event) {
				// retrieve selected element
				Object object = ((IStructuredSelection)paletteTreeViewer.getSelection()).getFirstElement();

				// if element = drawer => the new stack must be created at the end of the drawer's
				// children list
				// if element = tool => must be placed before this tool
				// else : nothinng to do
				if(object instanceof PaletteLocalDrawerProxy) {
					String id = generateID("Separator");
					PaletteLocalSeparatorProxy proxy = new PaletteLocalSeparatorProxy(id);
					((PaletteLocalDrawerProxy)object).addChild(proxy);
				} else if(object instanceof PaletteEntryProxy) {
					String id = generateID("Separator");
					PaletteLocalSeparatorProxy proxy = new PaletteLocalSeparatorProxy(id);
					// retrieve parent
					PaletteEntryProxy childProxy = (PaletteEntryProxy)object;
					PaletteContainerProxy parentProxy = childProxy.getParent();
					parentProxy.addChild(proxy, childProxy);
				}

				setPageComplete(validatePage());
			}
		};
	}

	/**
	 * Creates a toolbar item.
	 * 
	 * @param toolbar
	 *        the parent toolbar
	 * @param itemIcon
	 *        path for icon
	 * @param tooltip
	 *        tooltip text for the toolbar item
	 * @param listener
	 *        listener for tool bar item
	 */
	protected void createToolBarItem(ToolBar toolbar, String itemIcon, String tooltip, Listener listener, ToolBarItemValidator validator) {
		ToolItem item = new ToolItem(toolbar, SWT.BORDER);
		item.setImage(Activator.getPluginIconImage(Activator.ID, itemIcon));
		item.setToolTipText(tooltip);
		item.addListener(SWT.Selection, listener);
		item.setData(VALIDATOR, validator);
	}

	/**
	 * creates the buttons to add/remove entries
	 */
	protected void createAddRemoveButtons() {
		Composite composite = new Composite((Composite)getControl(), SWT.NONE);
		GridLayout layout = new GridLayout(1, true);
		composite.setLayout(layout);

		GridData data = new GridData(SWT.CENTER, SWT.CENTER, false, true);
		composite.setLayoutData(data);

		addButton = new Button(composite, SWT.NONE);
		addButton.setImage(Activator.getPluginIconImage(Activator.ID, ADD_ICON));
		addButton.setToolTipText(Messages.PapyrusPaletteCustomizerDialog_AddButtonTooltip);
		addButton.addMouseListener(createAddButtonListener());
		addButton.setEnabled(false);
		addButton.addListener(SWT.MouseUp, this);

		removeButton = new Button(composite, SWT.NONE);
		removeButton.setImage(Activator.getPluginIconImage(Activator.ID, REMOVE_ICON));
		removeButton.setToolTipText(Messages.PapyrusPaletteCustomizerDialog_RemoveButtonTooltip);
		removeButton.addMouseListener(createRemoveButtonListener());
		removeButton.setEnabled(false);
		removeButton.addListener(SWT.MouseUp, this);
	}

	/**
	 * selection listener for the tools viewer , to update the state of the add button
	 * 
	 * @return the new created selection listener
	 */
	protected ISelectionChangedListener createToolsViewerSelectionChangeListener() {
		return new ISelectionChangedListener() {

			/**
			 * {@inheritDoc}
			 */
			public void selectionChanged(SelectionChangedEvent event) {

				// get source and target selection
				// check source entry can be added to target entry
				Object source = ((IStructuredSelection)availableToolsViewer.getSelection()).getFirstElement();
				Object target = ((IStructuredSelection)paletteTreeViewer.getSelection()).getFirstElement();

				// manage add button
				if(isAddValidTarget(source, target)) {
					addButton.setEnabled(true);
				} else {
					addButton.setEnabled(false);
				}

				// manage remove button
				if(isRemoveValidSource(target)) {
					removeButton.setEnabled(true);
				} else {
					removeButton.setEnabled(false);
				}

			}

			/**
			 * Returns true if the source can be added to the target
			 * 
			 * @param source
			 *        the source object
			 * @param target
			 *        the target object
			 * @return <code>true</code> if the source can be added to the target
			 */
			protected boolean isAddValidTarget(Object source, Object target) {
				if(!(source instanceof PaletteEntry)) {
					return false;
				}

				// case1: source is a drawer.
				// it can only be added to the root element (no selection)
				// case2: source is a palette tool
				// it can't be added to the root element
				// it can only be added to a container element (drawer or stack)
				if(source instanceof PaletteDrawer) {
					if(target == null) {
						return true;
					}
					return false;
				} else if(source instanceof ToolEntry) {
					if(target instanceof PaletteEntryProxy) {
						EntryType type = ((PaletteEntryProxy)target).getType();
						switch(type) {
						case DRAWER:
						case STACK:
							return true;
						default:
							return false;
						}
					}
					return false;
				}
				return false;
			}

			/**
			 * Returns true if the source can be added to the target
			 * 
			 * @param source
			 *        the source object
			 * @return <code>true</code> if the source can be removed (not null and instanceof
			 *         PaletteEntryProxy)
			 */
			protected boolean isRemoveValidSource(Object source) {
				if(source instanceof PaletteEntryProxy) {
					return true;
				}
				return false;
			}
		};

	}

	/**
	 * Creates the add button listener
	 */
	protected MouseListener createAddButtonListener() {
		return new MouseListener() {

			public void mouseUp(MouseEvent e) {
				// add the element selected on the left to the right tree
				// check the selection.
				IStructuredSelection selection = (IStructuredSelection)availableToolsViewer.getSelection();
				if(selection == null || selection.size() < 1) {
					return;
				}
				PaletteEntry entry = (PaletteEntry)selection.getFirstElement();
				if(entry == null) {
					return;
				}

				// find the selection on the right
				selection = (IStructuredSelection)paletteTreeViewer.getSelection();

				PaletteEntryProxy parentNode = (PaletteEntryProxy)selection.getFirstElement();
				// Bugfix: only drawers can be added to root element
				if(parentNode == null && entry instanceof PaletteDrawer) {
					parentNode = (PaletteContainerProxy)paletteTreeViewer.getInput();
				}

				// check we have a containe here
				if(!(parentNode instanceof PaletteContainerProxy)) {
					return;
				}

				// create a new entry in the document
				PaletteEntryProxy proxy = createNodeFromEntry(entry);
				((PaletteContainerProxy)parentNode).addChild(proxy);

				paletteTreeViewer.expandToLevel(parentNode, 1);
			}

			/**
			 * {@inheritDoc}
			 */
			public void mouseDown(MouseEvent e) {
				// do nothing
			}

			/**
			 * {@inheritDoc}
			 */
			public void mouseDoubleClick(MouseEvent e) {
				// do nothing
			}
		};
	}

	/**
	 * Creates a node in the xml document from the given entry
	 * 
	 * @param entry
	 *        the palette entry from which to create the node
	 * @param parentNode
	 *        the parent node for the newly created node
	 * @return the created entry proxy or <code>null</code>
	 */
	protected PaletteEntryProxy createNodeFromEntry(Object entry) {
		PaletteEntryProxy entryProxy = null;

		if(entry instanceof AspectCreationEntry) {
			// should modify id of the element here, otherwise, different elements would have the same id...
			entryProxy = new PaletteAspectToolEntryProxy(((AspectCreationEntry)entry).clone());
		} else if(entry instanceof CombinedTemplateCreationEntry) {
			CombinedTemplateCreationEntry originalEntry = (CombinedTemplateCreationEntry)entry;
			// create a new Aspect entry proxy
			AspectCreationEntry aspectEntry = new AspectCreationEntry(originalEntry.getLabel(), originalEntry.getDescription(), originalEntry.getId() + "_" + System.currentTimeMillis(), originalEntry.getSmallIcon(), originalEntry, new HashMap<Object, Object>());
			entryProxy = new PaletteAspectToolEntryProxy(aspectEntry);
		} else if(entry instanceof PaletteContainer) {
			entryProxy = new PaletteContainerProxy((PaletteContainer)entry);
		} else if(entry instanceof PaletteEntryProxy) {
			entryProxy = ((PaletteEntryProxy)entry);
		}
		return entryProxy;
	}

	/**
	 * Creates the add button listener
	 */
	protected MouseListener createRemoveButtonListener() {
		return new MouseListener() {

			public void mouseUp(MouseEvent e) {
				// remove the element selected on the right
				// add the element selected on the left to the right tree
				// check the selection.
				IStructuredSelection selection = (IStructuredSelection)paletteTreeViewer.getSelection();
				if(selection == null || selection.size() < 1) {
					return;
				}
				PaletteEntryProxy proxyToDelete = (PaletteEntryProxy)selection.getFirstElement();
				if(proxyToDelete == null) {
					return;
				}

				// create a new entry in the document
				// get container of the proxy to be deleted
				PaletteContainerProxy parentProxy = proxyToDelete.getParent();
				parentProxy.removeChild(proxyToDelete);
			}

			/**
			 * {@inheritDoc}
			 */
			public void mouseDown(MouseEvent e) {
				// do nothing
			}

			/**
			 * {@inheritDoc}
			 */
			public void mouseDoubleClick(MouseEvent e) {
				// do nothing
			}
		};
	}

	/**
	 * creates the available entries group
	 */
	protected void createAvailableToolsGroup() {
		Composite parent = (Composite)getControl();
		Composite availableToolsComposite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(2, true);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		availableToolsComposite.setLayout(layout);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		availableToolsComposite.setLayoutData(data);

		Label label = new Label(availableToolsComposite, SWT.NONE);
		label.setText(Messages.Local_Palette_Available_Tools);
		data = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		label.setLayoutData(data);

		ToolBar toolbar = new ToolBar(availableToolsComposite, SWT.HORIZONTAL);
		data = new GridData(SWT.RIGHT, SWT.FILL, false, false);
		toolbar.setLayoutData(data);
		populateAvailableToolsToolBar(toolbar);

		createProfileCombo(availableToolsComposite);

		Tree tree = new Tree(availableToolsComposite, SWT.SINGLE | SWT.BORDER);
		data = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
		data.widthHint = 185;
		// Make the tree this tall even when there is nothing in it. This will keep the
		// dialog from shrinking to an unusually small size.
		data.heightHint = 200;
		tree.setLayoutData(data);
		availableToolsViewer = new TreeViewer(tree);
		availableToolsViewer.setContentProvider(new UMLToolsTreeContentProvider());
		paletteLabelProvider = new PaletteLabelProvider();
		availableToolsViewer.setLabelProvider(paletteLabelProvider);
		ViewerComparator labelComparator = new LabelViewerComparator();
		availableToolsViewer.setComparator(labelComparator);
		// remove the note stack and standard group
		availableToolsViewer.addFilter(new ViewerFilter() {

			/**
			 * {@inheritDoc}
			 */
			@Override
			public boolean select(Viewer viewer, Object parentElement, Object element) {
				if(element instanceof PaletteStack && "noteStack".equals(((PaletteStack)element).getId())) {
					return false;
				} else if(element instanceof PaletteToolbar && "standardGroup".equals(((PaletteToolbar)element).getId())) {
					return false;
				}
				return true;
			}
		});
		availableToolsViewer.addFilter(new DrawerFilter());
		// add drag support
		addAvailableToolsDragSupport();
		// availableToolsViewer.setInput(getAllVisibleStandardEntries());
	}

	/**
	 * Creates the profile combo
	 * 
	 * @param availableToolsComposite
	 *        the available tools composite
	 * @return the created combo
	 */
	protected Combo createProfileCombo(Composite availableToolsComposite) {
		// retrieve top package, to know which profiles are available
		// creates the combo
		profileCombo = new Combo(availableToolsComposite, SWT.BORDER | SWT.READ_ONLY);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
		profileCombo.setLayoutData(data);

		// retrieve all applied profiles
		List<Profile> profiles = getAllAppliedProfiles();

		int profileNumber = profiles.size();
		for(int i = 0; i < profileNumber; i++) {
			profileComboList.add(i, profiles.get(i).getName());
		}
		profileComboList.add(UML_TOOLS_LABEL);
		profileCombo.setItems(profileComboList.toArray(new String[]{}));

		// add selection listener for the combo. selects the "UML tools" item
		ProfileComboSelectionListener listener = new ProfileComboSelectionListener();
		profileCombo.addSelectionListener(listener);
		profileCombo.addModifyListener(listener);
		// profileCombo.select(profileNumber);

		return profileCombo;
	}

	/**
	 * returns the list of applied profile for the nearest package of the top element
	 * 
	 * @return the list of applied profile for the nearest package of the top element or an empty
	 *         list
	 */
	protected List<Profile> getAllAppliedProfiles() {
		Package topPackage = null;
		if(editorPart instanceof DiagramEditorWithFlyOutPalette) {
			EObject element = ((DiagramEditorWithFlyOutPalette)editorPart).getDiagram().getElement();
			if(element instanceof org.eclipse.uml2.uml.Element) {
				topPackage = ((org.eclipse.uml2.uml.Element)element).getNearestPackage();
			}
		}
		if(topPackage != null) {
			return topPackage.getAllAppliedProfiles();
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * Add drag support from the available tools viewer
	 */
	protected void addAvailableToolsDragSupport() {
		// transfer types
		Transfer[] transfers = new Transfer[]{ LocalSelectionTransfer.getTransfer() };

		// drag listener
		DragSourceListener listener = new DragSourceAdapter() {

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void dragStart(DragSourceEvent event) {
				super.dragStart(event);
				event.data = availableToolsViewer.getSelection();
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void dragSetData(DragSourceEvent event) {
				super.dragSetData(event);
				LocalSelectionTransfer.getTransfer().setSelection(availableToolsViewer.getSelection());
			}

		};

		availableToolsViewer.addDragSupport(DND.DROP_LINK, transfers, listener);
	}

	/**
	 * Adds elements to the tool bar for available tools viewer
	 * 
	 * @param toolbar
	 *        the toolbar to populate
	 */
	protected void populateAvailableToolsToolBar(ToolBar toolbar) {
		toggleContentProvider = createCheckToolBarItem(toolbar, SWITCH_CONTENT_PROVIDER_ICON, Messages.Local_Palette_SwitchToolsContentProvider_Tooltip, createSwitchToolsContentProviderListener());
		toggleContentProvider.setSelection(true);
		toggleContentProvider.setEnabled(false);
		createCheckToolBarItem(toolbar, SHOWN_TOOLS_ICON, Messages.Local_Palette_ShowTools_Tooltip, createsShowToolListener());
	}

	/**
	 * Creates the listener for the available tools content provider
	 * 
	 * @return the listener created
	 */
	protected Listener createSwitchToolsContentProviderListener() {
		return new Listener() {

			/**
			 * {@inheritDoc}
			 */
			public void handleEvent(Event event) {
				if(!(event.widget instanceof ToolItem)) {
					return;
				}
				ToolItem item = ((ToolItem)event.widget);
				// retrieve current profile selected in the combo profile
				int index = profileCombo.getSelectionIndex();
				Collection<PaletteEntry> standardEntries = getAllVisibleStandardEntries();
				Profile profile = getAllAppliedProfiles().get(index);

				if(item.getSelection()) {
					availableToolsViewer.setContentProvider(new ProfileToolsStereotypeMetaclassTreeContentProvider(profile, standardEntries));
					item.setSelection(true);
				} else {

					availableToolsViewer.setContentProvider(new ProfileToolsMetaclassStereotypeTreeContentProvider(profile, standardEntries));
					item.setSelection(false);
				}

				// generate tools for given profile
				availableToolsViewer.setInput(profile);
			}
		};
	}

	/**
	 * creates the tool item for drawers visibility listener
	 * 
	 * @return the listener for the tool button
	 */
	protected Listener createShowDrawerListener() {
		return new Listener() {

			/**
			 * {@inheritDoc}
			 */
			public void handleEvent(Event event) {
				if(!(event.widget instanceof ToolItem)) {
					return;
				}
				ToolItem item = ((ToolItem)event.widget);
				if(item.getSelection()) {
					// elements should be hidden
					availableToolsViewer.addFilter(drawerFilter);
					item.setSelection(true);
				} else {
					availableToolsViewer.removeFilter(drawerFilter);
					item.setSelection(false);
				}
			}
		};
	}

	/**
	 * creates the tool item for tools visibility listener
	 * 
	 * @return the listener for the tool button
	 */
	protected Listener createsShowToolListener() {
		return new Listener() {

			/**
			 * {@inheritDoc}
			 */
			public void handleEvent(Event event) {
				if(!(event.widget instanceof ToolItem)) {
					return;
				}
				ToolItem item = ((ToolItem)event.widget);
				if(item.getSelection()) {
					// elements should be hidden
					availableToolsViewer.addFilter(toolFilter);
					item.setSelection(true);
				} else {
					availableToolsViewer.removeFilter(toolFilter);
					item.setSelection(false);
				}
			}
		};
	}

	/**
	 * Creates a toolbar item which can be checked.
	 * 
	 * @param toolbar
	 *        the parent toolbar
	 * @param shownElementsIcon
	 *        path for shown elements icon
	 * @param listener
	 *        listener for button action
	 * @param tooltip
	 *        tooltip text for the toolbar item
	 */
	protected ToolItem createCheckToolBarItem(ToolBar toolbar, String shownElementsIcon, String tooltip, Listener listener) {
		ToolItem item = new ToolItem(toolbar, SWT.CHECK | SWT.BORDER);
		item.setImage(Activator.getPluginIconImage(Activator.ID, shownElementsIcon));
		item.setToolTipText(tooltip);
		item.addListener(SWT.Selection, listener);
		return item;
	}

	/**
	 * Validates the content of the fields in this page
	 */
	protected boolean validatePage() {
		boolean valid = true;

		if(valid) {
			setMessage(null);
			setErrorMessage(null);
		}
		return valid;
	}

	/**
	 * The <code>WizardNewFileCreationPage</code> implementation of this <code>Listener</code> method handles all events and enablements for controls
	 * on this page. Subclasses may extend.
	 */
	public void handleEvent(Event event) {
		setPageComplete(validatePage());
	}

	/**
	 * Content provider for available tools viewer
	 */
	public class UMLToolsTreeContentProvider implements ITreeContentProvider {

		/**
		 * Constructor
		 * 
		 * @param viewer
		 *        The viewer whose ContentProvider this content provider is
		 */
		public UMLToolsTreeContentProvider() {
		}

		/**
		 * {@inheritDoc}
		 */
		public Object[] getElements(Object inputElement) {
			Object[] elements = null;

			if(inputElement instanceof Collection<?>) {
				elements = ((Collection<?>)inputElement).toArray();
			} else if(inputElement instanceof PaletteRoot) {
				// paletteUil.getAllEntries(...) to add drawers
				// if so, uncomment the addFilterbutton for drawers in populate tool bar
				elements = PaletteUtil.getAllToolEntries(((PaletteRoot)inputElement)).toArray();
			}

			if(elements == null) {
				elements = new Object[0];
			}
			return elements;
		}

		/**
		 * {@inheritDoc}
		 */
		public void dispose() {
		}

		/**
		 * {@inheritDoc}
		 */
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

		}

		/**
		 * {@inheritDoc}
		 */
		public Object[] getChildren(Object parentElement) {
			Object[] elements = null;

			if(parentElement instanceof Collection<?>) {
				elements = ((Collection<?>)parentElement).toArray();
			} else if(parentElement instanceof PaletteRoot) {
				// paletteUil.getAllEntries(...) to add drawers
				// if so, uncomment the addFilterbutton for drawers in populate tool bar
				elements = PaletteUtil.getAllToolEntries(((PaletteRoot)parentElement)).toArray();
			}

			return elements;
		}

		/**
		 * {@inheritDoc}
		 */
		public Object getParent(Object element) {
			return null;
		}

		/**
		 * {@inheritDoc}
		 */
		public boolean hasChildren(Object element) {
			return getChildren(element) != null && getChildren(element).length > 0;
		}
	}

	/**
	 * Label provider for palette tools.
	 * <P>
	 * We should be using the Palette label provider from GEF, if it was not with visibility "package"...
	 * 
	 * @see org.eclipse.gef.ui.palette.customize.PaletteLabelProvider </P>
	 * 
	 */
	public class PaletteLabelProvider implements ILabelProvider {

		/**
		 * {@inheritDoc}
		 */
		public Image getImage(Object element) {
			if(element instanceof PaletteEntry) {
				ImageDescriptor descriptor = ((PaletteEntry)element).getSmallIcon();
				if(descriptor == null) {
					return null;
				}
				return Activator.getPluginIconImage(Activator.ID, descriptor);
			} else if(element instanceof Stereotype) {
				return Activator.getPluginIconImage(Activator.ID, "/icons/stereotype.gif");
			}
			return null;
		}

		/**
		 * {@inheritDoc}
		 */
		public String getText(Object element) {
			if(element instanceof PaletteEntry) {
				return ((PaletteEntry)element).getLabel();
			} else if(element instanceof Stereotype) {
				return ((Stereotype)element).getName();
			}
			return "unknown element";
		}

		/**
		 * {@inheritDoc}
		 */
		public void addListener(ILabelProviderListener listener) {

		}

		/**
		 * {@inheritDoc}
		 */
		public void dispose() {

		}

		/**
		 * {@inheritDoc}
		 */
		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		/**
		 * {@inheritDoc}
		 */
		public void removeListener(ILabelProviderListener listener) {

		}

	}

	/**
	 * Label provider for palette tools.
	 * <P>
	 * We should be using the Palette label provider from GEF, if it was not with visibility "package"...
	 * 
	 * @see org.eclipse.gef.ui.palette.customize.PaletteLabelProvider </P>
	 * 
	 */
	public class PaletteProxyLabelProvider implements ILabelProvider {

		/**
		 * {@inheritDoc}
		 */
		public Image getImage(Object element) {
			if(element instanceof PaletteEntryProxy) {
				return ((PaletteEntryProxy)element).getImage();
			}
			return null;
		}

		/**
		 * {@inheritDoc}
		 */
		public String getText(Object element) {
			if(element instanceof PaletteEntryProxy) {
				return ((PaletteEntryProxy)element).getLabel();
			}
			return "unknown element";
		}

		/**
		 * {@inheritDoc}
		 */
		public void addListener(ILabelProviderListener listener) {

		}

		/**
		 * {@inheritDoc}
		 */
		public void dispose() {

		}

		/**
		 * {@inheritDoc}
		 */
		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		/**
		 * {@inheritDoc}
		 */
		public void removeListener(ILabelProviderListener listener) {

		}

	}

	/**
	 * Filter for the viewer. Hide/show Drawers
	 */
	public class DrawerFilter extends ViewerFilter {

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean select(Viewer viewer, Object parentElement, Object element) {
			if(element instanceof PaletteDrawer) {
				return false;
			}
			return true;
		}

	}

	/**
	 * Filter for the viewer. Hide/show Drawers
	 */
	public class ToolFilter extends ViewerFilter {

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean select(Viewer viewer, Object parentElement, Object element) {
			if(element instanceof ToolEntry) {
				return false;
			}
			return true;
		}

	}

	/**
	 * Content provider for the palette
	 */
	public class PaletteContentProvider implements ITreeContentProvider {

		/** tree viewer this provider provides content */
		protected final TreeViewer viewer;

		/** the document root where to build the palette */
		protected PaletteContainerProxy rootProxy;

		/** model listener that will listens for all modifications in the entries */
		protected PropertyChangeListener modelListener = new PropertyChangeListener() {

			public void propertyChange(PropertyChangeEvent evt) {
				handlePropertyChanged(evt);
			}
		};

		/**
		 * Creates a new PaletteContentProvider.
		 * 
		 * @param treeviewer
		 *        tree viewer this provider provides content
		 */
		public PaletteContentProvider(TreeViewer treeviewer) {
			this.viewer = treeviewer;
		}

		/**
		 * {@inheritDoc}
		 */
		public void dispose() {
			// remove all listeners
			traverseModel(rootProxy, false);
		}

		/**
		 * {@inheritDoc}
		 */
		public Object[] getChildren(Object parentElement) {
			if(parentElement instanceof PaletteEntryProxy) {
				List<PaletteEntryProxy> children = ((PaletteEntryProxy)parentElement).getChildren();
				return (children != null) ? children.toArray() : new Object[0];
			}
			return null;
		}

		/**
		 * {@inheritDoc}
		 */
		public boolean hasChildren(Object element) {
			if(element instanceof PaletteEntryProxy) {
				return ((PaletteEntryProxy)element).getChildren() != null && !((PaletteEntryProxy)element).getChildren().isEmpty();
			}
			return false;
		}

		/**
		 * {@inheritDoc}
		 */
		public Object[] getElements(Object inputElement) {
			Object[] elements = getChildren(inputElement);
			if(elements == null) {
				elements = new Object[0];
			}
			return elements;
		}

		/**
		 * {@inheritDoc}
		 */
		public Object getParent(Object element) {
			if(element instanceof PaletteEntryProxy) {
				return ((PaletteEntryProxy)element).getParent();
			}
			return null;
		}

		/**
		 * {@inheritDoc}
		 */
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			if(rootProxy != null) {
				// warning.. the root has no entry, this is a virtual node
				traverseModel(rootProxy, false);
			}

			if(newInput != null) {
				rootProxy = ((PaletteContainerProxy)newInput);
				traverseModel(rootProxy, true);
			}
		}

		/**
		 * This method is invoked whenever there is any change in the model. It updates the
		 * viewer with the changes that were made to the model. Sub-classes may override this
		 * method to change or extend its functionality.
		 * 
		 * @param evt
		 *        The {@link PropertyChangeEvent} that was fired from the model
		 */
		protected void handlePropertyChanged(PropertyChangeEvent evt) {
			PaletteEntryProxy entry = ((PaletteEntryProxy)evt.getSource());
			String property = evt.getPropertyName();
			if(property.equals(PaletteEntry.PROPERTY_LABEL) || property.equals(PaletteEntry.PROPERTY_SMALL_ICON) || property.equals(PaletteEntryProxy.PROPERTY_ICON_PATH)) {
				viewer.update(entry, null);
			} else if(property.equals(PaletteContainerProxy.PROPERTY_ADD_CHILDREN)) {
				viewer.refresh(entry.getParent());
				// add the listeners to the child
				traverseModel(((PaletteEntryProxy)evt.getNewValue()), true);
			} else if(property.equals(PaletteContainerProxy.PROPERTY_REMOVE_CHILDREN)) {
				viewer.refresh(entry.getParent());
				// add the listeners to the child
				traverseModel(((PaletteEntryProxy)evt.getOldValue()), false);
			}

		}

		protected void traverseModel(PaletteEntryProxy entryProxy, boolean isHook) {
			if(entryProxy != null) {
				if(isHook) {
					entryProxy.addPropertyChangeListener(modelListener);
				} else {
					entryProxy.removePropertyChangeListener(modelListener);
				}
			}

			if(entryProxy.getChildren() != null && !entryProxy.getChildren().isEmpty()) {
				for(PaletteEntryProxy proxy : entryProxy.getChildren()) {
					traverseModel(proxy, isHook);
				}
			}
		}
	}

	/**
	 * Performs all action on finish
	 * 
	 * @param id
	 *        the path for the file
	 */
	public void performFinish(String path) {
		// creates the document
		Document document = createXMLDocumentFromPalettePreview();
		saveDocument(document, path);
		requiredProfiles = collectRequiredProfiles();
	}

	/**
	 * collect the required profiles from all tool provided by the local palette definition
	 */
	protected Set<String> collectRequiredProfiles() {
		Set<String> profiles = new HashSet<String>();
		PaletteContainerProxy rootProxy = (PaletteContainerProxy)paletteTreeViewer.getInput();
		collectRequiredProfiles(rootProxy.getChildren(), profiles);
		return profiles;
	}

	/**
	 * collect the required profiles from all tool provided by the local palette definition
	 */
	protected void collectRequiredProfiles(List<PaletteEntryProxy> proxies, Set<String> requiredProfiles) {
		for(PaletteEntryProxy proxy : proxies) {
			// add profile(s) if relevant, check for children

			if(proxy instanceof PaletteAspectToolEntryProxy) {
				// list of profiles
				for(String stereotypeQN : ((PaletteAspectToolEntryProxy)proxy).getStereotypesQNList()) {
					// retrieve list of profiles from the stereotype QN (only remove last segment
					// ?!)
					String profileName = PaletteUtil.findProfileNameFromStereotypeName(stereotypeQN);
					requiredProfiles.add(profileName);
				}
			}

			if(proxy.getChildren() != null) {
				collectRequiredProfiles(proxy.getChildren(), requiredProfiles);
			}
		}
	}

	/**
	 * Saves the xml document into file
	 * 
	 * @param document
	 *        the document to save
	 * @param path
	 *        name of the file
	 * @return the file created or updated
	 */
	protected File saveDocument(Document document, String path) {
		File file = null;
		try {
			// create the file that stores the XML configuration
			file = Activator.getDefault().getStateLocation().append(path).toFile();
			TransformerFactory tranFactory = TransformerFactory.newInstance();
			Transformer aTransformer;

			aTransformer = tranFactory.newTransformer();

			Source src = new DOMSource(document);
			Result dest = new StreamResult(file);
			aTransformer.transform(src, dest);
		} catch (TransformerConfigurationException e) {
			Activator.log.error(e);
		} catch (TransformerException e) {
			Activator.log.error(e);
		}
		return file;

	}

	/**
	 * Creates the document from the palette preview
	 * 
	 * @return the dom structure of the document
	 */
	protected Document createXMLDocumentFromPalettePreview() {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		documentBuilderFactory.setNamespaceAware(true);
		DocumentBuilder documentBuilder;
		try {
			documentBuilder = documentBuilderFactory.newDocumentBuilder();
			document = documentBuilder.newDocument();
			Element paletteDefElement = document.createElement(IPapyrusPaletteConstant.PALETTE_DEFINITION);
			document.appendChild(paletteDefElement);
			Element contentElement = document.createElement(IPapyrusPaletteConstant.CONTENT);
			paletteDefElement.appendChild(contentElement);

			generateXMLPaletteContent(document, contentElement);
			return document;
		} catch (ParserConfigurationException e) {
			Activator.getDefault().logError("impossible to create the palette tree viewer content", e);
		}
		return null;
	}

	/**
	 * Generates the xml content for the palette
	 * 
	 * @param document
	 *        the document to fill
	 * @param contentElement
	 *        the root for the xml content
	 */
	protected void generateXMLPaletteContent(Document document, Element contentElement) {
		PaletteContainerProxy rootProxy = (PaletteContainerProxy)paletteTreeViewer.getInput();
		for(PaletteEntryProxy proxy : rootProxy.getChildren()) {
			generateXMLPaletteContainerProxy(document, contentElement, proxy);
		}
	}

	/**
	 * Generates the xml content for the given container
	 * 
	 * @param document
	 *        the document to fill
	 * @param containerProxy
	 *        the entry proxy
	 */
	protected void generateXMLPaletteContainerProxy(Document document, Element contentElement, PaletteEntryProxy containerProxy) {

		Element element = null;
		List<PaletteEntryProxy> children = containerProxy.getChildren();
		// generate the element
		switch(containerProxy.getType()) {
		case DRAWER:
			element = document.createElement(IPapyrusPaletteConstant.DRAWER);
			element.setAttribute(IPapyrusPaletteConstant.NAME, containerProxy.getLabel());
			if(containerProxy instanceof PaletteLocalDrawerProxy) {
				element.setAttribute(IPapyrusPaletteConstant.ICON_PATH, ((PaletteLocalDrawerProxy)containerProxy).getImagePath());
			}
			break;
		case TOOL:
			element = document.createElement(IPapyrusPaletteConstant.TOOL);
			break;
		case SEPARATOR:
			element = document.createElement(IPapyrusPaletteConstant.SEPARATOR);
			break;
		case STACK:
			element = document.createElement(IPapyrusPaletteConstant.STACK);
			break;
		case ASPECT_TOOL:
			element = document.createElement(IPapyrusPaletteConstant.ASPECT_TOOL);
			// try to cast the element into PaletteAspectToolEntryProxy
			if(containerProxy instanceof PaletteAspectToolEntryProxy) {
				PaletteAspectToolEntryProxy aspectEntryProxy = (PaletteAspectToolEntryProxy)containerProxy;
				// element.setAttribute(IPapyrusPaletteConstant.ID, aspectEntryProxy.getId());
				element.setAttribute(IPapyrusPaletteConstant.NAME, aspectEntryProxy.getLabel());
				element.setAttribute(IPapyrusPaletteConstant.DESCRIPTION, aspectEntryProxy.getEntry().getDescription());
				element.setAttribute(IPapyrusPaletteConstant.REF_TOOL_ID, aspectEntryProxy.getReferencedPaletteID());

				if(aspectEntryProxy.getImagePath() != null && !aspectEntryProxy.getImagePath().equals("")) {
					element.setAttribute(IPapyrusPaletteConstant.ICON_PATH, aspectEntryProxy.getImagePath());
				}


				// add post action, stereotype list
				for(IAspectAction action : ((PaletteAspectToolEntryProxy)containerProxy).getPostActions()) {
					Element postActionNode = document.createElement(IPapyrusPaletteConstant.POST_ACTION);
					postActionNode.setAttribute(IPapyrusPaletteConstant.ID, action.getFactoryId());
					action.save(postActionNode);
					element.appendChild(postActionNode);
				}
				for(IAspectAction action : ((PaletteAspectToolEntryProxy)containerProxy).getPreActions()) {
					Element preActionNode = document.createElement(IPapyrusPaletteConstant.PRE_ACTION);
					preActionNode.setAttribute(IPapyrusPaletteConstant.ID, action.getFactoryId());
					action.save(preActionNode);
					element.appendChild(preActionNode);
				}
			}
		default:
			break;
		}

		element.setAttribute(IPapyrusPaletteConstant.ID, containerProxy.getId());
		contentElement.appendChild(element);

		if(children != null) {
			for(PaletteEntryProxy proxy : children) {
				generateXMLPaletteContainerProxy(document, element, proxy);
			}
		}
	}

	public enum EntryType {
		DRAWER, TOOL, STACK, SEPARATOR, ASPECT_TOOL
	}

	public class LabelViewerComparator extends ViewerComparator {

		/**
		 * Creates a new LabelViewerComparator.
		 */
		public LabelViewerComparator() {
			super();
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public int compare(Viewer testViewer, Object e1, Object e2) {
			String label1 = "";
			String label2 = "";

			if(e1 instanceof PaletteEntry) {
				label1 = ((PaletteEntry)e1).getLabel();
			} else if(e1 instanceof Stereotype) {
				label1 = ((Stereotype)e1).getName();
			}
			if(e2 instanceof PaletteEntry) {
				label2 = ((PaletteEntry)e2).getLabel();
			} else if(e2 instanceof Stereotype) {
				label2 = ((Stereotype)e2).getName();
			}

			if(label1 == null) {
				return 1;
			}
			if(label2 == null) {
				return -1;
			}

			return label1.compareTo(label2);
		}
	}

	/**
	 * Listener for the profile combo. It changes the input of the following viewer.
	 */
	public class ProfileComboSelectionListener implements SelectionListener, ModifyListener {

		/**
		 * {@inheritDoc}
		 */
		public void widgetDefaultSelected(SelectionEvent e) {
			// nothing to do
		}

		/**
		 * {@inheritDoc}
		 */
		public void widgetSelected(SelectionEvent e) {
			handleSelectionChanged();
		}

		/**
		 * {@inheritDoc}
		 */
		public void modifyText(ModifyEvent e) {
			handleSelectionChanged();
		}

		/**
		 * handles the change selection for the combo
		 */
		protected void handleSelectionChanged() {
			int index = profileCombo.getSelectionIndex();
			if(index < 0 || index >= profileCombo.getItems().length) {
				return;
			}
			String name = profileComboList.get(index);

			Collection<PaletteEntry> standardEntries = getAllVisibleStandardEntries();
			// retrieve the profile or uml standards tools to display
			if(UML_TOOLS_LABEL.equals(name)) {
				// change content provider
				availableToolsViewer.setContentProvider(new UMLToolsTreeContentProvider());
				availableToolsViewer.setInput(standardEntries);
				toggleContentProvider.setEnabled(false);
			} else {
				if(toggleContentProvider != null && !toggleContentProvider.isDisposed()) {
					toggleContentProvider.setEnabled(true);
				}
				// switch content provider
				// this is a profile in case of uml2 tools
				Profile profile = getAllAppliedProfiles().get(index);
				if(toggleContentProvider.getSelection()) {
					availableToolsViewer.setContentProvider(new ProfileToolsStereotypeMetaclassTreeContentProvider(profile, standardEntries));
				} else {
					availableToolsViewer.setContentProvider(new ProfileToolsMetaclassStereotypeTreeContentProvider(profile, standardEntries));
				}

				// generate tools for given profile
				availableToolsViewer.setInput(profile);
			}
		}
	}

	/**
	 * Content provider for the available tools viewer, when the tools to see are coming from a
	 * profile
	 */
	public class ProfileToolsStereotypeMetaclassTreeContentProvider implements ITreeContentProvider {

		/** standard uml tools palette entries */
		final protected Collection<PaletteEntry> standardEntries;

		/**
		 * Creates a new ProfileToolsStereotypeMetaclassTreeContentProvider.
		 * 
		 * @param profile
		 *        the profile for which tools are built
		 * @param standardEntries
		 *        list of standard uml tools palette entries
		 */
		public ProfileToolsStereotypeMetaclassTreeContentProvider(Profile profile, Collection<PaletteEntry> standardEntries) {
			this.standardEntries = standardEntries;
		}

		/**
		 * {@inheritDoc}
		 */
		public Object[] getChildren(Object parentElement) {
			if(parentElement instanceof Profile) {
				return ((Profile)parentElement).getOwnedStereotypes().toArray();
			} else if(parentElement instanceof Stereotype) {
				List<PaletteEntry> entries = new ArrayList<PaletteEntry>();
				Stereotype stereotype = (Stereotype)parentElement;

				for(PaletteEntry entry : standardEntries) {
					// retrieve the element type created by the tool.
					if(entry instanceof CombinedTemplateCreationEntry) {

						EClass toolMetaclass = PaletteUtil.getToolMetaclass((CombinedTemplateCreationEntry)entry);
						if(toolMetaclass != null) {
							List<Class> metaclasses = stereotype.getAllExtendedMetaclasses();
							for(Class stMetaclass : metaclasses) {
								// get Eclass
								java.lang.Class metaclassClass = stMetaclass.getClass();
								if(metaclassClass != null) {
									EClassifier metaClassifier = UMLPackage.eINSTANCE.getEClassifier(stMetaclass.getName());
									if(((EClass)metaClassifier).isSuperTypeOf(toolMetaclass)) {
										// should create the palette entry
										HashMap properties = new HashMap();
										properties.put(IPapyrusPaletteConstant.ASPECT_ACTION_KEY, StereotypeAspectActionProvider.createConfigurationNode(stereotype.getQualifiedName()));
										AspectCreationEntry aspectEntry = new AspectCreationEntry(stereotype.getName() + " (" + entry.getLabel() + ")", "Create an element with a stereotype", entry.getId() + "_" + System.currentTimeMillis(), entry.getSmallIcon(), (CombinedTemplateCreationEntry)entry, properties);
										entries.add(aspectEntry);
									}
								}

							}
						}
					}
				}
				return entries.toArray();
			} else {
				return new Object[0];
			}
		}

		/**
		 * {@inheritDoc}
		 */
		public Object getParent(Object element) {
			if(element instanceof Stereotype) {
				return ((Stereotype)element).getProfile();
			}
			return null;
		}

		/**
		 * {@inheritDoc}
		 */
		public boolean hasChildren(Object element) {
			if(element instanceof Profile) {
				return true;
			} else if(element instanceof Stereotype) {
				return true;
			}
			return false;
		}

		/**
		 * {@inheritDoc}
		 */
		public Object[] getElements(Object inputElement) {
			if(inputElement instanceof Profile) {
				List<Stereotype> stereotypes = ((Profile)inputElement).getOwnedStereotypes();
				return stereotypes.toArray();
			}
			return new Object[0];
		}

		/**
		 * {@inheritDoc}
		 */
		public void dispose() {
			// nothing to do here
		}

		/**
		 * {@inheritDoc}
		 */
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// nothing to do here
		}

	}

	/**
	 * Content provider for the available tools viewer, when the
	 */
	public class ProfileToolsMetaclassStereotypeTreeContentProvider implements ITreeContentProvider {

		/** standard uml tools palette entries */
		final protected Collection<PaletteEntry> standardEntries;

		/** profile to display */
		final protected Profile profile;

		/**
		 * Creates a new ProfileToolsMetaclassStereotypeTreeContentProvider.
		 * 
		 * @param profile
		 *        the profile for which tools are built
		 * @param standardEntries
		 *        list of standard uml tools palette entries
		 */
		public ProfileToolsMetaclassStereotypeTreeContentProvider(Profile profile, Collection<PaletteEntry> standardEntries) {
			this.profile = profile;
			this.standardEntries = standardEntries;
		}

		/**
		 * {@inheritDoc}
		 */
		public Object[] getChildren(Object parentElement) {
			if(parentElement instanceof Profile) {
				return standardEntries.toArray();
			} else if(parentElement instanceof AspectCreationEntry) {
				return new Object[0];
			} else if(parentElement instanceof PaletteEntry) {
				List<AspectCreationEntry> entries = new ArrayList<AspectCreationEntry>();
				// display all stereotypes applicable to the type of element created by this tool
				if(parentElement instanceof CombinedTemplateCreationEntry) {
					CombinedTemplateCreationEntry entry = (CombinedTemplateCreationEntry)parentElement;
					EClass toolMetaclass = PaletteUtil.getToolMetaclass(entry);
					if(toolMetaclass != null) {
						for(Stereotype stereotype : profile.getOwnedStereotypes()) {
							List<Class> metaclasses = stereotype.getAllExtendedMetaclasses();
							for(Class stMetaclass : metaclasses) {
								// get Eclass
								java.lang.Class metaclassClass = stMetaclass.getClass();
								if(metaclassClass != null) {
									EClassifier metaClassifier = UMLPackage.eINSTANCE.getEClassifier(stMetaclass.getName());
									if(((EClass)metaClassifier).isSuperTypeOf(toolMetaclass)) {
										// should create the palette entry
										HashMap properties = new HashMap();
										ArrayList<String> stereotypesQNToApply = new ArrayList<String>();
										properties.put(IPapyrusPaletteConstant.ASPECT_ACTION_KEY, StereotypeAspectActionProvider.createConfigurationNode(stereotype.getQualifiedName()));
										AspectCreationEntry aspectEntry = new AspectCreationEntry(stereotype.getName() + " (" + entry.getLabel() + ")", "Create an element with a stereotype", entry.getId() + "_" + System.currentTimeMillis(), entry.getSmallIcon(), entry, properties);
										entries.add(aspectEntry);
									}
								}

							}
						}
					}
				}
				return entries.toArray();
			} else {
				return new Object[0];
			}
		}

		/**
		 * {@inheritDoc}
		 */
		public Object getParent(Object element) {
			if(element instanceof Stereotype) {
				return ((Stereotype)element).getProfile();
			}
			return null;
		}

		/**
		 * {@inheritDoc}
		 */
		public boolean hasChildren(Object element) {
			if(element instanceof Profile) {
				return true;
			} else if(element instanceof AspectCreationEntry) {
				return false;
			} else if(element instanceof PaletteEntry) {
				return true;
			}
			return false;
		}

		/**
		 * {@inheritDoc}
		 */
		public Object[] getElements(Object inputElement) {
			if(inputElement instanceof Profile) {
				return standardEntries.toArray();
			}
			return new Object[0];
		}

		/**
		 * {@inheritDoc}
		 */
		public void dispose() {
			// nothing to do here
		}

		/**
		 * {@inheritDoc}
		 */
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// nothing to do here
		}

	}

	/**
	 * Returns the list of all visible palette entries
	 * 
	 * @return the list of all visible palette entries
	 */
	protected Collection<PaletteEntry> getAllVisibleStandardEntries() {
		HashSet<PaletteEntry> result = new HashSet<PaletteEntry>();
		for(PaletteEntry entry : PaletteUtil.getAvailableEntriesSet(editorPart, priority).values()) {
			// the entry is not just a defineOnly entry but a visible one
			if(getRootParent(entry) != null) {
				result.add(entry);
			}
		}
		return result;
	}

	/**
	 * Returns the Root element for the palette entry. It searches recursively from parent to parent, until it find the root element
	 * 
	 * @param entry
	 *        the palette entry for which root element is searched
	 * @return the root element or <code>null</code> if none was found
	 */
	protected PaletteRoot getRootParent(PaletteEntry entry) {
		PaletteContainer parent = entry.getParent();
		if(parent instanceof PaletteRoot) {
			return (PaletteRoot)parent;
		} else if(parent != null) {
			return getRootParent(parent);
		} else {
			return null;
		}
	}

	/**
	 * Returns the list of required profiles by this local palette definition
	 * 
	 * @return the profiles required by this palette
	 */
	public Set<String> getRequiredProfiles() {
		return requiredProfiles;
	}

	/**
	 * Item validator
	 */
	protected abstract class ToolBarItemValidator {

		/**
		 * Checks if the button should be enable or not
		 * 
		 * @return <code>true</code> if the button should be enable
		 */
		public abstract boolean isEnable();
	}

	/**
	 * validator for the edit element tool item. It does not remove
	 */
	protected class EditElementToolBarItemValidator extends ToolBarItemValidator {

		/**
		 * @{inheritDoc
		 */
		@Override
		public boolean isEnable() {
			// retrieve selection
			if(paletteTreeViewer != null && !paletteTreeViewer.getControl().isDisposed()) {
				// retrieve selection. first element should be a drawer
				IStructuredSelection selection = (IStructuredSelection)paletteTreeViewer.getSelection();
				if(selection == null) {
					return false;
				} else {
					// look for first element. should be an instance of drawer
					return (selection.getFirstElement() instanceof PaletteLocalDrawerProxy);
				}
			}
			return false;
		}
	}

	/**
	 * validator for the create separator or stack tool item. Only valid when selection is a {@link PaletteEntryProxy} or a
	 * {@link PaletteLocalDrawerProxy}
	 */
	protected class PaletteEntryProxySelectedValidator extends ToolBarItemValidator {

		/**
		 * @{inheritDoc
		 */
		@Override
		public boolean isEnable() {
			// retrieve selection
			if(paletteTreeViewer != null && !paletteTreeViewer.getControl().isDisposed()) {
				// retrieve selection. first element should be a drawer
				IStructuredSelection selection = (IStructuredSelection)paletteTreeViewer.getSelection();
				if(selection == null) {
					return false;
				} else {
					Object object = selection.getFirstElement();
					return (object instanceof PaletteEntryProxy);
				}
			}
			return false;
		}
	}

	/**
	 * Sets the priority of the current edited palette
	 * 
	 * @param priority
	 *        the priority of the current edited palette
	 */
	public void setPriority(ProviderPriority priority) {
		this.priority = priority;
	}
}

Back to the top