1 11 12 package org.eclipse.jdt.internal.ui.refactoring.nls.search; 13 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 ; 19 import java.util.Set ; 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 53 private NLSSearchQuery fQuery; 54 private final List fFileEntryGroups; 55 private final List fCompilationUnitGroups; 56 57 public NLSSearchResult(NLSSearchQuery query) { 58 fQuery= query; 59 fFileEntryGroups= new ArrayList (); 60 fCompilationUnitGroups= new ArrayList (); 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 74 public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) { 75 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 matches= new HashSet (); 83 collectMatches(matches, classFileEditorInput.getClassFile()); 84 return (Match[]) matches.toArray(new Match[matches.size()]); 85 } 86 return NO_MATCHES; 87 } 88 89 92 public Match[] computeContainedMatches(AbstractTextSearchResult result, IFile file) { 93 Set matches= new HashSet (); 94 for (Iterator 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 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 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 matches, IJavaElement element) { 123 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 } 140 } 141 } 142 143 146 public IFile getFile(Object 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 } 162 if (resource instanceof IFile) 163 return (IFile) resource; 164 else 165 return null; 166 } 167 } 168 169 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 206 public String getLabel() { 207 return fQuery.getResultLabel(getMatchCount()); 208 } 209 210 213 public String getTooltip() { 214 return getLabel(); 215 } 216 217 220 public ImageDescriptor getImageDescriptor() { 221 return JavaPluginImages.DESC_OBJS_SEARCH_REF; 222 } 223 224 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 |