private Content createTerminalInContentPanel(
      @NotNull AbstractTerminalRunner terminalRunner, final @NotNull ToolWindow toolWindow) {
    SimpleToolWindowPanel panel = new SimpleToolWindowPanel(false, true);

    final Content content = ContentFactory.SERVICE.getInstance().createContent(panel, "", false);
    content.setCloseable(true);

    myTerminalWidget = terminalRunner.createTerminalWidget(content);
    myTerminalWidget.addTabListener(
        new TabbedTerminalWidget.TabListener() {
          @Override
          public void tabClosed(JediTermWidget terminal) {
            UIUtil.invokeLaterIfNeeded(
                () -> {
                  if (myTerminalWidget != null) {
                    hideIfNoActiveSessions(toolWindow, myTerminalWidget);
                  }
                });
          }
        });

    panel.setContent(myTerminalWidget.getComponent());
    panel.addFocusListener(createFocusListener());

    ActionToolbar toolbar = createToolbar(terminalRunner, myTerminalWidget, toolWindow);
    toolbar.getComponent().addFocusListener(createFocusListener());
    toolbar.setTargetComponent(panel);
    panel.setToolbar(toolbar.getComponent());

    content.setPreferredFocusableComponent(myTerminalWidget.getComponent());

    return content;
  }
    @Override
    public void actionPerformed(AnActionEvent e) {
      myTerminal.closeCurrentSession();

      hideIfNoActiveSessions(myToolWindow, myTerminal);
    }
 private static void hideIfNoActiveSessions(
     @NotNull final ToolWindow toolWindow, @NotNull JBTabbedTerminalWidget terminal) {
   if (terminal.isNoActiveSessions()) {
     toolWindow.getContentManager().removeAllContents(true);
   }
 }
 private JComponent getComponentToFocus() {
   return myTerminalWidget != null ? myTerminalWidget.getComponent() : null;
 }