1 6 package de.uka.ipd.coverage.plugin.ui; 7 8 18 19 20 import java.text.Collator ; 21 import java.util.Arrays ; 22 import java.util.Comparator ; 23 24 import org.eclipse.jface.dialogs.IDialogSettings; 25 import org.eclipse.jface.viewers.Viewer; 26 import org.eclipse.jface.viewers.ViewerSorter; 27 28 29 public class TableSorter extends ViewerSorter implements Comparator { 30 31 public static final int MAX_DEPTH = 4; 32 public static final int ASCENDING = 1; 33 public static final int DESCENDING = -1; 34 35 protected IField[] fields; 36 protected Collator collator = Collator.getInstance(); 37 38 protected int[] priorities; 39 protected int[] directions; 40 protected int[] defaultPriorities; 41 protected int[] defaultDirections; 42 43 private final String TAG_DIALOG_SECTION = "sorter"; private final String TAG_PRIORITY = "priority"; private final String TAG_DIRECTION = "direction"; private final String TAG_DEFAULT_PRIORITY = "defaultPriority"; private final String TAG_DEFAULT_DIRECTION = "defaultDirection"; 49 public TableSorter(TableSorter other) { 50 this(other.getFields(), other.getDefaultPriorities(), other.getDefaultDirections()); 51 priorities = other.getPriorities(); 52 directions = other.getDirections(); 53 } 54 55 public TableSorter(IField[] properties, final int[] defaultPriorities, final int[] defaultDirections) { 56 super(); 57 this.fields = properties; 58 if (properties == null || defaultPriorities == null || defaultDirections == null || 59 !(properties.length == defaultPriorities.length && properties.length == defaultDirections.length) || 60 !verifyPriorities(defaultPriorities) || !verifyDirections(defaultDirections)) { 61 this.priorities = new int[0]; 62 this.directions = new int[0]; 63 this.defaultPriorities = new int[0]; 64 this.defaultDirections = new int[0]; 65 } 66 else { 67 this.priorities = new int[defaultPriorities.length]; 68 System.arraycopy(defaultPriorities, 0, this.priorities, 0, priorities.length); 69 this.directions = new int[defaultDirections.length]; 70 System.arraycopy(defaultDirections, 0, this.directions, 0, directions.length); 71 this.defaultPriorities = new int[defaultPriorities.length]; 72 System.arraycopy(defaultPriorities, 0, this.defaultPriorities, 0, defaultPriorities.length); 73 this.defaultDirections = new int[defaultDirections.length]; 74 System.arraycopy(defaultDirections, 0, this.defaultDirections, 0, defaultDirections.length); 75 } 76 } 77 78 protected void resetState() { 79 System.arraycopy(defaultPriorities, 0, priorities, 0, priorities.length); 80 System.arraycopy(defaultDirections, 0, directions, 0, directions.length); 81 } 82 83 public void reverseTopPriority() { 84 directions[priorities[0]] *= -1; 85 } 86 87 public void setTopPriority(IField property) { 88 for (int i = 0; i < fields.length; i++) { 89 if (fields[i].equals(property)) { 90 setTopPriority(i); 91 return; 92 } 93 } 94 } 95 96 public void setTopPriority(int priority) { 97 if (priority < 0 || priority >= priorities.length) 98 return; 99 100 int index = -1; 101 for (int i = 0; i < priorities.length; i++) { 102 if (priorities[i] == priority) 103 index = i; 104 } 105 106 if (index == -1) { 107 resetState(); 108 return; 109 } 110 111 for (int i = index; i > 0; i--) { 113 priorities[i] = priorities[i - 1]; 114 } 115 priorities[0] = priority; 116 directions[priority] = defaultDirections[priority]; 117 } 118 119 public void setTopPriorityDirection(int direction) { 120 if (direction == ASCENDING || direction == DESCENDING) 121 directions[priorities[0]] = direction; 122 } 123 124 public int getTopPriorityDirection() { 125 return directions[priorities[0]]; 126 } 127 128 public int getTopPriority() { 129 return priorities[0]; 130 } 131 132 public int[] getPriorities() { 133 int[] copy = new int[priorities.length]; 134 System.arraycopy(priorities, 0, copy, 0, copy.length); 135 return copy; 136 } 137 138 public int[] getDirections() { 139 int[] copy = new int[directions.length]; 140 System.arraycopy(directions, 0, copy, 0, copy.length); 141 return copy; 142 } 143 144 public int[] getDefaultPriorities() { 145 int[] copy = new int[defaultPriorities.length]; 146 System.arraycopy(defaultPriorities, 0, copy, 0, copy.length); 147 return copy; 148 } 149 150 public int[] getDefaultDirections() { 151 int[] copy = new int[defaultDirections.length]; 152 System.arraycopy(defaultDirections, 0, copy, 0, copy.length); 153 return copy; 154 } 155 156 public int compare(Viewer viewer, Object e1, Object e2) { 157 return compare(e1, e2, 0); 158 } 159 160 protected int compare(Object obj1, Object obj2, int depth) { 161 if (depth >= priorities.length) { 162 return 0; 163 } 164 165 int column = priorities[depth]; 166 IField property = fields[column]; 167 int result = property.compare(obj1, obj2); 168 if (result == 0) 169 return compare(obj1, obj2, depth + 1); 170 return result * directions[column]; 171 } 172 173 176 public IField[] getFields() { 177 return fields; 178 } 179 180 private boolean verifyPriorities(int[] priorities) { 181 int length = priorities.length; 182 boolean[] included = new boolean[length]; 183 Arrays.fill(included, false); 184 for (int i = 0; i < length; i++) { 185 int priority = priorities[i]; 186 if (priority < 0 || priority >= length) 187 return false; 188 if (included[priority]) 189 return false; 190 included[priority] = true; 191 } 192 return true; 193 } 194 195 private boolean verifyDirections(int[] directions) { 196 for (int i = 0; i < directions.length; i++) { 197 if (directions[i] != ASCENDING && directions[i] != DESCENDING) 198 return false; 199 } 200 return true; 201 } 202 203 206 public int compare(Object o1, Object o2) { 207 return compare(null, o1, o2); 208 } 209 210 public void saveState(IDialogSettings dialogSettings) { 211 if (dialogSettings == null) { 212 return; 213 } 214 215 IDialogSettings settings = dialogSettings.getSection(TAG_DIALOG_SECTION); 216 if (settings == null) { 217 settings = dialogSettings.addNewSection(TAG_DIALOG_SECTION); 218 } 219 220 for (int i = 0; i < priorities.length; i++) { 221 settings.put(TAG_PRIORITY + i, priorities[i]); 222 settings.put(TAG_DIRECTION + i, directions[i]); 223 settings.put(TAG_DEFAULT_PRIORITY + i, defaultPriorities[i]); 224 settings.put(TAG_DEFAULT_DIRECTION + i, defaultDirections[i]); 225 } 226 } 227 228 public void restoreState(IDialogSettings dialogSettings) { 229 if (dialogSettings == null) { 230 resetState(); 231 return; 232 } 233 234 IDialogSettings settings = dialogSettings.getSection(TAG_DIALOG_SECTION); 235 if (settings == null) { 236 resetState(); 237 return; 238 } 239 240 try { 241 for (int i = 0; i < priorities.length; i++) { 242 String priority = settings.get(TAG_PRIORITY + i); 243 if (priority == null) { 244 resetState(); 245 return; 246 } 247 priorities[i] = Integer.parseInt(priority); 248 String direction = settings.get(TAG_DIRECTION + i); 249 if (direction == null) { 250 resetState(); 251 return; 252 } 253 directions[i] = Integer.parseInt(direction); 254 String defaultPriority = settings.get(TAG_DEFAULT_PRIORITY + i); 255 if (defaultPriority == null) { 256 resetState(); 257 return; 258 } 259 defaultPriorities[i] = Integer.parseInt(defaultPriority); 260 String defaultDirection = settings.get(TAG_DEFAULT_DIRECTION + i); 261 if (defaultDirection == null) { 262 resetState(); 263 return; 264 } 265 defaultDirections[i] = Integer.parseInt(defaultDirection); 266 } 267 } 268 catch (NumberFormatException e) { 269 resetState(); 270 } 271 } 272 273 } 274 | Popular Tags |