KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.search.internal.ui.text;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IWorkspace;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.util.Assert;
20
21 import org.eclipse.ui.actions.WorkspaceModifyOperation;
22
23 import org.eclipse.search.internal.core.ISearchScope;
24 import org.eclipse.search.internal.core.text.ITextSearchResultCollector;
25 import org.eclipse.search.internal.core.text.MatchLocator;
26 import org.eclipse.search.internal.core.text.TextSearchEngine;
27 import org.eclipse.search.internal.core.text.TextSearchScope;
28 import org.eclipse.search.internal.ui.SearchMessages;
29 import org.eclipse.search.internal.ui.SearchPluginImages;
30
31 /**
32  * An operation to perform a regular text search.
33  */

34 public class TextSearchOperation extends WorkspaceModifyOperation {
35
36     public static final int NO_PRIORITY_CHANGE= -1;
37     
38     private IWorkspace fWorkspace;
39     private MatchLocator fMatchLocator;
40     private ISearchScope fScope;
41     private TextSearchResultCollector fCollector;
42     private IStatus fStatus;
43     
44     /**
45      * Creates a new text search operation.
46      */

47     public TextSearchOperation(IWorkspace workspace, String JavaDoc pattern, boolean isCaseSensitive, boolean isRegexSearch,
48         ISearchScope scope, TextSearchResultCollector collector) {
49         super(null);
50         Assert.isNotNull(collector);
51         fWorkspace= workspace;
52         fMatchLocator= new MatchLocator(pattern, isCaseSensitive, isRegexSearch);
53         fScope= scope;
54         fCollector= collector;
55         fCollector.setOperation(this);
56     }
57     
58     /**
59      * The actual algorithm.
60      */

61     protected void execute(IProgressMonitor monitor) {
62         fCollector.setProgressMonitor(monitor);
63         TextSearchEngine engine= new TextSearchEngine();
64         fStatus= engine.search(fWorkspace, fScope, false, fCollector, fMatchLocator);
65     }
66     
67     void searchInFile(IFile file, ITextSearchResultCollector collector) {
68         TextSearchEngine engine= new TextSearchEngine();
69         TextSearchScope scope= new TextSearchScope(""); //$NON-NLS-1$
70
scope.add(file);
71         scope.addExtension("*"); //$NON-NLS-1$
72
fStatus= engine.search(fWorkspace, scope, false, collector, fMatchLocator);
73     }
74
75     String JavaDoc getSingularLabel() {
76         String JavaDoc pattern= fMatchLocator.getPattern();
77         if (pattern == null || pattern.length() < 1)
78             return SearchMessages.getFormattedString("FileSearchOperation.singularLabelPostfix", new String JavaDoc[] {fScope.getDescription()}); //$NON-NLS-1$
79
else
80             return SearchMessages.getFormattedString("TextSearchOperation.singularLabelPostfix", new String JavaDoc[] {fMatchLocator.getPattern(), fScope.getDescription()}); //$NON-NLS-1$
81
}
82
83     String JavaDoc getPluralLabelPattern() {
84         String JavaDoc pattern= fMatchLocator.getPattern();
85         if (pattern == null || pattern.length() < 1)
86             return SearchMessages.getFormattedString("FileSearchOperation.pluralLabelPatternPostfix", new String JavaDoc[] {"{0}", fScope.getDescription()}); //$NON-NLS-2$ //$NON-NLS-1$
87
else
88             return SearchMessages.getFormattedString("TextSearchOperation.pluralLabelPatternPostfix", new String JavaDoc[] {fMatchLocator.getPattern(), "{0}", fScope.getDescription()}); //$NON-NLS-2$ //$NON-NLS-1$
89
}
90     
91     ImageDescriptor getImageDescriptor() {
92         return SearchPluginImages.DESC_OBJ_TSEARCH_DPDN;
93     }
94     
95     IStatus getStatus() {
96         return fStatus;
97     }
98     
99     String JavaDoc getPattern() {
100         return fMatchLocator.getPattern();
101     }
102 }
103
Popular Tags