KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > 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.pde.internal.ui.search;
12 import java.util.ArrayList JavaDoc;
13
14 import org.eclipse.core.resources.*;
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jdt.core.IJavaElement;
18 import org.eclipse.jdt.core.IPackageFragmentRoot;
19 import org.eclipse.jdt.core.JavaCore;
20 import org.eclipse.jdt.core.JavaModelException;
21 import org.eclipse.jdt.core.search.IJavaSearchConstants;
22 import org.eclipse.jdt.core.search.IJavaSearchScope;
23 import org.eclipse.jdt.core.search.SearchEngine;
24 import org.eclipse.jface.operation.*;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26
27
28 class JavaSearchOperation implements IWorkspaceRunnable, IRunnableWithProgress {
29     IJavaElement element;
30     IProject parentProject;
31     private static final String JavaDoc KEY_MATCH = "Search.singleMatch"; //$NON-NLS-1$
32
private static final String JavaDoc KEY_MATCHES = "Search.multipleMatches"; //$NON-NLS-1$
33

34     public JavaSearchOperation(IJavaElement element, IProject parentProject) {
35         this.element = element;
36         this.parentProject = parentProject;
37     }
38
39     public void run(IProgressMonitor monitor) {
40         doJavaSearch(monitor);
41     }
42     
43     private void doJavaSearch(IProgressMonitor monitor) {
44         try {
45             SearchEngine searchEngine = new SearchEngine();
46             searchEngine.search(
47                 PDEPlugin.getWorkspace(),
48                 element,
49                 IJavaSearchConstants.REFERENCES,
50                 getSearchScope(),
51                 new JavaSearchCollector(this, monitor));
52         } catch (JavaModelException e) {
53         }
54     }
55
56     private IJavaSearchScope getSearchScope() throws JavaModelException {
57         IPackageFragmentRoot[] roots =
58             JavaCore.create(parentProject).getPackageFragmentRoots();
59         ArrayList JavaDoc filteredRoots = new ArrayList JavaDoc();
60         for (int i = 0; i < roots.length; i++) {
61             if (roots[i].getResource() != null
62                 && roots[i].getResource().getProject().equals(parentProject)) {
63                 filteredRoots.add(roots[i]);
64             }
65         }
66         return SearchEngine.createJavaSearchScope(
67             (IJavaElement[]) filteredRoots.toArray(
68                 new IJavaElement[filteredRoots.size()]));
69     }
70     
71     public String JavaDoc getPluralLabel() {
72         return element.getElementName() + " - {0} " + PDEPlugin.getResourceString(KEY_MATCHES); //$NON-NLS-1$
73
}
74
75     public String JavaDoc getSingularLabel() {
76         return element.getElementName() + " - 1 " + PDEPlugin.getResourceString(KEY_MATCH); //$NON-NLS-1$
77
}
78
79 }
80
Popular Tags