1 11 package org.eclipse.jdt.internal.junit.ui; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.HashSet ; 15 import java.util.Set ; 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 47 public abstract class OpenEditorAction extends Action { 48 protected String fClassName; 49 protected TestRunnerViewPart fTestRunner; 50 private final boolean fActivate; 51 52 protected OpenEditorAction(TestRunnerViewPart testRunner, String testClassName) { 53 this(testRunner, testClassName, true); 54 } 55 56 public OpenEditorAction(TestRunnerViewPart testRunner, String className, boolean activate) { 57 super(JUnitMessages.OpenEditorAction_action_label); 58 fClassName= className; 59 fTestRunner= testRunner; 60 fActivate= activate; 61 } 62 63 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 94 protected IJavaProject getLaunchedProject() { 95 return fTestRunner.getLaunchedProject(); 96 } 97 98 protected String getClassName() { 99 return fClassName; 100 } 101 102 protected abstract IJavaElement findElement(IJavaProject project, String className) throws CoreException; 103 104 protected abstract void reveal(ITextEditor editor); 105 106 protected final IType findType(final IJavaProject project, String className) throws JavaModelException { 107 final IType[] result= { null }; 108 final String dottedName= className.replace('$', '.'); try { 110 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { 111 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 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 (), monitor); 132 } 133 } catch (JavaModelException e) { 134 throw new InvocationTargetException (e); 135 } 136 } 137 }); 138 } catch (InvocationTargetException e) { 139 JUnitPlugin.log(e); 140 } catch (InterruptedException e) { 141 } 143 return result[0]; 144 } 145 146 private IType internalFindType(IJavaProject project, String className, Set visitedProjects, IProgressMonitor monitor) throws JavaModelException { 147 try { 148 if (visitedProjects.contains(project)) 149 return null; 150 monitor.beginTask("", 2); IType type= project.findType(className, new SubProgressMonitor(monitor, 1)); 152 if (type != null) 153 return type; 154 visitedProjects.add(project); 156 IJavaModel javaModel= project.getJavaModel(); 157 String [] requiredProjectNames= project.getRequiredProjectNames(); 158 IProgressMonitor reqMonitor= new SubProgressMonitor(monitor, 1); 159 reqMonitor.beginTask("", requiredProjectNames.length); 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 |