KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > navigator > ResourceComparator


1 /*******************************************************************************
2  * Copyright (c) 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.views.navigator;
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.ViewerComparator;
17
18 /**
19  * Comparator 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  * @since 3.3
36  */

37 public class ResourceComparator extends ViewerComparator {
38
39     /**
40      * Constructor argument value that indicates to sort items by name.
41      */

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

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

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

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

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

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

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

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

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

184     private String JavaDoc getExtensionFor(IResource resource) {
185         String JavaDoc ext = resource.getFileExtension();
186         return ext == null ? "" : ext; //$NON-NLS-1$
187
}
188
189     /**
190      * Sets the sort criteria of this sorter.
191      *
192      * @param criteria the sort criterion:
193      * one of <code>ResourceSorter.NAME</code> or
194      * <code>ResourceSorter.TYPE</code>
195      */

196     public void setCriteria(int criteria) {
197         this.criteria = criteria;
198     }
199 }
200
Popular Tags