KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.dialogs.ErrorDialog;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.operation.IRunnableWithProgress;
27
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.texteditor.ITextEditor;
30
31 import org.eclipse.jdt.core.IJavaElement;
32 import org.eclipse.jdt.core.IJavaModel;
33 import org.eclipse.jdt.core.IJavaProject;
34 import org.eclipse.jdt.core.IType;
35 import org.eclipse.jdt.core.JavaModelException;
36 import org.eclipse.jdt.core.search.IJavaSearchConstants;
37 import org.eclipse.jdt.core.search.SearchEngine;
38 import org.eclipse.jdt.core.search.SearchPattern;
39 import org.eclipse.jdt.core.search.TypeNameMatch;
40 import org.eclipse.jdt.core.search.TypeNameMatchRequestor;
41
42 import org.eclipse.jdt.ui.JavaUI;
43
44 /**
45  * Abstract Action for opening a Java editor.
46  */

47 public abstract class OpenEditorAction extends Action {
48     protected String JavaDoc fClassName;
49     protected TestRunnerViewPart fTestRunner;
50     private final boolean fActivate;
51     
52     protected OpenEditorAction(TestRunnerViewPart testRunner, String JavaDoc testClassName) {
53         this(testRunner, testClassName, true);
54     }
55
56     public OpenEditorAction(TestRunnerViewPart testRunner, String JavaDoc className, boolean activate) {
57         super(JUnitMessages.OpenEditorAction_action_label);
58         fClassName= className;
59         fTestRunner= testRunner;
60         fActivate= activate;
61     }
62
63     /*
64      * @see IAction#run()
65      */

66     public void run() {
67         ITextEditor textEditor= null;
68         try {
69             IJavaElement element= findElement(getLaunchedProject(), fClassName);
70             if (element == null) {
71                 MessageDialog.openError(getShell(),
72                     JUnitMessages.OpenEditorAction_error_cannotopen_title, JUnitMessages.OpenEditorAction_error_cannotopen_message);
73                 return;
74             }
75             textEditor= (ITextEditor) JavaUI.openInEditor(element, fActivate, false);
76         } catch (CoreException e) {
77             ErrorDialog.openError(getShell(), JUnitMessages.OpenEditorAction_error_dialog_title, JUnitMessages.OpenEditorAction_error_dialog_message, e.getStatus());
78             return;
79         }
80         if (textEditor == null) {
81             fTestRunner.registerInfoMessage(JUnitMessages.OpenEditorAction_message_cannotopen);
82             return;
83         }
84         reveal(textEditor);
85     }
86     
87     protected Shell getShell() {
88         return fTestRunner.getSite().getShell();
89     }
90
91     /**
92      * @return the Java project, or <code>null</code>
93      */

94     protected IJavaProject getLaunchedProject() {
95         return fTestRunner.getLaunchedProject();
96     }
97     
98     protected String JavaDoc getClassName() {
99         return fClassName;
100     }
101
102     protected abstract IJavaElement findElement(IJavaProject project, String JavaDoc className) throws CoreException;
103     
104     protected abstract void reveal(ITextEditor editor);
105
106     protected final IType findType(final IJavaProject project, String JavaDoc className) throws JavaModelException {
107         final IType[] result= { null };
108         final String JavaDoc dottedName= className.replace('$', '.'); // for nested classes...
109
try {
110             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
111                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
112                     try {
113                         if (project == null) {
114                             int lastDot= dottedName.lastIndexOf('.');
115                             TypeNameMatchRequestor nameMatchRequestor= new TypeNameMatchRequestor() {
116                                 public void acceptTypeNameMatch(TypeNameMatch match) {
117                                     result[0]= match.getType();
118                                 }
119                             };
120                             new SearchEngine().searchAllTypeNames(
121                                     lastDot >= 0 ? dottedName.substring(0, lastDot).toCharArray() : null,
122                                     SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE,
123                                     (lastDot >= 0 ? dottedName.substring(lastDot + 1) : dottedName).toCharArray(),
124                                     SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE,
125                                     IJavaSearchConstants.TYPE,
126                                     SearchEngine.createWorkspaceScope(),
127                                     nameMatchRequestor,
128                                     IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
129                                     monitor);
130                         } else {
131                             result[0]= internalFindType(project, dottedName, new HashSet JavaDoc(), monitor);
132                         }
133                     } catch (JavaModelException e) {
134                         throw new InvocationTargetException JavaDoc(e);
135                     }
136                 }
137             });
138         } catch (InvocationTargetException JavaDoc e) {
139             JUnitPlugin.log(e);
140         } catch (InterruptedException JavaDoc e) {
141             // user cancelled
142
}
143         return result[0];
144     }
145
146     private IType internalFindType(IJavaProject project, String JavaDoc className, Set JavaDoc/*<IJavaProject>*/ visitedProjects, IProgressMonitor monitor) throws JavaModelException {
147         try {
148             if (visitedProjects.contains(project))
149                 return null;
150             monitor.beginTask("", 2); //$NON-NLS-1$
151
IType type= project.findType(className, new SubProgressMonitor(monitor, 1));
152             if (type != null)
153                 return type;
154             //fix for bug 87492: visit required projects explicitly to also find not exported types
155
visitedProjects.add(project);
156             IJavaModel javaModel= project.getJavaModel();
157             String JavaDoc[] requiredProjectNames= project.getRequiredProjectNames();
158             IProgressMonitor reqMonitor= new SubProgressMonitor(monitor, 1);
159             reqMonitor.beginTask("", requiredProjectNames.length); //$NON-NLS-1$
160
for (int i= 0; i < requiredProjectNames.length; i++) {
161                 IJavaProject requiredProject= javaModel.getJavaProject(requiredProjectNames[i]);
162                 if (requiredProject.exists()) {
163                     type= internalFindType(requiredProject, className, visitedProjects, new SubProgressMonitor(reqMonitor, 1));
164                     if (type != null)
165                         return type;
166                 }
167             }
168             return null;
169         } finally {
170             monitor.done();
171         }
172     }
173     
174 }
175
Popular Tags