KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.search;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.preferences.InstanceScope;
18
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.RGB;
22
23 import org.eclipse.jface.preference.PreferenceConverter;
24 import org.eclipse.jface.util.IPropertyChangeListener;
25 import org.eclipse.jface.util.PropertyChangeEvent;
26 import org.eclipse.jface.viewers.ILabelProvider;
27 import org.eclipse.jface.viewers.ILabelProviderListener;
28 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
29
30 import org.eclipse.ui.preferences.ScopedPreferenceStore;
31
32 import org.eclipse.search.ui.NewSearchUI;
33 import org.eclipse.search.ui.text.AbstractTextSearchResult;
34 import org.eclipse.search.ui.text.Match;
35
36 import org.eclipse.jdt.core.search.SearchMatch;
37
38 import org.eclipse.jdt.internal.corext.util.Messages;
39
40 import org.eclipse.jdt.ui.JavaElementLabels;
41 import org.eclipse.jdt.ui.ProblemsLabelDecorator;
42 import org.eclipse.jdt.ui.search.IMatchPresentation;
43
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
46 import org.eclipse.jdt.internal.ui.viewsupport.ColoredJavaElementLabels;
47 import org.eclipse.jdt.internal.ui.viewsupport.ColoredString;
48
49 public abstract class SearchLabelProvider extends AppearanceAwareLabelProvider {
50     
51     public static final String JavaDoc PROPERTY_MATCH_COUNT= "org.eclipse.jdt.search.matchCount"; //$NON-NLS-1$
52

53     // copied from SearchPreferencePage
54
private static final String JavaDoc EMPHASIZE_POTENTIAL_MATCHES= "org.eclipse.search.potentialMatch.emphasize"; //$NON-NLS-1$
55
private static final String JavaDoc POTENTIAL_MATCH_FG_COLOR= "org.eclipse.search.potentialMatch.fgColor"; //$NON-NLS-1$
56

57     protected static final long DEFAULT_SEARCH_TEXTFLAGS= (DEFAULT_TEXTFLAGS | JavaElementLabels.P_COMPRESSED) & ~JavaElementLabels.M_APP_RETURNTYPE;
58     protected static final int DEFAULT_SEARCH_IMAGEFLAGS= DEFAULT_IMAGEFLAGS;
59     
60     private Color fPotentialMatchFgColor;
61     private Map JavaDoc fLabelProviderMap;
62     
63     protected JavaSearchResultPage fPage;
64
65     private ScopedPreferenceStore fSearchPreferences;
66     private IPropertyChangeListener fSearchPropertyListener;
67
68     public SearchLabelProvider(JavaSearchResultPage page) {
69         super(DEFAULT_SEARCH_TEXTFLAGS, DEFAULT_SEARCH_IMAGEFLAGS);
70         addLabelDecorator(new ProblemsLabelDecorator(null));
71         
72         fPage= page;
73         fLabelProviderMap= new HashMap JavaDoc(5);
74         
75         fSearchPreferences= new ScopedPreferenceStore(new InstanceScope(), NewSearchUI.PLUGIN_ID);
76         fSearchPropertyListener= new IPropertyChangeListener() {
77             public void propertyChange(PropertyChangeEvent event) {
78                 doSearchPropertyChange(event);
79             }
80         };
81         fSearchPreferences.addPropertyChangeListener(fSearchPropertyListener);
82     }
83     
84     final void doSearchPropertyChange(PropertyChangeEvent event) {
85         if (fPotentialMatchFgColor == null)
86             return;
87         if (POTENTIAL_MATCH_FG_COLOR.equals(event.getProperty()) || EMPHASIZE_POTENTIAL_MATCHES.equals(event.getProperty())) {
88             fPotentialMatchFgColor.dispose();
89             fPotentialMatchFgColor= null;
90             LabelProviderChangedEvent lpEvent= new LabelProviderChangedEvent(SearchLabelProvider.this, null); // refresh all
91
fireLabelProviderChanged(lpEvent);
92         }
93     }
94
95     public Color getForeground(Object JavaDoc element) {
96         if (arePotentialMatchesEmphasized()) {
97             if (getNumberOfPotentialMatches(element) > 0)
98                 return getForegroundColor();
99         }
100         return super.getForeground(element);
101     }
102     
103     private Color getForegroundColor() {
104         if (fPotentialMatchFgColor == null) {
105             fPotentialMatchFgColor= new Color(JavaPlugin.getActiveWorkbenchShell().getDisplay(), getPotentialMatchForegroundColor());
106         }
107         return fPotentialMatchFgColor;
108     }
109
110     protected final int getNumberOfPotentialMatches(Object JavaDoc element) {
111         int res= 0;
112         AbstractTextSearchResult result= fPage.getInput();
113         if (result != null) {
114             Match[] matches= result.getMatches(element);
115             for (int i = 0; i < matches.length; i++) {
116                 if ((matches[i]) instanceof JavaElementMatch) {
117                     if (((JavaElementMatch)matches[i]).getAccuracy() == SearchMatch.A_INACCURATE)
118                         res++;
119                 }
120             }
121         }
122         return res;
123     }
124         
125     protected final ColoredString getColoredLabelWithCounts(Object JavaDoc element, ColoredString coloredName) {
126         String JavaDoc name= coloredName.getString();
127         String JavaDoc decorated= getLabelWithCounts(element, name);
128         if (decorated.length() > name.length()) {
129             ColoredJavaElementLabels.decorateColoredString(coloredName, decorated, ColoredJavaElementLabels.COUNTER_STYLE);
130         }
131         return coloredName;
132     }
133     
134     protected final String JavaDoc getLabelWithCounts(Object JavaDoc element, String JavaDoc elementName) {
135         int matchCount= fPage.getDisplayedMatchCount(element);
136         int potentialCount= getNumberOfPotentialMatches(element);
137         
138         if (matchCount < 2) {
139             if (matchCount == 1 && hasChildren(element)) {
140                 if (potentialCount > 0)
141                     return Messages.format(SearchMessages.SearchLabelProvider_potential_singular, elementName);
142                 return Messages.format(SearchMessages.SearchLabelProvider_exact_singular, elementName);
143             }
144             if (potentialCount > 0)
145                 return Messages.format(SearchMessages.SearchLabelProvider_potential_noCount, elementName);
146             return Messages.format(SearchMessages.SearchLabelProvider_exact_noCount, elementName);
147         } else {
148             int exactCount= matchCount - potentialCount;
149             
150             if (potentialCount > 0 && exactCount > 0) {
151                 String JavaDoc[] args= new String JavaDoc[] { elementName, String.valueOf(matchCount), String.valueOf(exactCount), String.valueOf(potentialCount) };
152                 return Messages.format(SearchMessages.SearchLabelProvider_exact_and_potential_plural, args);
153             } else if (exactCount == 0) {
154                 String JavaDoc[] args= new String JavaDoc[] { elementName, String.valueOf(matchCount) };
155                 return Messages.format(SearchMessages.SearchLabelProvider_potential_plural, args);
156             }
157             String JavaDoc[] args= new String JavaDoc[] { elementName, String.valueOf(matchCount) };
158             return Messages.format(SearchMessages.SearchLabelProvider_exact_plural, args);
159         }
160     }
161     
162     protected boolean hasChildren(Object JavaDoc elem) {
163         return false;
164     }
165     
166     public void dispose() {
167         if (fPotentialMatchFgColor != null) {
168             fPotentialMatchFgColor.dispose();
169             fPotentialMatchFgColor= null;
170         }
171         fSearchPreferences.removePropertyChangeListener(fSearchPropertyListener);
172         for (Iterator JavaDoc labelProviders = fLabelProviderMap.values().iterator(); labelProviders.hasNext();) {
173             ILabelProvider labelProvider = (ILabelProvider) labelProviders.next();
174             labelProvider.dispose();
175         }
176         fSearchPreferences= null;
177         fSearchPropertyListener= null;
178         fLabelProviderMap.clear();
179         
180         super.dispose();
181     }
182     
183     public void addListener(ILabelProviderListener listener) {
184         super.addListener(listener);
185         for (Iterator JavaDoc labelProviders = fLabelProviderMap.values().iterator(); labelProviders.hasNext();) {
186             ILabelProvider labelProvider = (ILabelProvider) labelProviders.next();
187             labelProvider.addListener(listener);
188         }
189     }
190
191     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
192         if (PROPERTY_MATCH_COUNT.equals(property))
193             return true;
194         return getLabelProvider(element).isLabelProperty(element, property);
195     }
196
197     public void removeListener(ILabelProviderListener listener) {
198         super.removeListener(listener);
199         for (Iterator JavaDoc labelProviders = fLabelProviderMap.values().iterator(); labelProviders.hasNext();) {
200             ILabelProvider labelProvider = (ILabelProvider) labelProviders.next();
201             labelProvider.removeListener(listener);
202         }
203     }
204
205     protected String JavaDoc getParticipantText(Object JavaDoc element) {
206         ILabelProvider labelProvider= getLabelProvider(element);
207         if (labelProvider != null)
208             return labelProvider.getText(element);
209         return ""; //$NON-NLS-1$
210

211     }
212
213     protected Image getParticipantImage(Object JavaDoc element) {
214         ILabelProvider lp= getLabelProvider(element);
215         if (lp == null)
216             return null;
217         return lp.getImage(element);
218     }
219
220
221     private ILabelProvider getLabelProvider(Object JavaDoc element) {
222         IMatchPresentation participant= ((JavaSearchResult) fPage.getInput()).getSearchParticpant(element);
223         if (participant == null)
224             return null;
225         
226         ILabelProvider lp= (ILabelProvider) fLabelProviderMap.get(participant);
227         if (lp == null) {
228             lp= participant.createLabelProvider();
229             fLabelProviderMap.put(participant, lp);
230             
231             Object JavaDoc[] listeners= fListeners.getListeners();
232             for (int i= 0; i < listeners.length; i++) {
233                 lp.addListener((ILabelProviderListener) listeners[i]);
234             }
235         }
236         return lp;
237     }
238     
239     private boolean arePotentialMatchesEmphasized() {
240         return fSearchPreferences.getBoolean(EMPHASIZE_POTENTIAL_MATCHES);
241     }
242
243     private RGB getPotentialMatchForegroundColor() {
244         return PreferenceConverter.getColor(fSearchPreferences, POTENTIAL_MATCH_FG_COLOR);
245     }
246 }
247
Popular Tags