KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > RunToLineAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.debug.ui.actions;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
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 /**
45  * Run to line target for the Java debugger
46  */

47 public class RunToLineAdapter implements IRunToLineTarget {
48     
49     /* (non-Javadoc)
50      * @see org.eclipse.debug.ui.actions.IRunToLineTarget#runToLine(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection, org.eclipse.debug.core.model.ISuspendResume)
51      */

52     public void runToLine(IWorkbenchPart part, ISelection selection, ISuspendResume target) throws CoreException {
53         ITextEditor textEditor = getTextEditor(part);
54         String JavaDoc 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 JavaDoc[] typeName = new String JavaDoc[1];
68                     final int[] lineNumber = new int[1];
69                     final ITextSelection textSelection = (ITextSelection) selection;
70                     Runnable JavaDoc r = new Runnable JavaDoc() {
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 JavaDoc attributes = new HashMap JavaDoc(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                         // invalid line
99
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     /* (non-Javadoc)
114      * @see org.eclipse.debug.ui.actions.IRunToLineTarget#canRunToLine(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection, org.eclipse.debug.core.model.ISuspendResume)
115      */

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     /**
126      * Returns the text editor associated with the given part or <code>null</code>
127      * if none. In case of a multi-page editor, this method should be used to retrieve
128      * the correct editor to perform the operation on.
129      *
130      * @param part workbench part
131      * @return text editor part or <code>null</code>
132      */

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