package jdtsearch.popup.actions; import java.util.Iterator; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.search.IJavaSearchConstants; import org.eclipse.jdt.core.search.SearchEngine; import org.eclipse.jdt.core.search.SearchMatch; import org.eclipse.jdt.core.search.SearchParticipant; import org.eclipse.jdt.core.search.SearchPattern; import org.eclipse.jdt.core.search.SearchRequestor; import org.eclipse.jdt.internal.core.search.JavaSearchParticipant; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IActionDelegate; import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPart; public class SearchAction implements IObjectActionDelegate { private Shell shell; private ISelection selection; private static SearchEngine engine = new SearchEngine(); /** * Constructor for Action1. */ public SearchAction() { super(); } /** * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) */ public void setActivePart(IAction action, IWorkbenchPart targetPart) { shell = targetPart.getSite().getShell(); } /** * @see IActionDelegate#run(IAction) */ public void run(IAction action) { IStructuredSelection structuredSelection = (IStructuredSelection)selection; for( Iterator it = structuredSelection.iterator(); it.hasNext(); ) { IResource currentResource = (IResource)( it.next()); IJavaElement element = JavaCore.create( currentResource ); // if the element is a compilation unit if (element.getElementType() == IJavaElement.COMPILATION_UNIT){ ICompilationUnit cu = (ICompilationUnit)element; try { // get all types IType[] types = cu.getAllTypes(); for (int i = 0; i < types.length; i++) { // get the type IType type = types[i]; MessageDialog.openInformation(shell,"JDTSearch Plug-in","Running search on: " + type.getElementName()); // create the search requester TypeDependencySearchRequestor requestor = new TypeDependencySearchRequestor(); // run the search // engine.searchDeclarationsOfReferencedTypes(type, requestor, null); SearchPattern pattern = SearchPattern.createPattern(type, IJavaSearchConstants.REFERENCES); engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createWorkspaceScope(), requestor, null); } }catch (Exception e) { } } } } private class TypeDependencySearchRequestor extends SearchRequestor { public TypeDependencySearchRequestor() { } public void acceptSearchMatch(SearchMatch match) throws CoreException { // get the matched element IJavaElement matchedElement = (IJavaElement) match.getElement(); // try to see if the type we got here is on of the types we are // interested in String message = "Found a reference in "; if (match.isInsideDocComment()) { message += "the javadoc comment of "; } message += matchedElement.getElementName(); MessageDialog.openInformation(shell,"JDTSearch Plug-in",message); } } /** * @see IActionDelegate#selectionChanged(IAction, ISelection) */ public void selectionChanged(IAction action, ISelection selection) { this.selection = selection; } }