KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > 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.ant.internal.ui.editor.actions;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.ant.internal.ui.AntUIPlugin;
17 import org.eclipse.ant.internal.ui.debug.IAntDebugConstants;
18 import org.eclipse.ant.internal.ui.debug.model.AntDebugElement;
19 import org.eclipse.ant.internal.ui.debug.model.AntLineBreakpoint;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IMarker;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IAdaptable;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.NullProgressMonitor;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.debug.core.model.IBreakpoint;
28 import org.eclipse.debug.core.model.IDebugTarget;
29 import org.eclipse.debug.core.model.ISuspendResume;
30 import org.eclipse.debug.ui.actions.IRunToLineTarget;
31 import org.eclipse.debug.ui.actions.RunToLineHandler;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.ITextSelection;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.ui.IEditorInput;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IWorkbenchPart;
38 import org.eclipse.ui.texteditor.IDocumentProvider;
39 import org.eclipse.ui.texteditor.ITextEditor;
40
41 /**
42  * Run to line target for the Ant debugger
43  */

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

49     public void runToLine(IWorkbenchPart part, ISelection selection, ISuspendResume target) throws CoreException {
50         IEditorPart editorPart = (IEditorPart)part;
51         IEditorInput input = editorPart.getEditorInput();
52         String JavaDoc errorMessage = null;
53         if (input == null) {
54             errorMessage = AntEditorActionMessages.getString("RunToLineAdapter.0"); //$NON-NLS-1$
55
} else {
56             ITextEditor textEditor = (ITextEditor)editorPart;
57             IDocumentProvider provider= textEditor.getDocumentProvider();
58             IDocument document= provider.getDocument(input);
59             
60             if (document == null) {
61                 errorMessage = AntEditorActionMessages.getString("RunToLineAdapter.1"); //$NON-NLS-1$
62
} else {
63                 
64                 ITextSelection textSelection = (ITextSelection) selection;
65                 int lineNumber = textSelection.getStartLine() + 1;
66                 
67                 IBreakpoint breakpoint= null;
68                 Map JavaDoc attributes = getRunToLineAttributes();
69                 IFile file = (IFile)input.getAdapter(IFile.class);
70                 if (file == null) {
71                     errorMessage= AntEditorActionMessages.getString("RunToLineAdapter.2"); //$NON-NLS-1$
72
} else {
73                     breakpoint= new AntLineBreakpoint(file, lineNumber, attributes, false);
74                     breakpoint.setPersisted(false);
75                     errorMessage = AntEditorActionMessages.getString("RunToLineAdapter.3"); //$NON-NLS-1$
76
if (target instanceof IAdaptable) {
77                         IDebugTarget debugTarget = (IDebugTarget) ((IAdaptable)target).getAdapter(IDebugTarget.class);
78                         if (debugTarget != null) {
79                             RunToLineHandler handler = new RunToLineHandler(debugTarget, target, breakpoint);
80                             handler.run(new NullProgressMonitor());
81                             return;
82                         }
83                     }
84                 }
85             }
86         }
87         throw new CoreException(new Status(IStatus.ERROR, AntUIPlugin.getUniqueIdentifier(), AntUIPlugin.INTERNAL_ERROR,
88                 errorMessage, null));
89     }
90
91     private Map JavaDoc getRunToLineAttributes() {
92         Map JavaDoc attributes = new HashMap JavaDoc();
93         attributes.put(IMarker.TRANSIENT, Boolean.TRUE);
94         attributes.put(IAntDebugConstants.ANT_RUN_TO_LINE, Boolean.TRUE);
95         return attributes;
96     }
97
98     /* (non-Javadoc)
99      * @see org.eclipse.debug.ui.actions.IRunToLineTarget#canRunToLine(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection, org.eclipse.debug.core.model.ISuspendResume)
100      */

101     public boolean canRunToLine(IWorkbenchPart part, ISelection selection, ISuspendResume target) {
102         return target instanceof AntDebugElement;
103     }
104 }
105
Popular Tags