1 11 12 package org.eclipse.ui.views.tasklist; 13 14 import org.eclipse.core.resources.IMarker; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.jface.dialogs.IDialogSettings; 18 import org.eclipse.jface.viewers.Viewer; 19 import org.eclipse.jface.viewers.ViewerComparator; 20 21 24 class TaskSorter extends ViewerComparator { 25 private int[] priorities; 26 27 private int[] directions; 28 29 final static int ASCENDING = 1; 30 31 final static int DEFAULT_DIRECTION = 0; 32 33 final static int DESCENDING = -1; 34 35 final static int TYPE = 0; 36 37 final static int COMPLETION = 1; 38 39 final static int PRIORITY = 2; 40 41 final static int DESCRIPTION = 3; 42 43 final static int RESOURCE = 4; 44 45 final static int FOLDER = 5; 46 47 final static int LOCATION = 6; 48 49 final static int CREATION_TIME = 7; 50 51 final static int[] DEFAULT_PRIORITIES = { FOLDER, RESOURCE, LOCATION, 52 DESCRIPTION, TYPE, PRIORITY, COMPLETION, CREATION_TIME }; 53 54 final static int[] DEFAULT_DIRECTIONS = { DESCENDING, DESCENDING, DESCENDING, ASCENDING, ASCENDING, ASCENDING, ASCENDING, ASCENDING }; 63 66 public TaskSorter() { 67 resetState(); 68 } 69 70 73 77 public int compare(Viewer viewer, Object e1, Object e2) { 78 IMarker m1 = (IMarker) e1; 79 IMarker m2 = (IMarker) e2; 80 return compareColumnValue(m1, m2, 0); 81 } 82 83 public void setTopPriority(int priority) { 84 if (priority < 0 || priority >= priorities.length) { 85 return; 86 } 87 88 int index = -1; 89 for (int i = 0; i < priorities.length; i++) { 90 if (priorities[i] == priority) { 91 index = i; 92 break; 93 } 94 } 95 96 if (index == -1) { 97 resetState(); 98 return; 99 } 100 101 for (int i = index; i > 0; i--) { 103 priorities[i] = priorities[i - 1]; 104 } 105 priorities[0] = priority; 106 directions[priority] = DEFAULT_DIRECTIONS[priority]; 107 } 108 109 public int getTopPriority() { 110 return priorities[0]; 111 } 112 113 public int[] getPriorities() { 114 return priorities; 115 } 116 117 public void setTopPriorityDirection(int direction) { 118 if (direction == DEFAULT_DIRECTION) { 119 directions[priorities[0]] = DEFAULT_DIRECTIONS[priorities[0]]; 120 } else if (direction == ASCENDING || direction == DESCENDING) { 121 directions[priorities[0]] = direction; 122 } 123 } 124 125 public int getTopPriorityDirection() { 126 return directions[priorities[0]]; 127 } 128 129 public void reverseTopPriority() { 130 directions[priorities[0]] *= -1; 131 } 132 133 public void resetState() { 134 priorities = new int[DEFAULT_PRIORITIES.length]; 135 System.arraycopy(DEFAULT_PRIORITIES, 0, priorities, 0, 136 priorities.length); 137 directions = new int[DEFAULT_DIRECTIONS.length]; 138 System.arraycopy(DEFAULT_DIRECTIONS, 0, directions, 0, 139 directions.length); 140 } 141 142 145 148 private int compareColumnValue(IMarker m1, IMarker m2, int depth) { 149 if (depth >= priorities.length) { 150 return 0; 151 } 152 153 int columnNumber = priorities[depth]; 154 int direction = directions[columnNumber]; 155 switch (columnNumber) { 156 case TYPE: { 157 158 int result = getCategoryOrder(m1) - getCategoryOrder(m2); 159 if (result == 0) { 160 return compareColumnValue(m1, m2, depth + 1); 161 } 162 return result * direction; 163 } 164 case COMPLETION: { 165 166 int result = getCompletedOrder(m1) - getCompletedOrder(m2); 167 if (result == 0) { 168 return compareColumnValue(m1, m2, depth + 1); 169 } 170 return result * direction; 171 } 172 case PRIORITY: { 173 174 int result = getPriorityOrder(m1) - getPriorityOrder(m2); 175 if (result == 0) { 176 return compareColumnValue(m1, m2, depth + 1); 177 } 178 return result * direction; 179 } 180 case DESCRIPTION: { 181 182 int result = getComparator().compare(MarkerUtil.getMessage(m1), MarkerUtil 183 .getMessage(m2)); 184 if (result == 0) { 185 return compareColumnValue(m1, m2, depth + 1); 186 } 187 return result * direction; 188 } 189 case RESOURCE: { 190 191 IResource r1 = m1.getResource(); 192 IResource r2 = m2.getResource(); 193 String n1 = r1.getName(); 194 String n2 = r2.getName(); 195 int result = getComparator().compare(n1, n2); 196 if (result == 0) { 197 return compareColumnValue(m1, m2, depth + 1); 198 } 199 return result * direction; 200 } 201 case FOLDER: { 202 203 String c1 = MarkerUtil.getContainerName(m1); 204 String c2 = MarkerUtil.getContainerName(m2); 205 int result = c1.equals(c2) ? 0 : getComparator().compare(c1, c2); 206 if (result == 0) { 207 return compareColumnValue(m1, m2, depth + 1); 208 } 209 return result * direction; 210 } 211 case LOCATION: { 212 213 int result = compareLineAndLocation(m1, m2); 214 if (result == 0) { 215 return compareColumnValue(m1, m2, depth + 1); 216 } 217 return result * direction; 218 } 219 case CREATION_TIME: { 220 221 int result = compareCreationTime(m1, m2); 222 if (result == 0) { 223 return compareColumnValue(m1, m2, depth + 1); 224 } 225 return result * direction; 226 } 227 default: 228 return 0; 229 } 230 } 231 232 235 private int compareCreationTime(IMarker m1, IMarker m2) { 236 long result; 237 try { 238 result = m1.getCreationTime() - m2.getCreationTime(); 239 } catch (CoreException e) { 240 result = 0; 241 } 242 if (result > 0) { 243 return 1; 244 } else if (result < 0) { 245 return -1; 246 } 247 return 0; 248 } 249 250 257 private int compareLineAndLocation(IMarker m1, IMarker m2) { 258 int line1 = MarkerUtil.getLineNumber(m1); 259 int line2 = MarkerUtil.getLineNumber(m2); 260 if (line1 != -1 && line2 != -1) { 261 if (line1 != line2) { 262 return line1 - line2; 263 } 264 int start1 = MarkerUtil.getCharStart(m1); 265 int start2 = MarkerUtil.getCharStart(m2); 266 if (start1 != -1 && start2 != -1) { 267 if (start1 != start2) { 268 return start1 - start2; 269 } 270 } 271 String loc1 = MarkerUtil.getLocation(m1); 272 String loc2 = MarkerUtil.getLocation(m2); 273 return getComparator().compare(loc1, loc2); 274 } 275 if (line1 == -1 && line2 == -1) { 276 String loc1 = MarkerUtil.getLocation(m1); 277 String loc2 = MarkerUtil.getLocation(m2); 278 return getComparator().compare(loc1, loc2); 279 } 280 String loc1 = MarkerUtil.getLineAndLocation(m1); 281 String loc2 = MarkerUtil.getLineAndLocation(m2); 282 return getComparator().compare(loc1, loc2); 283 } 284 285 289 private int getCategoryOrder(IMarker marker) { 290 if (MarkerUtil.isMarkerType(marker, IMarker.PROBLEM)) { 291 switch (MarkerUtil.getSeverity(marker)) { 292 case IMarker.SEVERITY_ERROR: 293 return 4; 294 case IMarker.SEVERITY_WARNING: 295 return 3; 296 case IMarker.SEVERITY_INFO: 297 return 2; 298 } 299 } else if (MarkerUtil.isMarkerType(marker, IMarker.TASK)) { 300 return 1; 301 } 302 return 1000; 303 } 304 305 309 private int getCompletedOrder(IMarker marker) { 310 if (MarkerUtil.isMarkerType(marker, IMarker.TASK)) { 311 return MarkerUtil.isComplete(marker) ? 2 : 1; 312 } 313 return 0; 314 } 315 316 319 private int getPriorityOrder(IMarker marker) { 320 if (MarkerUtil.isMarkerType(marker, IMarker.TASK)) { 321 return MarkerUtil.getPriority(marker); 322 } 323 return -1; 324 } 325 326 public void saveState(IDialogSettings settings) { 327 if (settings == null) { 328 return; 329 } 330 331 for (int i = 0; i < directions.length; i++) { 332 settings.put("direction" + i, directions[i]); settings.put("priority" + i, priorities[i]); } 335 } 336 337 public void restoreState(IDialogSettings settings) { 338 if (settings == null) { 339 return; 340 } 341 342 try { 343 for (int i = 0; i < priorities.length; i++) { 344 directions[i] = settings.getInt("direction" + i); priorities[i] = settings.getInt("priority" + i); } 347 } catch (NumberFormatException e) { 348 resetState(); 349 } 350 } 351 352 } 353 | Popular Tags |