1 11 package org.eclipse.search.internal.ui.text; 12 13 import com.ibm.icu.text.MessageFormat; 14 15 import org.eclipse.core.runtime.IPath; 16 17 import org.eclipse.core.resources.IResource; 18 19 import org.eclipse.swt.graphics.Image; 20 21 import org.eclipse.jface.viewers.ILabelProviderListener; 22 import org.eclipse.jface.viewers.LabelProvider; 23 24 import org.eclipse.ui.model.WorkbenchLabelProvider; 25 26 import org.eclipse.search.internal.ui.SearchMessages; 27 import org.eclipse.search.ui.text.AbstractTextSearchResult; 28 import org.eclipse.search.ui.text.AbstractTextSearchViewPage; 29 30 31 public class FileLabelProvider extends LabelProvider { 32 33 public static final int SHOW_LABEL= 1; 34 public static final int SHOW_LABEL_PATH= 2; 35 public static final int SHOW_PATH_LABEL= 3; 36 public static final int SHOW_PATH= 4; 37 38 private static final String fgSeparatorFormat= "{0} - {1}"; 40 private WorkbenchLabelProvider fLabelProvider; 41 private AbstractTextSearchViewPage fPage; 42 43 private int fOrder; 44 private String [] fArgs= new String [2]; 45 46 public FileLabelProvider(AbstractTextSearchViewPage page, int orderFlag) { 47 fLabelProvider= new WorkbenchLabelProvider(); 48 fOrder= orderFlag; 49 fPage= page; 50 } 51 52 public void setOrder(int orderFlag) { 53 fOrder= orderFlag; 54 } 55 56 public int getOrder() { 57 return fOrder; 58 } 59 60 public String getText(Object element) { 61 if (!(element instanceof IResource)) 62 return null; 63 64 IResource resource= (IResource)element; 65 String text= null; 66 67 if (!resource.exists()) 68 text= SearchMessages.FileLabelProvider_removed_resource_label; 69 70 else { 71 IPath path= resource.getFullPath().removeLastSegments(1); 72 if (path.getDevice() == null) 73 path= path.makeRelative(); 74 if (fOrder == SHOW_LABEL || fOrder == SHOW_LABEL_PATH) { 75 text= fLabelProvider.getText(resource); 76 if (path != null && fOrder == SHOW_LABEL_PATH) { 77 fArgs[0]= text; 78 fArgs[1]= path.toString(); 79 text= MessageFormat.format(fgSeparatorFormat, fArgs); 80 } 81 } else { 82 if (path != null) 83 text= path.toString(); 84 else 85 text= ""; if (fOrder == SHOW_PATH_LABEL) { 87 fArgs[0]= text; 88 fArgs[1]= fLabelProvider.getText(resource); 89 text= MessageFormat.format(fgSeparatorFormat, fArgs); 90 } 91 } 92 } 93 94 int matchCount= 0; 95 AbstractTextSearchResult result= fPage.getInput(); 96 if (result != null) 97 matchCount= result.getMatchCount(element); 98 if (matchCount <= 1) 99 return text; 100 String format= SearchMessages.FileLabelProvider_count_format; 101 return MessageFormat.format(format, new Object [] { text, new Integer (matchCount) }); 102 } 103 104 public Image getImage(Object element) { 105 if (!(element instanceof IResource)) 106 return null; 107 108 IResource resource= (IResource)element; 109 Image image= fLabelProvider.getImage(resource); 110 return image; 111 } 112 113 public void dispose() { 114 super.dispose(); 115 fLabelProvider.dispose(); 116 } 117 118 public boolean isLabelProperty(Object element, String property) { 119 return fLabelProvider.isLabelProperty(element, property); 120 } 121 122 public void removeListener(ILabelProviderListener listener) { 123 super.removeListener(listener); 124 fLabelProvider.removeListener(listener); 125 } 126 127 public void addListener(ILabelProviderListener listener) { 128 super.addListener(listener); 129 fLabelProvider.addListener(listener); 130 } 131 } 132 | Popular Tags |