package com.intellij.execution;

import com.intellij.execution.executors.DefaultRunExecutor;
import com.intellij.execution.filters.Filter;
import com.intellij.execution.filters.TextConsoleBuilder;
import com.intellij.execution.filters.TextConsoleBuilderFactory;
import com.intellij.execution.process.ProcessAdapter;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.execution.ui.actions.CloseAction;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.openapi.wm.ToolWindowManager;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Runs a process and prints the output in a content tab within the Run toolwindow.
 *
 * @author yole
 */
public class RunContentExecutor {
  private final Project myProject;
  private final ProcessHandler myProcess;
  private final List<Filter> myFilterList = new ArrayList<Filter>();
  private Runnable myRerun;
  private Runnable myAfterCompletion;
  private String myTitle = "Output";
  private String myHelpId = null;

  public RunContentExecutor(Project project, ProcessHandler process) {
    myProject = project;
    myProcess = process;
  }

  public RunContentExecutor withFilter(Filter filter) {
    myFilterList.add(filter);
    return this;
  }

  public RunContentExecutor withTitle(String title) {
    myTitle = title;
    return this;
  }

  public RunContentExecutor withRerun(Runnable rerun) {
    myRerun = rerun;
    return this;
  }

  public RunContentExecutor withAfterCompletion(Runnable afterCompletion) {
    myAfterCompletion = afterCompletion;
    return this;
  }

  public RunContentExecutor withHelpId(String helpId) {
    myHelpId = helpId;
    return this;
  }

  private ConsoleView createConsole(Project project, ProcessHandler processHandler) {
    TextConsoleBuilder consoleBuilder = TextConsoleBuilderFactory.getInstance().createBuilder(project);
    for (Filter filter : myFilterList) {
      consoleBuilder.addFilter(filter);
    }
    ConsoleView console = consoleBuilder.getConsole();
    console.attachToProcess(processHandler);
    return console;
  }

  public void run() {
    FileDocumentManager.getInstance().saveAllDocuments();

    ConsoleView view = createConsole(myProject, myProcess);
    if (myHelpId != null) {
      view.setHelpId(myHelpId);
    }
    Executor executor = ExecutorRegistry.getInstance().getExecutorById(DefaultRunExecutor.EXECUTOR_ID);
    DefaultActionGroup actions = new DefaultActionGroup();

    final JComponent consolePanel = createConsolePanel(view, actions);
    RunContentDescriptor descriptor = new RunContentDescriptor(view, myProcess, consolePanel, myTitle);

    actions.add(new RerunAction(consolePanel));
    actions.add(new CloseAction(executor, descriptor, myProject));

    ExecutionManager.getInstance(myProject).getContentManager().showRunContent(executor, descriptor);

    ApplicationManager.getApplication().invokeLater(new Runnable() {
      public void run() {
        ToolWindowManager.getInstance(myProject).getToolWindow(ToolWindowId.RUN).activate(null);
      }
    });

    if (myAfterCompletion != null) {
      myProcess.addProcessListener(new ProcessAdapter() {
        public void processTerminated(ProcessEvent event) {
          SwingUtilities.invokeLater(myAfterCompletion);
        }
      });
    }

    myProcess.startNotify();
  }

  private static JComponent createConsolePanel(ConsoleView view, ActionGroup actions) {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(view.getComponent(), BorderLayout.CENTER);
    panel.add(createToolbar(actions), BorderLayout.WEST);
    return panel;
  }

  private static JComponent createToolbar(ActionGroup actions) {
    ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, actions, false);
    return actionToolbar.getComponent();
  }

  private class RerunAction extends AnAction implements DumbAware {
    public RerunAction(JComponent consolePanel) {
      super("Rerun", "Rerun",
            IconLoader.getIcon("/actions/refreshUsages.png"));
      registerCustomShortcutSet(CommonShortcuts.getRerun(), consolePanel);
    }

    public void actionPerformed(AnActionEvent e) {
      myRerun.run();
    }

    @Override
    public void update(AnActionEvent e) {
      e.getPresentation().setVisible(myRerun != null);
    }
  }
}

@@ -1487,16 +1487,63 @@
  = null;
+%0A  private boolean myActivateToolWindow = true;
 %0A%0A  publ
@@ -2202,24 +2202,174 @@
  this;%0A  %7D%0A%0A
+  public RunContentExecutor withActivateToolWindow(boolean activateToolWindow) %7B%0A    myActivateToolWindow = activateToolWindow;%0A    return this;%0A  %7D%0A%0A
   private Co
@@ -3532,211 +3532,69 @@
     
-ApplicationManager.getApplication().invokeLater(new Runnable() %7B%0A      public void run() %7B%0A        ToolWindowManager.getInstance(myProject).getToolWindow(ToolWindowId.RUN).activate(null);%0A      %7D%0A    %7D);
+if (myActivateToolWindow) %7B%0A      activateToolWindow();%0A    %7D
 %0A%0A  
@@ -3826,24 +3826,24 @@
  %7D);%0A    %7D%0A%0A
-
     myProces
@@ -3856,32 +3856,282 @@
 tNotify();%0A  %7D%0A%0A
+  public void activateToolWindow() %7B%0A    ApplicationManager.getApplication().invokeLater(new Runnable() %7B%0A      public void run() %7B%0A        ToolWindowManager.getInstance(myProject).getToolWindow(ToolWindowId.RUN).activate(null);%0A      %7D%0A    %7D);%0A  %7D%0A%0A
   private static

allow to don't activate run tool window automatically after process start