1 11 12 package org.eclipse.jdt.internal.ui.search; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Status; 19 20 import org.eclipse.jface.text.IDocument; 21 22 import org.eclipse.search.ui.ISearchQuery; 23 import org.eclipse.search.ui.ISearchResult; 24 import org.eclipse.search.ui.text.Match; 25 26 import org.eclipse.jdt.core.IJavaElement; 27 28 import org.eclipse.jdt.internal.corext.util.Messages; 29 30 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 31 32 33 public class OccurrencesSearchQuery implements ISearchQuery { 34 35 private final OccurrencesSearchResult fResult; 36 private IOccurrencesFinder fFinder; 37 private IDocument fDocument; 38 private final IJavaElement fElement; 39 private final String fJobLabel; 40 private final String fSingularLabel; 41 private final String fPluralLabel; 42 private final String fName; 43 44 public OccurrencesSearchQuery(IOccurrencesFinder finder, IDocument document, IJavaElement element) { 45 fFinder= finder; 46 fDocument= document; 47 fElement= element; 48 fJobLabel= fFinder.getJobLabel(); 49 fResult= new OccurrencesSearchResult(this); 50 fSingularLabel= fFinder.getUnformattedSingularLabel(); 51 fPluralLabel= fFinder.getUnformattedPluralLabel(); 52 fName= fFinder.getElementName(); 53 } 54 55 58 public IStatus run(IProgressMonitor monitor) { 59 if (fFinder == null) { 60 return new StatusInfo(IStatus.ERROR, "Query has already been running"); } 62 try { 63 fFinder.perform(); 64 ArrayList resultingMatches= new ArrayList (); 65 fFinder.collectOccurrenceMatches(fElement, fDocument, resultingMatches); 66 if (!resultingMatches.isEmpty()) { 67 fResult.addMatches((Match[]) resultingMatches.toArray(new Match[resultingMatches.size()])); 68 } 69 fFinder= null; 71 fDocument= null; 72 } finally { 73 monitor.done(); 74 } 75 return Status.OK_STATUS; 76 } 77 78 81 public String getLabel() { 82 return fJobLabel; 83 } 84 85 public String getResultLabel(int nMatches) { 86 if (nMatches == 1) { 87 return Messages.format(fSingularLabel, new Object [] { fName, fElement.getElementName() }); 88 } else { 89 return Messages.format(fPluralLabel, new Object [] { fName, new Integer (nMatches), fElement.getElementName() }); 90 } 91 } 92 93 96 public boolean canRerun() { 97 return false; } 99 100 103 public boolean canRunInBackground() { 104 return true; 105 } 106 107 110 public ISearchResult getSearchResult() { 111 return fResult; 112 } 113 } 114 | Popular Tags |