1 /******************************************************************************* 2 * Copyright (c) 2000, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.internal.debug.ui.actions; 15 16 17 import org.eclipse.debug.core.DebugEvent; 18 import org.eclipse.debug.core.DebugException; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.IDebugEventSetListener; 21 import org.eclipse.jdt.debug.core.IJavaThread; 22 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 23 import org.eclipse.jface.action.IAction; 24 import org.eclipse.jface.dialogs.IDialogConstants; 25 import org.eclipse.jface.dialogs.MessageDialog; 26 import org.eclipse.jface.viewers.ISelection; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.swt.widgets.Display; 29 import org.eclipse.ui.IObjectActionDelegate; 30 import org.eclipse.ui.IWorkbenchPart; 31 32 /** 33 * Attempts to terminate an evaluation running in an IJavaThread. 34 */ 35 public class TerminateEvaluationAction implements IObjectActionDelegate, IDebugEventSetListener { 36 37 private IJavaThread fThread; 38 private boolean fTerminated; 39 40 @Override setActivePart(IAction action, IWorkbenchPart targetPart)41 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 42 } 43 44 @Override run(IAction action)45 public void run(IAction action) { 46 if (fThread == null) { 47 return; 48 } 49 DebugPlugin.getDefault().addDebugEventListener(this); 50 Thread timerThread= new Thread(new Runnable() { 51 @Override 52 public void run() { 53 fTerminated= false; 54 try { 55 Thread.sleep(3000); 56 } catch (InterruptedException e) { 57 return; 58 } 59 if (!fTerminated) { 60 fTerminated= true; 61 final Display display= JDIDebugUIPlugin.getStandardDisplay(); 62 display.asyncExec(new Runnable() { 63 @Override 64 public void run() { 65 MessageDialog dialog = new MessageDialog(display.getActiveShell(), ActionMessages.TerminateEvaluationActionTerminate_Evaluation_1, null, 66 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[] { IDialogConstants.OK_LABEL }, 0); 67 dialog.setBlockOnOpen(false); 68 dialog.open(); 69 } 70 }); 71 } 72 } 73 }); 74 timerThread.setDaemon(true); 75 timerThread.start(); 76 try { 77 fThread.terminateEvaluation(); 78 } catch (DebugException exception) { 79 JDIDebugUIPlugin.statusDialog(exception.getStatus()); 80 } 81 } 82 83 @Override selectionChanged(IAction action, ISelection selection)84 public void selectionChanged(IAction action, ISelection selection) { 85 if (selection instanceof IStructuredSelection) { 86 IStructuredSelection ss= (IStructuredSelection)selection; 87 if (ss.isEmpty() || ss.size() > 1) { 88 return; 89 } 90 Object element= ss.getFirstElement(); 91 if (element instanceof IJavaThread) { 92 setThread((IJavaThread)element); 93 } 94 } 95 } 96 setThread(IJavaThread thread)97 public void setThread(IJavaThread thread) { 98 fThread= thread; 99 } 100 101 @Override handleDebugEvents(DebugEvent[] events)102 public void handleDebugEvents(DebugEvent[] events) { 103 DebugEvent event; 104 for (int i= 0, numEvents= events.length; i < numEvents; i++) { 105 event= events[i]; 106 if ((event.getKind() & DebugEvent.SUSPEND) != 0 && event.getSource() instanceof IJavaThread && event.isEvaluation()) { 107 fTerminated= true; 108 } 109 } 110 DebugPlugin.getDefault(). removeDebugEventListener(this); 111 } 112 113 } 114