KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > ResourceSorter


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 package org.eclipse.ui.internal.ide.dialogs;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.jface.viewers.Viewer;
16 import org.eclipse.jface.viewers.ViewerSorter;
17
18 /**
19  * Sorter for viewers that display items of type <code>IResource</code>.
20  * The sorter supports two sort criteria:
21  * <p>
22  * <code>NAME</code>: Folders are given order precedence, followed by files.
23  * Within these two groups resources are ordered by name. All name comparisons
24  * are case-insensitive.
25  * </p>
26  * <p>
27  * <code>TYPE</code>: Folders are given order precedence, followed by files.
28  * Within these two groups resources are ordered by extension. All extension
29  * comparisons are case-insensitive.
30  * </p>
31  * <p>
32  * This class may be instantiated; it is not intended to be subclassed.
33  * </p>
34  */

35 public class ResourceSorter extends ViewerSorter {
36
37     /**
38      * Constructor argument value that indicates to sort items by name.
39      */

40     public final static int NAME = 1;
41
42     /**
43      * Constructor argument value that indicates to sort items by extension.
44      */

45     public final static int TYPE = 2;
46
47     private int criteria;
48
49     /**
50      * Creates a resource sorter that will use the given sort criteria.
51      *
52      * @param criteria the sort criterion to use: one of <code>NAME</code> or
53      * <code>TYPE</code>
54      */

55     public ResourceSorter(int criteria) {
56         super();
57         this.criteria = criteria;
58     }
59
60     /**
61      * Returns an integer value representing the relative sort priority of the
62      * given element based on its class.
63      * <p>
64      * <ul>
65      * <li>resources (<code>IResource</code>) - 2</li>
66      * <li>project references (<code>ProjectReference</code>) - 1</li>
67      * <li>everything else - 0</li>
68      * </ul>
69      * </p>
70      *
71      * @param element the element
72      * @return the sort priority (larger numbers means more important)
73      */

74     protected int classComparison(Object JavaDoc element) {
75         if (element instanceof IResource) {
76             return 2;
77         }
78         return 0;
79     }
80
81     /* (non-Javadoc)
82      * Method declared on ViewerSorter.
83      */

84     public int compare(Viewer viewer, Object JavaDoc o1, Object JavaDoc o2) {
85         //have to deal with non-resources in navigator
86
//if one or both objects are not resources, returned a comparison
87
//based on class.
88
if (!(o1 instanceof IResource && o2 instanceof IResource)) {
89             return compareClass(o1, o2);
90         }
91         IResource r1 = (IResource) o1;
92         IResource r2 = (IResource) o2;
93
94         if (r1 instanceof IContainer && r2 instanceof IContainer) {
95             return compareNames(r1, r2);
96         } else if (r1 instanceof IContainer) {
97             return -1;
98         } else if (r2 instanceof IContainer) {
99             return 1;
100         } else if (criteria == NAME) {
101             return compareNames(r1, r2);
102         } else if (criteria == TYPE) {
103             return compareTypes(r1, r2);
104         } else {
105             return 0;
106         }
107     }
108
109     /**
110      * Returns a number reflecting the collation order of the given elements
111      * based on their class.
112      *
113      * @param element1 the first element to be ordered
114      * @param element2 the second element to be ordered
115      * @return a negative number if the first element is less than the
116      * second element; the value <code>0</code> if the first element is
117      * equal to the second element; and a positive number if the first
118      * element is greater than the second element
119      */

120     protected int compareClass(Object JavaDoc element1, Object JavaDoc element2) {
121         return classComparison(element1) - classComparison(element2);
122     }
123
124     /**
125      * Returns a number reflecting the collation order of the given resources
126      * based on their resource names.
127      *
128      * @param resource1 the first resource element to be ordered
129      * @param resource2 the second resource element to be ordered
130      * @return a negative number if the first element is less than the
131      * second element; the value <code>0</code> if the first element is
132      * equal to the second element; and a positive number if the first
133      * element is greater than the second element
134      */

135     protected int compareNames(IResource resource1, IResource resource2) {
136         return collator.compare(resource1.getName(), resource2.getName());
137     }
138
139     /**
140      * Returns a number reflecting the collation order of the given resources
141      * based on their respective file extensions. Resources with the same file
142      * extension will be collated based on their names.
143      *
144      * @param resource1 the first resource element to be ordered
145      * @param resource2 the second resource element to be ordered
146      * @return a negative number if the first element is less than the
147      * second element; the value <code>0</code> if the first element is
148      * equal to the second element; and a positive number if the first
149      * element is greater than the second element
150      */

151     protected int compareTypes(IResource resource1, IResource resource2) {
152         String JavaDoc ext1 = getExtensionFor(resource1);
153         String JavaDoc ext2 = getExtensionFor(resource2);
154
155         // Compare extensions. If they're different then return a value that
156
// indicates correct extension ordering. If they're the same then
157
// return a value that indicates the correct NAME ordering.
158
int result = collator.compare(ext1, ext2);
159
160         if (result != 0) {
161             return result;
162         }
163
164         return compareNames(resource1, resource2);
165     }
166
167     /**
168      * Returns the sort criteria of this this sorter.
169      *
170      * @return the sort criterion: one of <code>NAME</code> or <code>TYPE</code>
171      */

172     public int getCriteria() {
173         return criteria;
174     }
175
176     /**
177      * Returns the extension portion of the given resource.
178      *
179      * @param resource the resource
180      * @return the file extension, possibily the empty string
181      */

182     private String JavaDoc getExtensionFor(IResource resource) {
183         String JavaDoc ext = resource.getFileExtension();
184         return ext == null ? "" : ext; //$NON-NLS-1$
185
}
186 }
187
Popular Tags