// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.wm.impl.content;

import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.ui.Gray;
import com.intellij.ui.awt.RelativeRectangle;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManagerEvent;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

abstract class ContentLayout {

  static final Color TAB_BORDER_ACTIVE_WINDOW = new Color(38, 63, 106);
  static final Color TAB_BORDER_PASSIVE_WINDOW = new Color(130, 120, 111);

  static final Color TAB_BG_ACTIVE_WND_SELECTED_FROM = Gray._111;
  static final Color TAB_BG_ACTIVE_WND_SELECTED_TO = Gray._164;
  static final Color TAB_BG_ACTIVE_WND_UNSELECTED_FROM = Gray._130;
  static final Color TAB_BG_ACTIVE_WND_UNSELECTED_TO = Gray._85;
  static final Color TAB_BG_PASSIVE_WND_FROM = new Color(152, 143, 134);
  static final Color TAB_BG_PASSIVE_WND_TO = new Color(165, 157, 149);

  static final int TAB_ARC = 2;
  static final int TAB_SHIFT = 2;

  ToolWindowContentUi myUi;
  BaseLabel myIdLabel;

  ContentLayout(ToolWindowContentUi ui) {
    myUi = ui;
  }

  public abstract void init();

  public abstract void reset();

  public abstract void layout();

  public abstract void paintComponent(Graphics g);

  public abstract void paintChildren(Graphics g);

  public abstract void update();

  public abstract void rebuild();

  public abstract int getMinimumWidth();

  public abstract void contentAdded(ContentManagerEvent event);

  public abstract void contentRemoved(ContentManagerEvent event);

  protected void updateIdLabel(BaseLabel label) {
    String title = myUi.myWindow.getStripeTitle();
    if (myUi.myManager.getContentCount() != 1 || myUi.myManager.canCloseContents()) {
      title += ":";
    }
    label.setText(title);

    label.setBorder(JBUI.Borders.empty(0, 2, 0, 7));
    label.setVisible(shouldShowId());
  }

  protected void fillTabShape(Graphics2D g2d, Shape shape, boolean isSelected, Rectangle bounds) {
    if (myUi.myWindow.isActive()) {
      if (isSelected) {
        g2d.setPaint(UIUtil.getGradientPaint(bounds.x, bounds.y, TAB_BG_ACTIVE_WND_SELECTED_FROM, bounds.x, (float)bounds.getMaxY(),
                                             TAB_BG_ACTIVE_WND_SELECTED_TO));
      }
      else {
        g2d.setPaint(UIUtil.getGradientPaint(bounds.x, bounds.y, TAB_BG_ACTIVE_WND_UNSELECTED_FROM, bounds.x, (float)bounds.getMaxY(), TAB_BG_ACTIVE_WND_UNSELECTED_TO));
      }
    }
    else {
      g2d.setPaint(
        UIUtil.getGradientPaint(bounds.x, bounds.y, TAB_BG_PASSIVE_WND_FROM, bounds.x, (float)bounds.getMaxY(), TAB_BG_PASSIVE_WND_TO));
    }

    g2d.fill(shape);
  }

  public abstract void showContentPopup(ListPopup listPopup);

  public abstract RelativeRectangle getRectangleFor(Content content);

  public abstract Component getComponentFor(Content content);

  public abstract String getCloseActionName();
  public abstract String getCloseAllButThisActionName();
  public abstract String getPreviousContentActionName();
  public abstract String getNextContentActionName();

  protected boolean shouldShowId() {
    final JComponent component = myUi.myWindow.getComponent();
    return component != null && !"true".equals(component.getClientProperty(ToolWindowContentUi.HIDE_ID_LABEL));
  }

  boolean isIdVisible() {
    return myIdLabel.isVisible();
  }
}

@@ -493,42 +493,8 @@
 .*;%0A
-import javax.swing.border.Border;%0A
 impo
@@ -1828,119 +1828,88 @@
 ();%0A
+%0A
     
-if (myUi.myManager.getContentCount() != 1 %7C%7C myUi.myManager.canCloseContents()) %7B%0A      title += %22:%22;%0A    %7D
+String suffix = getTitleSuffix();%0A    if (suffix != null) title += suffix;%0A
 %0A   
@@ -1931,17 +1931,16 @@
 title);%0A
-%0A
     labe
@@ -2027,16 +2027,494 @@
 );%0A  %7D%0A%0A
+  private String getTitleSuffix() %7B%0A    switch (myUi.myManager.getContentCount()) %7B%0A      case 0:%0A        return null;%0A      case 1:%0A        Content content = myUi.myManager.getContent(0);%0A        if (content == null) return null;%0A%0A        final String text = content.getDisplayName();%0A        if (text != null && text.trim().length() %3E 0 && myUi.myManager.canCloseContents()) %7B%0A          return %22:%22;%0A        %7D%0A        return null;%0A      default:%0A        return %22:%22;%0A    %7D%0A  %7D%0A%0A
   protec

remove colon from toolwindows header with single blank titled tab (#IDEA-190646)