|
Lines 13-24
Link Here
|
| 13 |
package org.eclipse.mylyn.internal.tasks.ui.util; |
13 |
package org.eclipse.mylyn.internal.tasks.ui.util; |
| 14 |
|
14 |
|
| 15 |
import java.util.Comparator; |
15 |
import java.util.Comparator; |
|
|
16 |
import java.util.Date; |
| 16 |
|
17 |
|
| 17 |
import org.eclipse.core.runtime.Assert; |
18 |
import org.eclipse.core.runtime.Assert; |
| 18 |
import org.eclipse.mylyn.internal.tasks.ui.dialogs.Messages; |
19 |
import org.eclipse.mylyn.internal.tasks.ui.dialogs.Messages; |
| 19 |
import org.eclipse.mylyn.internal.tasks.ui.views.TaskKeyComparator; |
20 |
import org.eclipse.mylyn.internal.tasks.ui.views.TaskKeyComparator; |
| 20 |
import org.eclipse.mylyn.tasks.core.IRepositoryElement; |
21 |
import org.eclipse.mylyn.tasks.core.IRepositoryElement; |
| 21 |
import org.eclipse.mylyn.tasks.core.ITask; |
22 |
import org.eclipse.mylyn.tasks.core.ITask; |
|
|
23 |
import org.eclipse.ui.IMemento; |
| 22 |
|
24 |
|
| 23 |
/** |
25 |
/** |
| 24 |
* @author Mik Kersten |
26 |
* @author Mik Kersten |
|
Lines 26-35
Link Here
|
| 26 |
*/ |
28 |
*/ |
| 27 |
public class TaskComparator implements Comparator<ITask> { |
29 |
public class TaskComparator implements Comparator<ITask> { |
| 28 |
|
30 |
|
| 29 |
private final TaskKeyComparator taskKeyComparator = new TaskKeyComparator(); |
|
|
| 30 |
|
| 31 |
public enum SortByIndex { |
31 |
public enum SortByIndex { |
| 32 |
PRIORITY, SUMMARY, DATE_CREATED, TASK_ID; |
32 |
DATE_CREATED, PRIORITY, SUMMARY, TASK_ID; |
|
|
33 |
|
| 34 |
public static SortByIndex valueOfLabel(String label) { |
| 35 |
for (SortByIndex value : values()) { |
| 36 |
if (value.getLabel().equals(label)) { |
| 37 |
return value; |
| 38 |
} |
| 39 |
} |
| 40 |
return null; |
| 41 |
} |
| 33 |
|
42 |
|
| 34 |
public String getLabel() { |
43 |
public String getLabel() { |
| 35 |
switch (this) { |
44 |
switch (this) { |
|
Lines 46-64
Link Here
|
| 46 |
} |
55 |
} |
| 47 |
} |
56 |
} |
| 48 |
|
57 |
|
| 49 |
public static SortByIndex valueOfLabel(String label) { |
|
|
| 50 |
for (SortByIndex value : values()) { |
| 51 |
if (value.getLabel().equals(label)) { |
| 52 |
return value; |
| 53 |
} |
| 54 |
} |
| 55 |
return null; |
| 56 |
} |
| 57 |
|
| 58 |
} |
58 |
} |
| 59 |
|
59 |
|
| 60 |
public static final int DEFAULT_SORT_DIRECTION = 1; |
60 |
public static final int DEFAULT_SORT_DIRECTION = 1; |
| 61 |
|
61 |
|
|
|
62 |
private static final SortByIndex DEFAULT_SORT_INDEX = SortByIndex.PRIORITY; |
| 63 |
|
| 64 |
private static final SortByIndex DEFAULT_SORT_INDEX2 = SortByIndex.DATE_CREATED; |
| 65 |
|
| 66 |
private static final String MEMENTO_KEY_SORT_DIRECTION = "sortDirection"; //$NON-NLS-1$ |
| 67 |
|
| 68 |
private static final String MEMENTO_KEY_SORT_DIRECTION2 = "sortDirection2"; //$NON-NLS-1$ |
| 69 |
|
| 70 |
private static final String MEMENTO_KEY_SORT_INDEX = "sortIndex"; //$NON-NLS-1$ |
| 71 |
|
| 72 |
private static final String MEMENTO_KEY_SORT_INDEX2 = "sortIndex2"; //$NON-NLS-1$ |
| 73 |
|
| 62 |
/** |
74 |
/** |
| 63 |
* Return a array of values to pass to taskKeyComparator.compare() for sorting |
75 |
* Return a array of values to pass to taskKeyComparator.compare() for sorting |
| 64 |
* |
76 |
* |
|
Lines 78-102
Link Here
|
| 78 |
return a; |
90 |
return a; |
| 79 |
} |
91 |
} |
| 80 |
|
92 |
|
| 81 |
private int sortDirection = DEFAULT_SORT_DIRECTION; |
93 |
private SortByIndex sortByIndex = DEFAULT_SORT_INDEX; |
| 82 |
|
94 |
|
| 83 |
private SortByIndex sortByIndex = SortByIndex.PRIORITY; |
95 |
private SortByIndex sortByIndex2 = DEFAULT_SORT_INDEX2; |
|
|
96 |
|
| 97 |
private int sortDirection = DEFAULT_SORT_DIRECTION; |
| 84 |
|
98 |
|
| 85 |
private int sortDirection2 = DEFAULT_SORT_DIRECTION; |
99 |
private int sortDirection2 = DEFAULT_SORT_DIRECTION; |
| 86 |
|
100 |
|
| 87 |
private SortByIndex sortByIndex2 = SortByIndex.DATE_CREATED; |
101 |
private final TaskKeyComparator taskKeyComparator = new TaskKeyComparator(); |
| 88 |
|
102 |
|
| 89 |
public TaskComparator() { |
103 |
public TaskComparator() { |
| 90 |
} |
104 |
} |
| 91 |
|
105 |
|
| 92 |
public int compare(ITask element1, ITask element2) { |
106 |
public int compare(ITask element1, ITask element2) { |
| 93 |
if (SortByIndex.PRIORITY.equals(sortByIndex)) { |
107 |
if (DEFAULT_SORT_INDEX.equals(sortByIndex)) { |
| 94 |
int result = sortByPriority(element1, element2, sortDirection); |
108 |
int result = sortByPriority(element1, element2, sortDirection); |
| 95 |
if (result != 0) { |
109 |
if (result != 0) { |
| 96 |
return result; |
110 |
return result; |
| 97 |
} |
111 |
} |
| 98 |
|
112 |
|
| 99 |
if (SortByIndex.DATE_CREATED.equals(sortByIndex2)) { |
113 |
if (DEFAULT_SORT_INDEX2.equals(sortByIndex2)) { |
| 100 |
return sortByDate(element1, element2, sortDirection2); |
114 |
return sortByDate(element1, element2, sortDirection2); |
| 101 |
} else { |
115 |
} else { |
| 102 |
if (SortByIndex.SUMMARY.equals(sortByIndex2)) { |
116 |
if (SortByIndex.SUMMARY.equals(sortByIndex2)) { |
|
Lines 107-118
Link Here
|
| 107 |
return result; |
121 |
return result; |
| 108 |
} |
122 |
} |
| 109 |
} |
123 |
} |
| 110 |
} else if (SortByIndex.DATE_CREATED.equals(sortByIndex)) { |
124 |
} else if (DEFAULT_SORT_INDEX2.equals(sortByIndex)) { |
| 111 |
int result = sortByDate(element1, element2, sortDirection); |
125 |
int result = sortByDate(element1, element2, sortDirection); |
| 112 |
if (result != 0) { |
126 |
if (result != 0) { |
| 113 |
return result; |
127 |
return result; |
| 114 |
} |
128 |
} |
| 115 |
if (SortByIndex.PRIORITY.equals(sortByIndex2)) { |
129 |
if (DEFAULT_SORT_INDEX.equals(sortByIndex2)) { |
| 116 |
return sortByPriority(element1, element2, sortDirection2); |
130 |
return sortByPriority(element1, element2, sortDirection2); |
| 117 |
} else { |
131 |
} else { |
| 118 |
if (SortByIndex.SUMMARY.equals(sortByIndex2)) { |
132 |
if (SortByIndex.SUMMARY.equals(sortByIndex2)) { |
|
Lines 128-137
Link Here
|
| 128 |
if (result != 0) { |
142 |
if (result != 0) { |
| 129 |
return result; |
143 |
return result; |
| 130 |
} |
144 |
} |
| 131 |
if (SortByIndex.DATE_CREATED.equals(sortByIndex2)) { |
145 |
if (DEFAULT_SORT_INDEX2.equals(sortByIndex2)) { |
| 132 |
return sortByDate(element1, element2, sortDirection2); |
146 |
return sortByDate(element1, element2, sortDirection2); |
| 133 |
} else { |
147 |
} else { |
| 134 |
if (SortByIndex.PRIORITY.equals(sortByIndex2)) { |
148 |
if (DEFAULT_SORT_INDEX.equals(sortByIndex2)) { |
| 135 |
return sortByPriority(element1, element2, sortDirection2); |
149 |
return sortByPriority(element1, element2, sortDirection2); |
| 136 |
} else if (SortByIndex.TASK_ID.equals(sortByIndex2)) { |
150 |
} else if (SortByIndex.TASK_ID.equals(sortByIndex2)) { |
| 137 |
return sortByID(element1, element2, sortDirection2); |
151 |
return sortByID(element1, element2, sortDirection2); |
|
Lines 144-153
Link Here
|
| 144 |
if (result != 0) { |
158 |
if (result != 0) { |
| 145 |
return result; |
159 |
return result; |
| 146 |
} |
160 |
} |
| 147 |
if (SortByIndex.DATE_CREATED.equals(sortByIndex2)) { |
161 |
if (DEFAULT_SORT_INDEX2.equals(sortByIndex2)) { |
| 148 |
return sortByDate(element1, element2, sortDirection2); |
162 |
return sortByDate(element1, element2, sortDirection2); |
| 149 |
} else { |
163 |
} else { |
| 150 |
if (SortByIndex.PRIORITY.equals(sortByIndex2)) { |
164 |
if (DEFAULT_SORT_INDEX.equals(sortByIndex2)) { |
| 151 |
return sortByPriority(element1, element2, sortDirection2); |
165 |
return sortByPriority(element1, element2, sortDirection2); |
| 152 |
} else if (SortByIndex.SUMMARY.equals(sortByIndex2)) { |
166 |
} else if (SortByIndex.SUMMARY.equals(sortByIndex2)) { |
| 153 |
return sortBySummary(element1, element2, sortDirection2); |
167 |
return sortBySummary(element1, element2, sortDirection2); |
|
Lines 158-200
Link Here
|
| 158 |
} |
172 |
} |
| 159 |
} |
173 |
} |
| 160 |
|
174 |
|
| 161 |
private int sortBySummary(ITask element1, ITask element2, int sortDirection) { |
175 |
public SortByIndex getSortByIndex() { |
| 162 |
String key1 = element1.getSummary(); |
176 |
return sortByIndex; |
| 163 |
String key2 = element2.getSummary(); |
|
|
| 164 |
if (key1 == null) { |
| 165 |
return (key2 != null) ? sortDirection : 0; |
| 166 |
} else if (key2 == null) { |
| 167 |
return -sortDirection; |
| 168 |
} |
| 169 |
return sortDirection * key1.compareToIgnoreCase(key2); |
| 170 |
} |
177 |
} |
| 171 |
|
178 |
|
| 172 |
private int sortByID(ITask element1, ITask element2, int sortDirection) { |
179 |
public SortByIndex getSortByIndex2() { |
| 173 |
String key1 = element1.getTaskKey(); |
180 |
return sortByIndex2; |
| 174 |
String key2 = element2.getTaskKey(); |
181 |
} |
| 175 |
if (key1 == null) { |
182 |
|
| 176 |
return (key2 != null) ? sortDirection : 0; |
183 |
public int getSortDirection() { |
| 177 |
} else if (key2 == null) { |
184 |
return sortDirection; |
| 178 |
return -sortDirection; |
185 |
} |
|
|
186 |
|
| 187 |
private int getSortDirection(IMemento memento, String key, int defaultValue) { |
| 188 |
Integer value = memento.getInteger(key); |
| 189 |
if (value != null) { |
| 190 |
return value >= 0 ? 1 : -1; |
| 179 |
} |
191 |
} |
| 180 |
return sortDirection * taskKeyComparator.compare2(key1, key2); |
192 |
return defaultValue; |
| 181 |
} |
193 |
} |
| 182 |
|
194 |
|
| 183 |
private int sortByPriority(ITask element1, ITask element2, int sortDirection) { |
195 |
public int getSortDirection2() { |
| 184 |
return sortDirection * (element1.getPriority().compareToIgnoreCase(element2.getPriority())); |
196 |
return sortDirection2; |
| 185 |
} |
197 |
} |
| 186 |
|
198 |
|
| 187 |
private int sortByDate(ITask element1, ITask element2, int sortDirection) { |
199 |
private SortByIndex getSortIndex(IMemento memento, String key, SortByIndex defaultValue) { |
| 188 |
if (element1.getCreationDate() == null) { |
200 |
String value = memento.getString(key); |
| 189 |
return (element2.getCreationDate() != null) ? sortDirection : 0; |
201 |
if (value != null) { |
| 190 |
} else if (element2.getCreationDate() == null) { |
202 |
try { |
| 191 |
return -sortDirection; |
203 |
return SortByIndex.valueOf(value); |
|
|
204 |
} catch (IllegalArgumentException e) { |
| 205 |
// ignore |
| 206 |
} |
| 192 |
} |
207 |
} |
| 193 |
return sortDirection * (element1.getCreationDate().compareTo(element2.getCreationDate())); |
208 |
return defaultValue; |
| 194 |
} |
209 |
} |
| 195 |
|
210 |
|
| 196 |
public SortByIndex getSortByIndex() { |
211 |
public void restoreState(IMemento memento) { |
| 197 |
return sortByIndex; |
212 |
setSortByIndex(getSortIndex(memento, MEMENTO_KEY_SORT_INDEX, DEFAULT_SORT_INDEX)); |
|
|
213 |
setSortDirection(getSortDirection(memento, MEMENTO_KEY_SORT_DIRECTION, DEFAULT_SORT_DIRECTION)); |
| 214 |
setSortByIndex2(getSortIndex(memento, MEMENTO_KEY_SORT_INDEX2, DEFAULT_SORT_INDEX2)); |
| 215 |
setSortDirection(getSortDirection(memento, MEMENTO_KEY_SORT_DIRECTION2, DEFAULT_SORT_DIRECTION)); |
| 216 |
} |
| 217 |
|
| 218 |
public void saveState(IMemento memento) { |
| 219 |
memento.putString(MEMENTO_KEY_SORT_INDEX, getSortByIndex().name()); |
| 220 |
memento.putInteger(MEMENTO_KEY_SORT_DIRECTION, getSortDirection()); |
| 221 |
memento.putString(MEMENTO_KEY_SORT_INDEX2, getSortByIndex2().name()); |
| 222 |
memento.putInteger(MEMENTO_KEY_SORT_DIRECTION2, getSortDirection2()); |
| 198 |
} |
223 |
} |
| 199 |
|
224 |
|
| 200 |
public void setSortByIndex(SortByIndex sortByIndex) { |
225 |
public void setSortByIndex(SortByIndex sortByIndex) { |
|
Lines 202-230
Link Here
|
| 202 |
this.sortByIndex = sortByIndex; |
227 |
this.sortByIndex = sortByIndex; |
| 203 |
} |
228 |
} |
| 204 |
|
229 |
|
| 205 |
public int getSortDirection() { |
230 |
public void setSortByIndex2(SortByIndex sortByIndex) { |
| 206 |
return sortDirection; |
231 |
Assert.isNotNull(sortByIndex); |
|
|
232 |
this.sortByIndex2 = sortByIndex; |
| 207 |
} |
233 |
} |
| 208 |
|
234 |
|
| 209 |
public void setSortDirection(int sortDirection) { |
235 |
public void setSortDirection(int sortDirection) { |
|
|
236 |
Assert.isTrue(sortDirection == -1 || sortDirection == 1); |
| 210 |
this.sortDirection = sortDirection; |
237 |
this.sortDirection = sortDirection; |
| 211 |
} |
238 |
} |
| 212 |
|
239 |
|
| 213 |
public SortByIndex getSortByIndex2() { |
240 |
public void setSortDirection2(int sortDirection) { |
| 214 |
return sortByIndex2; |
241 |
Assert.isTrue(sortDirection == -1 || sortDirection == 1); |
|
|
242 |
this.sortDirection2 = sortDirection; |
| 215 |
} |
243 |
} |
| 216 |
|
244 |
|
| 217 |
public void setSortByIndex2(SortByIndex sortByIndex) { |
245 |
private int sortByDate(ITask element1, ITask element2, int sortDirection) { |
| 218 |
Assert.isNotNull(sortByIndex); |
246 |
Date date1 = element1.getCreationDate(); |
| 219 |
this.sortByIndex2 = sortByIndex; |
247 |
Date date2 = element2.getCreationDate(); |
|
|
248 |
if (date1 == null) { |
| 249 |
return (date2 != null) ? sortDirection : 0; |
| 250 |
} else if (date2 == null) { |
| 251 |
return -sortDirection; |
| 252 |
} |
| 253 |
return sortDirection * date1.compareTo(date2); |
| 220 |
} |
254 |
} |
| 221 |
|
255 |
|
| 222 |
public int getSortDirection2() { |
256 |
private int sortByID(ITask element1, ITask element2, int sortDirection) { |
| 223 |
return sortDirection2; |
257 |
String key1 = element1.getTaskKey(); |
|
|
258 |
String key2 = element2.getTaskKey(); |
| 259 |
if (key1 == null) { |
| 260 |
return (key2 != null) ? sortDirection : 0; |
| 261 |
} else if (key2 == null) { |
| 262 |
return -sortDirection; |
| 263 |
} |
| 264 |
return sortDirection * taskKeyComparator.compare2(key1, key2); |
| 224 |
} |
265 |
} |
| 225 |
|
266 |
|
| 226 |
public void setSortDirection2(int sortDirection) { |
267 |
private int sortByPriority(ITask element1, ITask element2, int sortDirection) { |
| 227 |
this.sortDirection2 = sortDirection; |
268 |
return sortDirection * element1.getPriority().compareToIgnoreCase(element2.getPriority()); |
|
|
269 |
} |
| 270 |
|
| 271 |
private int sortBySummary(ITask element1, ITask element2, int sortDirection) { |
| 272 |
String key1 = element1.getSummary(); |
| 273 |
String key2 = element2.getSummary(); |
| 274 |
if (key1 == null) { |
| 275 |
return (key2 != null) ? sortDirection : 0; |
| 276 |
} else if (key2 == null) { |
| 277 |
return -sortDirection; |
| 278 |
} |
| 279 |
return sortDirection * key1.compareToIgnoreCase(key2); |
| 228 |
} |
280 |
} |
| 229 |
|
281 |
|
| 230 |
} |
282 |
} |