/* | |
* (c) Copyright IBM Corp. 2000, 2001. | |
* All Rights Reserved. | |
*/ | |
package org.eclipse.jdt.internal.junit.ui; | |
import org.eclipse.ui.texteditor.ITextEditor; | |
import org.eclipse.jdt.core.IJavaElement; | |
import org.eclipse.jdt.core.IJavaProject; | |
import org.eclipse.jdt.core.IMethod; | |
import org.eclipse.jdt.core.ISourceRange; | |
import org.eclipse.jdt.core.IType; | |
import org.eclipse.jdt.core.ITypeHierarchy; | |
import org.eclipse.jdt.core.JavaModelException; | |
/** | |
* Open a class on a Test method. | |
*/ | |
public class OpenTestAction extends OpenEditorAction { | |
private String fMethodName; | |
private ISourceRange fRange; | |
/** | |
* Constructor for OpenTestAction. | |
*/ | |
public OpenTestAction(TestRunnerViewPart testRunner, String className, String method) { | |
super(testRunner, className); | |
fMethodName= method; | |
} | |
public OpenTestAction(TestRunnerViewPart testRunner, String className) { | |
this(testRunner, className, null); | |
} | |
protected IJavaElement findElement(IJavaProject project, String className) throws JavaModelException { | |
IType type= JavaModelUtil.findTypeInProject(project, className); | |
if (type == null) | |
return null; | |
if (fMethodName == null) | |
return type; | |
IMethod method= findMethod(type); | |
if (method == null) { | |
ITypeHierarchy typeHierarchy= type.newSupertypeHierarchy(null); | |
IType[] types= typeHierarchy.getAllSuperclasses(type); | |
for (int i= 0; i < types.length; i++) { | |
method= findMethod(types[i]); | |
if (method != null) | |
break; | |
} | |
} | |
if (method != null) | |
fRange= method.getNameRange(); | |
return method; | |
} | |
IMethod findMethod(IType type) throws JavaModelException { | |
IMethod method= type.getMethod(fMethodName, new String[0]); | |
if (method != null && method.exists()) | |
return method; | |
return null; | |
} | |
protected void reveal(ITextEditor textEditor) { | |
if (fRange != null) | |
textEditor.selectAndReveal(fRange.getOffset(), fRange.getLength()); | |
} | |
} |