1 11 package org.eclipse.jdt.internal.junit.ui; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.jdt.core.ICompilationUnit; 17 import org.eclipse.jdt.core.IField; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.IMember; 20 import org.eclipse.jdt.core.IMethod; 21 import org.eclipse.jdt.core.IType; 22 import org.eclipse.jdt.core.JavaModelException; 23 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 24 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 25 26 import org.eclipse.jdt.internal.junit.Messages; 27 28 import org.eclipse.jdt.ui.JavaUI; 29 import org.eclipse.jface.action.IAction; 30 import org.eclipse.jface.dialogs.ErrorDialog; 31 import org.eclipse.jface.dialogs.MessageDialog; 32 import org.eclipse.jface.text.ITextSelection; 33 import org.eclipse.jface.viewers.ISelection; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.jface.window.Window; 36 import org.eclipse.swt.widgets.Shell; 37 import org.eclipse.ui.IEditorPart; 38 import org.eclipse.ui.IWorkbenchWindow; 39 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 40 import org.eclipse.ui.PartInitException; 41 import org.eclipse.ui.dialogs.SelectionStatusDialog; 42 43 46 public class GotoReferencedTestAction implements IWorkbenchWindowActionDelegate { 47 ISelection fSelection; 48 IWorkbenchWindow fWorkbench; 49 50 private void run(IStructuredSelection selection) { 51 IJavaElement[] elements= getSelectedElements(selection); 52 if (elements.length == 0) { 53 MessageDialog.openInformation(getShell(), JUnitMessages.GotoReferencedTestAction_dialog_title, JUnitMessages.GotoReferencedTestAction_dialog_message); 54 return; 55 } 56 try { 57 run(elements); 58 } catch (CoreException e) { 59 ErrorDialog.openError(getShell(), JUnitMessages.GotoReferencedTestAction_dialog_title, JUnitMessages.GotoReferencedTestAction_dialog_error, e.getStatus()); 60 } 61 } 62 63 private void run(ITextSelection ITextSelection) { 64 try { 65 JavaEditor editor= getActiveEditor(); 66 if (editor == null) 67 return; 68 IJavaElement element= SelectionConverter.getElementAtOffset(editor); 69 int type= element != null ? element.getElementType() : -1; 70 if (type != IJavaElement.METHOD && type != IJavaElement.TYPE) { 71 element= SelectionConverter.getTypeAtOffset(editor); 72 if (element == null) { 73 MessageDialog.openInformation(getShell(), JUnitMessages.GotoReferencedTestAction_dialog_title, JUnitMessages.GotoReferencedTestAction_dialog_error_nomethod); 74 return; 75 } 76 } 77 run(new IMember[] { (IMember)element }); 78 } catch (CoreException e) { 79 ErrorDialog.openError(getShell(), JUnitMessages.GotoReferencedTestAction_dialog_title, JUnitMessages.GotoReferencedTestAction_dialog_error, e.getStatus()); 80 } 81 } 82 83 private void run(IJavaElement[] elements) throws PartInitException, JavaModelException { 84 IJavaElement element= elements[0]; 85 86 SelectionStatusDialog dialog = new TestMethodSelectionDialog(getShell(), element); 87 dialog.setTitle(JUnitMessages.GotoReferencedTestAction_selectdialog_title); 88 String msg= Messages.format(JUnitMessages.GotoReferencedTestAction_dialog_select_message, element.getElementName()); 89 dialog.setMessage(msg); 90 91 if (dialog.open() == Window.CANCEL) 92 return; 93 94 Object result = dialog.getFirstResult(); 95 if (result == null) 96 return; 97 98 openElement((IJavaElement)result); 99 } 100 101 private void openElement(IJavaElement result) throws JavaModelException, PartInitException { 102 IEditorPart part= JavaUI.openInEditor(result); 103 JavaUI.revealInEditor(part, result); 104 } 105 106 private IJavaElement[] getSelectedElements(IStructuredSelection selection) { 107 List elements= selection.toList(); 108 int size= elements.size(); 109 if (size == 0) 110 return new IJavaElement[0]; 111 112 ArrayList result= new ArrayList (size); 113 114 for (int i= 0; i < size; i++) { 115 Object e= elements.get(i); 116 if (e instanceof ICompilationUnit) { 117 ICompilationUnit unit= (ICompilationUnit) e; 118 IType[] types= new IType[0]; 119 try { 120 types= unit.getTypes(); 121 } catch (JavaModelException ex) { 122 } 123 for (int j= 0; j < types.length; j++) { 124 result.add(types[j]); 125 } 126 } 127 else if (e instanceof IMethod || e instanceof IType || e instanceof IField) { 128 result.add(e); 129 } else { 130 return new IJavaElement[0]; 131 } 132 } 133 return (IJavaElement[])result.toArray(new IJavaElement[result.size()]); 134 } 135 136 public void run(IAction action) { 137 if (fSelection instanceof IStructuredSelection) 138 run((IStructuredSelection)fSelection); 139 else if (fSelection instanceof ITextSelection) 140 run((ITextSelection)fSelection); 141 } 142 143 public void selectionChanged(IAction action, ISelection selection) { 144 fSelection= selection; 145 action.setEnabled(getActiveEditor() != null); 146 } 147 148 private Shell getShell() { 149 if (fWorkbench != null) 150 return fWorkbench.getShell(); 151 return JUnitPlugin.getActiveWorkbenchShell(); 152 } 153 154 public void dispose() { 155 } 156 157 public void init(IWorkbenchWindow window) { 158 fWorkbench= window; 159 } 160 161 private JavaEditor getActiveEditor() { 162 IEditorPart editor= fWorkbench.getActivePage().getActiveEditor(); 163 if (editor instanceof JavaEditor) 164 return (JavaEditor) editor; 165 return null; 166 } 167 } 168 | Popular Tags |