1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.refactoring.nls.search;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.HashSet;
17 import java.util.Iterator;
18 import java.util.List;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IResource;
24 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.IFileEditorInput;
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;
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;
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;
46 public class NLSSearchResult extends AbstractTextSearchResult implements IEditorMatchAdapter, IFileMatchAdapter {
48 private static final Match[] NO_MATCHES= new Match[0];
51 * Element (group key) is always IJavaElement or FileEntry.
53 private NLSSearchQuery fQuery;
54 private final List<FileEntry> fFileEntryGroups;
55 private final List<CompilationUnitEntry> fCompilationUnitGroups;
57 public NLSSearchResult(NLSSearchQuery query) {
59 fFileEntryGroups= new ArrayList<FileEntry>();
60 fCompilationUnitGroups= new ArrayList<CompilationUnitEntry>();
63 public void addFileEntryGroup(FileEntry group) {
64 fFileEntryGroups.add(group);
67 public void addCompilationUnitGroup(CompilationUnitEntry group) {
68 fCompilationUnitGroups.add(group);
72 * @see org.eclipse.search.ui.text.AbstractTextSearchResult#findContainedMatches(org.eclipse.ui.IEditorPart)
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<Match> matches= new HashSet<Match>();
83 collectMatches(matches, classFileEditorInput.getClassFile());
84 return matches.toArray(new Match[matches.size()]);
90 * @see org.eclipse.search.ui.text.AbstractTextSearchResult#findContainedMatches(org.eclipse.core.resources.IFile)
92 public Match[] computeContainedMatches(AbstractTextSearchResult result, IFile file) {
93 Set<Match> matches= new HashSet<Match>();
94 for (Iterator<FileEntry> iter= fFileEntryGroups.iterator(); iter.hasNext();) {
95 FileEntry element= iter.next();
96 if (element.getPropertiesFile().equals(file)) {
97 matches.addAll(Arrays.asList(getMatches(element)));
100 if (matches.size() > 0)
101 return matches.toArray(new Match[matches.size()]);
104 for (Iterator<CompilationUnitEntry> iter= fCompilationUnitGroups.iterator(); iter.hasNext();) {
105 CompilationUnitEntry element= iter.next();
106 ICompilationUnit cu= element.getCompilationUnit();
107 if (cu.exists() && file.equals(cu.getCorrespondingResource())) {
108 matches.addAll(Arrays.asList(getMatches(element)));
111 } catch (JavaModelException e) {
116 //TODO: copied from JavaSearchResult:
117 IJavaElement javaElement= JavaCore.create(file);
118 collectMatches(matches, javaElement);
119 return matches.toArray(new Match[matches.size()]);
122 private void collectMatches(Set<Match> matches, IJavaElement element) {
123 //TODO: copied from JavaSearchResult:
124 Match[] m= getMatches(element);
126 for (int i= 0; i < m.length; i++) {
130 if (element instanceof IParent) {
131 IParent parent= (IParent) element;
133 IJavaElement[] children= parent.getChildren();
134 for (int i= 0; i < children.length; i++) {
135 collectMatches(matches, children[i]);
137 } catch (JavaModelException e) {
138 // we will not be tracking these results
144 * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getFile(java.lang.Object)
146 public IFile getFile(Object element) {
147 if (element instanceof FileEntry) {
148 return ((FileEntry) element).getPropertiesFile();
150 IJavaElement javaElement= null;
151 if (element instanceof CompilationUnitEntry) {
152 javaElement= ((CompilationUnitEntry)element).getCompilationUnit();
154 javaElement= (IJavaElement) element;
156 IResource resource= null;
158 resource= javaElement.getCorrespondingResource();
159 } catch (JavaModelException e) {
162 if (resource instanceof IFile)
163 return (IFile) resource;
170 * @see org.eclipse.search.ui.text.AbstractTextSearchResult#isShownInEditor(org.eclipse.search.ui.text.Match, org.eclipse.ui.IEditorPart)
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);
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();
184 je= ((CompilationUnitEntry)match.getElement()).getCompilationUnit();
186 if (editorInput instanceof IFileEditorInput) {
188 ICompilationUnit cu= (ICompilationUnit) je.getAncestor(IJavaElement.COMPILATION_UNIT);
192 return ((IFileEditorInput) editorInput).getFile().equals(cu.getCorrespondingResource());
193 } catch (JavaModelException e) {
196 } else if (editorInput instanceof IClassFileEditorInput) {
197 return ((IClassFileEditorInput) editorInput).getClassFile().equals(je.getAncestor(IJavaElement.CLASS_FILE));
204 * @see org.eclipse.search.ui.ISearchResult#getLabel()
206 public String getLabel() {
207 return fQuery.getResultLabel(getMatchCount());
211 * @see org.eclipse.search.ui.ISearchResult#getTooltip()
213 public String getTooltip() {
218 * @see org.eclipse.search.ui.ISearchResult#getImageDescriptor()
220 public ImageDescriptor getImageDescriptor() {
221 return JavaPluginImages.DESC_OBJS_SEARCH_REF;
225 * @see org.eclipse.search.ui.ISearchResult#getQuery()
227 public ISearchQuery getQuery() {
232 public IFileMatchAdapter getFileMatchAdapter() {
237 public IEditorMatchAdapter getEditorMatchAdapter() {