1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 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 27 public abstract class JavaSearchSorter extends ViewerSorter { 28 29 private Map fLabelCache= new HashMap (); 30 31 public boolean isSorterProperty(Object element, String property) { 32 return true; 33 } 34 35 protected String getLabel(Object element) { 36 String label= (String ) fLabelCache.get(element); 37 if (label != null) 38 return label; 39 return fLabelProvider.getText(element); 40 } 41 42 47 protected boolean setupLabelProvider() { 48 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 64 protected abstract int getLabelAppearance(); 65 66 protected ILabelProvider fLabelProvider; 67 68 public void sort(Viewer viewer, Object [] elements) { 69 if (!setupLabelProvider()) 70 return; 71 cacheLabels(elements); 72 super.sort(viewer, elements); 73 fLabelCache.clear(); 74 } 75 76 private void cacheLabels(Object [] elements) { 77 for (int i= 0; i < elements.length; i++) { 78 String label= fLabelProvider.getText(elements[i]); 79 if (label != null) 80 fLabelCache.put(elements[i], label); 81 } 82 } 83 84 } 85 | Popular Tags |