KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > nls > search > NLSSearchResult


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
12 package org.eclipse.jdt.internal.ui.refactoring.nls.search;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IResource;
23
24 import org.eclipse.jface.resource.ImageDescriptor;
25
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.IFileEditorInput;
29
30 import org.eclipse.search.ui.ISearchQuery;
31 import org.eclipse.search.ui.text.AbstractTextSearchResult;
32 import org.eclipse.search.ui.text.IEditorMatchAdapter;
33 import org.eclipse.search.ui.text.IFileMatchAdapter;
34 import org.eclipse.search.ui.text.Match;
35
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.JavaCore;
40 import org.eclipse.jdt.core.JavaModelException;
41
42 import org.eclipse.jdt.internal.ui.JavaPlugin;
43 import org.eclipse.jdt.internal.ui.JavaPluginImages;
44 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
45
46 public class NLSSearchResult extends AbstractTextSearchResult implements IEditorMatchAdapter, IFileMatchAdapter {
47
48     private static final Match[] NO_MATCHES= new Match[0];
49
50     /*
51      * Element (group key) is always IJavaElement or FileEntry.
52      */

53     private NLSSearchQuery fQuery;
54     private final List JavaDoc fFileEntryGroups;
55     private final List JavaDoc fCompilationUnitGroups;
56
57     public NLSSearchResult(NLSSearchQuery query) {
58         fQuery= query;
59         fFileEntryGroups= new ArrayList JavaDoc();
60         fCompilationUnitGroups= new ArrayList JavaDoc();
61     }
62     
63     public void addFileEntryGroup(FileEntry group) {
64         fFileEntryGroups.add(group);
65     }
66     
67     public void addCompilationUnitGroup(CompilationUnitEntry group) {
68         fCompilationUnitGroups.add(group);
69     }
70     
71     /*
72      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#findContainedMatches(org.eclipse.ui.IEditorPart)
73      */

74     public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
75         //TODO: copied from JavaSearchResult:
76
IEditorInput editorInput= editor.getEditorInput();
77         if (editorInput instanceof IFileEditorInput) {
78             IFileEditorInput fileEditorInput= (IFileEditorInput) editorInput;
79             return computeContainedMatches(result, fileEditorInput.getFile());
80         } else if (editorInput instanceof IClassFileEditorInput) {
81             IClassFileEditorInput classFileEditorInput= (IClassFileEditorInput) editorInput;
82             Set JavaDoc matches= new HashSet JavaDoc();
83             collectMatches(matches, classFileEditorInput.getClassFile());
84             return (Match[]) matches.toArray(new Match[matches.size()]);
85         }
86         return NO_MATCHES;
87     }
88
89     /*
90      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#findContainedMatches(org.eclipse.core.resources.IFile)
91      */

92     public Match[] computeContainedMatches(AbstractTextSearchResult result, IFile file) {
93         Set JavaDoc matches= new HashSet JavaDoc();
94         for (Iterator JavaDoc iter= fFileEntryGroups.iterator(); iter.hasNext();) {
95             FileEntry element= (FileEntry)iter.next();
96             if (element.getPropertiesFile().equals(file)) {
97                 matches.addAll(Arrays.asList(getMatches(element)));
98             }
99         }
100         if (matches.size() > 0)
101             return (Match[]) matches.toArray(new Match[matches.size()]);
102         
103         try {
104             for (Iterator JavaDoc iter= fCompilationUnitGroups.iterator(); iter.hasNext();) {
105                 CompilationUnitEntry element= (CompilationUnitEntry)iter.next();
106                 ICompilationUnit cu= element.getCompilationUnit();
107                 if (cu.exists() && file.equals(cu.getCorrespondingResource())) {
108                     matches.addAll(Arrays.asList(getMatches(element)));
109                 }
110             }
111         } catch (JavaModelException e) {
112             JavaPlugin.log(e);
113             return NO_MATCHES;
114         }
115         
116         //TODO: copied from JavaSearchResult:
117
IJavaElement javaElement= JavaCore.create(file);
118         collectMatches(matches, javaElement);
119         return (Match[]) matches.toArray(new Match[matches.size()]);
120     }
121     
122     private void collectMatches(Set JavaDoc matches, IJavaElement element) {
123         //TODO: copied from JavaSearchResult:
124
Match[] m= getMatches(element);
125         if (m.length != 0) {
126             for (int i= 0; i < m.length; i++) {
127                 matches.add(m[i]);
128             }
129         }
130         if (element instanceof IParent) {
131             IParent parent= (IParent) element;
132             try {
133                 IJavaElement[] children= parent.getChildren();
134                 for (int i= 0; i < children.length; i++) {
135                     collectMatches(matches, children[i]);
136                 }
137             } catch (JavaModelException e) {
138                 // we will not be tracking these results
139
}
140         }
141     }
142     
143     /*
144      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getFile(java.lang.Object)
145      */

146     public IFile getFile(Object JavaDoc element) {
147         if (element instanceof FileEntry) {
148             return ((FileEntry) element).getPropertiesFile();
149         } else {
150             IJavaElement javaElement= null;
151             if (element instanceof CompilationUnitEntry) {
152                 javaElement= ((CompilationUnitEntry)element).getCompilationUnit();
153             } else {
154                 javaElement= (IJavaElement) element;
155             }
156             IResource resource= null;
157             try {
158                 resource= javaElement.getCorrespondingResource();
159             } catch (JavaModelException e) {
160                 // no resource
161
}
162             if (resource instanceof IFile)
163                 return (IFile) resource;
164             else
165                 return null;
166         }
167     }
168
169     /*
170      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#isShownInEditor(org.eclipse.search.ui.text.Match, org.eclipse.ui.IEditorPart)
171      */

172     public boolean isShownInEditor(Match match, IEditorPart editor) {
173         IEditorInput editorInput= editor.getEditorInput();
174         if (match.getElement() instanceof FileEntry) {
175             IFile file= ((FileEntry) match.getElement()).getPropertiesFile();
176             if (editorInput instanceof IFileEditorInput) {
177                 return ((IFileEditorInput) editorInput).getFile().equals(file);
178             }
179         } else if (match.getElement() instanceof IJavaElement || match.getElement() instanceof CompilationUnitEntry) {
180             IJavaElement je= null;
181             if (match.getElement() instanceof IJavaElement) {
182                 je= (IJavaElement) match.getElement();
183             } else {
184                 je= ((CompilationUnitEntry)match.getElement()).getCompilationUnit();
185             }
186             if (editorInput instanceof IFileEditorInput) {
187                 try {
188                     ICompilationUnit cu= (ICompilationUnit) je.getAncestor(IJavaElement.COMPILATION_UNIT);
189                     if (cu == null)
190                         return false;
191                     else
192                         return ((IFileEditorInput) editorInput).getFile().equals(cu.getCorrespondingResource());
193                 } catch (JavaModelException e) {
194                     return false;
195                 }
196             } else if (editorInput instanceof IClassFileEditorInput) {
197                 return ((IClassFileEditorInput) editorInput).getClassFile().equals(je.getAncestor(IJavaElement.CLASS_FILE));
198             }
199         }
200         return false;
201     }
202
203     /*
204      * @see org.eclipse.search.ui.ISearchResult#getLabel()
205      */

206     public String JavaDoc getLabel() {
207         return fQuery.getResultLabel(getMatchCount());
208     }
209
210     /*
211      * @see org.eclipse.search.ui.ISearchResult#getTooltip()
212      */

213     public String JavaDoc getTooltip() {
214         return getLabel();
215     }
216
217     /*
218      * @see org.eclipse.search.ui.ISearchResult#getImageDescriptor()
219      */

220     public ImageDescriptor getImageDescriptor() {
221         return JavaPluginImages.DESC_OBJS_SEARCH_REF;
222     }
223
224     /*
225      * @see org.eclipse.search.ui.ISearchResult#getQuery()
226      */

227     public ISearchQuery getQuery() {
228         return fQuery;
229     }
230
231     public IFileMatchAdapter getFileMatchAdapter() {
232         return this;
233     }
234     
235     public IEditorMatchAdapter getEditorMatchAdapter() {
236         return this;
237     }
238
239 }
240
Popular Tags