KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > actions > RunToLineHandler


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.debug.ui.actions;
12
13 import org.eclipse.core.resources.IWorkspaceRunnable;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.debug.core.DebugEvent;
20 import org.eclipse.debug.core.DebugException;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.IBreakpointManager;
23 import org.eclipse.debug.core.IBreakpointManagerListener;
24 import org.eclipse.debug.core.IDebugEventSetListener;
25 import org.eclipse.debug.core.model.IBreakpoint;
26 import org.eclipse.debug.core.model.IDebugTarget;
27 import org.eclipse.debug.core.model.ISuspendResume;
28 import org.eclipse.debug.core.model.IThread;
29 import org.eclipse.debug.internal.ui.actions.ActionMessages;
30 import org.eclipse.debug.ui.DebugUITools;
31 import org.eclipse.debug.ui.IDebugUIConstants;
32
33 /**
34  * Handles a run to line operation. Clients implementing a run to line action
35  * can use this handler to carry out a run to line operation implemented with
36  * a breakpoint. Handles the user preference to skip breakpoints while performing
37  * a run to line operation, and cancelling the run to line operation if another
38  * breakpoint is encountered before the operation is completed.
39  * <p>
40  * Clients may instantiate this class. This class is not intended to be subclassed.
41  * </p>
42  * @since 3.1
43  */

44 public class RunToLineHandler implements IDebugEventSetListener, IBreakpointManagerListener, IWorkspaceRunnable {
45     
46     private IDebugTarget fTarget;
47     private ISuspendResume fResumee;
48     private IBreakpoint fBreakpoint;
49     private boolean fAutoSkip = false;
50     
51     /**
52      * Constructs a handler to perform a run to line operation.
53      *
54      * @param target the debug target in which the operation is to be performed
55      * @param suspendResume the element to be resumed to begin the operation
56      * @param breakpoint the run to line breakpoint
57      */

58     public RunToLineHandler(IDebugTarget target, ISuspendResume suspendResume, IBreakpoint breakpoint) {
59         fResumee = suspendResume;
60         fTarget = target;
61         fBreakpoint = breakpoint;
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
66      */

67     public void handleDebugEvents(DebugEvent[] events) {
68         for (int i = 0; i < events.length; i++) {
69             DebugEvent event= events[i];
70             Object JavaDoc source= event.getSource();
71             if (source instanceof IThread && event.getKind() == DebugEvent.SUSPEND &&
72                     event.getDetail() == DebugEvent.BREAKPOINT) {
73                 IThread thread = (IThread) source;
74                 IDebugTarget suspendee = (IDebugTarget) thread.getAdapter(IDebugTarget.class);
75                 if (fTarget.equals(suspendee)) {
76                     // cleanup if the breakpoint was hit or not
77
cancel();
78                 }
79             } else if (source instanceof IDebugTarget && event.getKind() == DebugEvent.TERMINATE) {
80                 if (source.equals(fTarget)) {
81                     // Clean up if the debug target terminates without
82
// hitting the breakpoint.
83
cancel();
84                 }
85             }
86         }
87         
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.debug.core.IBreakpointManagerListener#breakpointManagerEnablementChanged(boolean)
92      */

93     public void breakpointManagerEnablementChanged(boolean enabled) {
94         // if the user changes the breakpoint manager enablement, don't restore it
95
fAutoSkip = false;
96     }
97     
98     private IBreakpointManager getBreakpointManager() {
99         return getDebugPlugin().getBreakpointManager();
100     }
101     
102     private DebugPlugin getDebugPlugin() {
103         return DebugPlugin.getDefault();
104     }
105     
106     /**
107      * Cancels the run to line operation.
108      */

109     public void cancel() {
110         IBreakpointManager manager = getBreakpointManager();
111         try {
112             getDebugPlugin().removeDebugEventListener(this);
113             manager.removeBreakpointManagerListener(this);
114             fTarget.breakpointRemoved(fBreakpoint, null);
115         } finally {
116             if (fAutoSkip) {
117                 manager.setEnabled(true);
118             }
119         }
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
124      */

125     public void run(IProgressMonitor monitor) throws CoreException {
126         getDebugPlugin().addDebugEventListener(this);
127         IBreakpointManager breakpointManager = getBreakpointManager();
128         fAutoSkip = DebugUITools.getPreferenceStore().getBoolean(IDebugUIConstants.PREF_SKIP_BREAKPOINTS_DURING_RUN_TO_LINE) && breakpointManager.isEnabled();
129         if (fAutoSkip) {
130             getBreakpointManager().setEnabled(false);
131             breakpointManager.addBreakpointManagerListener(this);
132         }
133         Job job = new Job(ActionMessages.RunToLineHandler_0) {
134             protected IStatus run(IProgressMonitor jobMonitor) {
135                 if (!jobMonitor.isCanceled()) {
136                     fTarget.breakpointAdded(fBreakpoint);
137                     try {
138                         fResumee.resume();
139                     } catch (DebugException e) {
140                         cancel();
141                         return e.getStatus();
142                     }
143                 }
144                 return Status.OK_STATUS;
145             }
146         };
147         job.schedule();
148     }
149     
150 }
151
Popular Tags