private void assertTabCount(Content content) {
    //        PluginSettingsBean bean = (PluginSettingsBean)
    // SharedObjectPool.getUserData(SharedConstants.PLUGIN_SETTINGS);
    PluginSettingsBean bean = PluginKeys.PLUGIN_SETTINGS.getData();

    if (bean == null) {
      bean = new PluginSettingsBean();
    }

    int max = bean.getNumberOfTabs();

    if (max <= getTabComponent(content).getTabCount()) {
      // remove the oldest tab
      JTabbedPane tabbedPane = getTabComponent(content);
      long lastMin = Long.MAX_VALUE;
      int index = 0;
      for (int i = 0; i < tabbedPane.getTabCount(); i++) {
        JComponent tab = (JComponent) tabbedPane.getTabComponentAt(i);
        Long time = (Long) tab.getClientProperty(CREATE_TIME);
        if (time != null && lastMin < time) {
          lastMin = time;
          index = i;
        }
      }

      tabbedPane.remove(index);
    }
  }
  private QueryResultPanel getSelectedTab(@NotNull Content content) {
    //        Component[] comps = (content.getComponent()).getComponents();
    //        JTabbedPane tabbedPane = (JTabbedPane) comps[1];
    JTabbedPane tabbedPane = getTabComponent(content);
    int index = tabbedPane.getSelectedIndex();
    if (index == -1) {
      return null;
    }

    return (QueryResultPanel) tabbedPane.getComponentAt(index);
  }
  private void removeTab(@NotNull Content content, String displayName) {
    //        Component[] comps = (content.getComponent()).getComponents();
    //        JTabbedPane tabbedPane = (JTabbedPane) comps[1];
    JTabbedPane tabbedPane = getTabComponent(content);
    int index = tabbedPane.indexOfTab(displayName);
    if (index == -1) {
      return;
    }

    tabbedPane.remove(index);
  }
  private QueryStatisticsPanel addDMLStatsTab(
      @NotNull Content content, SqlStatementMarker sqlMarker, Icon icon, String s) {
    //        Component[] comps = (content.getComponent()).getComponents();
    //        JTabbedPane tabbedPane = (JTabbedPane) comps[1];
    JTabbedPane tabbedPane = getTabComponent(content);
    QueryStatisticsPanel dmlStatsPanel = new QueryStatisticsPanel();
    dmlStatsPanel.putClientProperty(CREATE_TIME, new Date().getTime());

    tabbedPane.addTab(sqlMarker.getName(), dmlStatsPanel);
    int index = tabbedPane.indexOfTab(sqlMarker.getName());
    tabbedPane.setTabComponentAt(index, new ButtonTabComponent(tabbedPane, icon, sqlMarker));

    return dmlStatsPanel;
  }
  private DataGridPanel addGridPanelTab(
      @NotNull Content content, SqlStatementMarker sqlMarker, Icon icon, String toolTip) {
    JTabbedPane tabbedPane = getTabComponent(content);
    DataGridPanel dataGridPanel = new DataGridPanel();
    dataGridPanel.putClientProperty(CREATE_TIME, new Date().getTime());

    tabbedPane.addTab(sqlMarker.getName(), dataGridPanel);
    int index = tabbedPane.indexOfTab(sqlMarker.getName());
    ButtonTabComponent buttonTab = new ButtonTabComponent(tabbedPane, icon, sqlMarker);
    if (toolTip != null) {
      buttonTab.setToolTipText(toolTip);
    }
    tabbedPane.setTabComponentAt(index, buttonTab);
    return dataGridPanel;
  }
  /**
   * Set the requested content "selected" and show ToolWindow if it hided
   *
   * @param displayName
   * @param wait
   */
  public void showContent(String displayName, final int wait) {
    //        Project project =
    // DataKeys.PROJECT.getData(DataManager.getInstance().getDataContext());
    ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
    ToolWindow wm = toolWindowManager.getToolWindow(QUERY_RESULT_PANE);

    if (wm == null) {
      wm = toolWindowManager.registerToolWindow(QUERY_RESULT_PANE, true, ToolWindowAnchor.BOTTOM);
      wm.setIcon(Icons.QUERY_RESULT_PANE);
      wm.setToHideOnEmptyContent(true);
    }

    String contentName = connectionManager.getDbUrl().getUserHostPortServiceName().toLowerCase();
    Content content = wm.getContentManager().findContent(contentName);

    if (content == null) {
      return;
    }

    wm.getContentManager().setSelectedContent(content);
    JTabbedPane tabbedPane = getTabComponent(content);
    int index = tabbedPane.indexOfTab(displayName);
    if (index == -1) {
      return;
    }

    tabbedPane.requestFocusInWindow();
    tabbedPane.setSelectedIndex(index);

    if (!wm.isVisible()) {
      wm.activate(
          new Runnable() {
            public void run() {
              try {
                Thread.sleep(wait);
              } catch (InterruptedException e1) {
              }
            }
            //            }, false);
          },
          true);
    }
  }
  @NotNull
  private Content createContent(String contentName) {
    if (project == null) {
      throw new IllegalStateException("Project not opened");
    }

    ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
    ToolWindow wm = ToolWindowManager.getInstance(project).getToolWindow(QUERY_RESULT_PANE);

    if (wm == null) {
      wm = toolWindowManager.registerToolWindow(QUERY_RESULT_PANE, true, ToolWindowAnchor.BOTTOM);
      wm.setIcon(Icons.QUERY_RESULT_PANE);
      wm.setToHideOnEmptyContent(true);
    }

    Content content = wm.getContentManager().findContent(contentName);
    if (content == null) {
      JTabbedPane tabbedPane = new JTabbedPane();
      tabbedPane.addContainerListener(new JTabbedPaneListener());

      tabbedPane.addKeyListener(
          new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
              ((JComponent) e.getComponent()).putClientProperty(JTABBED_CONTROL, e.isControlDown());
            }

            public void keyReleased(KeyEvent e) {
              ((JComponent) e.getComponent()).putClientProperty(JTABBED_CONTROL, e.isControlDown());
            }
          });
      tabbedPane.addFocusListener(
          new FocusAdapter() {
            public void focusGained(FocusEvent e) {
              Object source = e.getSource();
              Component c = e.getOppositeComponent();
            }

            public void focusLost(FocusEvent e) {
              ((JComponent) e.getSource()).putClientProperty(JTABBED_CONTROL, false);
            }
          });

      JPanel jpanel = new JPanel(new BorderLayout());

      DefaultActionGroup actionGroup = new DefaultActionGroup("PsiActionGroup22", false);
      actionGroup.add(
          new LocalToggleAction(
              "Refresh Query Result",
              "Refresh Query Result",
              Icons.REFRESH_RESULTSET,
              REFRESH_RESULTSET));
      actionGroup.add(new LocalToggleAction("Copy", "Copy", Icons.EXPORT_DATA, EXPORT_DATA));

      // todo -- problem with keeping sql markers and tabs in sync
      //            actionGroup.add(new LocalToggleAction("Close", "Close", Icons.CLOSE_PANEL,
      // CLOSE_PANEL));

      ActionManager actionManager = ActionManager.getInstance();
      ActionToolbar toolBar =
          actionManager.createActionToolbar("DataGridActionToolbar", actionGroup, false);
      jpanel.add(toolBar.getComponent(), "West");
      jpanel.add(tabbedPane, "Center");

      //            ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
      //            ContentFactory contentFactory = PeerFactory.getInstance().getContentFactory();
      ContentFactory contentFactory = ContentFactoryEx.getContentFactory();
      content = contentFactory.createContent(jpanel, contentName, false);
      // content.setActions(actionGroup, "UKNOWN", null);
      content.setIcon(Icons.DISCONNECT);
      wm.getContentManager().addContent(content);
    }

    return content;
  }