KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > tasklist > TaskSorter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
22  * This is the task list's sorter.
23  */

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, //type
55
DESCENDING, //completed
56
DESCENDING, //priority
57
ASCENDING, //description
58
ASCENDING, //resource
59
ASCENDING, //folder
60
ASCENDING, //location
61
ASCENDING }; //creation time
62

63     /**
64      * Creates a new task sorter.
65      */

66     public TaskSorter() {
67         resetState();
68     }
69
70     /* (non-Javadoc)
71      * Method declared on ViewerSorter.
72      */

73     /**
74      * Compares two markers, sorting first by the main column of this sorter,
75      * then by subsequent columns, depending on the column sort order.
76      */

77     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc 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         //shift the array
102
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     /* (non-Javadoc)
143      * Method declared on ViewerSorter.
144      */

145     /**
146      * Compares two markers, based only on the value of the specified column.
147      */

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             /* category */
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             /* completed */
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             /* priority */
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             /* description */
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             /* resource name */
191             IResource r1 = m1.getResource();
192             IResource r2 = m2.getResource();
193             String JavaDoc n1 = r1.getName();
194             String JavaDoc 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             /* container name */
203             String JavaDoc c1 = MarkerUtil.getContainerName(m1);
204             String JavaDoc 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             /* line and location */
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             /* creation time */
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     /**
233      * Compares the creation time of two markers.
234      */

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     /**
251      * Compares the line number and location of the two markers.
252      * If line number is specified for both, this sorts first by line number (numerically),
253      * then by start offset (numerically), then by location (textually).
254      * If line number is not specified for either, this sorts by location.
255      * Otherwise, if only one has a line number, this sorts by the combined text for line number and location.
256      */

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 JavaDoc loc1 = MarkerUtil.getLocation(m1);
272             String JavaDoc loc2 = MarkerUtil.getLocation(m2);
273             return getComparator().compare(loc1, loc2);
274         }
275         if (line1 == -1 && line2 == -1) {
276             String JavaDoc loc1 = MarkerUtil.getLocation(m1);
277             String JavaDoc loc2 = MarkerUtil.getLocation(m2);
278             return getComparator().compare(loc1, loc2);
279         }
280         String JavaDoc loc1 = MarkerUtil.getLineAndLocation(m1);
281         String JavaDoc loc2 = MarkerUtil.getLineAndLocation(m2);
282         return getComparator().compare(loc1, loc2);
283     }
284
285     /**
286      * Returns the sort order for the given marker based on its category.
287      * Lower numbers appear first.
288      */

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     /**
306      * Returns the sort order for the given marker based on its completion status.
307      * Lower numbers appear first.
308      */

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     /**
317      * Returns the sort order for the given marker based on its priority.
318      */

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]);//$NON-NLS-1$
333
settings.put("priority" + i, priorities[i]);//$NON-NLS-1$
334
}
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);//$NON-NLS-1$
345
priorities[i] = settings.getInt("priority" + i);//$NON-NLS-1$
346
}
347         } catch (NumberFormatException JavaDoc e) {
348             resetState();
349         }
350     }
351
352 }
353
Popular Tags