KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > OpenTestAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
33  * Open a class on a Test method.
34  */

35 public class OpenTestAction extends OpenEditorAction {
36     
37     private String JavaDoc fMethodName;
38     private ISourceRange fRange;
39     
40     public OpenTestAction(TestRunnerViewPart testRunner, String JavaDoc className, String JavaDoc method) {
41         this(testRunner, className, method, true);
42     }
43     
44     public OpenTestAction(TestRunnerViewPart testRunner, String JavaDoc className) {
45         this(testRunner, className, null);
46     }
47
48     public OpenTestAction(TestRunnerViewPart testRunner, String JavaDoc className, String JavaDoc 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 JavaDoc 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 JavaDoc title= JUnitMessages.OpenTestAction_error_title;
74             String JavaDoc 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 JavaDoc[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