1 9 10 package org.eclipse.ui.navigator; 11 12 import java.util.Set ; 13 14 import org.eclipse.jface.viewers.TreePath; 15 import org.eclipse.jface.viewers.TreePathViewerSorter; 16 import org.eclipse.jface.viewers.Viewer; 17 import org.eclipse.jface.viewers.ViewerSorter; 18 import org.eclipse.ui.internal.navigator.NavigatorContentService; 19 20 44 public final class CommonViewerSorter extends TreePathViewerSorter { 45 46 private NavigatorContentService contentService; 47 48 private INavigatorSorterService sorterService; 49 50 57 public void setContentService(INavigatorContentService aContentService) { 58 contentService = (NavigatorContentService) aContentService; 59 sorterService = contentService.getSorterService(); 60 } 61 62 67 public int category(Object element) { 68 if (contentService == null) 69 return 0; 70 71 INavigatorContentDescriptor source = contentService.getSourceOfContribution(element); 72 if (source == null) 73 source = getSource(element); 74 return source != null ? source.getPriority() : Priority.NORMAL_PRIORITY_VALUE; 75 } 76 77 public int compare(Viewer viewer, TreePath parentPath, Object e1, Object e2) { 78 if (contentService == null) 79 return -1; 80 INavigatorContentDescriptor sourceOfLvalue = contentService.getSourceOfContribution(e1); 81 INavigatorContentDescriptor sourceOfRvalue = contentService.getSourceOfContribution(e2); 82 83 if (sourceOfLvalue == null) 84 sourceOfLvalue = getSource(e1); 85 if (sourceOfRvalue == null) 86 sourceOfRvalue = getSource(e2); 87 88 if (sourceOfLvalue != null && sourceOfLvalue == sourceOfRvalue) { 90 Object parent; 91 if (parentPath == null) { 92 parent = viewer.getInput(); 93 } else { 94 parent = parentPath.getLastSegment(); 95 } 96 ViewerSorter sorter = sorterService.findSorter(sourceOfLvalue, parent, e1, e2); 97 if (sorter != null) { 98 return sorter.compare(viewer, e1, e2); 99 } 100 } 101 int categoryDelta = category(e1) - category(e2); 102 if (categoryDelta == 0) { 103 return super.compare(viewer, e1, e2); 104 } 105 return categoryDelta; 106 } 107 108 private INavigatorContentDescriptor getSource(Object o) { 109 Set descriptors = contentService.findDescriptorsWithPossibleChild(o); 110 if (descriptors != null && descriptors.size() > 0) { 111 return (INavigatorContentDescriptor) descriptors.iterator().next(); 112 } 113 return null; 114 } 115 116 } 117 | Popular Tags |