1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.core.resources.mapping.IModelProviderDescriptor; 15 import org.eclipse.core.resources.mapping.ModelProvider; 16 import org.eclipse.jface.viewers.*; 17 import org.eclipse.team.internal.ui.Utils; 18 import org.eclipse.ui.navigator.CommonViewerSorter; 19 20 public class TeamViewerSorter extends TreePathViewerSorter { 21 22 private CommonViewerSorter sorter; 23 24 public TeamViewerSorter(CommonViewerSorter sorter) { 25 this.sorter = sorter; 26 } 27 28 public int category(Object element) { 29 if (element instanceof ModelProvider) { 30 return 2; 31 } 32 IResource resource = Utils.getResource(element); 33 if (resource != null && resource.getType() == IResource.PROJECT) { 34 return 1; 35 } 36 37 return super.category(element); 38 } 39 40 public int compare(Viewer viewer, TreePath parentPath, Object e1, Object e2) { 41 if (parentPath == null || parentPath.getSegmentCount() == 0) { 42 int cat1 = category(e1); 44 int cat2 = category(e2); 45 46 if (cat1 != cat2) 47 return cat1 - cat2; 48 49 if (e1 instanceof ModelProvider && e2 instanceof ModelProvider) { 50 ModelProvider mp1 = (ModelProvider) e1; 51 ModelProvider mp2 = (ModelProvider) e2; 52 if (isExtends(mp1, mp2.getDescriptor())) { 53 return 1; 54 } 55 if (isExtends(mp2, mp1.getDescriptor())) { 56 return -1; 57 } 58 return mp1.getDescriptor().getLabel().compareTo(mp2.getDescriptor().getLabel()); 59 } 60 IResource r1 = Utils.getResource(e1); 61 IResource r2 = Utils.getResource(e2); 62 if (r1 != null && r2 != null) { 63 return r1.getName().compareTo(r2.getName()); 64 } 65 } 66 return sorter.compare(viewer, parentPath, e1, e2); 67 } 68 69 private boolean isExtends(ModelProvider mp1, IModelProviderDescriptor desc) { 70 String [] extended = mp1.getDescriptor().getExtendedModels(); 71 for (int i = 0; i < extended.length; i++) { 72 String id = extended[i]; 73 if (id.equals(desc.getId())) { 74 return true; 75 } 76 } 77 for (int i = 0; i < extended.length; i++) { 78 String id = extended[i]; 79 IModelProviderDescriptor desc2 = ModelProvider.getModelProviderDescriptor(id); 80 if (isExtends(mp1, desc2)) { 81 return true; 82 } 83 } 84 return false; 85 } 86 } 87 | Popular Tags |