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
|
/*******************************************************************************
* Copyright (c) 2000, 2010 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.wst.jsdt.internal.ui.packageview;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.IBasicPropertyConstants;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.progress.UIJob;
import org.eclipse.wst.jsdt.core.ElementChangedEvent;
import org.eclipse.wst.jsdt.core.IClassFile;
import org.eclipse.wst.jsdt.core.IElementChangedListener;
import org.eclipse.wst.jsdt.core.IIncludePathAttribute;
import org.eclipse.wst.jsdt.core.IIncludePathEntry;
import org.eclipse.wst.jsdt.core.IJavaScriptElement;
import org.eclipse.wst.jsdt.core.IJavaScriptElementDelta;
import org.eclipse.wst.jsdt.core.IJavaScriptModel;
import org.eclipse.wst.jsdt.core.IJavaScriptProject;
import org.eclipse.wst.jsdt.core.IJavaScriptUnit;
import org.eclipse.wst.jsdt.core.IPackageFragment;
import org.eclipse.wst.jsdt.core.IPackageFragmentRoot;
import org.eclipse.wst.jsdt.core.IParent;
import org.eclipse.wst.jsdt.core.JavaScriptCore;
import org.eclipse.wst.jsdt.core.JavaScriptModelException;
import org.eclipse.wst.jsdt.internal.corext.util.JavaModelUtil;
import org.eclipse.wst.jsdt.internal.ui.JavaScriptPlugin;
import org.eclipse.wst.jsdt.internal.ui.navigator.ContainerFolder;
import org.eclipse.wst.jsdt.internal.ui.workingsets.WorkingSetModel;
import org.eclipse.wst.jsdt.ui.PreferenceConstants;
import org.eclipse.wst.jsdt.ui.ProjectLibraryRoot;
import org.eclipse.wst.jsdt.ui.StandardJavaScriptElementContentProvider;
/**
* Content provider for the PackageExplorer.
*
* <p>
* Since 2.1 this content provider can provide the children for flat or hierarchical
* layout.
* </p>
*
* @see org.eclipse.wst.jsdt.ui.StandardJavaScriptElementContentProvider
*/
public class PackageExplorerContentProvider extends StandardJavaScriptElementContentProvider implements ITreeContentProvider, IElementChangedListener, IPropertyChangeListener {
protected static final int ORIGINAL= 0;
protected static final int PARENT= 1 << 0;
protected static final int GRANT_PARENT= 1 << 1;
protected static final int PROJECT= 1 << 2;
private TreeViewer fViewer;
private Object fInput;
private boolean fIsFlatLayout;
private boolean fShowLibrariesNode;
private boolean fFoldPackages;
private Collection fPendingUpdates;
private UIJob fUpdateJob;
/**
* Creates a new content provider for Java elements.
* @param provideMembers if set, members of compilation units and class files are shown
*/
public PackageExplorerContentProvider(boolean provideMembers) {
super(provideMembers);
fShowLibrariesNode= false;
fIsFlatLayout= false;
fFoldPackages= arePackagesFoldedInHierarchicalLayout();
fPendingUpdates= null;
JavaScriptPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
fUpdateJob= null;
}
private boolean arePackagesFoldedInHierarchicalLayout(){
return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.APPEARANCE_FOLD_PACKAGES_IN_PACKAGE_EXPLORER);
}
protected Object getViewerInput() {
return fInput;
}
/* (non-Javadoc)
* Method declared on IElementChangedListener.
*/
public void elementChanged(final ElementChangedEvent event) {
final ArrayList runnables= new ArrayList();
try {
// 58952 delete project does not update Package Explorer [package explorer]
// if the input to the viewer is deleted then refresh to avoid the display of stale elements
if (inputDeleted(runnables))
return;
processDelta(event.getDelta(), runnables);
} catch (JavaScriptModelException e) {
JavaScriptPlugin.log(e);
} finally {
executeRunnables(runnables);
}
}
protected final void executeRunnables(final Collection runnables) {
// now post all collected runnables
final Control ctrl= fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
//Are we in the UIThread? If so spin it until we are done
if ((ctrl.getDisplay().getThread() == Thread.currentThread()) && !fViewer.isBusy()) {
runUpdates(runnables);
} else {
synchronized (this) {
if (fPendingUpdates == null) {
fPendingUpdates= runnables;
} else {
fPendingUpdates.addAll(runnables);
}
}
postAsyncUpdate(ctrl.getDisplay());
}
}
}
private void postAsyncUpdate(final Display display) {
if (fUpdateJob == null) {
fUpdateJob= new UIJob(display, PackagesMessages.PackageExplorerContentProvider_update_job_description) {
public IStatus runInUIThread(IProgressMonitor monitor) {
TreeViewer viewer= fViewer;
if (viewer != null && viewer.isBusy()) {
schedule(100); // reschedule when viewer is busy: bug 184991
} else {
runPendingUpdates();
}
return Status.OK_STATUS;
}
};
fUpdateJob.setSystem(true);
}
fUpdateJob.schedule();
}
/**
* Run all of the runnables that are the widget updates. Must be called in the display thread.
*/
public void runPendingUpdates() {
Collection pendingUpdates;
synchronized (this) {
pendingUpdates= fPendingUpdates;
fPendingUpdates= null;
}
if (pendingUpdates != null && fViewer != null) {
Control control = fViewer.getControl();
if (control != null && !control.isDisposed()) {
runUpdates(pendingUpdates);
}
}
}
private void runUpdates(Collection runnables) {
Iterator runnableIterator = runnables.iterator();
while (runnableIterator.hasNext()){
((Runnable) runnableIterator.next()).run();
}
}
private boolean inputDeleted(Collection runnables) {
if (fInput == null)
return false;
if ((fInput instanceof IJavaScriptElement) && ((IJavaScriptElement) fInput).exists())
return false;
if ((fInput instanceof IResource) && ((IResource) fInput).exists())
return false;
if (fInput instanceof WorkingSetModel)
return false;
if (fInput instanceof IWorkingSet) // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=156239
return false;
postRefresh(fInput, ORIGINAL, fInput, runnables);
return true;
}
/* (non-Javadoc)
* Method declared on IContentProvider.
*/
public void dispose() {
super.dispose();
JavaScriptCore.removeElementChangedListener(this);
JavaScriptPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this);
}
/* (non-Javadoc)
* @see org.eclipse.wst.jsdt.ui.StandardJavaScriptElementContentProvider#getPackageFragmentRootContent(org.eclipse.wst.jsdt.core.IPackageFragmentRoot)
*/
protected Object[] getPackageFragmentRootContent(IPackageFragmentRoot root) throws JavaScriptModelException {
if (fIsFlatLayout) {
return super.getPackageFragmentRootContent(root);
}
// hierarchical package mode
ArrayList result= new ArrayList();
getHierarchicalPackageChildren(root, null, result);
if (!isProjectPackageFragmentRoot(root)) {
Object[] nonJavaResources= root.getNonJavaScriptResources();
for (int i= 0; i < nonJavaResources.length; i++) {
result.add(nonJavaResources[i]);
}
}
return result.toArray();
}
/* (non-Javadoc)
* @see org.eclipse.wst.jsdt.ui.StandardJavaScriptElementContentProvider#getPackageContent(org.eclipse.wst.jsdt.core.IPackageFragment)
*/
protected Object[] getPackageContent(IPackageFragment fragment) throws JavaScriptModelException {
if (fIsFlatLayout) {
return super.getPackageContent(fragment);
}
// hierarchical package mode
ArrayList result= new ArrayList();
getHierarchicalPackageChildren((IPackageFragmentRoot) fragment.getParent(), fragment, result);
Object[] nonPackages= super.getPackageContent(fragment);
if (result.isEmpty())
return nonPackages;
for (int i= 0; i < nonPackages.length; i++) {
result.add(nonPackages[i]);
}
return result.toArray();
}
/* (non-Javadoc)
* @see org.eclipse.wst.jsdt.ui.StandardJavaScriptElementContentProvider#getFolderContent(org.eclipse.core.resources.IFolder)
*/
protected Object[] getFolderContent(IFolder folder) throws CoreException {
if (fIsFlatLayout) {
return super.getFolderContent(folder);
}
// hierarchical package mode
ArrayList result= new ArrayList();
getHierarchicalPackagesInFolder(folder, result);
Object[] others= super.getFolderContent(folder);
if (result.isEmpty())
return others;
for (int i= 0; i < others.length; i++) {
result.add(others[i]);
}
return result.toArray();
}
public Object[] getChildren(Object parentElement) {
try {
if (parentElement instanceof IJavaScriptModel)
return concatenate(getJavaProjects((IJavaScriptModel)parentElement), getNonJavaProjects((IJavaScriptModel)parentElement));
if(parentElement instanceof ContainerFolder) {
return getContainerPackageFragmentRoots((PackageFragmentRootContainer)((ContainerFolder)parentElement).getParentObject());
}
if (parentElement instanceof PackageFragmentRootContainer)
return getContainerPackageFragmentRoots((PackageFragmentRootContainer)parentElement, fIsFlatLayout, null);
else if (parentElement instanceof NamespaceGroup && ((NamespaceGroup) parentElement).getPackageFragmentRootContainer() != null) {
return getContainerPackageFragmentRoots(((NamespaceGroup) parentElement).getPackageFragmentRootContainer(), true, ((NamespaceGroup) parentElement));
}
if(parentElement instanceof ProjectLibraryRoot) {
// return ((ProjectLibraryRoot)parentElement).getChildren();
// Include source folders (and also scour their model contents)
Object[] children1 = ((ProjectLibraryRoot) parentElement).getChildren();
List sourceRoots = new ArrayList();
try {
IPackageFragmentRoot[] packageFragmentRoots = ((ProjectLibraryRoot) parentElement).getProject().getPackageFragmentRoots();
for (int i = 0; i < packageFragmentRoots.length; i++) {
IIncludePathEntry entry = packageFragmentRoots[i].getRawIncludepathEntry();
if (IIncludePathEntry.CPE_SOURCE == entry.getEntryKind()) {
boolean hidden = false;
IIncludePathAttribute[] attribs = entry.getExtraAttributes();
for (int k = 0; !hidden && attribs != null && k < attribs.length; k++) {
hidden |= (attribs[k] == IIncludePathAttribute.HIDE);
}
if (!hidden) {
sourceRoots.add(packageFragmentRoots[i]);
}
}
}
}
catch (JavaScriptModelException e) {
e.printStackTrace();
}
Object[] combined = new Object[children1.length + sourceRoots.size()];
System.arraycopy(children1, 0, combined, 0, children1.length);
if (!sourceRoots.isEmpty()) {
System.arraycopy(sourceRoots.toArray(), 0, combined, children1.length, sourceRoots.size());
}
return combined;
}
// if (parentElement instanceof IPackageFragmentRoot) {
// Object[] children = super.getChildren(parentElement);
// for (int i = 0; i < children.length; i++) {
// // replace the "default package" with its contents
// if (children[i] instanceof IPackageFragment && ((IPackageFragment) children[i]).isDefaultPackage()) {
// List combined = new ArrayList(children.length);
// for (int j = 0; j < children.length; j++) {
// if (j != i) {
// combined.add(children[j]);
// }
// else {
// Object[] defaultChildren = super.getChildren(children[j]);
// for (int k = 0; k < defaultChildren.length; k++) {
// combined.add(defaultChildren[k]);
// }
// }
// }
// return combined.toArray();
// }
// }
// return children;
// }
if (parentElement instanceof IProject) {
IProject project= (IProject) parentElement;
if (project.isAccessible())
return project.members();
return NO_CHILDREN;
}
if (parentElement instanceof IPackageFragmentRoot && ((IPackageFragmentRoot)parentElement).isVirtual()) {
return getLibraryChildren((IPackageFragmentRoot)parentElement, fIsFlatLayout, null);
}
else if (parentElement instanceof NamespaceGroup && ((NamespaceGroup) parentElement).getPackageFragmentRoot() != null && ((NamespaceGroup) parentElement).getPackageFragmentRoot().isVirtual()) {
return getLibraryChildren(((NamespaceGroup) parentElement).getPackageFragmentRoot(), true, ((NamespaceGroup) parentElement));
}
else if(parentElement instanceof IPackageFragmentRoot && IIncludePathEntry.CPE_SOURCE == ((IPackageFragmentRoot) parentElement).getRawIncludepathEntry().getEntryKind()) {
return getSourceChildren(parentElement, fIsFlatLayout, null);
}
else if (parentElement instanceof NamespaceGroup && ((NamespaceGroup) parentElement).getPackageFragmentRoot() != null && IIncludePathEntry.CPE_SOURCE == ((NamespaceGroup) parentElement).getPackageFragmentRoot().getRawIncludepathEntry().getEntryKind()) {
return getSourceChildren(((NamespaceGroup) parentElement).getPackageFragmentRoot(), true, ((NamespaceGroup) parentElement));
}
// if script unit
else if (parentElement instanceof IJavaScriptUnit) {
return getSourceChildren(parentElement, fIsFlatLayout, null);
}
// if group with script unit as parent
else if(parentElement instanceof NamespaceGroup && ((NamespaceGroup) parentElement).getJavaScriptUnit() != null) {
return getSourceChildren(((NamespaceGroup) parentElement).getJavaScriptUnit(), true, ((NamespaceGroup) parentElement));
}
return super.getChildren(parentElement);
} catch (JavaScriptModelException e) {
//ignore, caused by trying to get children of files in libraries
return NO_CHILDREN;
} catch (CoreException e) {
e.printStackTrace();
return NO_CHILDREN;
}
}
/* (non-Javadoc)
* @see org.eclipse.wst.jsdt.ui.StandardJavaScriptElementContentProvider#hasChildren(java.lang.Object)
*/
public boolean hasChildren(Object element) {
if (element instanceof JsGlobalScopeContainer) {
return ((JsGlobalScopeContainer) element).hasChildren();
}
if (element instanceof ProjectLibraryRoot) {
return ((ProjectLibraryRoot) element).hasChildren();
}
return super.hasChildren(element);
}
private Object[] getSourceChildren(Object parentElement, boolean neverGroup, NamespaceGroup onlyGroup) throws JavaScriptModelException {
/* if the parent is a fragment root use its children
* else if parent element is a script unit, use it as the child
*/
Object[] rawChildren = new Object[0];
if(parentElement instanceof IPackageFragmentRoot) {
rawChildren = ((IParent) parentElement).getChildren();
} else if(parentElement instanceof IJavaScriptUnit) {
rawChildren = new Object[] { parentElement };
}
if (rawChildren == null)
return new Object[0];
ArrayList allChildren = new ArrayList();
ArrayList expanded = new ArrayList();
expanded.addAll(Arrays.asList(rawChildren));
if (expanded.isEmpty())
return new Object[0];
Object next = expanded.remove(0);
Map groups = new HashMap();
while (next != null) {
if (next instanceof IPackageFragment) {
expanded.addAll(Arrays.asList(((IPackageFragment) next).getChildren()));
}
else if (next instanceof IPackageFragmentRoot) {
expanded.addAll(Arrays.asList(((IPackageFragmentRoot) next).getChildren()));
}
else if (next instanceof IClassFile || next instanceof IJavaScriptUnit) {
IJavaScriptElement[] filtered = filter(((IParent) next).getChildren());
List newChildren = Arrays.asList(filtered);
allChildren.removeAll(newChildren);
if (fIsFlatLayout || neverGroup) {
if (onlyGroup == null) {
allChildren.addAll(newChildren);
}
else {
for (int j = 0; j < filtered.length; j++) {
switch(filtered[j].getElementType()) {
case IJavaScriptElement.TYPE :
case IJavaScriptElement.FIELD :
case IJavaScriptElement.METHOD :
case IJavaScriptElement.INITIALIZER :
case IJavaScriptElement.LOCAL_VARIABLE : {
String displayName = filtered[j].getDisplayName();
int groupNamesEnd = displayName.lastIndexOf('.');
if (groupNamesEnd == onlyGroup.fNamePrefixLength && displayName.startsWith(onlyGroup.fNamePrefix)) {
allChildren.add(filtered[j]);
}
break;
}
default : {
allChildren.add(filtered[j]);
}
}
}
}
}
else {
for (int j = 0; j < filtered.length; j++) {
switch(filtered[j].getElementType()) {
case IJavaScriptElement.TYPE :
case IJavaScriptElement.FIELD :
case IJavaScriptElement.METHOD :
case IJavaScriptElement.INITIALIZER :
case IJavaScriptElement.LOCAL_VARIABLE : {
String displayName = filtered[j].getDisplayName();
int groupEnd = displayName.lastIndexOf('.');
if (groupEnd > 0) {
String groupName = displayName.substring(0, groupEnd);
if (!groups.containsKey(groupName)) {
// create the group based on the parent type
NamespaceGroup group = null;
if(parentElement instanceof IPackageFragmentRoot) {
group = new NamespaceGroup((IPackageFragmentRoot) parentElement, groupName);
} else if(parentElement instanceof IJavaScriptUnit) {
group = new NamespaceGroup((IJavaScriptUnit) parentElement, groupName);
}
if(group != null) {
groups.put(groupName, group);
allChildren.add(group);
}
}
}
else {
allChildren.add(filtered[j]);
}
break;
}
default : {
allChildren.add(filtered[j]);
}
}
}
}
}
if (expanded.size() > 0)
next = expanded.remove(0);
else
next = null;
}
return allChildren.toArray();
}
private Object[] getLibraryChildren(IPackageFragmentRoot container, boolean neverGroup, NamespaceGroup onlyGroup) {
Object[] children=null;
try {
children = container.getChildren();
} catch (JavaScriptModelException ex1) {
ex1.printStackTrace();
}
if(children==null) return null;
ArrayList allChildren = new ArrayList();
Map groups = null;
if (!fIsFlatLayout) {
groups = new HashMap();
}
boolean unique = false;
try {
while(!unique && children!=null && children.length>0) {
for(int i = 0;i<children.length;i++) {
String display1 = ((IJavaScriptElement)children[0]).getDisplayName();
String display2 = ((IJavaScriptElement)children[i]).getDisplayName();
if(!( (display1==display2) || (display1!=null && display1.compareTo(display2)==0))){
allChildren.addAll(Arrays.asList(children));
unique=true;
break;
}
}
List more = new ArrayList();
for(int i = 0;!unique && i<children.length;i++) {
if(children[i] instanceof IPackageFragment) {
more.addAll(Arrays.asList(((IPackageFragment)children[i]).getChildren()));
}else if(children[i] instanceof IPackageFragmentRoot) {
more.addAll(Arrays.asList(((IPackageFragmentRoot)children[i]).getChildren()));
}else if(children[i] instanceof IClassFile || children[i] instanceof IJavaScriptUnit) {
IJavaScriptElement[] filtered = filter(((IParent) children[i]).getChildren());
List newChildren = Arrays.asList(filtered);
allChildren.removeAll(newChildren);
if (fIsFlatLayout || neverGroup) {
if (onlyGroup == null) {
allChildren.addAll(newChildren);
}
else {
for (int j = 0; j < filtered.length; j++) {
switch(filtered[j].getElementType()) {
case IJavaScriptElement.TYPE :
case IJavaScriptElement.FIELD :
case IJavaScriptElement.METHOD :
case IJavaScriptElement.INITIALIZER :
case IJavaScriptElement.LOCAL_VARIABLE : {
String displayName = filtered[j].getDisplayName();
int groupNamesEnd = displayName.lastIndexOf('.');
if (groupNamesEnd == onlyGroup.fNamePrefixLength && displayName.startsWith(onlyGroup.fNamePrefix)) {
allChildren.add(filtered[j]);
}
break;
}
default : {
allChildren.add(filtered[j]);
}
}
}
}
}
else {
for (int j = 0; j < filtered.length; j++) {
switch(filtered[j].getElementType()) {
case IJavaScriptElement.TYPE :
case IJavaScriptElement.FIELD :
case IJavaScriptElement.METHOD :
case IJavaScriptElement.INITIALIZER :
case IJavaScriptElement.LOCAL_VARIABLE : {
String displayName = filtered[j].getDisplayName();
int groupEnd = displayName.lastIndexOf('.');
if (groupEnd > 0) {
String groupName = displayName.substring(0, groupEnd);
if (!groups.containsKey(groupName)) {
NamespaceGroup group = new NamespaceGroup(container, groupName);
groups.put(groupName, group);
allChildren.add(group);
}
}
else {
allChildren.add(filtered[j]);
}
break;
}
default : {
allChildren.add(filtered[j]);
}
}
}
}
}
else {
/* bottomed out, now at javaScriptElement level */
unique=true;
break;
}
}
if(!unique) children = more.toArray();
}
} catch (JavaScriptModelException ex) {
ex.printStackTrace();
}
return allChildren.toArray();
}
/* (non-Javadoc)
* @see org.eclipse.wst.jsdt.ui.StandardJavaScriptElementContentProvider#getPackageFragmentRoots(org.eclipse.wst.jsdt.core.IJavaScriptProject)
*/
protected Object[] getPackageFragmentRoots(IJavaScriptProject project) throws JavaScriptModelException {
if (!project.getProject().isOpen())
return NO_CHILDREN;
List result= new ArrayList();
boolean addJARContainer= false;
ArrayList projectPackageFragmentRoots = new ArrayList();
IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
for (int i= 0; i < roots.length; i++) {
IPackageFragmentRoot root= roots[i];
IIncludePathEntry classpathEntry= root.getRawIncludepathEntry();
IIncludePathAttribute[] attribs = classpathEntry.getExtraAttributes();
boolean shouldHide = false;
for(int p = 0;p<attribs.length;p++){
if(attribs[p]==IIncludePathAttribute.HIDE) shouldHide = true;
}
int entryKind= classpathEntry.getEntryKind();
if (entryKind == IIncludePathEntry.CPE_CONTAINER) {
// all JsGlobalScopeContainers are added later
} else if (fShowLibrariesNode && (entryKind != IIncludePathEntry.CPE_SOURCE) && entryKind!=IIncludePathEntry.CPE_CONTAINER) {
addJARContainer= true;
projectPackageFragmentRoots.add(root);
} else {
if (isProjectPackageFragmentRoot(root)) {
// filter out package fragments that correspond to projects and
// replace them with the package fragments directly
// Object[] fragments= getPackageFragmentRootContent(root);
// for (int j= 0; j < fragments.length; j++) {
// result.add(fragments[j]);
// }
} /*else if(!shouldHide){
result.add(root);
}*/
}
}
if (addJARContainer) {
projectPackageFragmentRoots.add(new LibraryContainer(project));
}
// separate loop to make sure all containers are on the classpath
IIncludePathEntry[] rawClasspath= project.getRawIncludepath();
for (int i= 0; i < rawClasspath.length; i++) {
IIncludePathEntry classpathEntry= rawClasspath[i];
if (classpathEntry.getEntryKind() == IIncludePathEntry.CPE_CONTAINER) {
projectPackageFragmentRoots.add(new JsGlobalScopeContainer(project, classpathEntry));
}
}
// Object[] resources= project.getNonJavaScriptResources();
// for (int i= 0; i < resources.length; i++) {
// result.add(resources[i]);
// }
ProjectLibraryRoot projectLibs = new ProjectLibraryRoot(project);
result.add(0,projectLibs);
return result.toArray();
}
public Object getParent(Object element) {
if (element instanceof NamespaceGroup) {
return ((NamespaceGroup)element).getParent();
}
if (element instanceof IPackageFragmentRoot) {
IJavaScriptProject project = (IJavaScriptProject) ((IPackageFragmentRoot) element).getAncestor(IJavaScriptElement.JAVASCRIPT_PROJECT);
if (project != null) {
return new ProjectLibraryRoot(project);
}
}
return super.getParent(element);
}
// private Object[] getContainerPackageFragmentRoots3(PackageFragmentRootContainer container) {
// Object[] children = container.getChildren();
// if(children==null) return null;
// ArrayList allChildren = new ArrayList();
// for(int i=0;i<children.length;i++) {
// try {
// allChildren.addAll(Arrays.asList(((IPackageFragmentRoot)children[i]).getChildren()));
// } catch (JavaScriptModelException ex) {
//
// }
// }
// return allChildren.toArray();
// }
//
private Object[] getContainerPackageFragmentRoots(PackageFragmentRootContainer container) {
return getContainerPackageFragmentRoots(container, false, null);
}
private Object[] getContainerPackageFragmentRoots(PackageFragmentRootContainer container, boolean neverGroup, NamespaceGroup onlyGroup) {
Object[] children = container.getChildren();
if(children==null) return new Object[0];
ArrayList allChildren = new ArrayList();
ArrayList expanded = new ArrayList();
expanded.addAll(Arrays.asList(children));
if(expanded.isEmpty()) return new Object[0];
Map groups = null;
if (!fIsFlatLayout) {
groups = new HashMap();
}
Object next = expanded.remove(0);
while(next!=null) {
try {
if(next instanceof IPackageFragment) {
expanded.addAll(Arrays.asList(((IPackageFragment)next).getChildren()));
}else if(next instanceof IPackageFragmentRoot) {
expanded.addAll(Arrays.asList(((IPackageFragmentRoot)next).getChildren()));
}else if(next instanceof IClassFile || next instanceof IJavaScriptUnit) {
IJavaScriptElement[] filtered = filter(((IParent) next).getChildren());
List newChildren = Arrays.asList(filtered);
allChildren.removeAll(newChildren);
if (fIsFlatLayout || neverGroup) {
if (onlyGroup == null) {
allChildren.addAll(newChildren);
}
else {
for (int j = 0; j < filtered.length; j++) {
switch(filtered[j].getElementType()) {
case IJavaScriptElement.TYPE :
case IJavaScriptElement.FIELD :
case IJavaScriptElement.METHOD :
case IJavaScriptElement.INITIALIZER :
case IJavaScriptElement.LOCAL_VARIABLE : {
String displayName = filtered[j].getDisplayName();
int groupNamesEnd = displayName.lastIndexOf('.');
if (groupNamesEnd == onlyGroup.fNamePrefixLength && displayName.startsWith(onlyGroup.fNamePrefix)) {
allChildren.add(filtered[j]);
}
break;
}
default : {
allChildren.add(filtered[j]);
}
}
}
}
}
else {
for (int j = 0; j < filtered.length; j++) {
switch(filtered[j].getElementType()) {
case IJavaScriptElement.TYPE :
case IJavaScriptElement.FIELD :
case IJavaScriptElement.METHOD :
case IJavaScriptElement.INITIALIZER :
case IJavaScriptElement.LOCAL_VARIABLE : {
String displayName = filtered[j].getDisplayName();
int groupEnd = displayName.lastIndexOf('.');
if (groupEnd > 0) {
String groupName = displayName.substring(0, groupEnd);
if (!groups.containsKey(groupName)) {
NamespaceGroup group = new NamespaceGroup(container, groupName);
groups.put(groupName, group);
allChildren.add(group);
}
}
else {
allChildren.add(filtered[j]);
}
break;
}
default : {
allChildren.add(filtered[j]);
}
}
}
}
}
else {
allChildren.add(next);
}
} catch (JavaScriptModelException ex) {
ex.printStackTrace();
}
if(expanded.size()>0)
next = expanded.remove(0);
else
next = null;
}
return allChildren.toArray();
}
private Object[] getContainerPackageFragmentRootsDeprc(PackageFragmentRootContainer container, boolean createFolder) {
if(container!=null) {
Object[] children = container.getChildren();
if(children==null) return null;
ArrayList allChildren = new ArrayList();
boolean unique = false;
while(!unique && children!=null && children.length>0) {
String display1=null;
for(int i = 0;i<children.length;i++) {
display1 = ((IJavaScriptElement)children[0]).getDisplayName();
String display2 = ((IJavaScriptElement)children[i]).getDisplayName();
if(!( (display1==display2) || (display1!=null && display1.compareTo(display2)==0))){
allChildren.addAll(Arrays.asList(children));
unique=true;
break;
}
}
if(!unique && createFolder) {
ContainerFolder folder = new ContainerFolder(display1, container);
return new Object[] {folder};
}
ArrayList more = new ArrayList();
for(int i = 0;!unique && i<children.length;i++) {
try {
if(children[i] instanceof IPackageFragment) {
more.addAll(Arrays.asList(((IPackageFragment)children[i]).getChildren()));
}else if(children[i] instanceof IPackageFragmentRoot) {
more.addAll(Arrays.asList(((IPackageFragmentRoot)children[i]).getChildren()));
}else if(children[i] instanceof IClassFile) {
more.addAll(Arrays.asList( filter(((IClassFile)children[i]).getChildren())) );
}else if(children[i] instanceof IJavaScriptUnit) {
more.addAll(Arrays.asList( filter(((IJavaScriptUnit)children[i]).getChildren())) );
}else {
/* bottomed out, now at javaElement level */
unique=true;
break;
}
} catch (JavaScriptModelException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
if(!unique) children = more.toArray();
}
return allChildren.toArray();
}else {
Object[] children = container.getChildren();
if(children==null) return null;
ArrayList allChildren = new ArrayList();
for(int i=0;i<children.length;i++) {
try {
allChildren.addAll(Arrays.asList(((IPackageFragmentRoot)children[i]).getChildren()));
} catch (JavaScriptModelException ex) {
}
}
return allChildren.toArray();
}
}
private Object[] getNonJavaProjects(IJavaScriptModel model) throws JavaScriptModelException {
return model.getNonJavaScriptResources();
}
protected Object internalGetParent(Object element) {
if (!fIsFlatLayout && element instanceof IPackageFragment) {
return getHierarchicalPackageParent((IPackageFragment) element);
} else if (element instanceof IPackageFragmentRoot) {
// since we insert logical package containers we have to fix
// up the parent for package fragment roots so that they refer
// to the container and containers refer to the project
IPackageFragmentRoot root= (IPackageFragmentRoot)element;
try {
IIncludePathEntry entry= root.getRawIncludepathEntry();
int entryKind= entry.getEntryKind();
if (entryKind == IIncludePathEntry.CPE_CONTAINER) {
return new JsGlobalScopeContainer(root.getJavaScriptProject(), entry);
} else if (fShowLibrariesNode && (entryKind == IIncludePathEntry.CPE_LIBRARY || entryKind == IIncludePathEntry.CPE_VARIABLE)) {
return new LibraryContainer(root.getJavaScriptProject());
}
} catch (JavaScriptModelException e) {
// fall through
}
} else if (element instanceof PackageFragmentRootContainer) {
return ((PackageFragmentRootContainer)element).getJavaProject();
}
return super.internalGetParent(element);
}
/* (non-Javadoc)
* Method declared on IContentProvider.
*/
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
super.inputChanged(viewer, oldInput, newInput);
fViewer= (TreeViewer)viewer;
if (oldInput == null && newInput != null) {
JavaScriptCore.addElementChangedListener(this);
} else if (oldInput != null && newInput == null) {
JavaScriptCore.removeElementChangedListener(this);
}
fInput= newInput;
}
// hierarchical packages
/**
* Returns the hierarchical packages inside a given fragment or root.
* @param parent The parent package fragment root
* @param fragment The package to get the children for or 'null' to get the children of the root.
* @param result Collection where the resulting elements are added
* @throws JavaScriptModelException
*/
private void getHierarchicalPackageChildren(IPackageFragmentRoot parent, IPackageFragment fragment, Collection result) throws JavaScriptModelException {
IJavaScriptElement[] children= parent.getChildren();
String prefix= fragment != null ? fragment.getElementName() + '/' : ""; //$NON-NLS-1$
if (prefix.length()==1)
prefix=""; //$NON-NLS-1$
int prefixLen= prefix.length();
for (int i= 0; i < children.length; i++) {
IPackageFragment curr= (IPackageFragment) children[i];
if (fragment==null)
{
if (curr.isDefaultPackage())
result.add(curr);
}
else
{
String name= curr.getElementName();
if (name.startsWith(prefix) && name.length() > prefixLen && name.indexOf('/', prefixLen) == -1) {
if (fFoldPackages) {
curr= getFolded(children, curr);
}
result.add(curr);
}
}
}
}
/**
* Returns the hierarchical packages inside a given folder.
* @param folder The parent folder
* @param result Collection where the resulting elements are added
* @throws CoreException thrown when elements could not be accessed
*/
private void getHierarchicalPackagesInFolder(IFolder folder, Collection result) throws CoreException {
IResource[] resources= folder.members();
for (int i= 0; i < resources.length; i++) {
IResource resource= resources[i];
if (resource instanceof IFolder) {
IFolder curr= (IFolder) resource;
IJavaScriptElement element= JavaScriptCore.create(curr);
if (element instanceof IPackageFragment) {
if (element.exists()) {
if (fFoldPackages) {
IPackageFragment fragment= (IPackageFragment) element;
IPackageFragmentRoot root= (IPackageFragmentRoot) fragment.getParent();
element= getFolded(root.getChildren(), fragment);
}
result.add(element);
}
}
}
}
}
public Object getHierarchicalPackageParent(IPackageFragment child) {
String name= child.getElementName();
IPackageFragmentRoot parent= (IPackageFragmentRoot) child.getParent();
int index= name.lastIndexOf('/');
if (index != -1) {
String realParentName= name.substring(0, index);
IPackageFragment element= parent.getPackageFragment(realParentName);
if (element.exists()) {
try {
if (fFoldPackages && isEmpty(element) && findSinglePackageChild(element, parent.getChildren()) != null) {
return getHierarchicalPackageParent(element);
}
} catch (JavaScriptModelException e) {
// ignore
}
return element;
} else { // bug 65240
IResource resource= element.getResource();
if (resource != null) {
return resource;
}
}
}
if (parent.getResource() instanceof IProject) {
return parent.getJavaScriptProject();
}
return parent;
}
private static IPackageFragment getFolded(IJavaScriptElement[] children, IPackageFragment pack) throws JavaScriptModelException {
while (isEmpty(pack)) {
IPackageFragment collapsed= findSinglePackageChild(pack, children);
if (collapsed == null) {
return pack;
}
pack= collapsed;
}
return pack;
}
private static boolean isEmpty(IPackageFragment fragment) throws JavaScriptModelException {
return !fragment.containsJavaResources() && fragment.getNonJavaScriptResources().length == 0;
}
private static IPackageFragment findSinglePackageChild(IPackageFragment fragment, IJavaScriptElement[] children) {
String prefix= fragment.getElementName() + '/';
int prefixLen= prefix.length();
IPackageFragment found= null;
for (int i= 0; i < children.length; i++) {
IJavaScriptElement element= children[i];
String name= element.getElementName();
if (name.startsWith(prefix) && name.length() > prefixLen && name.indexOf('/', prefixLen) == -1) {
if (found == null) {
found= (IPackageFragment) element;
} else {
return null;
}
}
}
return found;
}
// ------ delta processing ------
/**
* Processes a delta recursively. When more than two children are affected the
* tree is fully refreshed starting at this node.
*
* @param delta the delta to process
* @param runnables the resulting view changes as runnables (type {@link Runnable})
* @return true is returned if the conclusion is to refresh a parent of an element. In that case no siblings need
* to be processed
* @throws JavaScriptModelException thrown when the access to an element failed
*/
private boolean processDelta(IJavaScriptElementDelta delta, Collection runnables) throws JavaScriptModelException {
int kind= delta.getKind();
int flags= delta.getFlags();
IJavaScriptElement element= delta.getElement();
int elementType= element.getElementType();
if (elementType != IJavaScriptElement.JAVASCRIPT_MODEL && elementType != IJavaScriptElement.JAVASCRIPT_PROJECT) {
IJavaScriptProject proj= element.getJavaScriptProject();
if (proj == null || !proj.getProject().isOpen()) // TODO: Not needed if parent already did the 'open' check!
return false;
}
if (!fIsFlatLayout && elementType == IJavaScriptElement.PACKAGE_FRAGMENT) {
if (kind == IJavaScriptElementDelta.REMOVED) {
final Object parent = getHierarchicalPackageParent((IPackageFragment) element);
if (parent instanceof IPackageFragmentRoot) {
postRemove(element, runnables);
return false;
} else {
postRefresh(internalGetParent(parent), GRANT_PARENT, element, runnables);
return true;
}
} else if (kind == IJavaScriptElementDelta.ADDED) {
final Object parent = getHierarchicalPackageParent((IPackageFragment) element);
if (parent instanceof IPackageFragmentRoot) {
//postAdd(parent, element, runnables);
return false;
} else {
postRefresh(internalGetParent(parent), GRANT_PARENT, element, runnables);
return true;
}
}
handleAffectedChildren(delta, element, runnables);
return false;
}
if (elementType == IJavaScriptElement.JAVASCRIPT_UNIT) {
IJavaScriptUnit cu= (IJavaScriptUnit) element;
if (!JavaModelUtil.isPrimary(cu)) {
return false;
}
if (!getProvideMembers() && cu.isWorkingCopy() && kind == IJavaScriptElementDelta.CHANGED) {
return false;
}
if ((kind == IJavaScriptElementDelta.CHANGED) /* && !isStructuralCUChange(flags)*/) {
// XXX: Remove structure change optimization until ASTConvertor is ready
if ((flags & IJavaScriptElementDelta.F_CONTENT) == 1)
return false; // test moved ahead
}
if (!isOnClassPath(cu)) { // TODO: isOnClassPath expensive! Should be put after all cheap tests
return false;
}
}
if (elementType == IJavaScriptElement.JAVASCRIPT_PROJECT) {
// handle open and closing of a project
if ((flags & (IJavaScriptElementDelta.F_CLOSED | IJavaScriptElementDelta.F_OPENED)) != 0) {
postRefresh(element, ORIGINAL, element, runnables);
return false;
}
// if the raw class path has changed we refresh the entire project
if ((flags & IJavaScriptElementDelta.F_INCLUDEPATH_CHANGED) != 0) {
postRefresh(element, ORIGINAL, element, runnables);
if (element.getElementType() == IJavaScriptElement.JAVASCRIPT_PROJECT) {
postRefresh(((IJavaScriptProject)element).getProject(), ORIGINAL, ((IJavaScriptProject)element).getProject(), runnables);
}
return false;
}
// if added it could be that the corresponding IProject is already shown. Remove it first.
// bug 184296
if (kind == IJavaScriptElementDelta.ADDED) {
//postRemove(element.getResource(), runnables);
//postAdd(element.getParent(), element, runnables);
return false;
}
}
if (kind == IJavaScriptElementDelta.REMOVED) {
Object parent= internalGetParent(element);
if (element instanceof IPackageFragment) {
// refresh package fragment root to allow filtering empty (parent) packages: bug 72923
if (fViewer.testFindItem(parent) != null)
postRefresh(parent, PARENT, element, runnables);
return true;
}
postRemove(element, runnables);
if (parent instanceof IPackageFragment)
postUpdateIcon((IPackageFragment)parent, runnables);
// we are filtering out empty subpackages, so we
// a package becomes empty we remove it from the viewer.
if (isPackageFragmentEmpty(element.getParent())) {
if (fViewer.testFindItem(parent) != null)
postRefresh(internalGetParent(parent), GRANT_PARENT, element, runnables);
return true;
}
return false;
}
if (kind == IJavaScriptElementDelta.ADDED) {
Object parent= internalGetParent(element);
// we are filtering out empty subpackages, so we
// have to handle additions to them specially.
if (parent instanceof IPackageFragment) {
Object grandparent= internalGetParent(parent);
// 1GE8SI6: ITPJUI:WIN98 - Rename is not shown in Packages View
// avoid posting a refresh to an invisible parent
if (parent.equals(fInput)) {
postRefresh(parent, PARENT, element, runnables);
} else {
// refresh from grandparent if parent isn't visible yet
if (fViewer.testFindItem(parent) == null)
postRefresh(grandparent, GRANT_PARENT, element, runnables);
else {
postRefresh(parent, PARENT, element, runnables);
}
}
return true;
} else {
//postAdd(parent, element, runnables);
}
}
if (elementType == IJavaScriptElement.JAVASCRIPT_UNIT) {
if (kind == IJavaScriptElementDelta.CHANGED) {
// isStructuralCUChange already performed above
postRefresh(element, ORIGINAL, element, runnables);
IResource underlyingResource = ((IJavaScriptUnit)element).getUnderlyingResource();
if(underlyingResource != null) {
postRefresh(underlyingResource, ORIGINAL, element, runnables);
}
if ((flags & IJavaScriptElementDelta.F_PRIMARY_RESOURCE) != 0) {
IJavaScriptElement parent2 = element.getAncestor(IJavaScriptElement.PACKAGE_FRAGMENT_ROOT);
if (parent2 != null) {
postRefresh(parent2, ORIGINAL, element, runnables);
}
}
updateSelection(delta, runnables);
}
return false;
}
// no changes possible in class files
if (elementType == IJavaScriptElement.CLASS_FILE)
return false;
if (elementType == IJavaScriptElement.PACKAGE_FRAGMENT_ROOT) {
// the contents of an external JAR has changed
if ((flags & IJavaScriptElementDelta.F_ARCHIVE_CONTENT_CHANGED) != 0) {
postRefresh(element, ORIGINAL, element, runnables);
return false;
}
// the source attachment of a JAR has changed
if ((flags & (IJavaScriptElementDelta.F_SOURCEATTACHED | IJavaScriptElementDelta.F_SOURCEDETACHED)) != 0)
postUpdateIcon(element, runnables);
if (isClassPathChange(delta)) {
// throw the towel and do a full refresh of the affected java project.
postRefresh(element.getJavaScriptProject(), PROJECT, element, runnables);
return true;
}
}
handleAffectedChildren(delta, element, runnables);
return false;
}
private static boolean isStructuralCUChange(int flags) {
// No refresh on working copy creation (F_PRIMARY_WORKING_COPY)
return ((flags & IJavaScriptElementDelta.F_CHILDREN) != 0) || ((flags & (IJavaScriptElementDelta.F_CONTENT | IJavaScriptElementDelta.F_FINE_GRAINED)) == IJavaScriptElementDelta.F_CONTENT);
}
/* package */ void handleAffectedChildren(IJavaScriptElementDelta delta, IJavaScriptElement element, Collection runnables) throws JavaScriptModelException {
int count= 0;
IResourceDelta[] resourceDeltas= delta.getResourceDeltas();
if (resourceDeltas != null) {
for (int i= 0; i < resourceDeltas.length; i++) {
int kind= resourceDeltas[i].getKind();
if (kind == IResourceDelta.ADDED || kind == IResourceDelta.REMOVED) {
count++;
}
}
}
IJavaScriptElementDelta[] affectedChildren= delta.getAffectedChildren();
for (int i= 0; i < affectedChildren.length; i++) {
int kind= affectedChildren[i].getKind();
if (kind == IJavaScriptElementDelta.ADDED || kind == IJavaScriptElementDelta.REMOVED) {
count++;
}
}
if (count > 1) {
// more than one child changed, refresh from here downwards
if (element instanceof IPackageFragment) {
// a package fragment might become non empty refresh from the parent
IJavaScriptElement parent= (IJavaScriptElement) internalGetParent(element);
// 1GE8SI6: ITPJUI:WIN98 - Rename is not shown in Packages View
// avoid posting a refresh to an invisible parent
if (element.equals(fInput)) {
postRefresh(element, ORIGINAL, element, runnables);
} else {
postRefresh(parent, PARENT, element, runnables);
}
} else if (element instanceof IPackageFragmentRoot) {
Object toRefresh= internalGetParent(element);
postRefresh(toRefresh, ORIGINAL, toRefresh, runnables);
} else {
postRefresh(element, ORIGINAL, element, runnables);
}
return;
}
if (resourceDeltas != null) {
for (int i= 0; i < resourceDeltas.length; i++) {
if (processResourceDelta(resourceDeltas[i], element, runnables)) {
return; // early return, element got refreshed
}
}
}
for (int i= 0; i < affectedChildren.length; i++) {
if (processDelta(affectedChildren[i], runnables)) {
return; // early return, element got refreshed
}
}
}
protected void processAffectedChildren(IJavaScriptElementDelta[] affectedChildren, Collection runnables) throws JavaScriptModelException {
for (int i= 0; i < affectedChildren.length; i++) {
processDelta(affectedChildren[i], runnables);
}
}
private boolean isOnClassPath(IJavaScriptUnit element) {
IJavaScriptProject project= element.getJavaScriptProject();
if (project == null || !project.exists())
return false;
return project.isOnIncludepath(element);
}
/**
* Updates the selection. It finds newly added elements
* and selects them.
* @param delta the delta to process
* @param runnables the resulting view changes as runnables (type {@link Runnable})
*/
private void updateSelection(IJavaScriptElementDelta delta, Collection runnables) {
final IJavaScriptElement addedElement= findAddedElement(delta);
if (addedElement != null) {
final StructuredSelection selection= new StructuredSelection(addedElement);
runnables.add(new Runnable() {
public void run() {
// 19431
// if the item is already visible then select it
if (fViewer.testFindItem(addedElement) != null)
fViewer.setSelection(selection);
}
});
}
}
private IJavaScriptElement findAddedElement(IJavaScriptElementDelta delta) {
if (delta.getKind() == IJavaScriptElementDelta.ADDED)
return delta.getElement();
IJavaScriptElementDelta[] affectedChildren= delta.getAffectedChildren();
for (int i= 0; i < affectedChildren.length; i++)
return findAddedElement(affectedChildren[i]);
return null;
}
/**
* Updates the package icon
* @param element the element to update
* @param runnables the resulting view changes as runnables (type {@link Runnable})
*/
private void postUpdateIcon(final IJavaScriptElement element, Collection runnables) {
runnables.add(new Runnable() {
public void run() {
// 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
fViewer.update(element, new String[]{IBasicPropertyConstants.P_IMAGE});
}
});
}
/**
* Process a resource delta.
*
* @param delta the delta to process
* @param parent the parent
* @param runnables the resulting view changes as runnables (type {@link Runnable})
* @return true if the parent got refreshed
*/
private boolean processResourceDelta(IResourceDelta delta, Object parent, Collection runnables) {
int status= delta.getKind();
int flags= delta.getFlags();
IResource resource= delta.getResource();
// filter out changes affecting the output folder
if (resource == null)
return false;
// this could be optimized by handling all the added children in the parent
if ((status & IResourceDelta.REMOVED) != 0) {
if (parent instanceof IPackageFragment) {
// refresh one level above to deal with empty package filtering properly
postRefresh(internalGetParent(parent), PARENT, parent, runnables);
return true;
} else {
postRemove(resource, runnables);
return false;
}
}
if ((status & IResourceDelta.ADDED) != 0) {
if (parent instanceof IPackageFragment) {
// refresh one level above to deal with empty package filtering properly
postRefresh(internalGetParent(parent), PARENT, parent, runnables);
return true;
} else
{
//postAdd(parent, resource, runnables);
return false;
}
}
if ((status & IResourceDelta.CHANGED) != 0) {
if ((flags & IResourceDelta.TYPE) != 0) {
postRefresh(parent, PARENT, resource, runnables);
return true;
}
}
// open/close state change of a project
if ((flags & IResourceDelta.OPEN) != 0) {
postProjectStateChanged(internalGetParent(parent), runnables);
return true;
}
IResourceDelta[] resourceDeltas= delta.getAffectedChildren();
int count= 0;
for (int i= 0; i < resourceDeltas.length; i++) {
int kind= resourceDeltas[i].getKind();
if (kind == IResourceDelta.ADDED || kind == IResourceDelta.REMOVED) {
count++;
if (count > 1) {
postRefresh(parent, PARENT, resource, runnables);
return true;
}
}
}
for (int i= 0; i < resourceDeltas.length; i++) {
if (processResourceDelta(resourceDeltas[i], resource, runnables)) {
return false; // early return, element got refreshed
}
}
return false;
}
public void setIsFlatLayout(boolean state) {
fIsFlatLayout= state;
}
public void setShowLibrariesNode(boolean state) {
fShowLibrariesNode= state;
}
private void postRefresh(Object root, int relation, Object affectedElement, Collection runnables) {
// JFace doesn't refresh when object isn't part of the viewer
// Therefore move the refresh start down to the viewer's input
if (isParent(root, fInput))
root= fInput;
List toRefresh= new ArrayList(1);
toRefresh.add(root);
augmentElementToRefresh(toRefresh, relation, affectedElement);
postRefresh(toRefresh, true, runnables);
}
/**
* Can be implemented by subclasses to add additional elements to refresh
*
* @param toRefresh the elements to refresh
* @param relation the relation to the affected element ({@link #GRANT_PARENT}, {@link #PARENT}, {@link #ORIGINAL}, {@link #PROJECT})
* @param affectedElement the affected element
*/
protected void augmentElementToRefresh(List toRefresh, int relation, Object affectedElement) {
}
private boolean isParent(Object root, Object child) {
Object parent= getParent(child);
if (parent == null)
return false;
if (parent.equals(root))
return true;
return isParent(root, parent);
}
protected void postRefresh(final List toRefresh, final boolean updateLabels, Collection runnables) {
runnables.add(new Runnable() {
public void run() {
for (Iterator iter= toRefresh.iterator(); iter.hasNext();) {
fViewer.refresh(iter.next(), updateLabels);
}
}
});
}
// protected void postAdd(final Object parent, final Object element, Collection runnables) {
// runnables.add(new Runnable() {
// public void run() {
// Widget[] items= fViewer.testFindItems(element);
// for (int i= 0; i < items.length; i++) {
// Widget item= items[i];
// if (item instanceof TreeItem && !item.isDisposed()) {
// TreeItem parentItem= ((TreeItem) item).getParentItem();
// if (parentItem != null && !parentItem.isDisposed() && parent.equals(parentItem.getData())) {
// return; // no add, element already added (most likely by a refresh)
// }
// }
// }
// fViewer.add(parent, element);
// }
// });
// }
protected void postRemove(final Object element, Collection runnables) {
runnables.add(new Runnable() {
public void run() {
fViewer.remove(element);
}
});
}
protected void postProjectStateChanged(final Object root, Collection runnables) {
runnables.add(new Runnable() {
public void run() {
fViewer.refresh(root, true);
// trigger a synthetic selection change so that action refresh their
// enable state.
fViewer.setSelection(fViewer.getSelection());
}
});
}
/*
* @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
*/
public void propertyChange(PropertyChangeEvent event) {
if (arePackagesFoldedInHierarchicalLayout() != fFoldPackages){
fFoldPackages= arePackagesFoldedInHierarchicalLayout();
if (fViewer != null && !fViewer.getControl().isDisposed()) {
fViewer.getControl().setRedraw(false);
Object[] expandedObjects= fViewer.getExpandedElements();
fViewer.refresh();
fViewer.setExpandedElements(expandedObjects);
fViewer.getControl().setRedraw(true);
}
}
}
}
|