KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15 import org.eclipse.jface.viewers.ILabelProvider;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.jface.viewers.ViewerSorter;
18 import org.eclipse.search.ui.ISearchResultView;
19 import org.eclipse.search.ui.SearchUI;
20
21 /**
22  * @author tma
23  *
24  * To change the template for this generated type comment go to
25  * Window - Preferences - Java - Code Generation - Code and Comments
26  */

27 public abstract class JavaSearchSorter extends ViewerSorter {
28     
29     private Map JavaDoc fLabelCache= new HashMap JavaDoc();
30
31     public boolean isSorterProperty(Object JavaDoc element, String JavaDoc property) {
32         return true;
33     }
34     
35     protected String JavaDoc getLabel(Object JavaDoc element) {
36         String JavaDoc label= (String JavaDoc) fLabelCache.get(element);
37         if (label != null)
38             return label;
39         return fLabelProvider.getText(element);
40     }
41
42     /**
43      * Sets up the label provider according to the result from
44      * {@link #getLabelAppearance()}.
45      * @return true if the sort can proceed, false otherwise.
46      */

47     protected boolean setupLabelProvider() {
48         // Set label provider to show "element - path"
49
ISearchResultView view= SearchUI.getSearchResultView();
50         if (view == null)
51             return false;
52         fLabelProvider= view.getLabelProvider();
53         if (fLabelProvider instanceof JavaSearchResultLabelProvider) {
54             ((JavaSearchResultLabelProvider)fLabelProvider).setAppearance(getLabelAppearance());
55             return true;
56         }
57         return false;
58     }
59
60     /**
61      * @return The appearance flag for this sort order.
62      * One of the <code>int</code> constants in {@link JavaSearchResultLabelProvider}
63      */

64     protected abstract int getLabelAppearance();
65
66     protected ILabelProvider fLabelProvider;
67
68     public void sort(Viewer viewer, Object JavaDoc[] elements) {
69         if (!setupLabelProvider())
70             return;
71         cacheLabels(elements);
72         super.sort(viewer, elements);
73         fLabelCache.clear();
74     }
75
76     private void cacheLabels(Object JavaDoc[] elements) {
77         for (int i= 0; i < elements.length; i++) {
78             String JavaDoc label= fLabelProvider.getText(elements[i]);
79             if (label != null)
80                 fLabelCache.put(elements[i], label);
81         }
82     }
83
84 }
85
Popular Tags