1 11 package org.eclipse.ui.internal.ide.dialogs; 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 35 public class ResourceSorter extends ViewerSorter { 36 37 40 public final static int NAME = 1; 41 42 45 public final static int TYPE = 2; 46 47 private int criteria; 48 49 55 public ResourceSorter(int criteria) { 56 super(); 57 this.criteria = criteria; 58 } 59 60 74 protected int classComparison(Object element) { 75 if (element instanceof IResource) { 76 return 2; 77 } 78 return 0; 79 } 80 81 84 public int compare(Viewer viewer, Object o1, Object o2) { 85 if (!(o1 instanceof IResource && o2 instanceof IResource)) { 89 return compareClass(o1, o2); 90 } 91 IResource r1 = (IResource) o1; 92 IResource r2 = (IResource) o2; 93 94 if (r1 instanceof IContainer && r2 instanceof IContainer) { 95 return compareNames(r1, r2); 96 } else if (r1 instanceof IContainer) { 97 return -1; 98 } else if (r2 instanceof IContainer) { 99 return 1; 100 } else if (criteria == NAME) { 101 return compareNames(r1, r2); 102 } else if (criteria == TYPE) { 103 return compareTypes(r1, r2); 104 } else { 105 return 0; 106 } 107 } 108 109 120 protected int compareClass(Object element1, Object element2) { 121 return classComparison(element1) - classComparison(element2); 122 } 123 124 135 protected int compareNames(IResource resource1, IResource resource2) { 136 return collator.compare(resource1.getName(), resource2.getName()); 137 } 138 139 151 protected int compareTypes(IResource resource1, IResource resource2) { 152 String ext1 = getExtensionFor(resource1); 153 String ext2 = getExtensionFor(resource2); 154 155 int result = collator.compare(ext1, ext2); 159 160 if (result != 0) { 161 return result; 162 } 163 164 return compareNames(resource1, resource2); 165 } 166 167 172 public int getCriteria() { 173 return criteria; 174 } 175 176 182 private String getExtensionFor(IResource resource) { 183 String ext = resource.getFileExtension(); 184 return ext == null ? "" : ext; } 186 } 187 | Popular Tags |