1 11 package org.eclipse.jdt.internal.debug.ui.actions; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.NullProgressMonitor; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.debug.core.model.IBreakpoint; 23 import org.eclipse.debug.core.model.IDebugElement; 24 import org.eclipse.debug.core.model.IDebugTarget; 25 import org.eclipse.debug.core.model.ISuspendResume; 26 import org.eclipse.debug.ui.actions.IRunToLineTarget; 27 import org.eclipse.debug.ui.actions.RunToLineHandler; 28 import org.eclipse.jdt.core.dom.AST; 29 import org.eclipse.jdt.core.dom.ASTParser; 30 import org.eclipse.jdt.core.dom.CompilationUnit; 31 import org.eclipse.jdt.debug.core.IJavaDebugTarget; 32 import org.eclipse.jdt.debug.core.JDIDebugModel; 33 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 34 import org.eclipse.jdt.internal.debug.ui.BreakpointUtils; 35 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 36 import org.eclipse.jface.text.IDocument; 37 import org.eclipse.jface.text.ITextSelection; 38 import org.eclipse.jface.viewers.ISelection; 39 import org.eclipse.swt.custom.BusyIndicator; 40 import org.eclipse.ui.IEditorInput; 41 import org.eclipse.ui.IWorkbenchPart; 42 import org.eclipse.ui.texteditor.ITextEditor; 43 44 47 public class RunToLineAdapter implements IRunToLineTarget { 48 49 52 public void runToLine(IWorkbenchPart part, ISelection selection, ISuspendResume target) throws CoreException { 53 ITextEditor textEditor = getTextEditor(part); 54 String errorMessage = null; 55 if (textEditor == null) { 56 errorMessage = ActionMessages.RunToLineAdapter_1; 57 } else { 58 IEditorInput input = textEditor.getEditorInput(); 59 if (input == null) { 60 errorMessage = ActionMessages.RunToLineAdapter_0; 61 } else { 62 final IDocument document= textEditor.getDocumentProvider().getDocument(input); 63 if (document == null) { 64 errorMessage = ActionMessages.RunToLineAdapter_1; 65 } else { 66 final int[] validLine = new int[1]; 67 final String [] typeName = new String [1]; 68 final int[] lineNumber = new int[1]; 69 final ITextSelection textSelection = (ITextSelection) selection; 70 Runnable r = new Runnable () { 71 public void run() { 72 lineNumber[0] = textSelection.getStartLine() + 1; 73 ASTParser parser = ASTParser.newParser(AST.JLS3); 74 parser.setSource(document.get().toCharArray()); 75 CompilationUnit compilationUnit= (CompilationUnit)parser.createAST(null); 76 ValidBreakpointLocationLocator locator= new ValidBreakpointLocationLocator(compilationUnit, lineNumber[0], false, false); 77 compilationUnit.accept(locator); 78 validLine[0]= locator.getLineLocation(); 79 typeName[0]= locator.getFullyQualifiedTypeName(); 80 } 81 }; 82 BusyIndicator.showWhile(JDIDebugUIPlugin.getStandardDisplay(), r); 83 if (validLine[0] == lineNumber[0]) { 84 IBreakpoint breakpoint= null; 85 Map attributes = new HashMap (4); 86 BreakpointUtils.addRunToLineAttributes(attributes); 87 breakpoint= JDIDebugModel.createLineBreakpoint(ResourcesPlugin.getWorkspace().getRoot(), typeName[0], lineNumber[0], -1, -1, 1, false, attributes); 88 errorMessage = ActionMessages.RunToLineAdapter_2; 89 if (target instanceof IAdaptable) { 90 IDebugTarget debugTarget = (IDebugTarget) ((IAdaptable)target).getAdapter(IDebugTarget.class); 91 if (debugTarget != null) { 92 RunToLineHandler handler = new RunToLineHandler(debugTarget, target, breakpoint); 93 handler.run(new NullProgressMonitor()); 94 return; 95 } 96 } 97 } else { 98 if (textSelection.getLength() > 0) { 100 errorMessage = ActionMessages.RunToLineAdapter_3; 101 } else { 102 errorMessage = ActionMessages.RunToLineAdapter_4; 103 } 104 105 } 106 } 107 } 108 } 109 throw new CoreException(new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, 110 errorMessage, null)); 111 } 112 113 116 public boolean canRunToLine(IWorkbenchPart part, ISelection selection, ISuspendResume target) { 117 if (target instanceof IDebugElement) { 118 IDebugElement element = (IDebugElement) target; 119 IJavaDebugTarget adapter = (IJavaDebugTarget) element.getDebugTarget().getAdapter(IJavaDebugTarget.class); 120 return adapter != null; 121 } 122 return false; 123 } 124 125 133 protected ITextEditor getTextEditor(IWorkbenchPart part) { 134 if (part instanceof ITextEditor) { 135 return (ITextEditor) part; 136 } 137 return (ITextEditor) part.getAdapter(ITextEditor.class); 138 } 139 } 140 | Popular Tags |