1 11 package org.eclipse.ui.views.navigator; 12 13 import org.eclipse.core.resources.IContainer; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.jface.viewers.Viewer; 16 import org.eclipse.jface.viewers.ViewerSorter; 17 18 37 public class ResourceSorter extends ViewerSorter { 38 39 42 public final static int NAME = 1; 43 44 47 public final static int TYPE = 2; 48 49 private int criteria; 50 51 57 public ResourceSorter(int criteria) { 58 super(); 59 this.criteria = criteria; 60 } 61 62 76 protected int classComparison(Object element) { 77 if (element instanceof IResource) { 78 return 2; 79 } 80 return 0; 81 } 82 83 86 public int compare(Viewer viewer, Object o1, Object o2) { 87 if (!(o1 instanceof IResource && o2 instanceof IResource)) { 91 return compareClass(o1, o2); 92 } 93 IResource r1 = (IResource) o1; 94 IResource r2 = (IResource) o2; 95 96 if (r1 instanceof IContainer && r2 instanceof IContainer) { 97 return compareNames(r1, r2); 98 } else if (r1 instanceof IContainer) { 99 return -1; 100 } else if (r2 instanceof IContainer) { 101 return 1; 102 } else if (criteria == NAME) { 103 return compareNames(r1, r2); 104 } else if (criteria == TYPE) { 105 return compareTypes(r1, r2); 106 } else { 107 return 0; 108 } 109 } 110 111 122 protected int compareClass(Object element1, Object element2) { 123 return classComparison(element1) - classComparison(element2); 124 } 125 126 137 protected int compareNames(IResource resource1, IResource resource2) { 138 return collator.compare(resource1.getName(), resource2.getName()); 139 } 140 141 153 protected int compareTypes(IResource resource1, IResource resource2) { 154 String ext1 = getExtensionFor(resource1); 155 String ext2 = getExtensionFor(resource2); 156 157 int result = collator.compare(ext1, ext2); 161 162 if (result != 0) { 163 return result; 164 } 165 166 return compareNames(resource1, resource2); 167 } 168 169 174 public int getCriteria() { 175 return criteria; 176 } 177 178 184 private String getExtensionFor(IResource resource) { 185 String ext = resource.getFileExtension(); 186 return ext == null ? "" : ext; } 188 189 196 public void setCriteria(int criteria) { 197 this.criteria = criteria; 198 } 199 } 200 | Popular Tags |