1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import org.eclipse.core.resources.IMarker; 14 import org.eclipse.jdt.core.IImportDeclaration; 15 import org.eclipse.jdt.core.IJavaElement; 16 import org.eclipse.jdt.internal.ui.JavaPluginImages; 17 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider; 18 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementLabels; 19 import org.eclipse.jdt.ui.ProblemsLabelDecorator; 20 import org.eclipse.jface.viewers.ILabelDecorator; 21 import org.eclipse.jface.viewers.ILabelProviderListener; 22 import org.eclipse.jface.viewers.LabelProvider; 23 import org.eclipse.search.ui.ISearchResultViewEntry; 24 import org.eclipse.search.ui.SearchUI; 25 import org.eclipse.swt.graphics.Image; 26 import org.eclipse.ui.PlatformUI; 27 28 public class JavaSearchResultLabelProvider extends LabelProvider { 29 public static final int SHOW_ELEMENT_CONTAINER= 1; 30 public static final int SHOW_CONTAINER_ELEMENT= 2; 31 public static final int SHOW_PATH= 3; 32 33 public static final String POTENTIAL_MATCH= SearchMessages.getString("JavaSearchResultLabelProvider.potentialMatch"); 35 private AppearanceAwareLabelProvider fLabelProvider; 36 private ILabelDecorator fDecorator; 37 38 private IMarker fLastMarker; 40 private IJavaElement fLastJavaElement; 41 42 public JavaSearchResultLabelProvider() { 43 fDecorator= PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator(); 44 fLabelProvider= 45 new AppearanceAwareLabelProvider( 46 AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.P_COMPRESSED, 47 AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS); 48 fLabelProvider.addLabelDecorator(new ProblemsLabelDecorator(null)); 49 } 50 51 public String getText(Object o) { 52 fLastMarker= null; IJavaElement javaElement= getJavaElement(o); boolean isPotentialMatch= fLastMarker != null && fLastMarker.getAttribute(SearchUI.POTENTIAL_MATCH, false); 55 if (javaElement == null) { 56 if (fLastMarker != null) { 57 if (isPotentialMatch) 58 return super.getText(fLastMarker.getResource()) + POTENTIAL_MATCH; 59 else 60 return super.getText(fLastMarker.getResource()); 61 } 62 else 63 return ""; } 65 66 if (javaElement instanceof IImportDeclaration) 67 javaElement= ((IImportDeclaration)javaElement).getParent().getParent(); 68 69 String text= ""; if (isPotentialMatch) 71 text= fLabelProvider.getText(javaElement) + POTENTIAL_MATCH; 72 else 73 text= fLabelProvider.getText(javaElement); 74 75 if (fDecorator != null) { 76 String decoratedText= fDecorator.decorateText(text, javaElement); 77 if (decoratedText != null) 78 return decoratedText; 79 } 80 return text; 81 } 82 83 public Image getImage(Object o) { 84 IJavaElement javaElement= getJavaElement(o); 85 86 Image image= fLabelProvider.getImage(o); 87 if (image == null) 88 return JavaPluginImages.get(JavaPluginImages.IMG_OBJS_UNKNOWN); 89 90 if (fDecorator != null && javaElement != null) { 91 Image decoratedImage= fDecorator.decorateImage(image, javaElement); 92 if (decoratedImage != null) 93 return decoratedImage; 94 } 95 return image; 96 } 97 98 public void setAppearance(int appearanceFlag) { 99 int flags= AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.P_COMPRESSED; 100 if (appearanceFlag == SHOW_ELEMENT_CONTAINER) 101 flags |= JavaElementLabels.F_POST_QUALIFIED | JavaElementLabels.M_POST_QUALIFIED | JavaElementLabels.I_POST_QUALIFIED | JavaElementLabels.M_PARAMETER_TYPES 102 | JavaElementLabels.T_POST_QUALIFIED | JavaElementLabels.D_POST_QUALIFIED | JavaElementLabels.CF_POST_QUALIFIED | JavaElementLabels.CU_POST_QUALIFIED; 103 104 else if (appearanceFlag == SHOW_CONTAINER_ELEMENT) 105 flags |= JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.I_FULLY_QUALIFIED | JavaElementLabels.M_PARAMETER_TYPES 106 | JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.D_QUALIFIED | JavaElementLabels.CF_QUALIFIED | JavaElementLabels.CU_QUALIFIED; 107 else if (appearanceFlag == SHOW_PATH) { 108 flags |= JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.I_FULLY_QUALIFIED | JavaElementLabels.M_PARAMETER_TYPES 109 | JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.D_QUALIFIED | JavaElementLabels.CF_QUALIFIED | JavaElementLabels.CU_QUALIFIED; 110 flags |= JavaElementLabels.PREPEND_ROOT_PATH; 111 } 112 fLabelProvider.setTextFlags(flags); 113 } 114 115 116 private IJavaElement getJavaElement(Object o) { 117 if (o instanceof IJavaElement) 118 return (IJavaElement)o; 119 120 IMarker marker= getMarker(o); 121 if (marker == null) 122 return null; 123 124 return getJavaElement(marker, (ISearchResultViewEntry)o); 125 } 126 127 protected IMarker getMarker(Object o) { 128 if (!(o instanceof ISearchResultViewEntry)) 129 return null; 130 return ((ISearchResultViewEntry)o).getSelectedMarker(); 131 } 132 133 private IJavaElement getJavaElement(IMarker marker, ISearchResultViewEntry entry) { 134 if (fLastMarker != marker) { 135 boolean canUseGroupByKey= !marker.getAttribute(SearchUI.POTENTIAL_MATCH, false) 136 && !marker.getAttribute(IJavaSearchUIConstants.ATT_IS_WORKING_COPY, false) 137 && !marker.getAttribute(IJavaSearchUIConstants.ATT_JE_HANDLE_ID_CHANGED, false); 138 139 if (canUseGroupByKey && entry.getGroupByKey() instanceof IJavaElement) 140 fLastJavaElement= (IJavaElement)entry.getGroupByKey(); 141 else 142 fLastJavaElement= SearchUtil.getJavaElement(marker); 143 fLastMarker= marker; 144 } 145 return fLastJavaElement; 146 } 147 148 public void addListener(ILabelProviderListener listener) { 149 super.addListener(listener); 150 fLabelProvider.addListener(listener); 151 } 152 153 public void dispose() { 154 super.dispose(); 155 fLabelProvider.dispose(); 156 } 157 158 public boolean isLabelProperty(Object element, String property) { 159 return fLabelProvider.isLabelProperty(element, property); 160 } 161 162 public void removeListener(ILabelProviderListener listener) { 163 super.removeListener(listener); 164 fLabelProvider.removeListener(listener); 165 } 166 } 167 | Popular Tags |