KickJava   Java API By Example, From Geeks To Geeks.

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


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.IMarker;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.search.ui.ISearchResultViewEntry;
18
19 /**
20  * Sorts the search result viewer by the path name.
21  */

22 public class PathNameSorter extends JavaSearchSorter {
23
24     /*
25      * Overrides method from ViewerSorter
26      */

27     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
28         String JavaDoc name1= null;
29         String JavaDoc name2= null;
30         ISearchResultViewEntry entry1= null;
31         ISearchResultViewEntry entry2= null;
32
33         if (e1 instanceof ISearchResultViewEntry) {
34             entry1= (ISearchResultViewEntry)e1;
35             name1= getLabel(e1);
36         }
37         if (e2 instanceof ISearchResultViewEntry) {
38             entry2= (ISearchResultViewEntry)e2;
39             name2= getLabel(e2);
40         }
41         if (name1 == null)
42             name1= ""; //$NON-NLS-1$
43
if (name2 == null)
44             name2= ""; //$NON-NLS-1$
45

46         IResource resource= null;
47         if (entry1 != null)
48             resource= entry1.getResource();
49         if (resource != null && entry2 != null && resource == entry2.getResource()) {
50
51             if (resource instanceof IProject || resource.getFileExtension().equalsIgnoreCase("jar") || resource.getFileExtension().equalsIgnoreCase("zip")) //$NON-NLS-2$ //$NON-NLS-1$
52
// binary archives
53
return getCollator().compare(name1, name2);
54
55             // Sort by marker start position if resource is equal.
56
int startPos1= -1;
57             int startPos2= -1;
58             IMarker marker1= entry1.getSelectedMarker();
59             IMarker marker2= entry2.getSelectedMarker();
60
61             if (marker1 != null)
62                 startPos1= marker1.getAttribute(IMarker.CHAR_START, -1);
63             if (marker2 != null)
64                 startPos2= marker2.getAttribute(IMarker.CHAR_START, -1);
65             return startPos1 - startPos2;
66         }
67         
68         return getCollator().compare(name1, name2);
69     }
70
71     protected int getLabelAppearance() {
72         return JavaSearchResultLabelProvider.SHOW_PATH;
73     }
74 }
75
76
Popular Tags