public static void showTestResultsToolWindow(
      @NotNull final Project project, @NotNull final String message, boolean solved) {
    final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
    ToolWindow window = toolWindowManager.getToolWindow(StudyTestResultsToolWindowFactoryKt.ID);
    if (window == null) {
      toolWindowManager.registerToolWindow(
          StudyTestResultsToolWindowFactoryKt.ID, true, ToolWindowAnchor.BOTTOM);
      window = toolWindowManager.getToolWindow(StudyTestResultsToolWindowFactoryKt.ID);
      new StudyTestResultsToolWindowFactory().createToolWindowContent(project, window);
    }

    final Content[] contents = window.getContentManager().getContents();
    for (Content content : contents) {
      final JComponent component = content.getComponent();
      if (component instanceof ConsoleViewImpl) {
        ((ConsoleViewImpl) component).clear();
        if (!solved) {
          ((ConsoleViewImpl) component).print(message, ConsoleViewContentType.ERROR_OUTPUT);
        } else {
          ((ConsoleViewImpl) component).print(message, ConsoleViewContentType.NORMAL_OUTPUT);
        }
        window.setAvailable(true, () -> {});
        window.show(() -> {});
        return;
      }
    }
  }
  public void createToolWindowContent(@NotNull ToolWindow toolWindow) {
    // Create runner UI layout
    RunnerLayoutUi.Factory factory = RunnerLayoutUi.Factory.getInstance(myProject);
    RunnerLayoutUi layoutUi = factory.create("", "", "session", myProject);

    // Adding actions
    DefaultActionGroup group = new DefaultActionGroup();
    layoutUi.getOptions().setLeftToolbar(group, ActionPlaces.UNKNOWN);

    Content console =
        layoutUi.createContent(
            GradleConsoleToolWindowFactory.ID, myConsoleView.getComponent(), "", null, null);
    AnAction[] consoleActions = myConsoleView.createConsoleActions();
    for (AnAction action : consoleActions) {
      if (!shouldIgnoreAction(action)) {
        group.add(action);
      }
    }
    layoutUi.addContent(console, 0, PlaceInGrid.right, false);

    JComponent layoutComponent = layoutUi.getComponent();
    myConsolePanel.add(layoutComponent, BorderLayout.CENTER);

    //noinspection ConstantConditions
    Content content =
        ContentFactory.SERVICE.getInstance().createContent(layoutComponent, null, true);
    toolWindow.getContentManager().addContent(content);
  }
 public void clear() {
   if (myConsoleView.isShowing()) {
     myConsoleView.clear();
   } else {
     // "clear" does not work when the console is not visible. We need to flush the text from
     // previous sessions. It has to be done in the
     // UI thread, but we cannot call "invokeLater" because it will delete text belonging to the
     // current session.
     ApplicationManager.getApplication()
         .invokeAndWait(
             new Runnable() {
               @Override
               public void run() {
                 // "flushDeferredText" is really "delete text from previous sessions and leave the
                 // text of the current session untouched."
                 myConsoleView.flushDeferredText();
               }
             },
             ModalityState.NON_MODAL);
   }
 }
 /**
  * Gets highlighted information from test console. Some parts of output (like file links) may be
  * highlighted, and you need to check them.
  *
  * @return pair of [[ranges], [texts]] where range is [from,to] in doc. for each region, and
  *     "text" is text extracted from this region. For example assume that in document "spam eggs
  *     ham" words "ham" and "spam" are highlighted. You should have 2 ranges (0, 4) and (10, 13)
  *     and 2 strings (spam and ham)
  */
 @NotNull
 public Pair<List<Pair<Integer, Integer>>, List<String>> getHighlightedStringsInConsole() {
   final List<String> resultStrings = new ArrayList<>();
   final List<Pair<Integer, Integer>> resultRanges = new ArrayList<>();
   ApplicationManager.getApplication()
       .invokeAndWait(
           () -> {
             myConsole.flushDeferredText();
             final Editor editor = myConsole.getEditor();
             for (final RangeHighlighter highlighter :
                 editor.getMarkupModel().getAllHighlighters()) {
               if (highlighter instanceof RangeHighlighterEx) {
                 final int start = ((RangeHighlighterEx) highlighter).getAffectedAreaStartOffset();
                 final int end = ((RangeHighlighterEx) highlighter).getAffectedAreaEndOffset();
                 resultRanges.add(Pair.create(start, end));
                 resultStrings.add(editor.getDocument().getText().substring(start, end));
               }
             }
           },
           ModalityState.NON_MODAL);
   return Pair.create(resultRanges, resultStrings);
 }
 /** @return Short-cut to get text from console */
 @NotNull
 public String getAllConsoleText() {
   return myConsole.getEditor().getDocument().getText();
 }
 public void print(@NotNull String text, @NotNull ConsoleViewContentType contentType) {
   myConsoleView.print(text, contentType);
 }