KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.internal.ui.search;
12
13 import org.eclipse.core.resources.IWorkspace;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IMethod;
18 import org.eclipse.jdt.core.search.IJavaSearchConstants;
19 import org.eclipse.jdt.core.search.IJavaSearchScope;
20 import org.eclipse.jdt.core.search.SearchEngine;
21 import org.eclipse.jdt.internal.ui.JavaPluginImages;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.ui.actions.WorkspaceModifyOperation;
24
25 public class JavaSearchOperation extends WorkspaceModifyOperation {
26     
27     private IWorkspace fWorkspace;
28     private IJavaElement fElementPattern;
29     private int fLimitTo;
30     private String JavaDoc fStringPattern;
31     private boolean fIsCaseSensitive;
32     private int fSearchFor;
33     private IJavaSearchScope fScope;
34     private String JavaDoc fScopeDescription;
35     private JavaSearchResultCollector fCollector;
36     
37     protected JavaSearchOperation(
38                 IWorkspace workspace,
39                 int limitTo,
40                 IJavaSearchScope scope,
41                 String JavaDoc scopeDescription,
42                 JavaSearchResultCollector collector) {
43         super(null);
44         fWorkspace= workspace;
45         fLimitTo= limitTo;
46         fScope= scope;
47         fScopeDescription= scopeDescription;
48         fCollector= collector;
49         fCollector.setOperation(this);
50     }
51     
52     public JavaSearchOperation(
53                 IWorkspace workspace,
54                 IJavaElement pattern,
55                 int limitTo,
56                 IJavaSearchScope scope,
57                 String JavaDoc scopeDescription,
58                 JavaSearchResultCollector collector) {
59         this(workspace, limitTo, scope, scopeDescription, collector);
60         fElementPattern= pattern;
61     }
62     
63     public JavaSearchOperation(
64                 IWorkspace workspace,
65                 String JavaDoc pattern,
66                 boolean caseSensitive,
67                 int searchFor,
68                 int limitTo,
69                 IJavaSearchScope scope,
70                 String JavaDoc scopeDescription,
71                 JavaSearchResultCollector collector) {
72         this(workspace, limitTo, scope, scopeDescription, collector);
73         fStringPattern= pattern;
74         fIsCaseSensitive= caseSensitive;
75         fSearchFor= searchFor;
76     }
77     
78     protected void execute(IProgressMonitor monitor) throws CoreException {
79         fCollector.setProgressMonitor(monitor);
80         
81         // Also search working copies
82
SearchEngine engine;
83         if (true) {
84             engine= new SearchEngine();
85         }
86         
87         if (fElementPattern != null)
88             engine.search(fWorkspace, fElementPattern, fLimitTo, fScope, fCollector);
89         else
90             engine.search(fWorkspace, SearchEngine.createSearchPattern(fStringPattern, fSearchFor, fLimitTo, fIsCaseSensitive), fScope, fCollector);
91     }
92
93     String JavaDoc getSingularLabel() {
94         String JavaDoc desc= null;
95         if (fElementPattern != null) {
96             if (fLimitTo == IJavaSearchConstants.REFERENCES
97             && fElementPattern.getElementType() == IJavaElement.METHOD)
98                 desc= PrettySignature.getUnqualifiedMethodSignature((IMethod)fElementPattern);
99             else
100                 desc= fElementPattern.getElementName();
101             if ("".equals(desc) && fElementPattern.getElementType() == IJavaElement.PACKAGE_FRAGMENT) //$NON-NLS-1$
102
desc= SearchMessages.getString("JavaSearchOperation.default_package"); //$NON-NLS-1$
103
}
104         else
105             desc= fStringPattern;
106
107         String JavaDoc[] args= new String JavaDoc[] {desc, fScopeDescription}; //$NON-NLS-1$
108
switch (fLimitTo) {
109             case IJavaSearchConstants.IMPLEMENTORS:
110                 return SearchMessages.getFormattedString("JavaSearchOperation.singularImplementorsPostfix", args); //$NON-NLS-1$
111
case IJavaSearchConstants.DECLARATIONS:
112                 return SearchMessages.getFormattedString("JavaSearchOperation.singularDeclarationsPostfix", args); //$NON-NLS-1$
113
case IJavaSearchConstants.REFERENCES:
114                 return SearchMessages.getFormattedString("JavaSearchOperation.singularReferencesPostfix", args); //$NON-NLS-1$
115
case IJavaSearchConstants.ALL_OCCURRENCES:
116                 return SearchMessages.getFormattedString("JavaSearchOperation.singularOccurrencesPostfix", args); //$NON-NLS-1$
117
case IJavaSearchConstants.READ_ACCESSES:
118                 return SearchMessages.getFormattedString("JavaSearchOperation.singularReadReferencesPostfix", args); //$NON-NLS-1$
119
case IJavaSearchConstants.WRITE_ACCESSES:
120                 return SearchMessages.getFormattedString("JavaSearchOperation.singularWriteReferencesPostfix", args); //$NON-NLS-1$
121
default:
122                 return SearchMessages.getFormattedString("JavaSearchOperation.singularOccurrencesPostfix", args); //$NON-NLS-1$;
123
}
124     }
125
126     String JavaDoc getPluralLabelPattern() {
127         String JavaDoc desc= null;
128         if (fElementPattern != null) {
129             if (fLimitTo == IJavaSearchConstants.REFERENCES
130             && fElementPattern.getElementType() == IJavaElement.METHOD)
131                 desc= PrettySignature.getUnqualifiedMethodSignature((IMethod)fElementPattern);
132             else
133                 desc= fElementPattern.getElementName();
134             if ("".equals(desc) && fElementPattern.getElementType() == IJavaElement.PACKAGE_FRAGMENT) //$NON-NLS-1$
135
desc= SearchMessages.getString("JavaSearchOperation.default_package"); //$NON-NLS-1$
136
}
137         else
138             desc= fStringPattern;
139
140         String JavaDoc[] args= new String JavaDoc[] {desc, "{0}", fScopeDescription}; //$NON-NLS-1$
141
switch (fLimitTo) {
142             case IJavaSearchConstants.IMPLEMENTORS:
143                 return SearchMessages.getFormattedString("JavaSearchOperation.pluralImplementorsPostfix", args); //$NON-NLS-1$
144
case IJavaSearchConstants.DECLARATIONS:
145                 return SearchMessages.getFormattedString("JavaSearchOperation.pluralDeclarationsPostfix", args); //$NON-NLS-1$
146
case IJavaSearchConstants.REFERENCES:
147                 return SearchMessages.getFormattedString("JavaSearchOperation.pluralReferencesPostfix", args); //$NON-NLS-1$
148
case IJavaSearchConstants.ALL_OCCURRENCES:
149                 return SearchMessages.getFormattedString("JavaSearchOperation.pluralOccurrencesPostfix", args); //$NON-NLS-1$
150
case IJavaSearchConstants.READ_ACCESSES:
151                 return SearchMessages.getFormattedString("JavaSearchOperation.pluralReadReferencesPostfix", args); //$NON-NLS-1$
152
case IJavaSearchConstants.WRITE_ACCESSES:
153                 return SearchMessages.getFormattedString("JavaSearchOperation.pluralWriteReferencesPostfix", args); //$NON-NLS-1$
154
default:
155                 return SearchMessages.getFormattedString("JavaSearchOperation.pluralOccurrencesPostfix", args); //$NON-NLS-1$;
156
}
157     }
158     
159     ImageDescriptor getImageDescriptor() {
160         if (fLimitTo == IJavaSearchConstants.IMPLEMENTORS || fLimitTo == IJavaSearchConstants.DECLARATIONS)
161             return JavaPluginImages.DESC_OBJS_SEARCH_DECL;
162         else
163             return JavaPluginImages.DESC_OBJS_SEARCH_REF;
164     }
165 }
166
Popular Tags