KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.search;
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IResource;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.IFileEditorInput;
22
23 import org.eclipse.search.ui.ISearchQuery;
24 import org.eclipse.search.ui.text.AbstractTextSearchResult;
25 import org.eclipse.search.ui.text.IEditorMatchAdapter;
26 import org.eclipse.search.ui.text.IFileMatchAdapter;
27 import org.eclipse.search.ui.text.Match;
28
29 import org.eclipse.jdt.core.IClassFile;
30 import org.eclipse.jdt.core.IJavaElement;
31 import org.eclipse.jdt.core.JavaModelException;
32
33 import org.eclipse.jdt.internal.ui.JavaPluginImages;
34 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
35
36
37 public class OccurrencesSearchResult extends AbstractTextSearchResult implements IEditorMatchAdapter, IFileMatchAdapter {
38
39     protected static final Match[] NO_MATCHES= new Match[0];
40     private OccurrencesSearchQuery fQuery;
41
42     public OccurrencesSearchResult(OccurrencesSearchQuery query) {
43         fQuery= query;
44     }
45     
46     /*
47      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#findContainedMatches(org.eclipse.core.resources.IFile)
48      */

49     public Match[] computeContainedMatches(AbstractTextSearchResult result, IFile file) {
50         Object JavaDoc[] elements= getElements();
51         if (elements.length == 0)
52             return NO_MATCHES;
53         //all matches from same file:
54
JavaElementLine jel= (JavaElementLine) elements[0];
55         try {
56             if (file.equals(jel.getJavaElement().getCorrespondingResource()))
57                 return collectMatches(elements);
58         } catch (JavaModelException e) {
59             // no resource
60
}
61         return NO_MATCHES;
62     }
63
64     /*
65      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#findContainedMatches(org.eclipse.ui.IEditorPart)
66      */

67     public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
68         //TODO same code in JavaSearchResult
69
IEditorInput editorInput= editor.getEditorInput();
70         if (editorInput instanceof IFileEditorInput) {
71             IFileEditorInput fileEditorInput= (IFileEditorInput) editorInput;
72             return computeContainedMatches(result, fileEditorInput.getFile());
73             
74         } else if (editorInput instanceof IClassFileEditorInput) {
75             IClassFileEditorInput classFileEditorInput= (IClassFileEditorInput) editorInput;
76             IClassFile classFile= classFileEditorInput.getClassFile();
77             
78             Object JavaDoc[] elements= getElements();
79             if (elements.length == 0)
80                 return NO_MATCHES;
81             //all matches from same file:
82
JavaElementLine jel= (JavaElementLine) elements[0];
83             if (jel.getJavaElement().equals(classFile))
84                 return collectMatches(elements);
85         }
86         return NO_MATCHES;
87     }
88     
89     /*
90      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getFile(java.lang.Object)
91      */

92     public IFile getFile(Object JavaDoc element) {
93         JavaElementLine jel= (JavaElementLine) element;
94         IResource resource= null;
95         try {
96             resource= jel.getJavaElement().getCorrespondingResource();
97         } catch (JavaModelException e) {
98             // no resource
99
}
100         if (resource instanceof IFile)
101             return (IFile) resource;
102         else
103             return null;
104     }
105     
106     /*
107      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#isShownInEditor(org.eclipse.search.ui.text.Match, org.eclipse.ui.IEditorPart)
108      */

109     public boolean isShownInEditor(Match match, IEditorPart editor) {
110         Object JavaDoc element= match.getElement();
111         IJavaElement je= ((JavaElementLine) element).getJavaElement();
112         IEditorInput editorInput= editor.getEditorInput();
113         if (editorInput instanceof IFileEditorInput) {
114             try {
115                 return ((IFileEditorInput)editorInput).getFile().equals(je.getCorrespondingResource());
116             } catch (JavaModelException e) {
117                 return false;
118             }
119         } else if (editorInput instanceof IClassFileEditorInput) {
120             return ((IClassFileEditorInput)editorInput).getClassFile().equals(je);
121         }
122         
123         return false;
124     }
125     
126     /*
127      * @see org.eclipse.search.ui.ISearchResult#getLabel()
128      */

129     public String JavaDoc getLabel() {
130         return fQuery.getResultLabel(getMatchCount());
131     }
132
133     /*
134      * @see org.eclipse.search.ui.ISearchResult#getTooltip()
135      */

136     public String JavaDoc getTooltip() {
137         return getLabel();
138     }
139
140     /*
141      * @see org.eclipse.search.ui.ISearchResult#getImageDescriptor()
142      */

143     public ImageDescriptor getImageDescriptor() {
144         return JavaPluginImages.DESC_OBJS_SEARCH_REF;
145     }
146
147     /*
148      * @see org.eclipse.search.ui.ISearchResult#getQuery()
149      */

150     public ISearchQuery getQuery() {
151         return fQuery;
152     }
153
154     public IFileMatchAdapter getFileMatchAdapter() {
155         return this;
156     }
157     
158     public IEditorMatchAdapter getEditorMatchAdapter() {
159         return this;
160     }
161
162     private Match[] collectMatches(Object JavaDoc[] elements) {
163         Match[] matches= new Match[getMatchCount()];
164         int writeIndex= 0;
165         for (int i= 0; i < elements.length; i++) {
166             Match[] perElement= getMatches(elements[i]);
167             for (int j= 0; j < perElement.length; j++) {
168                 matches[writeIndex++]= perElement[j];
169             }
170         }
171         return matches;
172     }
173 }
174
Popular Tags