1 11 12 package org.eclipse.jface.viewers; 13 14 import java.util.Arrays ; 15 import java.util.Comparator ; 16 17 import org.eclipse.jface.util.Policy; 18 19 40 public class ViewerComparator { 41 44 private Comparator comparator; 45 46 51 public ViewerComparator(){ 52 this(null); 53 } 54 55 61 public ViewerComparator(Comparator comparator){ 62 this.comparator = comparator; 63 } 64 65 70 protected Comparator getComparator() { 71 if (comparator == null){ 72 comparator = Policy.getComparator(); 73 } 74 return comparator; 75 } 76 77 91 public int category(Object element) { 92 return 0; 93 } 94 95 116 public int compare(Viewer viewer, Object e1, Object e2) { 117 int cat1 = category(e1); 118 int cat2 = category(e2); 119 120 if (cat1 != cat2) { 121 return cat1 - cat2; 122 } 123 124 String name1; 125 String name2; 126 127 if (viewer == null || !(viewer instanceof ContentViewer)) { 128 name1 = e1.toString(); 129 name2 = e2.toString(); 130 } else { 131 IBaseLabelProvider prov = ((ContentViewer) viewer) 132 .getLabelProvider(); 133 if (prov instanceof ILabelProvider) { 134 ILabelProvider lprov = (ILabelProvider) prov; 135 name1 = lprov.getText(e1); 136 name2 = lprov.getText(e2); 137 } else { 138 name1 = e1.toString(); 139 name2 = e2.toString(); 140 } 141 } 142 if (name1 == null) { 143 name1 = ""; } 145 if (name2 == null) { 146 name2 = ""; } 148 149 return getComparator().compare(name1, name2); 151 } 152 153 166 public boolean isSorterProperty(Object element, String property) { 167 return false; 168 } 169 170 184 public void sort(final Viewer viewer, Object [] elements) { 185 Arrays.sort(elements, new Comparator () { 186 public int compare(Object a, Object b) { 187 return ViewerComparator.this.compare(viewer, a, b); 188 } 189 }); 190 } 191 } 192 | Popular Tags |