KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > text > FileSearchQuery


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 package org.eclipse.search.internal.ui.text;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.regex.Pattern JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22
23 import org.eclipse.search.ui.ISearchQuery;
24 import org.eclipse.search.ui.ISearchResult;
25 import org.eclipse.search.ui.text.AbstractTextSearchResult;
26 import org.eclipse.search.ui.text.FileTextSearchScope;
27 import org.eclipse.search.ui.text.Match;
28
29 import org.eclipse.search.core.text.TextSearchEngine;
30 import org.eclipse.search.core.text.TextSearchMatchAccess;
31 import org.eclipse.search.core.text.TextSearchRequestor;
32
33 import org.eclipse.search.internal.core.text.PatternConstructor;
34 import org.eclipse.search.internal.ui.Messages;
35 import org.eclipse.search.internal.ui.SearchMessages;
36
37
38 public class FileSearchQuery implements ISearchQuery {
39     
40     private final static class TextSearchResultCollector extends TextSearchRequestor {
41         
42         private final AbstractTextSearchResult fResult;
43         private final boolean fIsFileSearchOnly;
44         private final boolean fSearchInBinaries;
45         private ArrayList JavaDoc fCachedMatches;
46         
47         private TextSearchResultCollector(AbstractTextSearchResult result, boolean isFileSearchOnly, boolean searchInBinaries) {
48             fResult= result;
49             fIsFileSearchOnly= isFileSearchOnly;
50             fSearchInBinaries= searchInBinaries;
51             
52         }
53         
54         public boolean acceptFile(IFile file) throws CoreException {
55             if (fIsFileSearchOnly) {
56                 fResult.addMatch(new FileMatch(file, 0, 0));
57             }
58             flushMatches();
59             return true;
60         }
61         
62         /* (non-Javadoc)
63          * @see org.eclipse.search.core.text.TextSearchRequestor#reportBinaryFile(org.eclipse.core.resources.IFile)
64          */

65         public boolean reportBinaryFile(IFile file) {
66             return fSearchInBinaries;
67         }
68
69         public boolean acceptPatternMatch(TextSearchMatchAccess matchRequestor) throws CoreException {
70             fCachedMatches.add(new FileMatch(matchRequestor.getFile(), matchRequestor.getMatchOffset(), matchRequestor.getMatchLength()));
71             return true;
72         }
73
74         public void beginReporting() {
75             fCachedMatches= new ArrayList JavaDoc();
76         }
77         
78         public void endReporting() {
79             flushMatches();
80             fCachedMatches= null;
81         }
82
83         private void flushMatches() {
84             if (!fCachedMatches.isEmpty()) {
85                 fResult.addMatches((Match[]) fCachedMatches.toArray(new Match[fCachedMatches.size()]));
86                 fCachedMatches.clear();
87             }
88         }
89     }
90     
91     private final FileTextSearchScope fScope;
92     private final String JavaDoc fSearchText;
93     private final boolean fIsRegEx;
94     private final boolean fIsCaseSensitive;
95     
96     private FileSearchResult fResult;
97     
98     public FileSearchQuery(String JavaDoc searchText, boolean isRegEx, boolean isCaseSensitive, FileTextSearchScope scope) {
99         fSearchText= searchText;
100         fIsRegEx= isRegEx;
101         fIsCaseSensitive= isCaseSensitive;
102         fScope= scope;
103     }
104     
105     public FileTextSearchScope getSearchScope() {
106         return fScope;
107     }
108     
109     public boolean canRunInBackground() {
110         return true;
111     }
112
113     public IStatus run(final IProgressMonitor monitor) {
114         AbstractTextSearchResult textResult= (AbstractTextSearchResult) getSearchResult();
115         textResult.removeAll();
116         
117         Pattern JavaDoc searchPattern= getSearchPattern();
118         boolean isFileSearchOnly= searchPattern.pattern().length() == 0;
119         boolean searchInBinaries= !isScopeAllFileTypes();
120         
121         TextSearchResultCollector collector= new TextSearchResultCollector(textResult, isFileSearchOnly, searchInBinaries);
122         return TextSearchEngine.create().search(fScope, collector, searchPattern, monitor);
123     }
124     
125     private boolean isScopeAllFileTypes() {
126         String JavaDoc[] fileNamePatterns= fScope.getFileNamePatterns();
127         if (fileNamePatterns == null)
128             return true;
129         for (int i= 0; i < fileNamePatterns.length; i++) {
130             if ("*".equals(fileNamePatterns[i])) { //$NON-NLS-1$
131
return true;
132             }
133         }
134         return false;
135     }
136     
137
138     public String JavaDoc getLabel() {
139         return SearchMessages.FileSearchQuery_label;
140     }
141     
142     public String JavaDoc getSearchString() {
143         return fSearchText;
144     }
145     
146     public String JavaDoc getResultLabel(int nMatches) {
147         String JavaDoc searchString= getSearchString();
148         if (searchString.length() > 0) {
149             // text search
150
if (isScopeAllFileTypes()) {
151                 // search all file extensions
152
if (nMatches == 1) {
153                     Object JavaDoc[] args= { searchString, fScope.getDescription() };
154                     return Messages.format(SearchMessages.FileSearchQuery_singularLabel, args);
155                 }
156                 Object JavaDoc[] args= { searchString, new Integer JavaDoc(nMatches), fScope.getDescription() };
157                 return Messages.format(SearchMessages.FileSearchQuery_pluralPattern, args);
158             }
159             // search selected file extensions
160
if (nMatches == 1) {
161                 Object JavaDoc[] args= { searchString, fScope.getDescription(), fScope.getFilterDescription() };
162                 return Messages.format(SearchMessages.FileSearchQuery_singularPatternWithFileExt, args);
163             }
164             Object JavaDoc[] args= { searchString, new Integer JavaDoc(nMatches), fScope.getDescription(), fScope.getFilterDescription() };
165             return Messages.format(SearchMessages.FileSearchQuery_pluralPatternWithFileExt, args);
166         }
167         // file search
168
if (nMatches == 1) {
169             Object JavaDoc[] args= { fScope.getFilterDescription(), fScope.getDescription() };
170             return Messages.format(SearchMessages.FileSearchQuery_singularLabel_fileNameSearch, args);
171         }
172         Object JavaDoc[] args= { fScope.getFilterDescription(), new Integer JavaDoc(nMatches), fScope.getDescription() };
173         return Messages.format(SearchMessages.FileSearchQuery_pluralPattern_fileNameSearch, args);
174     }
175
176     /**
177      * @param result all result are added to this search result
178      * @param monitor the progress monitor to use
179      * @param file the file to search in
180      * @return returns the status of the operation
181      */

182     public IStatus searchInFile(final AbstractTextSearchResult result, final IProgressMonitor monitor, IFile file) {
183         FileTextSearchScope scope= FileTextSearchScope.newSearchScope(new IResource[] { file }, new String JavaDoc[] { "*" }, true); //$NON-NLS-1$
184

185         Pattern JavaDoc searchPattern= getSearchPattern();
186         boolean isFileSearchOnly= searchPattern.pattern().length() == 0;
187         TextSearchResultCollector collector= new TextSearchResultCollector(result, isFileSearchOnly, true);
188         
189         return TextSearchEngine.create().search(scope, collector, searchPattern, monitor);
190     }
191     
192     protected Pattern JavaDoc getSearchPattern() {
193         return PatternConstructor.createPattern(fSearchText, fIsCaseSensitive, fIsRegEx);
194     }
195     
196     public boolean isRegexSearch() {
197         return fIsRegEx;
198     }
199     
200     public boolean isCaseSensitive() {
201         return fIsCaseSensitive;
202     }
203
204     public boolean canRerun() {
205         return true;
206     }
207
208     public ISearchResult getSearchResult() {
209         if (fResult == null) {
210             fResult= new FileSearchResult(this);
211             new SearchResultUpdater(fResult);
212         }
213         return fResult;
214     }
215 }
216
Popular Tags