KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
14 import org.eclipse.debug.core.DebugEvent;
15 import org.eclipse.debug.core.DebugException;
16 import org.eclipse.debug.core.DebugPlugin;
17 import org.eclipse.debug.core.IDebugEventSetListener;
18 import org.eclipse.jdt.debug.core.IJavaThread;
19 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.ui.IObjectActionDelegate;
27 import org.eclipse.ui.IWorkbenchPart;
28
29 /**
30  * Attempts to terminate an evaluation running in an IJavaThread.
31  */

32 public class TerminateEvaluationAction implements IObjectActionDelegate, IDebugEventSetListener {
33     
34     private IJavaThread fThread;
35     private boolean fTerminated;
36
37     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
38     }
39
40     public void run(IAction action) {
41         if (fThread == null) {
42             return;
43         }
44         DebugPlugin.getDefault().addDebugEventListener(this);
45         Thread JavaDoc timerThread= new Thread JavaDoc(new Runnable JavaDoc() {
46             public void run() {
47                 fTerminated= false;
48                 try {
49                     Thread.sleep(3000);
50                 } catch (InterruptedException JavaDoc e) {
51                     return;
52                 }
53                 if (!fTerminated) {
54                     fTerminated= true;
55                     final Display display= JDIDebugUIPlugin.getStandardDisplay();
56                         display.asyncExec(new Runnable JavaDoc() {
57                             public void run() {
58                                 MessageDialog dialog = new MessageDialog(display.getActiveShell(), ActionMessages.TerminateEvaluationActionTerminate_Evaluation_1, null,
59                                     ActionMessages.TerminateEvaluationActionAttempts_to_terminate_an_evaluation_can_only_stop_a_series_of_statements__The_currently_executing_statement__such_as_a_method_invocation__cannot_be_interrupted__2, MessageDialog.INFORMATION, new String JavaDoc[] { IDialogConstants.OK_LABEL }, 0);
60                                 dialog.setBlockOnOpen(false);
61                                 dialog.open();
62                             }
63                     });
64                 }
65             }
66         });
67         timerThread.setDaemon(true);
68         timerThread.start();
69         try {
70             fThread.terminateEvaluation();
71         } catch (DebugException exception) {
72             JDIDebugUIPlugin.statusDialog(exception.getStatus());
73         }
74     }
75
76     public void selectionChanged(IAction action, ISelection selection) {
77         if (selection instanceof IStructuredSelection) {
78             IStructuredSelection ss= (IStructuredSelection)selection;
79             if (ss.isEmpty() || ss.size() > 1) {
80                 return;
81             }
82             Object JavaDoc element= ss.getFirstElement();
83             if (element instanceof IJavaThread) {
84                 setThread((IJavaThread)element);
85             }
86         }
87     }
88     
89     public void setThread(IJavaThread thread) {
90         fThread= thread;
91     }
92
93     public void handleDebugEvents(DebugEvent[] events) {
94         DebugEvent event;
95         for (int i= 0, numEvents= events.length; i < numEvents; i++) {
96             event= events[i];
97             if ((event.getKind() & DebugEvent.SUSPEND) != 0 && event.getSource() instanceof IJavaThread && event.isEvaluation()) {
98                 fTerminated= true;
99             }
100         }
101         DebugPlugin.getDefault(). removeDebugEventListener(this);
102     }
103
104 }
105
Popular Tags