1 11 package org.eclipse.pde.internal.ui.search; 12 import java.util.ArrayList ; 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 KEY_MATCH = "Search.singleMatch"; private static final String KEY_MATCHES = "Search.multipleMatches"; 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 filteredRoots = new ArrayList (); 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 getPluralLabel() { 72 return element.getElementName() + " - {0} " + PDEPlugin.getResourceString(KEY_MATCHES); } 74 75 public String getSingularLabel() { 76 return element.getElementName() + " - 1 " + PDEPlugin.getResourceString(KEY_MATCH); } 78 79 } 80 | Popular Tags |