/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.execution.actions;

import com.intellij.execution.KillableProcess;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.project.DumbAwareAction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author Sergey Simonchik
 */
public class StopProcessAction extends DumbAwareAction implements AnAction.TransparentUpdate {

  private final ProcessHandler myProcessHandler;

  public StopProcessAction(@NotNull String text, @Nullable String description, @NotNull ProcessHandler processHandler) {
    super(text, description, AllIcons.Actions.Suspend);
    myProcessHandler = processHandler;
  }

  @Override
  public void update(final AnActionEvent e) {
    update(e.getPresentation(), getTemplatePresentation(), myProcessHandler);
  }

  public static void update(@NotNull Presentation presentation,
                            @NotNull Presentation templatePresentation,
                            @Nullable ProcessHandler processHandler) {
    boolean enable = false;
    Icon icon = templatePresentation.getIcon();
    String description = templatePresentation.getDescription();
    if (processHandler != null && !processHandler.isProcessTerminated()) {
      enable = true;
      if (processHandler.isProcessTerminating() && processHandler instanceof KillableProcess) {
        KillableProcess killableProcess = (KillableProcess) processHandler;
        if (killableProcess.canKillProcess()) {
          // 'force quite' action presentation
          icon = AllIcons.Debugger.KillProcess;
          description = "Kill process";
        }
      }
    }
    presentation.setEnabled(enable);
    presentation.setIcon(icon);
    presentation.setDescription(description);
  }


  @Override
  public void actionPerformed(AnActionEvent e) {
    stopProcess(myProcessHandler);
  }

  public static void stopProcess(@NotNull ProcessHandler processHandler) {
    if (processHandler instanceof KillableProcess && processHandler.isProcessTerminating()) {
      // process termination was requested, but it's still alive
      // in this case 'force quit' will be performed
      ((KillableProcess)processHandler).killProcess();
      return;
    }

    if (processHandler.detachIsDefault()) {
      processHandler.detachProcess();
    }
    else {
      processHandler.destroyProcess();
    }
  }

}

@@ -1238,22 +1238,16 @@
 private 
-final 
 ProcessH
@@ -1353,22 +1353,23 @@
 tion, @N
-otN
 ull
+able
  Process
@@ -1494,16 +1494,135 @@
 r;%0A  %7D%0A%0A
+  public void setProcessHandler(@Nullable ProcessHandler processHandler) %7B%0A    myProcessHandler = processHandler;%0A  %7D%0A%0A
   @Overr
@@ -2833,22 +2833,23 @@
 ocess(@N
-otN
 ull
+able
  Process
@@ -3162,16 +3162,52 @@
 %0A    %7D%0A%0A
+    if (processHandler != null) %7B%0A  
     if (
@@ -3238,24 +3238,26 @@
 efault()) %7B%0A
+  
       proces
@@ -3282,26 +3282,30 @@
 cess();%0A    
+ 
+ 
 %7D%0A
+  
     else %7B%0A 
@@ -3303,16 +3303,18 @@
  else %7B%0A
+  
       pr
@@ -3340,24 +3340,32 @@
 yProcess();%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A%7D

allow to set process handler