private void initConsoleUI(Process process) {
    // Init console view
    myConsoleView = createConsoleView();
    myConsoleView.setBorder(new SideBorder(UIUtil.getBorderColor(), SideBorder.LEFT));

    myProcessHandler = createProcessHandler(process, myProvider.getCommandLineString());

    myConsoleExecuteActionHandler = createConsoleExecuteActionHandler();

    ProcessTerminatedListener.attach(myProcessHandler);

    myProcessHandler.addProcessListener(
        new ProcessAdapter() {
          @Override
          public void processTerminated(ProcessEvent event) {
            finishConsole();
          }
        });

    // Attach to process
    myConsoleView.attachToProcess(myProcessHandler);

    // Runner creating
    final Executor defaultExecutor =
        ExecutorRegistry.getInstance().getExecutorById(DefaultRunExecutor.EXECUTOR_ID);
    final DefaultActionGroup toolbarActions = new DefaultActionGroup();
    final ActionToolbar actionToolbar =
        ActionManager.getInstance()
            .createActionToolbar(ActionPlaces.UNKNOWN, toolbarActions, false);

    // Runner creating
    final JPanel panel = new JPanel(new BorderLayout());
    panel.add(actionToolbar.getComponent(), BorderLayout.WEST);
    panel.add(myConsoleView.getComponent(), BorderLayout.CENTER);

    actionToolbar.setTargetComponent(panel);

    final RunContentDescriptor contentDescriptor =
        new RunContentDescriptor(
            myConsoleView, myProcessHandler, panel, constructConsoleTitle(myConsoleTitle));

    // tool bar actions
    final List<AnAction> actions =
        fillToolBarActions(toolbarActions, defaultExecutor, contentDescriptor);
    registerActionShortcuts(actions, getLanguageConsole().getConsoleEditor().getComponent());
    registerActionShortcuts(actions, panel);
    panel.updateUI();
    showConsole(defaultExecutor, contentDescriptor);

    // Run
    myProcessHandler.startNotify();
  }
 public void update(final AnActionEvent e) {
   final EditorEx editor = myLanguageConsole.getConsoleEditor();
   final Lookup lookup = LookupManager.getActiveLookup(editor);
   e.getPresentation()
       .setEnabled(
           !editor.isRendererMode()
               && !myProcessHandler.isProcessTerminated()
               && (lookup == null || !(lookup.isCompletion() && lookup.isFocused())));
 }
  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 = 0;
      for (RunContentDescriptor dsc : consoles) {
        ProcessHandler handler = dsc.getProcessHandler();
        if (handler != null && !handler.isProcessTerminated()) {
          if (max == 0) {
            max = 1;
          }
          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;
  }