1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.core.runtime.ISafeRunnable; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.PerformanceStats; 18 import org.eclipse.core.runtime.SafeRunner; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.core.runtime.SubProgressMonitor; 21 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.IResource; 24 25 import org.eclipse.jface.resource.ImageDescriptor; 26 27 import org.eclipse.search.ui.ISearchQuery; 28 import org.eclipse.search.ui.ISearchResult; 29 import org.eclipse.search.ui.NewSearchUI; 30 import org.eclipse.search.ui.text.Match; 31 32 import org.eclipse.jdt.core.IJavaElement; 33 import org.eclipse.jdt.core.search.IJavaSearchConstants; 34 import org.eclipse.jdt.core.search.SearchEngine; 35 import org.eclipse.jdt.core.search.SearchParticipant; 36 import org.eclipse.jdt.core.search.SearchPattern; 37 38 import org.eclipse.jdt.internal.corext.util.Messages; 39 import org.eclipse.jdt.internal.corext.util.SearchUtils; 40 41 import org.eclipse.jdt.ui.JavaElementLabels; 42 import org.eclipse.jdt.ui.search.ElementQuerySpecification; 43 import org.eclipse.jdt.ui.search.IMatchPresentation; 44 import org.eclipse.jdt.ui.search.IQueryParticipant; 45 import org.eclipse.jdt.ui.search.ISearchRequestor; 46 import org.eclipse.jdt.ui.search.PatternQuerySpecification; 47 import org.eclipse.jdt.ui.search.QuerySpecification; 48 49 import org.eclipse.jdt.internal.ui.JavaPlugin; 50 import org.eclipse.jdt.internal.ui.JavaPluginImages; 51 52 public class JavaSearchQuery implements ISearchQuery { 53 54 private static final String PERF_SEARCH_PARTICIPANT= "org.eclipse.jdt.ui/perf/search/participants"; 56 private ISearchResult fResult; 57 private final QuerySpecification fPatternData; 58 59 public JavaSearchQuery(QuerySpecification data) { 60 if (data == null) { 61 throw new IllegalArgumentException ("data must not be null"); } 63 fPatternData= data; 64 } 65 66 private static class SearchRequestor implements ISearchRequestor { 67 private IQueryParticipant fParticipant; 68 private JavaSearchResult fSearchResult; 69 public void reportMatch(Match match) { 70 IMatchPresentation participant= fParticipant.getUIParticipant(); 71 if (participant == null || match.getElement() instanceof IJavaElement || match.getElement() instanceof IResource) { 72 fSearchResult.addMatch(match); 73 } else { 74 fSearchResult.addMatch(match, participant); 75 } 76 } 77 78 protected SearchRequestor(IQueryParticipant participant, JavaSearchResult result) { 79 super(); 80 fParticipant= participant; 81 fSearchResult= result; 82 } 83 } 84 85 public IStatus run(IProgressMonitor monitor) { 86 final JavaSearchResult textResult= (JavaSearchResult) getSearchResult(); 87 textResult.removeAll(); 88 SearchEngine engine= new SearchEngine(); 90 try { 91 92 int totalTicks= 1000; 93 IProject[] projects= JavaSearchScopeFactory.getInstance().getProjects(fPatternData.getScope()); 94 final SearchParticipantRecord[] participantDescriptors= SearchParticipantsExtensionPoint.getInstance().getSearchParticipants(projects); 95 final int[] ticks= new int[participantDescriptors.length]; 96 for (int i= 0; i < participantDescriptors.length; i++) { 97 final int iPrime= i; 98 ISafeRunnable runnable= new ISafeRunnable() { 99 public void handleException(Throwable exception) { 100 ticks[iPrime]= 0; 101 String message= SearchMessages.JavaSearchQuery_error_participant_estimate; 102 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, message, exception)); 103 } 104 105 public void run() throws Exception { 106 ticks[iPrime]= participantDescriptors[iPrime].getParticipant().estimateTicks(fPatternData); 107 } 108 }; 109 110 SafeRunner.run(runnable); 111 totalTicks+= ticks[i]; 112 } 113 114 SearchPattern pattern; 115 String stringPattern; 116 117 if (fPatternData instanceof ElementQuerySpecification) { 118 IJavaElement element= ((ElementQuerySpecification) fPatternData).getElement(); 119 stringPattern= JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT); 120 if (!element.exists()) { 121 return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(SearchMessages.JavaSearchQuery_error_element_does_not_exist, stringPattern), null); 122 } 123 pattern= SearchPattern.createPattern(element, fPatternData.getLimitTo(), SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE); 124 } else { 125 PatternQuerySpecification patternSpec = (PatternQuerySpecification) fPatternData; 126 stringPattern= patternSpec.getPattern(); 127 int matchMode= getMatchMode(stringPattern) | SearchPattern.R_ERASURE_MATCH; 128 if (patternSpec.isCaseSensitive()) 129 matchMode |= SearchPattern.R_CASE_SENSITIVE; 130 pattern= SearchPattern.createPattern(patternSpec.getPattern(), patternSpec.getSearchFor(), patternSpec.getLimitTo(), matchMode); 131 } 132 133 if (pattern == null) { 134 return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(SearchMessages.JavaSearchQuery_error_unsupported_pattern, stringPattern), null); 135 } 136 monitor.beginTask(Messages.format(SearchMessages.JavaSearchQuery_task_label, stringPattern), totalTicks); 137 IProgressMonitor mainSearchPM= new SubProgressMonitor(monitor, 1000); 138 139 boolean ignorePotentials= NewSearchUI.arePotentialMatchesIgnored(); 140 NewSearchResultCollector collector= new NewSearchResultCollector(textResult, ignorePotentials); 141 142 143 engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, fPatternData.getScope(), collector, mainSearchPM); 144 for (int i= 0; i < participantDescriptors.length; i++) { 145 final ISearchRequestor requestor= new SearchRequestor(participantDescriptors[i].getParticipant(), textResult); 146 final IProgressMonitor participantPM= new SubProgressMonitor(monitor, ticks[i]); 147 148 final int iPrime= i; 149 ISafeRunnable runnable= new ISafeRunnable() { 150 public void handleException(Throwable exception) { 151 participantDescriptors[iPrime].getDescriptor().disable(); 152 String message= SearchMessages.JavaSearchQuery_error_participant_search; 153 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, message, exception)); 154 } 155 156 public void run() throws Exception { 157 158 final IQueryParticipant participant= participantDescriptors[iPrime].getParticipant(); 159 160 final PerformanceStats stats= PerformanceStats.getStats(PERF_SEARCH_PARTICIPANT, participant); 161 stats.startRun(); 162 163 participant.search(requestor, fPatternData, participantPM); 164 165 stats.endRun(); 166 } 167 }; 168 169 SafeRunner.run(runnable); 170 } 171 172 } catch (CoreException e) { 173 return e.getStatus(); 174 } 175 String message= Messages.format(SearchMessages.JavaSearchQuery_status_ok_message, String.valueOf(textResult.getMatchCount())); 176 return new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, message, null); 177 } 178 179 private int getMatchMode(String pattern) { 180 if (pattern.indexOf('*') != -1 || pattern.indexOf('?') != -1) { 181 return SearchPattern.R_PATTERN_MATCH; 182 } else if (SearchUtils.isCamelCasePattern(pattern)) { 183 return SearchPattern.R_CAMELCASE_MATCH; 184 } 185 return SearchPattern.R_EXACT_MATCH; 186 } 187 188 public String getLabel() { 189 return SearchMessages.JavaSearchQuery_label; 190 } 191 192 public String getResultLabel(int nMatches) { 193 if (nMatches == 1) { 194 String [] args= { getSearchPatternDescription(), fPatternData.getScopeDescription() }; 195 switch (fPatternData.getLimitTo()) { 196 case IJavaSearchConstants.IMPLEMENTORS: 197 return Messages.format(SearchMessages.JavaSearchOperation_singularImplementorsPostfix, args); 198 case IJavaSearchConstants.DECLARATIONS: 199 return Messages.format(SearchMessages.JavaSearchOperation_singularDeclarationsPostfix, args); 200 case IJavaSearchConstants.REFERENCES: 201 return Messages.format(SearchMessages.JavaSearchOperation_singularReferencesPostfix, args); 202 case IJavaSearchConstants.ALL_OCCURRENCES: 203 return Messages.format(SearchMessages.JavaSearchOperation_singularOccurrencesPostfix, args); 204 case IJavaSearchConstants.READ_ACCESSES: 205 return Messages.format(SearchMessages.JavaSearchOperation_singularReadReferencesPostfix, args); 206 case IJavaSearchConstants.WRITE_ACCESSES: 207 return Messages.format(SearchMessages.JavaSearchOperation_singularWriteReferencesPostfix, args); 208 default: 209 return Messages.format(SearchMessages.JavaSearchOperation_singularOccurrencesPostfix, args); 210 } 211 } else { 212 Object [] args= { getSearchPatternDescription(), new Integer (nMatches), fPatternData.getScopeDescription() }; 213 switch (fPatternData.getLimitTo()) { 214 case IJavaSearchConstants.IMPLEMENTORS: 215 return Messages.format(SearchMessages.JavaSearchOperation_pluralImplementorsPostfix, args); 216 case IJavaSearchConstants.DECLARATIONS: 217 return Messages.format(SearchMessages.JavaSearchOperation_pluralDeclarationsPostfix, args); 218 case IJavaSearchConstants.REFERENCES: 219 return Messages.format(SearchMessages.JavaSearchOperation_pluralReferencesPostfix, args); 220 case IJavaSearchConstants.ALL_OCCURRENCES: 221 return Messages.format(SearchMessages.JavaSearchOperation_pluralOccurrencesPostfix, args); 222 case IJavaSearchConstants.READ_ACCESSES: 223 return Messages.format(SearchMessages.JavaSearchOperation_pluralReadReferencesPostfix, args); 224 case IJavaSearchConstants.WRITE_ACCESSES: 225 return Messages.format(SearchMessages.JavaSearchOperation_pluralWriteReferencesPostfix, args); 226 default: 227 return Messages.format(SearchMessages.JavaSearchOperation_pluralOccurrencesPostfix, args); 228 } 229 } 230 } 231 232 private String getSearchPatternDescription() { 233 if (fPatternData instanceof ElementQuerySpecification) { 234 IJavaElement element= ((ElementQuerySpecification) fPatternData).getElement(); 235 return JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT 236 | JavaElementLabels.ALL_FULLY_QUALIFIED | JavaElementLabels.USE_RESOLVED); 237 } 238 return ((PatternQuerySpecification) fPatternData).getPattern(); 239 } 240 241 ImageDescriptor getImageDescriptor() { 242 if (fPatternData.getLimitTo() == IJavaSearchConstants.IMPLEMENTORS || fPatternData.getLimitTo() == IJavaSearchConstants.DECLARATIONS) 243 return JavaPluginImages.DESC_OBJS_SEARCH_DECL; 244 else 245 return JavaPluginImages.DESC_OBJS_SEARCH_REF; 246 } 247 248 public boolean canRerun() { 249 return true; 250 } 251 252 public boolean canRunInBackground() { 253 return true; 254 } 255 256 public ISearchResult getSearchResult() { 257 if (fResult == null) { 258 JavaSearchResult result= new JavaSearchResult(this); 259 new SearchResultUpdater(result); 260 fResult= result; 261 } 262 return fResult; 263 } 264 265 QuerySpecification getSpecification() { 266 return fPatternData; 267 } 268 } 269 | Popular Tags |