KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 JavaDoc POTENTIAL_MATCH= SearchMessages.getString("JavaSearchResultLabelProvider.potentialMatch"); //$NON-NLS-1$
34

35     private AppearanceAwareLabelProvider fLabelProvider;
36     private ILabelDecorator fDecorator;
37     
38     // Cache
39
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 JavaDoc getText(Object JavaDoc o) {
52         fLastMarker= null; // reset cache
53
IJavaElement javaElement= getJavaElement(o); // sets fLastMarker as side effect
54
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 ""; //$NON-NLS-1$
64
}
65
66         if (javaElement instanceof IImportDeclaration)
67             javaElement= ((IImportDeclaration)javaElement).getParent().getParent();
68
69         String JavaDoc text= ""; //$NON-NLS-1$
70
if (isPotentialMatch)
71             text= fLabelProvider.getText(javaElement) + POTENTIAL_MATCH;
72         else
73             text= fLabelProvider.getText(javaElement);
74
75         if (fDecorator != null) {
76             String JavaDoc decoratedText= fDecorator.decorateText(text, javaElement);
77             if (decoratedText != null)
78                 return decoratedText;
79         }
80         return text;
81     }
82
83     public Image getImage(Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc element, String JavaDoc 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