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