package com.intellij.execution.actions;

import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.execution.ui.RunContentManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;

/*
 * Copyright (c) 2000-2004 by JetBrains s.r.o. All Rights Reserved.
 * Use is subject to license terms.
 */

public class StopAction extends AnAction{
  public void actionPerformed(final AnActionEvent e) {
    final RunContentDescriptor contentDescriptor = RunContentManager.RUN_CONTENT_DESCRIPTOR.getData(e.getDataContext());
    final ProcessHandler processHandler = contentDescriptor == null ? null : contentDescriptor.getProcessHandler();
    if(processHandler == null) return;
    if(processHandler.detachIsDefault()) {
      processHandler.detachProcess();
    } else {
      processHandler.destroyProcess();
    }
  }

  public void update(final AnActionEvent e) {
    final RunContentDescriptor contentDescriptor = RunContentManager.RUN_CONTENT_DESCRIPTOR.getData(e.getDataContext());
    final ProcessHandler processHandler = contentDescriptor == null? null : contentDescriptor.getProcessHandler();
    boolean enable = processHandler != null && !processHandler.isProcessTerminating() && !processHandler.isProcessTerminated();
    e.getPresentation().setEnabled(enable);
  }
}

@@ -34,16 +34,64 @@
 tions;%0A%0A
+import com.intellij.execution.ExecutionManager;%0A
 import c
@@ -349,16 +349,163 @@
 onEvent;
+%0Aimport com.intellij.openapi.actionSystem.PlatformDataKeys;%0Aimport com.intellij.openapi.project.Project;%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0A/*%0A * 
@@ -721,214 +721,40 @@
 nal 
-RunContentDescriptor contentDescriptor = RunContentManager.RUN_CONTENT_DESCRIPTOR.getData(e.getDataContext());%0A    final ProcessHandler processHandler = contentDescriptor == null ? null : contentDescriptor.
+ProcessHandler processHandler = 
 getP
@@ -763,24 +763,25 @@
 cessHandler(
+e
 );%0A    if(pr
@@ -992,32 +992,360 @@
 ctionEvent e) %7B%0A
+    final ProcessHandler processHandler = getProcessHandler(e);%0A    boolean enable = processHandler != null && !processHandler.isProcessTerminating() && !processHandler.isProcessTerminated();%0A    e.getPresentation().setEnabled(enable);%0A  %7D%0A%0A  @Nullable%0A  private static ProcessHandler getProcessHandler(final AnActionEvent e) %7B%0A
     final RunCon
@@ -1488,19 +1488,26 @@
 sHandler
- = 
+;%0A    if (
 contentD
@@ -1520,23 +1520,66 @@
 tor 
-=
+!
 = null
-? null :
+) %7B%0A      // toolwindow case%0A      processHandler =
  con
@@ -1622,174 +1622,410 @@
     
-boolea
+%7D%0A    else %7B%0A      // mai
 n 
+m
 en
-able = processHandler != null && !processHandler.isProcessTerminating() && !processHandler.isProcessTerminated();%0A    e.getPresentation().setEnabled(enable)
+u toolbar%0A      final Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());%0A      final RunContentDescriptor selectedContent = project == null? null : ExecutionManager.getInstance(project).getContentManager().getSelectedContent();%0A      processHandler = selectedContent == null? null : selectedContent.getProcessHandler();%0A    %7D%0A    return processHandler
 ;%0A  

fix wrong process being closed problem 2