001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.search;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.IProgressMonitor;
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.core.runtime.Status;
017:
018: import org.eclipse.search.ui.ISearchQuery;
019: import org.eclipse.search.ui.ISearchResult;
020: import org.eclipse.search.ui.text.Match;
021:
022: import org.eclipse.jdt.core.ITypeRoot;
023:
024: import org.eclipse.jdt.internal.corext.util.Messages;
025:
026: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
027:
028: public class OccurrencesSearchQuery implements ISearchQuery {
029:
030: private final OccurrencesSearchResult fResult;
031: private IOccurrencesFinder fFinder;
032: private final ITypeRoot fElement;
033: private final String fJobLabel;
034: private final String fSingularLabel;
035: private final String fPluralLabel;
036: private final String fName;
037:
038: public OccurrencesSearchQuery(IOccurrencesFinder finder,
039: ITypeRoot element) {
040: fFinder = finder;
041: fElement = element;
042: fJobLabel = fFinder.getJobLabel();
043: fResult = new OccurrencesSearchResult(this );
044: fSingularLabel = fFinder.getUnformattedSingularLabel();
045: fPluralLabel = fFinder.getUnformattedPluralLabel();
046: fName = fFinder.getElementName();
047: }
048:
049: /*
050: * @see org.eclipse.search.ui.ISearchQuery#run(org.eclipse.core.runtime.IProgressMonitor)
051: */
052: public IStatus run(IProgressMonitor monitor) {
053: if (fFinder == null) {
054: return new StatusInfo(IStatus.ERROR,
055: "Query has already been running"); //$NON-NLS-1$
056: }
057: try {
058: fFinder.perform();
059: ArrayList resultingMatches = new ArrayList();
060: fFinder
061: .collectOccurrenceMatches(fElement,
062: resultingMatches);
063: if (!resultingMatches.isEmpty()) {
064: fResult.addMatches((Match[]) resultingMatches
065: .toArray(new Match[resultingMatches.size()]));
066: }
067: //Don't leak AST:
068: fFinder = null;
069: } finally {
070: monitor.done();
071: }
072: return Status.OK_STATUS;
073: }
074:
075: /*
076: * @see org.eclipse.search.ui.ISearchQuery#getLabel()
077: */
078: public String getLabel() {
079: return fJobLabel;
080: }
081:
082: public String getResultLabel(int nMatches) {
083: if (nMatches == 1) {
084: return Messages.format(fSingularLabel, new Object[] {
085: fName, fElement.getElementName() });
086: } else {
087: return Messages.format(fPluralLabel, new Object[] { fName,
088: new Integer(nMatches), fElement.getElementName() });
089: }
090: }
091:
092: /*
093: * @see org.eclipse.search.ui.ISearchQuery#canRerun()
094: */
095: public boolean canRerun() {
096: return false; // must release finder to not keep AST reference
097: }
098:
099: /*
100: * @see org.eclipse.search.ui.ISearchQuery#canRunInBackground()
101: */
102: public boolean canRunInBackground() {
103: return true;
104: }
105:
106: /*
107: * @see org.eclipse.search.ui.ISearchQuery#getSearchResult()
108: */
109: public ISearchResult getSearchResult() {
110: return fResult;
111: }
112: }
|