1 11 12 package org.eclipse.ui.views.markers.internal; 13 14 import java.util.Arrays ; 15 import java.util.Comparator ; 16 17 import org.eclipse.jface.dialogs.IDialogSettings; 18 import org.eclipse.jface.viewers.TreeViewer; 19 import org.eclipse.jface.viewers.Viewer; 20 import org.eclipse.jface.viewers.ViewerComparator; 21 22 public class TableComparator extends ViewerComparator implements Comparator { 23 24 public static final int MAX_DEPTH = 4; 25 26 public static final int ASCENDING = 1; 27 28 public static final int DESCENDING = -1; 29 30 protected IField[] fields; 31 32 protected int[] priorities; 33 34 protected int[] directions; 35 36 protected int[] defaultPriorities; 37 38 protected int[] defaultDirections; 39 40 public static final String TAG_DIALOG_SECTION = "sorter"; 42 private static final String TAG_PRIORITY = "priority"; 44 private static final String TAG_DIRECTION = "direction"; 46 private static final String TAG_DEFAULT_PRIORITY = "defaultPriority"; 48 private static final String TAG_DEFAULT_DIRECTION = "defaultDirection"; 50 public TableComparator(TableComparator other) { 51 this(other.getFields(), other.getDefaultPriorities(), other 52 .getDefaultDirections()); 53 priorities = other.getPriorities(); 54 directions = other.getDirections(); 55 } 56 57 public TableComparator(IField[] properties, final int[] defaultPriorities, 58 final int[] defaultDirections) { 59 super(); 60 this.fields = properties; 61 if (properties == null 62 || defaultPriorities == null 63 || defaultDirections == null 64 || !(properties.length == defaultPriorities.length && properties.length == defaultDirections.length) 65 || !verifyPriorities(defaultPriorities) 66 || !verifyDirections(defaultDirections)) { 67 this.priorities = new int[0]; 68 this.directions = new int[0]; 69 this.defaultPriorities = new int[0]; 70 this.defaultDirections = new int[0]; 71 } else { 72 this.priorities = new int[defaultPriorities.length]; 73 System.arraycopy(defaultPriorities, 0, this.priorities, 0, 74 priorities.length); 75 this.directions = new int[defaultDirections.length]; 76 System.arraycopy(defaultDirections, 0, this.directions, 0, 77 directions.length); 78 this.defaultPriorities = new int[defaultPriorities.length]; 79 System.arraycopy(defaultPriorities, 0, this.defaultPriorities, 0, 80 defaultPriorities.length); 81 this.defaultDirections = new int[defaultDirections.length]; 82 System.arraycopy(defaultDirections, 0, this.defaultDirections, 0, 83 defaultDirections.length); 84 } 85 } 86 87 92 static TableComparator createTableSorter(IField[] sortingFields) { 93 int[] defaultPriorities = new int[sortingFields.length]; 94 for (int i = 0; i < defaultPriorities.length; i++) { 95 defaultPriorities[i] = i; 96 } 97 98 int[] directions = new int[sortingFields.length]; 99 for (int i = 0; i < directions.length; i++) { 100 directions[i] = sortingFields[i].getDefaultDirection(); 101 102 } 103 104 return new TableComparator(sortingFields, defaultPriorities, directions); 105 } 106 107 protected void resetState() { 108 System 109 .arraycopy(defaultPriorities, 0, priorities, 0, 110 priorities.length); 111 System 112 .arraycopy(defaultDirections, 0, directions, 0, 113 directions.length); 114 } 115 116 public void reverseTopPriority() { 117 directions[priorities[0]] *= -1; 118 } 119 120 public void setTopPriority(IField property) { 121 for (int i = 0; i < fields.length; i++) { 122 if (fields[i].equals(property)) { 123 setTopPriority(i); 124 return; 125 } 126 } 127 } 128 129 public void setTopPriority(int priority) { 130 if (priority < 0 || priority >= priorities.length) { 131 return; 132 } 133 134 int index = -1; 135 for (int i = 0; i < priorities.length; i++) { 136 if (priorities[i] == priority) { 137 index = i; 138 } 139 } 140 141 if (index == -1) { 142 resetState(); 143 return; 144 } 145 146 for (int i = index; i > 0; i--) { 148 priorities[i] = priorities[i - 1]; 149 } 150 priorities[0] = priority; 151 directions[priority] = defaultDirections[priority]; 152 } 153 154 public void setTopPriorityDirection(int direction) { 155 if (direction == ASCENDING || direction == DESCENDING) { 156 directions[priorities[0]] = direction; 157 } 158 } 159 160 public int getTopPriorityDirection() { 161 return directions[priorities[0]]; 162 } 163 164 public int getTopPriority() { 165 return priorities[0]; 166 } 167 168 173 public IField getTopField() { 174 return fields[getTopPriority()]; 175 } 176 177 public int[] getPriorities() { 178 int[] copy = new int[priorities.length]; 179 System.arraycopy(priorities, 0, copy, 0, copy.length); 180 return copy; 181 } 182 183 public int[] getDirections() { 184 int[] copy = new int[directions.length]; 185 System.arraycopy(directions, 0, copy, 0, copy.length); 186 return copy; 187 } 188 189 public int[] getDefaultPriorities() { 190 int[] copy = new int[defaultPriorities.length]; 191 System.arraycopy(defaultPriorities, 0, copy, 0, copy.length); 192 return copy; 193 } 194 195 public int[] getDefaultDirections() { 196 int[] copy = new int[defaultDirections.length]; 197 System.arraycopy(defaultDirections, 0, copy, 0, copy.length); 198 return copy; 199 } 200 201 public int compare(Viewer viewer, Object e1, Object e2) { 202 return compare(e1, e2, 0, true); 203 } 204 205 215 protected int compare(Object obj1, Object obj2, int depth, 216 boolean continueSearching) { 217 if (depth >= priorities.length) { 218 return 0; 219 } 220 221 int column = priorities[depth]; 222 IField property = fields[column]; 223 int result = property.compare(obj1, obj2); 224 if (result == 0 && continueSearching) { 225 return compare(obj1, obj2, depth + 1, continueSearching); 226 } 227 return result * directions[column]; 228 } 229 230 233 public IField[] getFields() { 234 return fields; 235 } 236 237 private boolean verifyPriorities(int[] priorities) { 238 int length = priorities.length; 239 boolean[] included = new boolean[length]; 240 Arrays.fill(included, false); 241 for (int i = 0; i < length; i++) { 242 int priority = priorities[i]; 243 if (priority < 0 || priority >= length) { 244 return false; 245 } 246 if (included[priority]) { 247 return false; 248 } 249 included[priority] = true; 250 } 251 return true; 252 } 253 254 private boolean verifyDirections(int[] directions) { 255 for (int i = 0; i < directions.length; i++) { 256 if (directions[i] != ASCENDING && directions[i] != DESCENDING) { 257 return false; 258 } 259 } 260 return true; 261 } 262 263 268 public int compare(Object o1, Object o2) { 269 return compare(null, o1, o2); 270 } 271 272 public void saveState(IDialogSettings dialogSettings) { 273 if (dialogSettings == null) { 274 return; 275 } 276 277 IDialogSettings settings = dialogSettings 278 .getSection(TAG_DIALOG_SECTION); 279 if (settings == null) { 280 settings = dialogSettings.addNewSection(TAG_DIALOG_SECTION); 281 } 282 283 for (int i = 0; i < priorities.length; i++) { 284 settings.put(TAG_PRIORITY + i, priorities[i]); 285 settings.put(TAG_DIRECTION + i, directions[i]); 286 settings.put(TAG_DEFAULT_PRIORITY + i, defaultPriorities[i]); 287 settings.put(TAG_DEFAULT_DIRECTION + i, defaultDirections[i]); 288 } 289 } 290 291 public void restoreState(IDialogSettings dialogSettings) { 292 if (dialogSettings == null) { 293 resetState(); 294 return; 295 } 296 297 IDialogSettings settings = dialogSettings 298 .getSection(TAG_DIALOG_SECTION); 299 if (settings == null) { 300 resetState(); 301 return; 302 } 303 304 try { 305 for (int i = 0; i < priorities.length; i++) { 306 String priority = settings.get(TAG_PRIORITY + i); 307 if (priority == null) { 308 resetState(); 309 return; 310 } 311 312 int fieldIndex = Integer.parseInt(priority); 313 314 if(fieldIndex < fields.length) { 316 priorities[i] = fieldIndex; 317 } 318 319 String direction = settings.get(TAG_DIRECTION + i); 320 if (direction == null) { 321 resetState(); 322 return; 323 } 324 directions[i] = Integer.parseInt(direction); 325 String defaultPriority = settings.get(TAG_DEFAULT_PRIORITY + i); 326 if (defaultPriority == null) { 327 resetState(); 328 return; 329 } 330 defaultPriorities[i] = Integer.parseInt(defaultPriority); 331 String defaultDirection = settings.get(TAG_DEFAULT_DIRECTION 332 + i); 333 if (defaultDirection == null) { 334 resetState(); 335 return; 336 } 337 defaultDirections[i] = Integer.parseInt(defaultDirection); 338 } 339 } catch (NumberFormatException e) { 340 resetState(); 341 } 342 } 343 344 350 public void sort(TreeViewer viewer, MarkerList lastMarkers) { 351 sort(viewer, lastMarkers.getArray()); 352 353 } 354 355 364 public void sort(final Viewer viewer, Object [] elements, int start, int end) { 365 Arrays.sort(elements, start, end, new Comparator () { 366 public int compare(Object a, Object b) { 367 return TableComparator.this.compare(viewer, a, b); 368 } 369 }); 370 } 371 372 } 373 | Popular Tags |