KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > uka > ipd > coverage > plugin > ui > TableSorter


1 /*
2  * Created on Apr 19, 2005
3  *
4  * written by Matthias Kempka
5  */

6 package de.uka.ipd.coverage.plugin.ui;
7
8 /*******************************************************************************
9  * Copyright (c) 2000, 2004 IBM Corporation and others.
10  * All rights reserved. This program and the accompanying materials
11  * are made available under the terms of the Common Public License v1.0
12  * which accompanies this distribution, and is available at
13  * http://www.eclipse.org/legal/cpl-v10.html
14  *
15  * Contributors:
16  * IBM Corporation - initial API and implementation
17  *******************************************************************************/

18
19
20 import java.text.Collator JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Comparator JavaDoc;
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 JavaDoc {
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 JavaDoc 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 JavaDoc TAG_DIALOG_SECTION = "sorter"; //$NON-NLS-1$
44
private final String JavaDoc TAG_PRIORITY = "priority"; //$NON-NLS-1$
45
private final String JavaDoc TAG_DIRECTION = "direction"; //$NON-NLS-1$
46
private final String JavaDoc TAG_DEFAULT_PRIORITY = "defaultPriority"; //$NON-NLS-1$
47
private final String JavaDoc TAG_DEFAULT_DIRECTION = "defaultDirection"; //$NON-NLS-1$
48

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         //shift the array
112
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 JavaDoc e1, Object JavaDoc e2) {
157         return compare(e1, e2, 0);
158     }
159
160     protected int compare(Object JavaDoc obj1, Object JavaDoc 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     /**
174      * @return IField[] an array of fields
175      */

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     /* (non-Javadoc)
204      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
205      */

206     public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc priority = settings.get(TAG_PRIORITY + i);
243                 if (priority == null) {
244                     resetState();
245                     return;
246                 }
247                 priorities[i] = Integer.parseInt(priority);
248                 String JavaDoc direction = settings.get(TAG_DIRECTION + i);
249                 if (direction == null) {
250                     resetState();
251                     return;
252                 }
253                 directions[i] = Integer.parseInt(direction);
254                 String JavaDoc defaultPriority = settings.get(TAG_DEFAULT_PRIORITY + i);
255                 if (defaultPriority == null) {
256                     resetState();
257                     return;
258                 }
259                 defaultPriorities[i] = Integer.parseInt(defaultPriority);
260                 String JavaDoc 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 JavaDoc e) {
269             resetState();
270         }
271     }
272
273 }
274
Popular Tags