KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > TableSorter


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.markers.internal;
13
14 import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16
17 import org.eclipse.jface.dialogs.IDialogSettings;
18 import org.eclipse.jface.viewers.TreeViewer;
19 import org.eclipse.jface.viewers.Viewer;
20 import org.eclipse.jface.viewers.ViewerSorter;
21
22 public class TableSorter extends ViewerSorter implements Comparator JavaDoc {
23
24     public static final int MAX_DEPTH = 4;
25
26     public static final int ASCENDING = 1;
27
28     public static final int DESCENDING = -1;
29
30     protected IField[] fields;
31
32     protected int[] priorities;
33
34     protected int[] directions;
35
36     protected int[] defaultPriorities;
37
38     protected int[] defaultDirections;
39
40     public static final String JavaDoc TAG_DIALOG_SECTION = "sorter"; //$NON-NLS-1$
41

42     private static final String JavaDoc TAG_PRIORITY = "priority"; //$NON-NLS-1$
43

44     private static final String JavaDoc TAG_DIRECTION = "direction"; //$NON-NLS-1$
45

46     private static final String JavaDoc TAG_DEFAULT_PRIORITY = "defaultPriority"; //$NON-NLS-1$
47

48     private static final String JavaDoc TAG_DEFAULT_DIRECTION = "defaultDirection"; //$NON-NLS-1$
49

50     public TableSorter(TableSorter other) {
51         this(other.getFields(), other.getDefaultPriorities(), other
52                 .getDefaultDirections());
53         priorities = other.getPriorities();
54         directions = other.getDirections();
55     }
56
57     public TableSorter(IField[] properties, final int[] defaultPriorities,
58             final int[] defaultDirections) {
59         super();
60         this.fields = properties;
61         if (properties == null
62                 || defaultPriorities == null
63                 || defaultDirections == null
64                 || !(properties.length == defaultPriorities.length && properties.length == defaultDirections.length)
65                 || !verifyPriorities(defaultPriorities)
66                 || !verifyDirections(defaultDirections)) {
67             this.priorities = new int[0];
68             this.directions = new int[0];
69             this.defaultPriorities = new int[0];
70             this.defaultDirections = new int[0];
71         } else {
72             this.priorities = new int[defaultPriorities.length];
73             System.arraycopy(defaultPriorities, 0, this.priorities, 0,
74                     priorities.length);
75             this.directions = new int[defaultDirections.length];
76             System.arraycopy(defaultDirections, 0, this.directions, 0,
77                     directions.length);
78             this.defaultPriorities = new int[defaultPriorities.length];
79             System.arraycopy(defaultPriorities, 0, this.defaultPriorities, 0,
80                     defaultPriorities.length);
81             this.defaultDirections = new int[defaultDirections.length];
82             System.arraycopy(defaultDirections, 0, this.defaultDirections, 0,
83                     defaultDirections.length);
84         }
85     }
86
87     /**
88      * Return a TableSorter based on the supplied fields.
89      *
90      * @param sortingFields
91      */

92     static TableSorter createTableSorter(IField[] sortingFields) {
93         int[] defaultPriorities = new int[sortingFields.length];
94         for (int i = 0; i < defaultPriorities.length; i++) {
95             defaultPriorities[i] = i;
96         }
97
98         int[] directions = new int[sortingFields.length];
99         for (int i = 0; i < directions.length; i++) {
100             directions[i] = sortingFields[i].getDefaultDirection();
101
102         }
103
104         return new TableSorter(sortingFields, defaultPriorities, directions);
105     }
106
107     protected void resetState() {
108         System
109                 .arraycopy(defaultPriorities, 0, priorities, 0,
110                         priorities.length);
111         System
112                 .arraycopy(defaultDirections, 0, directions, 0,
113                         directions.length);
114     }
115
116     public void reverseTopPriority() {
117         directions[priorities[0]] *= -1;
118     }
119
120     public void setTopPriority(IField property) {
121         for (int i = 0; i < fields.length; i++) {
122             if (fields[i].equals(property)) {
123                 setTopPriority(i);
124                 return;
125             }
126         }
127     }
128
129     public void setTopPriority(int priority) {
130         if (priority < 0 || priority >= priorities.length) {
131             return;
132         }
133
134         int index = -1;
135         for (int i = 0; i < priorities.length; i++) {
136             if (priorities[i] == priority) {
137                 index = i;
138             }
139         }
140
141         if (index == -1) {
142             resetState();
143             return;
144         }
145
146         // shift the array
147
for (int i = index; i > 0; i--) {
148             priorities[i] = priorities[i - 1];
149         }
150         priorities[0] = priority;
151         directions[priority] = defaultDirections[priority];
152     }
153
154     public void setTopPriorityDirection(int direction) {
155         if (direction == ASCENDING || direction == DESCENDING) {
156             directions[priorities[0]] = direction;
157         }
158     }
159
160     public int getTopPriorityDirection() {
161         return directions[priorities[0]];
162     }
163
164     public int getTopPriority() {
165         return priorities[0];
166     }
167
168     /**
169      * Return the field at the top priority.
170      *
171      * @return IField
172      */

173     public IField getTopField() {
174         return fields[getTopPriority()];
175     }
176
177     public int[] getPriorities() {
178         int[] copy = new int[priorities.length];
179         System.arraycopy(priorities, 0, copy, 0, copy.length);
180         return copy;
181     }
182
183     public int[] getDirections() {
184         int[] copy = new int[directions.length];
185         System.arraycopy(directions, 0, copy, 0, copy.length);
186         return copy;
187     }
188
189     public int[] getDefaultPriorities() {
190         int[] copy = new int[defaultPriorities.length];
191         System.arraycopy(defaultPriorities, 0, copy, 0, copy.length);
192         return copy;
193     }
194
195     public int[] getDefaultDirections() {
196         int[] copy = new int[defaultDirections.length];
197         System.arraycopy(defaultDirections, 0, copy, 0, copy.length);
198         return copy;
199     }
200
201     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
202         return compare(e1, e2, 0, true);
203     }
204
205     /**
206      * Compare obj1 and obj2 at depth. If continueSearching continue searching
207      * below depth to continue the comparison.
208      *
209      * @param obj1
210      * @param obj2
211      * @param depth
212      * @param continueSearching
213      * @return int
214      */

215     protected int compare(Object JavaDoc obj1, Object JavaDoc obj2, int depth,
216             boolean continueSearching) {
217         if (depth >= priorities.length) {
218             return 0;
219         }
220
221         int column = priorities[depth];
222         IField property = fields[column];
223         int result = property.compare(obj1, obj2);
224         if (result == 0 && continueSearching) {
225             return compare(obj1, obj2, depth + 1, continueSearching);
226         }
227         return result * directions[column];
228     }
229
230     /**
231      * @return IField[] an array of fields
232      */

233     public IField[] getFields() {
234         return fields;
235     }
236
237     private boolean verifyPriorities(int[] priorities) {
238         int length = priorities.length;
239         boolean[] included = new boolean[length];
240         Arrays.fill(included, false);
241         for (int i = 0; i < length; i++) {
242             int priority = priorities[i];
243             if (priority < 0 || priority >= length) {
244                 return false;
245             }
246             if (included[priority]) {
247                 return false;
248             }
249             included[priority] = true;
250         }
251         return true;
252     }
253
254     private boolean verifyDirections(int[] directions) {
255         for (int i = 0; i < directions.length; i++) {
256             if (directions[i] != ASCENDING && directions[i] != DESCENDING) {
257                 return false;
258             }
259         }
260         return true;
261     }
262
263     /*
264      * (non-Javadoc)
265      *
266      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
267      */

268     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
269         return compare(null, o1, o2);
270     }
271
272     public void saveState(IDialogSettings dialogSettings) {
273         if (dialogSettings == null) {
274             return;
275         }
276
277         IDialogSettings settings = dialogSettings
278                 .getSection(TAG_DIALOG_SECTION);
279         if (settings == null) {
280             settings = dialogSettings.addNewSection(TAG_DIALOG_SECTION);
281         }
282
283         for (int i = 0; i < priorities.length; i++) {
284             settings.put(TAG_PRIORITY + i, priorities[i]);
285             settings.put(TAG_DIRECTION + i, directions[i]);
286             settings.put(TAG_DEFAULT_PRIORITY + i, defaultPriorities[i]);
287             settings.put(TAG_DEFAULT_DIRECTION + i, defaultDirections[i]);
288         }
289     }
290
291     public void restoreState(IDialogSettings dialogSettings) {
292         if (dialogSettings == null) {
293             resetState();
294             return;
295         }
296
297         IDialogSettings settings = dialogSettings
298                 .getSection(TAG_DIALOG_SECTION);
299         if (settings == null) {
300             resetState();
301             return;
302         }
303
304         try {
305             for (int i = 0; i < priorities.length; i++) {
306                 String JavaDoc priority = settings.get(TAG_PRIORITY + i);
307                 if (priority == null) {
308                     resetState();
309                     return;
310                 }
311                 
312                 int fieldIndex = Integer.parseInt(priority);
313                 
314                 //Make sure it is not old data from a different sized array
315
if(fieldIndex < fields.length) {
316                     priorities[i] = fieldIndex;
317                 }
318                 
319                 String JavaDoc direction = settings.get(TAG_DIRECTION + i);
320                 if (direction == null) {
321                     resetState();
322                     return;
323                 }
324                 directions[i] = Integer.parseInt(direction);
325                 String JavaDoc defaultPriority = settings.get(TAG_DEFAULT_PRIORITY + i);
326                 if (defaultPriority == null) {
327                     resetState();
328                     return;
329                 }
330                 defaultPriorities[i] = Integer.parseInt(defaultPriority);
331                 String JavaDoc defaultDirection = settings.get(TAG_DEFAULT_DIRECTION
332                         + i);
333                 if (defaultDirection == null) {
334                     resetState();
335                     return;
336                 }
337                 defaultDirections[i] = Integer.parseInt(defaultDirection);
338             }
339         } catch (NumberFormatException JavaDoc e) {
340             resetState();
341         }
342     }
343
344     /**
345      * Sort the array of markers in lastMarkers in place.
346      *
347      * @param viewer
348      * @param lastMarkers
349      */

350     public void sort(TreeViewer viewer, MarkerList lastMarkers) {
351         sort(viewer, lastMarkers.getArray());
352
353     }
354
355     /**
356      * Sorts the given elements in-place, modifying the given array from index
357      * start to index end. <
358      *
359      * @param viewer
360      * @param elements
361      * @param start
362      * @param end
363      */

364     public void sort(final Viewer viewer, Object JavaDoc[] elements, int start, int end) {
365         Arrays.sort(elements, start, end, new Comparator JavaDoc() {
366             public int compare(Object JavaDoc a, Object JavaDoc b) {
367                 return TableSorter.this.compare(viewer, a, b);
368             }
369         });
370     }
371
372 }
373
Popular Tags