KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > PathSorter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.search;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.Path;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jface.viewers.Viewer;
18
19 public class PathSorter extends NameSorter {
20     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
21         IPath path1= getPath(e1);
22         IPath path2=getPath(e2);
23         int result= compare(path1, path2);
24         if (result != 0)
25             return result;
26         else
27             return super.compare(viewer, e1, e2);
28     }
29     
30     /**
31      * @param e1
32      * @return
33      */

34     private IPath getPath(Object JavaDoc element) {
35         if (element instanceof IJavaElement)
36             return ((IJavaElement)element).getPath();
37         if (element instanceof IResource)
38             return ((IResource)element).getFullPath();
39         return new Path(""); //$NON-NLS-1$
40
}
41
42     protected int compare(IPath path1, IPath path2) {
43         int segmentCount= Math.min(path1.segmentCount(), path2.segmentCount());
44         for (int i= 0; i < segmentCount; i++) {
45             int value= collator.compare(path1.segment(i), path2.segment(i));
46             if (value != 0)
47                 return value;
48         }
49         return path1.segmentCount() - path2.segmentCount();
50     }
51 }
52
Popular Tags