Python execute in console: added selection of the console in case of more than one.
authorDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Wed, 2 Mar 2011 13:43:47 +0000 (16:43 +0300)
committerDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Thu, 3 Mar 2011 12:23:14 +0000 (15:23 +0300)
platform/lang-impl/src/com/intellij/execution/ExecutionHelper.java
platform/lang-impl/src/com/intellij/execution/runners/AbstractConsoleRunnerWithHistory.java

index 284c0f06c0f245e390747a34004d6e69fc90280b..9642213cc6446aaa18f0d6af818af566702fc7ad 100644 (file)
 
 package com.intellij.execution;
 
 
 package com.intellij.execution;
 
+import com.google.common.collect.Lists;
 import com.intellij.execution.executors.DefaultRunExecutor;
 import com.intellij.execution.process.OSProcessHandler;
 import com.intellij.execution.process.ProcessHandler;
 import com.intellij.execution.ui.RunContentDescriptor;
 import com.intellij.execution.ui.RunContentManagerImpl;
 import com.intellij.ide.errorTreeView.NewErrorTreeViewPanel;
 import com.intellij.execution.executors.DefaultRunExecutor;
 import com.intellij.execution.process.OSProcessHandler;
 import com.intellij.execution.process.ProcessHandler;
 import com.intellij.execution.ui.RunContentDescriptor;
 import com.intellij.execution.ui.RunContentManagerImpl;
 import com.intellij.ide.errorTreeView.NewErrorTreeViewPanel;
+import com.intellij.ide.ui.ListCellRendererWrapper;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.command.CommandProcessor;
 import com.intellij.openapi.components.ServiceManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.command.CommandProcessor;
 import com.intellij.openapi.components.ServiceManager;
 import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.progress.ProgressIndicator;
 import com.intellij.openapi.progress.ProgressManager;
 import com.intellij.openapi.progress.Task;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.Messages;
 import com.intellij.openapi.progress.ProgressIndicator;
 import com.intellij.openapi.progress.ProgressManager;
 import com.intellij.openapi.progress.Task;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.Messages;
+import com.intellij.openapi.ui.popup.PopupChooserBuilder;
 import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.wm.ToolWindow;
 import com.intellij.openapi.wm.ToolWindowId;
 import com.intellij.openapi.wm.ToolWindowManager;
 import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.wm.ToolWindow;
 import com.intellij.openapi.wm.ToolWindowId;
 import com.intellij.openapi.wm.ToolWindowManager;
+import com.intellij.ui.components.JBList;
 import com.intellij.ui.content.Content;
 import com.intellij.ui.content.ContentFactory;
 import com.intellij.ui.content.MessageView;
 import com.intellij.ui.content.Content;
 import com.intellij.ui.content.ContentFactory;
 import com.intellij.ui.content.MessageView;
+import com.intellij.util.Consumer;
 import com.intellij.util.Function;
 import com.intellij.util.NotNullFunction;
 import com.intellij.util.concurrency.Semaphore;
 import com.intellij.util.Function;
 import com.intellij.util.NotNullFunction;
 import com.intellij.util.concurrency.Semaphore;
@@ -48,6 +54,7 @@ import com.intellij.util.ui.MessageCategory;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import javax.swing.*;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -140,37 +147,115 @@ public class ExecutionHelper {
     }
   }
 
     }
   }
 
+  public static Collection<RunContentDescriptor> findRunningConsoleByCmdLine(final Project project,
+                                                                             @NotNull final NotNullFunction<String, Boolean> cmdLineMatcher) {
+    return findRunningConsole(project, new NotNullFunction<RunContentDescriptor, Boolean>() {
+      @NotNull
+      @Override
+      public Boolean fun(RunContentDescriptor selectedContent) {
+        final ProcessHandler processHandler = selectedContent.getProcessHandler();
+        if (processHandler instanceof OSProcessHandler && !processHandler.isProcessTerminated()) {
+          final String commandLine = ((OSProcessHandler)processHandler).getCommandLine();
+          return cmdLineMatcher.fun(commandLine);
+        }
+        return false;
+      }
+    });
+  }
+
+  public static Collection<RunContentDescriptor> findRunningConsoleByTitle(final Project project,
+                                                                           @NotNull final NotNullFunction<String, Boolean> titleMatcher) {
+    return findRunningConsole(project, new NotNullFunction<RunContentDescriptor, Boolean>() {
+      @NotNull
+      @Override
+      public Boolean fun(RunContentDescriptor selectedContent) {
+        return titleMatcher.fun(selectedContent.getDisplayName());
+      }
+    });
+  }
+
   public static Collection<RunContentDescriptor> findRunningConsole(final Project project,
   public static Collection<RunContentDescriptor> findRunningConsole(final Project project,
-                                                                    @NotNull final NotNullFunction<String, Boolean> cmdLineMatcher) {
+                                                                    @NotNull final NotNullFunction<RunContentDescriptor, Boolean> descriptorMatcher) {
     final ExecutionManager executionManager = ExecutionManager.getInstance(project);
     final ExecutionManager executionManager = ExecutionManager.getInstance(project);
-    final ToolWindow runToolWindow = ToolWindowManager.getInstance(project).getToolWindow(DefaultRunExecutor.getRunExecutorInstance().getId());
+    final ToolWindow runToolWindow =
+      ToolWindowManager.getInstance(project).getToolWindow(DefaultRunExecutor.getRunExecutorInstance().getId());
     if (runToolWindow != null && runToolWindow.isVisible()) {
       final RunContentDescriptor selectedContent = executionManager.getContentManager().getSelectedContent();
     if (runToolWindow != null && runToolWindow.isVisible()) {
       final RunContentDescriptor selectedContent = executionManager.getContentManager().getSelectedContent();
-      if (selectedContent != null){
-        final ProcessHandler processHandler = selectedContent.getProcessHandler();
-        if (processHandler instanceof OSProcessHandler && !processHandler.isProcessTerminated()) {
-          final String commandLine = ((OSProcessHandler)processHandler).getCommandLine();
-          if (cmdLineMatcher.fun(commandLine).booleanValue()) {
-            return Collections.singletonList(selectedContent);
-          }
+      if (selectedContent != null) {
+        if (descriptorMatcher.fun(selectedContent)) {
+          return Collections.singletonList(selectedContent);
         }
       }
     }
 
         }
       }
     }
 
-    final ArrayList<RunContentDescriptor> result = new ArrayList<RunContentDescriptor>();
+    final ArrayList<RunContentDescriptor> result = Lists.newArrayList();
     final RunContentDescriptor[] runContentDescriptors = ((RunContentManagerImpl)executionManager.getContentManager()).getAllDescriptors();
     for (RunContentDescriptor runContentDescriptor : runContentDescriptors) {
     final RunContentDescriptor[] runContentDescriptors = ((RunContentManagerImpl)executionManager.getContentManager()).getAllDescriptors();
     for (RunContentDescriptor runContentDescriptor : runContentDescriptors) {
-      final ProcessHandler processHandler = runContentDescriptor.getProcessHandler();
-      if (processHandler instanceof OSProcessHandler && !processHandler.isProcessTerminated()) {
-        final String commandLine = ((OSProcessHandler)processHandler).getCommandLine();
-        if (cmdLineMatcher.fun(commandLine).booleanValue()) {
-          result.add(runContentDescriptor);
-        }
+      if (descriptorMatcher.fun(runContentDescriptor)) {
+        result.add(runContentDescriptor);
+      }
+    }
+    return result;
+  }
+
+  public static List<RunContentDescriptor> collectConsolesByDisplayName(final Project project,
+                                                                        @NotNull NotNullFunction<String, Boolean> titleMatcher) {
+    List<RunContentDescriptor> result = Lists.newArrayList();
+    final ExecutionManager executionManager = ExecutionManager.getInstance(project);
+    final RunContentDescriptor[] runContentDescriptors = ((RunContentManagerImpl)executionManager.getContentManager()).getAllDescriptors();
+    for (RunContentDescriptor runContentDescriptor : runContentDescriptors) {
+      if (titleMatcher.fun(runContentDescriptor.getDisplayName())) {
+        result.add(runContentDescriptor);
       }
     }
     return result;
   }
 
       }
     }
     return result;
   }
 
+  public static void selectContentDescriptor(final @NotNull Editor editor,
+                                                             @NotNull Collection<RunContentDescriptor> consoles,
+                                                             String selectDialogTitle, final Consumer<RunContentDescriptor> descriptorConsumer) {
+    if (consoles.size() == 1) {
+      RunContentDescriptor descriptor = consoles.iterator().next();
+      descriptorConsumer.consume(descriptor);
+      descriptorToFront(editor, descriptor);
+    }
+    else if (consoles.size() > 1) {
+      final JList list = new JBList(consoles);
+      final Icon icon = DefaultRunExecutor.getRunExecutorInstance().getIcon();
+      list.setCellRenderer(new ListCellRendererWrapper<RunContentDescriptor>(list.getCellRenderer()) {
+        @Override
+        public void customize(final JList list,
+                              final RunContentDescriptor value,
+                              final int index,
+                              final boolean selected,
+                              final boolean hasFocus) {
+          setText(value.getDisplayName());
+          setIcon(icon);
+        }
+      });
+
+      final PopupChooserBuilder builder = new PopupChooserBuilder(list);
+      builder.setTitle(selectDialogTitle);
+
+      builder.setItemChoosenCallback(new Runnable() {
+        @Override
+        public void run() {
+          final Object selectedValue = list.getSelectedValue();
+          if (selectedValue instanceof RunContentDescriptor) {
+            RunContentDescriptor descriptor = (RunContentDescriptor)selectedValue;
+            descriptorConsumer.consume(descriptor);
+            descriptorToFront(editor, descriptor);
+          }
+        }
+      }).createPopup().showInBestPositionFor(editor);
+    }
+  }
+
+  private static void descriptorToFront(Editor editor, RunContentDescriptor descriptor) {
+    ExecutionManager.getInstance(editor.getProject()).getContentManager()
+      .toFrontRunContent(DefaultRunExecutor.getRunExecutorInstance(), descriptor);
+  }
+
   public static class RailsErrorViewPanel extends NewErrorTreeViewPanel {
     public RailsErrorViewPanel(final Project project) {
       super(project, "reference.toolWindows.messages");
   public static class RailsErrorViewPanel extends NewErrorTreeViewPanel {
     public RailsErrorViewPanel(final Project project) {
       super(project, "reference.toolWindows.messages");
@@ -199,7 +284,8 @@ public class ExecutionHelper {
             processHandler.waitFor();
           }
         };
             processHandler.waitFor();
           }
         };
-      } else {
+      }
+      else {
         process = createTimelimitedExecutionProcess(processHandler, mode.getTimeout());
       }
     }
         process = createTimelimitedExecutionProcess(processHandler, mode.getTimeout());
       }
     }
@@ -302,7 +388,8 @@ public class ExecutionHelper {
               LOG.error("Timeout (" + timeout + " sec) on executing: " + processHandler.getCommandLine());
               processHandler.destroyProcess();
             }
               LOG.error("Timeout (" + timeout + " sec) on executing: " + processHandler.getCommandLine());
               processHandler.destroyProcess();
             }
-          } finally {
+          }
+          finally {
             mySemaphore.up();
           }
         }
             mySemaphore.up();
           }
         }
index 4fa5ab1871b0783b7a3aa5e8f5e1af443215e764..becca3d05c6905e16df30715e374db96e9fe5275 100644 (file)
@@ -18,10 +18,7 @@ package com.intellij.execution.runners;
 import com.google.common.collect.Lists;
 import com.intellij.codeInsight.lookup.Lookup;
 import com.intellij.codeInsight.lookup.LookupManager;
 import com.google.common.collect.Lists;
 import com.intellij.codeInsight.lookup.Lookup;
 import com.intellij.codeInsight.lookup.LookupManager;
-import com.intellij.execution.ExecutionException;
-import com.intellij.execution.ExecutionManager;
-import com.intellij.execution.Executor;
-import com.intellij.execution.ExecutorRegistry;
+import com.intellij.execution.*;
 import com.intellij.execution.console.LanguageConsoleImpl;
 import com.intellij.execution.console.LanguageConsoleViewImpl;
 import com.intellij.execution.executors.DefaultRunExecutor;
 import com.intellij.execution.console.LanguageConsoleImpl;
 import com.intellij.execution.console.LanguageConsoleViewImpl;
 import com.intellij.execution.executors.DefaultRunExecutor;
@@ -46,6 +43,7 @@ import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.wm.IdeFocusManager;
 import com.intellij.openapi.wm.ToolWindow;
 import com.intellij.openapi.wm.ToolWindowManager;
 import com.intellij.openapi.wm.IdeFocusManager;
 import com.intellij.openapi.wm.ToolWindow;
 import com.intellij.openapi.wm.ToolWindowManager;
+import com.intellij.util.NotNullFunction;
 import com.intellij.util.PairProcessor;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import com.intellij.util.PairProcessor;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -139,7 +137,7 @@ public abstract class AbstractConsoleRunnerWithHistory {
     panel.add(myConsoleView.getComponent(), BorderLayout.CENTER);
 
     final RunContentDescriptor contentDescriptor =
     panel.add(myConsoleView.getComponent(), BorderLayout.CENTER);
 
     final RunContentDescriptor contentDescriptor =
-      new RunContentDescriptor(myConsoleView, myProcessHandler, panel, myConsoleTitle);
+      new RunContentDescriptor(myConsoleView, myProcessHandler, panel, constructConsoleTitle(myConsoleTitle));
 
 // tool bar actions
     final AnAction[] actions = fillToolBarActions(toolbarActions, defaultExecutor, contentDescriptor);
 
 // tool bar actions
     final AnAction[] actions = fillToolBarActions(toolbarActions, defaultExecutor, contentDescriptor);
@@ -152,6 +150,39 @@ public abstract class AbstractConsoleRunnerWithHistory {
     myProcessHandler.startNotify();
   }
 
     myProcessHandler.startNotify();
   }
 
+  private String constructConsoleTitle(final @NotNull String consoleTitle) {
+    if (shouldAddNumberToTitle()) {
+      List<RunContentDescriptor> consoles = ExecutionHelper.collectConsolesByDisplayName(myProject, new NotNullFunction<String, Boolean>() {
+        @NotNull
+        @Override
+        public Boolean fun(String dom) {
+          return dom.contains(consoleTitle);
+        }
+      });
+      int max = consoles.size() > 0 ? 1 : 0;
+      for (RunContentDescriptor dsc : consoles) {
+        try {
+          int num = Integer.parseInt(dsc.getDisplayName().substring(consoleTitle.length()+1, dsc.getDisplayName().length()-1));
+          if (num > max) {
+            max = num;
+          }
+        }
+        catch (Exception e) {
+          //skip
+        }
+      }
+      if (max >= 1) {
+        return consoleTitle + "(" + (max + 1) + ")";
+      }
+    }
+
+    return consoleTitle;
+  }
+
+  protected boolean shouldAddNumberToTitle() {
+    return false;
+  }
+
   protected void showConsole(Executor defaultExecutor, RunContentDescriptor myDescriptor) {
     // Show in run toolwindow
     ExecutionManager.getInstance(myProject).getContentManager().showRunContent(defaultExecutor, myDescriptor);
   protected void showConsole(Executor defaultExecutor, RunContentDescriptor myDescriptor) {
     // Show in run toolwindow
     ExecutionManager.getInstance(myProject).getContentManager().showRunContent(defaultExecutor, myDescriptor);