KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > DependencyExtentSearchOperation


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.pde.internal.ui.search;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashSet JavaDoc;
18
19 import org.eclipse.core.resources.*;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.jdt.core.IClassFile;
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IPackageFragment;
26 import org.eclipse.jdt.core.IPackageFragmentRoot;
27 import org.eclipse.jdt.core.IType;
28 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.jdt.core.JavaModelException;
30 import org.eclipse.jdt.core.search.IJavaSearchConstants;
31 import org.eclipse.jdt.core.search.IJavaSearchResultCollector;
32 import org.eclipse.jdt.core.search.IJavaSearchScope;
33 import org.eclipse.jdt.core.search.SearchEngine;
34 import org.eclipse.jface.operation.*;
35 import org.eclipse.pde.core.plugin.IPluginBase;
36 import org.eclipse.pde.core.plugin.IPluginExtension;
37 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
38 import org.eclipse.pde.core.plugin.IPluginImport;
39 import org.eclipse.pde.internal.core.PDECore;
40 import org.eclipse.pde.internal.ui.PDEPlugin;
41
42
43 public class DependencyExtentSearchOperation implements IWorkspaceRunnable, IRunnableWithProgress {
44     
45     private static final String JavaDoc KEY_DEPENDENCY = "DependencyExtent.singular"; //$NON-NLS-1$
46
private static final String JavaDoc KEY_DEPENDENCIES = "DependencyExtent.plural"; //$NON-NLS-1$
47
private static final String JavaDoc KEY_SEARCHING = "DependencyExtent.searching"; //$NON-NLS-1$
48

49     IPluginImport object;
50     IProject parentProject;
51     IPluginBase[] models = new IPluginBase[0];
52     IPackageFragment[] packageFragments = new IPackageFragment[0];
53     DependencyExtentSearchResultCollector resultCollector;
54     
55     class SearchResultCollector implements IJavaSearchResultCollector {
56     
57         protected IProgressMonitor monitor;
58         HashSet JavaDoc result = new HashSet JavaDoc();
59         
60         
61         public SearchResultCollector(IProgressMonitor monitor) {
62             this.monitor = monitor;
63         }
64         
65         public void accept(
66             IResource resource,
67             int start,
68             int end,
69             IJavaElement enclosingElement,
70             int accuracy)
71             throws CoreException {
72             if (accuracy == IJavaSearchConstants.EXACT_MATCH) {
73                 result.add(enclosingElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT));
74             }
75         }
76     
77         public void aboutToStart() {
78         }
79     
80         public void done() {}
81     
82         public IProgressMonitor getProgressMonitor() {
83             return monitor;
84         }
85         
86         public IJavaElement[] getResult() {
87             return (IJavaElement[])result.toArray(new IJavaElement[result.size()]);
88         }
89         
90     }
91
92     public DependencyExtentSearchOperation(IPluginImport object) {
93         this.object = object;
94         parentProject = object.getModel().getUnderlyingResource().getProject();
95     }
96
97     public void run (IProgressMonitor monitor) {
98         resultCollector =
99             new DependencyExtentSearchResultCollector(this, monitor);
100
101         try {
102             HashSet JavaDoc set = new HashSet JavaDoc();
103             PluginJavaSearchUtil.collectAllPrerequisites(
104                 PDECore.getDefault().findPlugin(object.getId()),
105                 set);
106             models = (IPluginBase[]) set.toArray(new IPluginBase[set.size()]);
107
108             if (parentProject.hasNature(JavaCore.NATURE_ID))
109                 packageFragments = PluginJavaSearchUtil.collectPackageFragments(models,parentProject);
110
111             monitor.setTaskName(PDEPlugin.getResourceString(KEY_SEARCHING));
112             monitor.beginTask("",packageFragments.length + 1); //$NON-NLS-1$
113
resultCollector.searchStarted();
114             
115             findExtensionPoints(monitor);
116
117             if (packageFragments.length > 0)
118                 doJavaSearch(monitor);
119
120         } catch (CoreException e) {
121             PDEPlugin.log(e.getStatus());
122         
123         } finally {
124             resultCollector.done();
125         }
126     }
127
128     private void findExtensionPoints(IProgressMonitor monitor) {
129         HashSet JavaDoc ids = new HashSet JavaDoc();
130         IPluginExtension[] extensions = object.getPluginBase().getExtensions();
131         for (int i = 0; i < extensions.length; i++) {
132             if (ids.add(extensions[i].getPoint())) {
133                 IPluginExtensionPoint point =
134                     getExtensionPoint(extensions[i].getPoint());
135                 if (point != null) {
136                     resultCollector.accept(point);
137                 }
138             }
139         }
140         monitor.worked(1);
141     }
142     
143     private IPluginExtensionPoint getExtensionPoint(String JavaDoc targetId) {
144         for (int i = 0; i < models.length; i++) {
145             IPluginExtensionPoint[] extPoints = models[i].getExtensionPoints();
146             for (int j = 0; j < extPoints.length; j++) {
147                 if (extPoints[j].getFullId().equals(targetId))
148                     return extPoints[j];
149             }
150         }
151         return null;
152     }
153             
154         
155     private void doJavaSearch(IProgressMonitor monitor)
156         throws JavaModelException {
157         SearchEngine searchEngine = new SearchEngine();
158         IJavaSearchScope scope = getSearchScope();
159
160         for (int i = 0; i < packageFragments.length; i++) {
161             IPackageFragment packageFragment = packageFragments[i];
162             if (!packageFragment.hasSubpackages()) {
163                 SearchResultCollector collector =
164                     new SearchResultCollector(monitor);
165                 searchEngine.search(
166                     PDEPlugin.getWorkspace(),
167                     SearchEngine.createSearchPattern(
168                         packageFragment.getElementName() + ".*", //$NON-NLS-1$
169
IJavaSearchConstants.TYPE,
170                         IJavaSearchConstants.REFERENCES,
171                         true),
172                     scope,
173                     collector);
174                 IJavaElement[] enclosingElements = collector.getResult();
175                 if (enclosingElements.length > 0) {
176                     searchForTypes(
177                         packageFragment,
178                         searchEngine,
179                         SearchEngine.createJavaSearchScope(enclosingElements),
180                         monitor);
181                 }
182             } else {
183                 searchForTypes(packageFragment, searchEngine, scope, monitor);
184             }
185             monitor.worked(1);
186         }
187     }
188     
189
190     private void searchForTypes(
191         IPackageFragment fragment,
192         SearchEngine searchEngine,
193         IJavaSearchScope scope,
194         IProgressMonitor monitor)
195         throws JavaModelException {
196         IJavaElement[] children = fragment.getChildren();
197         for (int i = 0; i < children.length; i++) {
198             IJavaElement child = children[i];
199             IType[] types = new IType[0];
200             if (child instanceof IClassFile)
201                 types = new IType[] {((IClassFile) child).getType()};
202             else if (child instanceof ICompilationUnit)
203                 types = ((ICompilationUnit) child).getAllTypes();
204
205             for (int j = 0; j < types.length; j++) {
206                 SearchResultCollector collector =
207                     new SearchResultCollector(monitor);
208                 searchEngine.search(
209                     PDEPlugin.getWorkspace(),
210                     SearchEngine.createSearchPattern(
211                         types[j],
212                         IJavaSearchConstants.REFERENCES),
213                     scope,
214                     collector);
215                 if (collector.getResult().length > 0) {
216                     resultCollector.accept(types[j]);
217                 }
218             }
219         }
220     }
221
222
223     private IJavaSearchScope getSearchScope() throws JavaModelException {
224         IPackageFragmentRoot[] roots = JavaCore.create(parentProject).getPackageFragmentRoots();
225         ArrayList JavaDoc filteredRoots = new ArrayList JavaDoc();
226         for (int i = 0; i < roots.length; i++) {
227             if (roots[i].getResource() != null
228                 && roots[i].getResource().getProject().equals(parentProject)) {
229                 filteredRoots.add(roots[i]);
230             }
231         }
232         return SearchEngine.createJavaSearchScope(
233             (IJavaElement[]) filteredRoots.toArray(
234                 new IJavaElement[filteredRoots.size()]));
235     }
236     
237     public String JavaDoc getPluralLabel() {
238         return object.getId() + " - {0} " + PDEPlugin.getResourceString(KEY_DEPENDENCIES); //$NON-NLS-1$
239
}
240     
241     public String JavaDoc getSingularLabel() {
242         return object.getId() + " - 1 " + PDEPlugin.getResourceString(KEY_DEPENDENCY); //$NON-NLS-1$
243
}
244     
245     public IProject getProject() {
246         return parentProject;
247     }
248     
249     
250 }
251
Popular Tags