1 11 package org.eclipse.jdt.internal.junit.ui; 12 13 import org.eclipse.core.runtime.IStatus; 14 15 import org.eclipse.ui.PlatformUI; 16 import org.eclipse.ui.texteditor.ITextEditor; 17 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.IJavaProject; 20 import org.eclipse.jdt.core.IMethod; 21 import org.eclipse.jdt.core.ISourceRange; 22 import org.eclipse.jdt.core.IType; 23 import org.eclipse.jdt.core.ITypeHierarchy; 24 import org.eclipse.jdt.core.JavaConventions; 25 import org.eclipse.jdt.core.JavaCore; 26 import org.eclipse.jdt.core.JavaModelException; 27 28 import org.eclipse.jdt.internal.junit.Messages; 29 30 import org.eclipse.jface.dialogs.MessageDialog; 31 32 35 public class OpenTestAction extends OpenEditorAction { 36 37 private String fMethodName; 38 private ISourceRange fRange; 39 40 public OpenTestAction(TestRunnerViewPart testRunner, String className, String method) { 41 this(testRunner, className, method, true); 42 } 43 44 public OpenTestAction(TestRunnerViewPart testRunner, String className) { 45 this(testRunner, className, null); 46 } 47 48 public OpenTestAction(TestRunnerViewPart testRunner, String className, String method, boolean activate) { 49 super(testRunner, className, activate); 50 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJUnitHelpContextIds.OPENTEST_ACTION); 51 fMethodName= method; 52 } 53 54 protected IJavaElement findElement(IJavaProject project, String className) throws JavaModelException { 55 IType type= findType(project, className); 56 if (type == null) 57 return null; 58 59 if (fMethodName == null) 60 return type; 61 62 IMethod method= findMethod(type); 63 if (method == null) { 64 ITypeHierarchy typeHierarchy= type.newSupertypeHierarchy(null); 65 IType[] types= typeHierarchy.getAllSuperclasses(type); 66 for (int i= 0; i < types.length; i++) { 67 method= findMethod(types[i]); 68 if (method != null) 69 break; 70 } 71 } 72 if (method == null) { 73 String title= JUnitMessages.OpenTestAction_error_title; 74 String message= Messages.format(JUnitMessages.OpenTestAction_error_methodNoFound, fMethodName); 75 MessageDialog.openInformation(getShell(), title, message); 76 return type; 77 } 78 fRange= method.getNameRange(); 79 return method; 80 } 81 82 IMethod findMethod(IType type) { 83 IStatus status= JavaConventions.validateMethodName(fMethodName, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3); 84 if (! status.isOK()) 85 return null; 86 IMethod method= type.getMethod(fMethodName, new String [0]); 87 if (method != null && method.exists()) 88 return method; 89 return null; 90 } 91 92 protected void reveal(ITextEditor textEditor) { 93 if (fRange != null) 94 textEditor.selectAndReveal(fRange.getOffset(), fRange.getLength()); 95 } 96 97 } 98 | Popular Tags |