1 11 12 package org.eclipse.ui.views.bookmarkexplorer; 13 14 import org.eclipse.core.resources.IMarker; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.jface.dialogs.IDialogSettings; 17 import org.eclipse.jface.viewers.Viewer; 18 import org.eclipse.jface.viewers.ViewerComparator; 19 20 class BookmarkSorter extends ViewerComparator { 21 22 private int[] directions; 23 24 private int[] priorities; 25 26 final static int ASCENDING = 1; 27 28 final static int DESCENDING = -1; 29 30 final static int DESCRIPTION = 0; 31 32 final static int RESOURCE = 1; 33 34 final static int FOLDER = 2; 35 36 final static int LOCATION = 3; 37 38 final static int CREATION_TIME = 4; 39 40 final static int[] DEFAULT_PRIORITIES = { FOLDER, RESOURCE, LOCATION, 41 DESCRIPTION, CREATION_TIME }; 42 43 final static int[] DEFAULT_DIRECTIONS = { ASCENDING, ASCENDING, ASCENDING, ASCENDING, ASCENDING, }; 49 public BookmarkSorter() { 50 resetState(); 51 } 52 53 public void reverseTopPriority() { 54 directions[priorities[0]] *= -1; 55 } 56 57 public void setTopPriority(int priority) { 58 if (priority < 0 || priority >= priorities.length) { 59 return; 60 } 61 62 int index = -1; 63 for (int i = 0; i < priorities.length; i++) { 64 if (priorities[i] == priority) { 65 index = i; 66 } 67 } 68 69 if (index == -1) { 70 resetState(); 71 return; 72 } 73 74 for (int i = index; i > 0; i--) { 76 priorities[i] = priorities[i - 1]; 77 } 78 priorities[0] = priority; 79 directions[priority] = DEFAULT_DIRECTIONS[priority]; 80 } 81 82 public void setTopPriorityDirection(int direction) { 83 if (direction == ASCENDING || direction == DESCENDING) { 84 directions[priorities[0]] = direction; 85 } 86 } 87 88 public int getTopPriorityDirection() { 89 return directions[priorities[0]]; 90 } 91 92 public int getTopPriority() { 93 return priorities[0]; 94 } 95 96 public int[] getPriorities() { 97 return priorities; 98 } 99 100 public void resetState() { 101 priorities = new int[DEFAULT_PRIORITIES.length]; 102 System.arraycopy(DEFAULT_PRIORITIES, 0, priorities, 0, 103 priorities.length); 104 directions = new int[DEFAULT_DIRECTIONS.length]; 105 System.arraycopy(DEFAULT_DIRECTIONS, 0, directions, 0, 106 directions.length); 107 } 108 109 private int compare(IMarker marker1, IMarker marker2, int depth) { 110 if (depth >= priorities.length) { 111 return 0; 112 } 113 114 int column = priorities[depth]; 115 switch (column) { 116 case DESCRIPTION: { 117 String desc1 = marker1.getAttribute(IMarker.MESSAGE, ""); String desc2 = marker2.getAttribute(IMarker.MESSAGE, ""); int result = getComparator().compare(desc1, desc2); 120 if (result == 0) { 121 return compare(marker1, marker2, depth + 1); 122 } 123 return result * directions[column]; 124 } 125 case RESOURCE: { 126 String res1 = marker1.getResource().getName(); 127 String res2 = marker2.getResource().getName(); 128 int result = getComparator().compare(res1, res2); 129 if (result == 0) { 130 return compare(marker1, marker2, depth + 1); 131 } 132 return result * directions[column]; 133 } 134 case FOLDER: { 135 String folder1 = BookmarkLabelProvider.getContainerName(marker1); 136 String folder2 = BookmarkLabelProvider.getContainerName(marker2); 137 int result = getComparator().compare(folder1, folder2); 138 if (result == 0) { 139 return compare(marker1, marker2, depth + 1); 140 } 141 return result * directions[column]; 142 } 143 case LOCATION: { 144 int line1 = marker1.getAttribute(IMarker.LINE_NUMBER, -1); 145 int line2 = marker2.getAttribute(IMarker.LINE_NUMBER, -1); 146 int result = line1 - line2; 147 if (result == 0) { 148 return compare(marker1, marker2, depth + 1); 149 } 150 return result * directions[column]; 151 } 152 case CREATION_TIME: { 153 long result; 154 try { 155 result = marker1.getCreationTime() - marker2.getCreationTime(); 156 } catch (CoreException e) { 157 result = 0; 158 } 159 if (result == 0) { 160 return compare(marker1, marker2, depth + 1); 161 } 162 return ((int) result) * directions[column]; 163 } 164 } 165 166 return 0; 167 } 168 169 public int compare(Viewer viewer, Object e1, Object e2) { 170 IMarker marker1 = (IMarker) e1; 171 IMarker marker2 = (IMarker) e2; 172 173 return compare(marker1, marker2, 0); 174 } 175 176 public void saveState(IDialogSettings settings) { 177 if (settings == null) { 178 return; 179 } 180 181 for (int i = 0; i < priorities.length; i++) { 182 settings.put("priority" + i, priorities[i]); settings.put("direction" + i, directions[i]); } 185 } 186 187 public void restoreState(IDialogSettings settings) { 188 if (settings == null) { 189 return; 190 } 191 192 try { 193 for (int i = 0; i < priorities.length; i++) { 194 priorities[i] = settings.getInt("priority" + i); directions[i] = settings.getInt("direction" + i); } 197 } catch (NumberFormatException e) { 198 resetState(); 199 } 200 } 201 } 202 | Popular Tags |