001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide.dialogs;
011:
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IResource;
014: import org.eclipse.jface.viewers.Viewer;
015: import org.eclipse.jface.viewers.ViewerComparator;
016:
017: /**
018: * Sorter for viewers that display items of type <code>IResource</code>.
019: * The sorter supports two sort criteria:
020: * <p>
021: * <code>NAME</code>: Folders are given order precedence, followed by files.
022: * Within these two groups resources are ordered by name. All name comparisons
023: * are case-insensitive.
024: * </p>
025: * <p>
026: * <code>TYPE</code>: Folders are given order precedence, followed by files.
027: * Within these two groups resources are ordered by extension. All extension
028: * comparisons are case-insensitive.
029: * </p>
030: * <p>
031: * This class may be instantiated; it is not intended to be subclassed.
032: * </p>
033: */
034: public class ResourceComparator extends ViewerComparator {
035:
036: /**
037: * Constructor argument value that indicates to sort items by name.
038: */
039: public final static int NAME = 1;
040:
041: /**
042: * Constructor argument value that indicates to sort items by extension.
043: */
044: public final static int TYPE = 2;
045:
046: private int criteria;
047:
048: /**
049: * Creates a resource sorter that will use the given sort criteria.
050: *
051: * @param criteria the sort criterion to use: one of <code>NAME</code> or
052: * <code>TYPE</code>
053: */
054: public ResourceComparator(int criteria) {
055: super ();
056: this .criteria = criteria;
057: }
058:
059: /**
060: * Returns an integer value representing the relative sort priority of the
061: * given element based on its class.
062: * <p>
063: * <ul>
064: * <li>resources (<code>IResource</code>) - 2</li>
065: * <li>project references (<code>ProjectReference</code>) - 1</li>
066: * <li>everything else - 0</li>
067: * </ul>
068: * </p>
069: *
070: * @param element the element
071: * @return the sort priority (larger numbers means more important)
072: */
073: protected int classComparison(Object element) {
074: if (element instanceof IResource) {
075: return 2;
076: }
077: return 0;
078: }
079:
080: /* (non-Javadoc)
081: * Method declared on ViewerSorter.
082: */
083: public int compare(Viewer viewer, Object o1, Object o2) {
084: //have to deal with non-resources in navigator
085: //if one or both objects are not resources, returned a comparison
086: //based on class.
087: if (!(o1 instanceof IResource && o2 instanceof IResource)) {
088: return compareClass(o1, o2);
089: }
090: IResource r1 = (IResource) o1;
091: IResource r2 = (IResource) o2;
092:
093: if (r1 instanceof IContainer && r2 instanceof IContainer) {
094: return compareNames(r1, r2);
095: } else if (r1 instanceof IContainer) {
096: return -1;
097: } else if (r2 instanceof IContainer) {
098: return 1;
099: } else if (criteria == NAME) {
100: return compareNames(r1, r2);
101: } else if (criteria == TYPE) {
102: return compareTypes(r1, r2);
103: } else {
104: return 0;
105: }
106: }
107:
108: /**
109: * Returns a number reflecting the collation order of the given elements
110: * based on their class.
111: *
112: * @param element1 the first element to be ordered
113: * @param element2 the second element to be ordered
114: * @return a negative number if the first element is less than the
115: * second element; the value <code>0</code> if the first element is
116: * equal to the second element; and a positive number if the first
117: * element is greater than the second element
118: */
119: protected int compareClass(Object element1, Object element2) {
120: return classComparison(element1) - classComparison(element2);
121: }
122:
123: /**
124: * Returns a number reflecting the collation order of the given resources
125: * based on their resource names.
126: *
127: * @param resource1 the first resource element to be ordered
128: * @param resource2 the second resource element to be ordered
129: * @return a negative number if the first element is less than the
130: * second element; the value <code>0</code> if the first element is
131: * equal to the second element; and a positive number if the first
132: * element is greater than the second element
133: */
134: protected int compareNames(IResource resource1, IResource resource2) {
135: return getComparator().compare(resource1.getName(),
136: 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 ext1 = getExtensionFor(resource1);
153: String 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 = getComparator().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 getExtensionFor(IResource resource) {
183: String ext = resource.getFileExtension();
184: return ext == null ? "" : ext; //$NON-NLS-1$
185: }
186: }
|