Skip to content

Commit 95fe4d8

Browse files
authored
[#716] Fix varying order of elements in project explorer (#717)
1 parent 207bdda commit 95fe4d8

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CElementSorter.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,12 @@ public int compare(Viewer viewer, Object e1, Object e2) {
311311

312312
// non - c resources are sorted using the label from the viewers label provider
313313
if (cat1 == RESOURCES || cat1 == RESOURCEFOLDERS || cat1 == STORAGE || cat1 == OTHERS) {
314-
return compareWithLabelProvider(viewer, e1, e2);
314+
String name1 = getNonCElementLabel(viewer, e1);
315+
String name2 = getNonCElementLabel(viewer, e2);
316+
if (name1 != null && name2 != null) {
317+
return getComparator().compare(name1, name2);
318+
}
319+
return 0; // can't compare
315320
}
316321

317322
String ns1 = ""; //$NON-NLS-1$
@@ -427,20 +432,28 @@ private ISourceRoot getSourceRoot(Object element) {
427432
return (ISourceRoot) celement;
428433
}
429434

430-
private int compareWithLabelProvider(Viewer viewer, Object e1, Object e2) {
435+
// implementation taken from org.eclipse.jdt.ui.JavaElementComparator
436+
private String getNonCElementLabel(Viewer viewer, Object element) {
437+
// try to use the workbench adapter for non - C++ resources or if not available, use the viewers label provider
438+
if (element instanceof IResource) {
439+
return ((IResource) element).getName();
440+
}
441+
if (element instanceof IStorage) {
442+
return ((IStorage) element).getName();
443+
}
444+
if (element instanceof IAdaptable) {
445+
IWorkbenchAdapter adapter = ((IAdaptable) element).getAdapter(IWorkbenchAdapter.class);
446+
if (adapter != null) {
447+
return adapter.getLabel(element);
448+
}
449+
}
431450
if (viewer instanceof ContentViewer) {
432451
IBaseLabelProvider prov = ((ContentViewer) viewer).getLabelProvider();
433452
if (prov instanceof ILabelProvider) {
434-
ILabelProvider lprov = (ILabelProvider) prov;
435-
String name1 = lprov.getText(e1);
436-
String name2 = lprov.getText(e2);
437-
if (name1 != null && name2 != null) {
438-
final Comparator<? super String> comparator = getComparator();
439-
return comparator.compare(name1, name2);
440-
}
453+
return ((ILabelProvider) prov).getText(element);
441454
}
442455
}
443-
return 0; // can't compare
456+
return null;
444457
}
445458

446459
private int getPathEntryIndex(ISourceRoot root) {

0 commit comments

Comments
 (0)