/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.wm.impl.content;

import com.intellij.ide.IdeEventQueue;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Pair;
import com.intellij.ui.ClickListener;
import com.intellij.ui.components.JBList;
import com.intellij.ui.content.TabbedContent;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

/**
 * @author Konstantin Bulenkov
 */
public class TabbedContentTabLabel extends ContentTabLabel {
  private final ComboIcon myComboIcon = new ComboIcon() {
    @Override
    public Rectangle getIconRec() {
      return new Rectangle(getWidth() - getIconWidth() - 3, 0, getIconWidth(), getHeight());
    }

    @Override
    public boolean isActive() {
      return true;
    }
  };
  private final TabbedContent myContent;

  public TabbedContentTabLabel(TabbedContent content, TabContentLayout layout) {
    super(content, layout);
    myContent = content;
    new ClickListener() {
      @Override
      public boolean onClick(@NotNull MouseEvent event, int clickCount) {
        showPopup();
        return true;
      }
    }.installOn(this);
  }

  private void showPopup() {
    IdeEventQueue.getInstance().getPopupManager().closeAllPopups();
    ArrayList<String> names = new ArrayList<String>();
    for (Pair<String, JComponent> tab : myContent.getTabs()) {
      names.add(tab.first);
    }
    final JBList list = new JBList(names);
    JBPopupFactory.getInstance().createListPopupBuilder(list).setItemChoosenCallback(new Runnable() {
      @Override
      public void run() {
        int index = list.getSelectedIndex();
        if (index != -1) {
          myContent.selectContent(index);
        }
      }
    }).createPopup().showUnderneathOf(this);
  }

  @Override
  public void update() {
    super.update();
    if (myContent != null) {
      setText(myContent.getTabName());
    }
    setHorizontalAlignment(LEFT);
  }

  @Override
  public Dimension getPreferredSize() {
    final Dimension size = super.getPreferredSize();
    return new Dimension(size.width + 12, size.height);
  }

  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    myComboIcon.paintIcon(this, g);
  }
}

@@ -894,24 +894,102 @@
 bedContent;%0A
+import com.intellij.util.NotNullFunction;%0Aimport com.intellij.util.ui.UIUtil;%0A
 import org.j
@@ -1037,24 +1037,63 @@
 ax.swing.*;%0A
+import javax.swing.border.EmptyBorder;%0A
 import java.
@@ -2208,16 +2208,367 @@
 names);%0A
+    list.installCellRenderer(new NotNullFunction%3CObject, JComponent%3E() %7B%0A      final JLabel label = new JLabel();%0A      %7B%0A        label.setBorder(new EmptyBorder(UIUtil.getListCellPadding()));%0A      %7D%0A      @NotNull%0A      @Override%0A      public JComponent fun(Object dom) %7B%0A        label.setText(dom.toString());%0A        return label;%0A      %7D%0A    %7D);%0A
     JBPo

add standard insets to list renderer for dropdown popup