KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > text > ResourcenameSorter


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.search.internal.ui.text;
12
13 import org.eclipse.jface.viewers.ILabelProvider;
14 import org.eclipse.jface.viewers.Viewer;
15 import org.eclipse.jface.viewers.ViewerSorter;
16
17 import org.eclipse.search.ui.ISearchResultView;
18 import org.eclipse.search.ui.ISearchResultViewEntry;
19 import org.eclipse.search.ui.SearchUI;
20
21 import org.eclipse.search.internal.ui.util.FileLabelProvider;
22
23 /**
24  * Sorts the search result viewer by the resource name.
25  */

26 public class ResourcenameSorter extends ViewerSorter {
27
28     /*
29      * Overrides method from ViewerSorter
30      */

31     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
32         String JavaDoc name1= null;
33         String JavaDoc name2= null;
34         
35         if (e1 instanceof ISearchResultViewEntry)
36             name1= ((ISearchResultViewEntry)e1).getResource().getName();
37         if (e2 instanceof ISearchResultViewEntry)
38             name2= ((ISearchResultViewEntry)e2).getResource().getName();
39         if (name1 == null)
40             name1= ""; //$NON-NLS-1$
41
if (name2 == null)
42             name2= ""; //$NON-NLS-1$
43
return getCollator().compare(name1, name2);
44     }
45
46     /*
47      * Overrides method from ViewerSorter
48      */

49     public boolean isSorterProperty(Object JavaDoc element, String JavaDoc property) {
50         return true;
51     }
52
53     /*
54      * Overrides method from ViewerSorter
55      */

56     public void sort(Viewer viewer, Object JavaDoc[] elements) {
57         // Set label provider to show "resource - path"
58
ISearchResultView view= SearchUI.getSearchResultView();
59         if (view != null) {
60             ILabelProvider labelProvider= view.getLabelProvider();
61             if (labelProvider instanceof FileLabelProvider)
62                 ((FileLabelProvider)labelProvider).setOrder(FileLabelProvider.SHOW_LABEL_PATH);
63         }
64         super.sort(viewer, elements);
65     }
66 }
67
Popular Tags