KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.HashSet JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21
22 import org.eclipse.core.resources.IFile;
23
24 import org.eclipse.jface.resource.ImageDescriptor;
25
26 import org.eclipse.ui.IEditorPart;
27
28 import org.eclipse.search.ui.ISearchQuery;
29 import org.eclipse.search.ui.text.AbstractTextSearchResult;
30 import org.eclipse.search.ui.text.IEditorMatchAdapter;
31 import org.eclipse.search.ui.text.IFileMatchAdapter;
32 import org.eclipse.search.ui.text.Match;
33 import org.eclipse.search.ui.text.MatchFilter;
34
35 import org.eclipse.jdt.core.IClassFile;
36 import org.eclipse.jdt.core.ICompilationUnit;
37 import org.eclipse.jdt.core.IJavaElement;
38 import org.eclipse.jdt.core.IParent;
39 import org.eclipse.jdt.core.JavaModelException;
40
41 import org.eclipse.jdt.ui.search.IMatchPresentation;
42
43 import org.eclipse.jdt.internal.ui.JavaPlugin;
44
45 public class JavaSearchResult extends AbstractTextSearchResult implements IEditorMatchAdapter, IFileMatchAdapter {
46     
47     private static final Match[] NO_MATCHES= new Match[0];
48     
49     private final JavaSearchQuery fQuery;
50     private final Map JavaDoc fElementsToParticipants;
51
52     public JavaSearchResult(JavaSearchQuery query) {
53         fQuery= query;
54         fElementsToParticipants= new HashMap JavaDoc();
55         setActiveMatchFilters(JavaMatchFilter.getLastUsedFilters());
56     }
57
58     public ImageDescriptor getImageDescriptor() {
59         return fQuery.getImageDescriptor();
60     }
61
62     public String JavaDoc getLabel() {
63         return fQuery.getResultLabel(getMatchCount());
64     }
65
66     public String JavaDoc getTooltip() {
67         return getLabel();
68     }
69     
70     public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
71         return computeContainedMatches(editor.getEditorInput());
72     }
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#setMatchFilters(org.eclipse.search.ui.text.MatchFilter[])
76      */

77     public void setActiveMatchFilters(MatchFilter[] filters) {
78         super.setActiveMatchFilters(filters);
79         JavaMatchFilter.setLastUsedFilters(filters);
80     }
81     
82     /* (non-Javadoc)
83      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getAllMatchFilters()
84      */

85     public MatchFilter[] getAllMatchFilters() {
86         return JavaMatchFilter.allFilters(fQuery);
87     }
88
89     public Match[] computeContainedMatches(AbstractTextSearchResult result, IFile file) {
90         return computeContainedMatches(file);
91     }
92     
93     private Match[] computeContainedMatches(IAdaptable adaptable) {
94         IJavaElement javaElement= (IJavaElement) adaptable.getAdapter(IJavaElement.class);
95         Set JavaDoc matches= new HashSet JavaDoc();
96         if (javaElement != null) {
97             collectMatches(matches, javaElement);
98         }
99         IFile file= (IFile) adaptable.getAdapter(IFile.class);
100         if (file != null) {
101             collectMatches(matches, file);
102         }
103         if (!matches.isEmpty()) {
104             return (Match[]) matches.toArray(new Match[matches.size()]);
105         }
106         return NO_MATCHES;
107     }
108     
109     
110     private void collectMatches(Set JavaDoc matches, IFile element) {
111         Match[] m= getMatches(element);
112         if (m.length != 0) {
113             for (int i= 0; i < m.length; i++) {
114                 matches.add(m[i]);
115             }
116         }
117     }
118     
119     private void collectMatches(Set JavaDoc matches, IJavaElement element) {
120         Match[] m= getMatches(element);
121         if (m.length != 0) {
122             for (int i= 0; i < m.length; i++) {
123                 matches.add(m[i]);
124             }
125         }
126         if (element instanceof IParent) {
127             IParent parent= (IParent) element;
128             try {
129                 IJavaElement[] children= parent.getChildren();
130                 for (int i= 0; i < children.length; i++) {
131                     collectMatches(matches, children[i]);
132                 }
133             } catch (JavaModelException e) {
134                 // we will not be tracking these results
135
}
136         }
137     }
138     /* (non-Javadoc)
139      * @see org.eclipse.search.ui.ISearchResultCategory#getFile(java.lang.Object)
140      */

141     public IFile getFile(Object JavaDoc element) {
142         if (element instanceof IJavaElement) {
143             IJavaElement javaElement= (IJavaElement) element;
144             ICompilationUnit cu= (ICompilationUnit) javaElement.getAncestor(IJavaElement.COMPILATION_UNIT);
145             if (cu != null) {
146                 return (IFile) cu.getResource();
147             } else {
148                 IClassFile cf= (IClassFile) javaElement.getAncestor(IJavaElement.CLASS_FILE);
149                 if (cf != null)
150                     return (IFile) cf.getResource();
151             }
152             return null;
153         }
154         if (element instanceof IFile)
155             return (IFile) element;
156         return null;
157     }
158     
159     /*
160      * (non-Javadoc)
161      *
162      * @see org.eclipse.search2.ui.text.IStructureProvider#isShownInEditor(org.eclipse.search2.ui.text.Match,
163      * org.eclipse.ui.IEditorPart)
164      */

165     public boolean isShownInEditor(Match match, IEditorPart editor) {
166         Object JavaDoc element= match.getElement();
167         if (element instanceof IJavaElement) {
168             element= ((IJavaElement) element).getOpenable(); // class file or compilation unit
169
return element != null && element.equals(editor.getEditorInput().getAdapter(IJavaElement.class));
170         } else if (element instanceof IFile) {
171             return element.equals(editor.getEditorInput().getAdapter(IFile.class));
172         }
173         return false;
174     }
175
176     /* (non-Javadoc)
177      * @see org.eclipse.search.ui.ISearchResult#getQuery()
178      */

179     public ISearchQuery getQuery() {
180         return fQuery;
181     }
182     
183     synchronized IMatchPresentation getSearchParticpant(Object JavaDoc element) {
184         return (IMatchPresentation) fElementsToParticipants.get(element);
185     }
186
187     boolean addMatch(Match match, IMatchPresentation participant) {
188         Object JavaDoc element= match.getElement();
189         if (fElementsToParticipants.get(element) != null) {
190             // TODO must access the participant id / label to properly report the error.
191
JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), 0, "A second search participant was found for an element", null)); //$NON-NLS-1$
192
return false;
193         }
194         fElementsToParticipants.put(element, participant);
195         addMatch(match);
196         return true;
197     }
198     
199     public void removeAll() {
200         synchronized(this) {
201             fElementsToParticipants.clear();
202         }
203         super.removeAll();
204     }
205     
206     public void removeMatch(Match match) {
207         synchronized(this) {
208             if (getMatchCount(match.getElement()) == 1)
209                 fElementsToParticipants.remove(match.getElement());
210         }
211         super.removeMatch(match);
212     }
213     public IFileMatchAdapter getFileMatchAdapter() {
214         return this;
215     }
216     
217     public IEditorMatchAdapter getEditorMatchAdapter() {
218         return this;
219     }
220
221 }
222
Popular Tags