1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 
15 package org.eclipse.jdt.internal.ui.search;
16 
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IResource;
19 
20 import org.eclipse.jface.resource.ImageDescriptor;
21 
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IFileEditorInput;
25 
26 import org.eclipse.search.ui.ISearchQuery;
27 import org.eclipse.search.ui.text.AbstractTextSearchResult;
28 import org.eclipse.search.ui.text.IEditorMatchAdapter;
29 import org.eclipse.search.ui.text.IFileMatchAdapter;
30 import org.eclipse.search.ui.text.Match;
31 
32 import org.eclipse.jdt.core.IClassFile;
33 import org.eclipse.jdt.core.IJavaElement;
34 import org.eclipse.jdt.core.JavaModelException;
35 
36 import org.eclipse.jdt.internal.ui.JavaPluginImages;
37 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
38 
39 
40 public class OccurrencesSearchResult extends AbstractTextSearchResult implements IEditorMatchAdapter, IFileMatchAdapter {
41 
42 	protected static final Match[] NO_MATCHES= new Match[0];
43 	private OccurrencesSearchQuery fQuery;
44 
OccurrencesSearchResult(OccurrencesSearchQuery query)45 	public OccurrencesSearchResult(OccurrencesSearchQuery query) {
46 		fQuery= query;
47 	}
48 
49 	/*
50 	 * @see org.eclipse.search.ui.text.AbstractTextSearchResult#findContainedMatches(org.eclipse.core.resources.IFile)
51 	 */
52 	@Override
computeContainedMatches(AbstractTextSearchResult result, IFile file)53 	public Match[] computeContainedMatches(AbstractTextSearchResult result, IFile file) {
54 		Object[] elements= getElements();
55 		if (elements.length == 0)
56 			return NO_MATCHES;
57 		//all matches from same file:
58 		JavaElementLine jel= (JavaElementLine) elements[0];
59 		if (file.equals(jel.getJavaElement().getResource()))
60 			return collectMatches(elements);
61 		return NO_MATCHES;
62 	}
63 
64 	/*
65 	 * @see org.eclipse.search.ui.text.AbstractTextSearchResult#findContainedMatches(org.eclipse.ui.IEditorPart)
66 	 */
67 	@Override
computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor)68 	public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
69 		//TODO same code in JavaSearchResult
70 		IEditorInput editorInput= editor.getEditorInput();
71 		if (editorInput instanceof IFileEditorInput)  {
72 			IFileEditorInput fileEditorInput= (IFileEditorInput) editorInput;
73 			return computeContainedMatches(result, fileEditorInput.getFile());
74 
75 		} else if (editorInput instanceof IClassFileEditorInput) {
76 			IClassFileEditorInput classFileEditorInput= (IClassFileEditorInput) editorInput;
77 			IClassFile classFile= classFileEditorInput.getClassFile();
78 
79 			Object[] elements= getElements();
80 			if (elements.length == 0)
81 				return NO_MATCHES;
82 			//all matches from same file:
83 			JavaElementLine jel= (JavaElementLine) elements[0];
84 			if (jel.getJavaElement().equals(classFile))
85 				return collectMatches(elements);
86 		}
87 		return NO_MATCHES;
88 	}
89 
90 	/*
91 	 * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getFile(java.lang.Object)
92 	 */
93 	@Override
getFile(Object element)94 	public IFile getFile(Object element) {
95 		JavaElementLine jel= (JavaElementLine) element;
96 		IResource resource= null;
97 		try {
98 			resource= jel.getJavaElement().getCorrespondingResource();
99 		} catch (JavaModelException e) {
100 			// no resource
101 		}
102 		if (resource instanceof IFile)
103 			return (IFile) resource;
104 		else
105 			return null;
106 	}
107 
108 	/*
109 	 * @see org.eclipse.search.ui.text.AbstractTextSearchResult#isShownInEditor(org.eclipse.search.ui.text.Match, org.eclipse.ui.IEditorPart)
110 	 */
111 	@Override
isShownInEditor(Match match, IEditorPart editor)112 	public boolean isShownInEditor(Match match, IEditorPart editor) {
113 		Object element= match.getElement();
114 		IJavaElement je= ((JavaElementLine) element).getJavaElement();
115 		IEditorInput editorInput= editor.getEditorInput();
116 		if (editorInput instanceof IFileEditorInput) {
117 			try {
118 				return ((IFileEditorInput)editorInput).getFile().equals(je.getCorrespondingResource());
119 			} catch (JavaModelException e) {
120 				return false;
121 			}
122 		} else if (editorInput instanceof IClassFileEditorInput) {
123 			return ((IClassFileEditorInput)editorInput).getClassFile().equals(je);
124 		}
125 
126 		return false;
127 	}
128 
129 	/*
130 	 * @see org.eclipse.search.ui.ISearchResult#getLabel()
131 	 */
132 	@Override
getLabel()133 	public String getLabel() {
134 		return fQuery.getResultLabel(getMatchCount());
135 	}
136 
137 	/*
138 	 * @see org.eclipse.search.ui.ISearchResult#getTooltip()
139 	 */
140 	@Override
getTooltip()141 	public String getTooltip() {
142 		return getLabel();
143 	}
144 
145 	/*
146 	 * @see org.eclipse.search.ui.ISearchResult#getImageDescriptor()
147 	 */
148 	@Override
getImageDescriptor()149 	public ImageDescriptor getImageDescriptor() {
150 		return JavaPluginImages.DESC_OBJS_SEARCH_REF;
151 	}
152 
153 	/*
154 	 * @see org.eclipse.search.ui.ISearchResult#getQuery()
155 	 */
156 	@Override
getQuery()157 	public ISearchQuery getQuery() {
158 		return fQuery;
159 	}
160 
161 	@Override
getFileMatchAdapter()162 	public IFileMatchAdapter getFileMatchAdapter() {
163 		return this;
164 	}
165 
166 	@Override
getEditorMatchAdapter()167 	public IEditorMatchAdapter getEditorMatchAdapter() {
168 		return this;
169 	}
170 
collectMatches(Object[] elements)171 	private Match[] collectMatches(Object[] elements) {
172 		Match[] matches= new Match[getMatchCount()];
173 		int writeIndex= 0;
174 		for (Object element : elements) {
175 			for (Match perElement : getMatches(element)) {
176 				matches[writeIndex++]= perElement;
177 			}
178 		}
179 		return matches;
180 	}
181 }
182