KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > OccurrencesSearchQuery


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.search;
13
14 import java.util.ArrayList JavaDoc;
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 JavaDoc fJobLabel;
40     private final String JavaDoc fSingularLabel;
41     private final String JavaDoc fPluralLabel;
42     private final String JavaDoc 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     /*
56      * @see org.eclipse.search.ui.ISearchQuery#run(org.eclipse.core.runtime.IProgressMonitor)
57      */

58     public IStatus run(IProgressMonitor monitor) {
59         if (fFinder == null) {
60             return new StatusInfo(IStatus.ERROR, "Query has already been running"); //$NON-NLS-1$
61
}
62         try {
63             fFinder.perform();
64             ArrayList JavaDoc resultingMatches= new ArrayList JavaDoc();
65             fFinder.collectOccurrenceMatches(fElement, fDocument, resultingMatches);
66             if (!resultingMatches.isEmpty()) {
67                 fResult.addMatches((Match[]) resultingMatches.toArray(new Match[resultingMatches.size()]));
68             }
69             //Don't leak AST:
70
fFinder= null;
71             fDocument= null;
72         } finally {
73             monitor.done();
74         }
75         return Status.OK_STATUS;
76     }
77     
78     /*
79      * @see org.eclipse.search.ui.ISearchQuery#getLabel()
80      */

81     public String JavaDoc getLabel() {
82         return fJobLabel;
83     }
84     
85     public String JavaDoc getResultLabel(int nMatches) {
86         if (nMatches == 1) {
87             return Messages.format(fSingularLabel, new Object JavaDoc[] { fName, fElement.getElementName() });
88         } else {
89             return Messages.format(fPluralLabel, new Object JavaDoc[] { fName, new Integer JavaDoc(nMatches), fElement.getElementName() });
90         }
91     }
92         
93     /*
94      * @see org.eclipse.search.ui.ISearchQuery#canRerun()
95      */

96     public boolean canRerun() {
97         return false; // must release finder to not keep AST reference
98
}
99
100     /*
101      * @see org.eclipse.search.ui.ISearchQuery#canRunInBackground()
102      */

103     public boolean canRunInBackground() {
104         return true;
105     }
106
107     /*
108      * @see org.eclipse.search.ui.ISearchQuery#getSearchResult()
109      */

110     public ISearchResult getSearchResult() {
111         return fResult;
112     }
113 }
114
Popular Tags