1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 22 import org.eclipse.core.resources.IFile; 23 24 import org.eclipse.jface.resource.ImageDescriptor; 25 26 import org.eclipse.ui.IEditorPart; 27 28 import org.eclipse.search.ui.ISearchQuery; 29 import org.eclipse.search.ui.text.AbstractTextSearchResult; 30 import org.eclipse.search.ui.text.IEditorMatchAdapter; 31 import org.eclipse.search.ui.text.IFileMatchAdapter; 32 import org.eclipse.search.ui.text.Match; 33 import org.eclipse.search.ui.text.MatchFilter; 34 35 import org.eclipse.jdt.core.IClassFile; 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.JavaModelException; 40 41 import org.eclipse.jdt.ui.search.IMatchPresentation; 42 43 import org.eclipse.jdt.internal.ui.JavaPlugin; 44 45 public class JavaSearchResult extends AbstractTextSearchResult implements IEditorMatchAdapter, IFileMatchAdapter { 46 47 private static final Match[] NO_MATCHES= new Match[0]; 48 49 private final JavaSearchQuery fQuery; 50 private final Map fElementsToParticipants; 51 52 public JavaSearchResult(JavaSearchQuery query) { 53 fQuery= query; 54 fElementsToParticipants= new HashMap (); 55 setActiveMatchFilters(JavaMatchFilter.getLastUsedFilters()); 56 } 57 58 public ImageDescriptor getImageDescriptor() { 59 return fQuery.getImageDescriptor(); 60 } 61 62 public String getLabel() { 63 return fQuery.getResultLabel(getMatchCount()); 64 } 65 66 public String getTooltip() { 67 return getLabel(); 68 } 69 70 public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) { 71 return computeContainedMatches(editor.getEditorInput()); 72 } 73 74 77 public void setActiveMatchFilters(MatchFilter[] filters) { 78 super.setActiveMatchFilters(filters); 79 JavaMatchFilter.setLastUsedFilters(filters); 80 } 81 82 85 public MatchFilter[] getAllMatchFilters() { 86 return JavaMatchFilter.allFilters(fQuery); 87 } 88 89 public Match[] computeContainedMatches(AbstractTextSearchResult result, IFile file) { 90 return computeContainedMatches(file); 91 } 92 93 private Match[] computeContainedMatches(IAdaptable adaptable) { 94 IJavaElement javaElement= (IJavaElement) adaptable.getAdapter(IJavaElement.class); 95 Set matches= new HashSet (); 96 if (javaElement != null) { 97 collectMatches(matches, javaElement); 98 } 99 IFile file= (IFile) adaptable.getAdapter(IFile.class); 100 if (file != null) { 101 collectMatches(matches, file); 102 } 103 if (!matches.isEmpty()) { 104 return (Match[]) matches.toArray(new Match[matches.size()]); 105 } 106 return NO_MATCHES; 107 } 108 109 110 private void collectMatches(Set matches, IFile element) { 111 Match[] m= getMatches(element); 112 if (m.length != 0) { 113 for (int i= 0; i < m.length; i++) { 114 matches.add(m[i]); 115 } 116 } 117 } 118 119 private void collectMatches(Set matches, IJavaElement element) { 120 Match[] m= getMatches(element); 121 if (m.length != 0) { 122 for (int i= 0; i < m.length; i++) { 123 matches.add(m[i]); 124 } 125 } 126 if (element instanceof IParent) { 127 IParent parent= (IParent) element; 128 try { 129 IJavaElement[] children= parent.getChildren(); 130 for (int i= 0; i < children.length; i++) { 131 collectMatches(matches, children[i]); 132 } 133 } catch (JavaModelException e) { 134 } 136 } 137 } 138 141 public IFile getFile(Object element) { 142 if (element instanceof IJavaElement) { 143 IJavaElement javaElement= (IJavaElement) element; 144 ICompilationUnit cu= (ICompilationUnit) javaElement.getAncestor(IJavaElement.COMPILATION_UNIT); 145 if (cu != null) { 146 return (IFile) cu.getResource(); 147 } else { 148 IClassFile cf= (IClassFile) javaElement.getAncestor(IJavaElement.CLASS_FILE); 149 if (cf != null) 150 return (IFile) cf.getResource(); 151 } 152 return null; 153 } 154 if (element instanceof IFile) 155 return (IFile) element; 156 return null; 157 } 158 159 165 public boolean isShownInEditor(Match match, IEditorPart editor) { 166 Object element= match.getElement(); 167 if (element instanceof IJavaElement) { 168 element= ((IJavaElement) element).getOpenable(); return element != null && element.equals(editor.getEditorInput().getAdapter(IJavaElement.class)); 170 } else if (element instanceof IFile) { 171 return element.equals(editor.getEditorInput().getAdapter(IFile.class)); 172 } 173 return false; 174 } 175 176 179 public ISearchQuery getQuery() { 180 return fQuery; 181 } 182 183 synchronized IMatchPresentation getSearchParticpant(Object element) { 184 return (IMatchPresentation) fElementsToParticipants.get(element); 185 } 186 187 boolean addMatch(Match match, IMatchPresentation participant) { 188 Object element= match.getElement(); 189 if (fElementsToParticipants.get(element) != null) { 190 JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), 0, "A second search participant was found for an element", null)); return false; 193 } 194 fElementsToParticipants.put(element, participant); 195 addMatch(match); 196 return true; 197 } 198 199 public void removeAll() { 200 synchronized(this) { 201 fElementsToParticipants.clear(); 202 } 203 super.removeAll(); 204 } 205 206 public void removeMatch(Match match) { 207 synchronized(this) { 208 if (getMatchCount(match.getElement()) == 1) 209 fElementsToParticipants.remove(match.getElement()); 210 } 211 super.removeMatch(match); 212 } 213 public IFileMatchAdapter getFileMatchAdapter() { 214 return this; 215 } 216 217 public IEditorMatchAdapter getEditorMatchAdapter() { 218 return this; 219 } 220 221 } 222 | Popular Tags |