/*
 * 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.ui.content.impl;

import com.intellij.openapi.util.Pair;
import com.intellij.ui.content.TabbedContent;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Konstantin Bulenkov
 */
public class TabbedContentImpl extends ContentImpl implements TabbedContent {
  private final List<Pair<String, JComponent>> myTabs = new ArrayList<Pair<String, JComponent>>();
  private String myPrefix;

  public TabbedContentImpl(JComponent component, String displayName, boolean isPinnable, String titlePrefix) {
    super(component, displayName, isPinnable);
    myPrefix = titlePrefix;
    addContent(component, displayName, true);
  }

  @Override
  public void addContent(@NotNull JComponent content, @NotNull String name, boolean selectTab) {
    myTabs.add(Pair.create(name, content));
    if (selectTab) {
      setComponent(content);
    }
  }

  @Override
  public void removeContent(@NotNull JComponent content) {
    Pair<String, JComponent> toRemove = null;
    for (Pair<String, JComponent> tab : myTabs) {
      if (tab.second == content) {
        toRemove = tab;
        break;
      }
    }
    myTabs.remove(toRemove);
  }

  @Override
  public void selectContent(int index) {
    Pair<String, JComponent> tab = myTabs.get(index);
    setDisplayName(tab.first);
    setComponent(tab.second);
  }

  @Override
  public String getTabName() {
    String selected = findTabNameByComponent(getComponent());
    if (myPrefix != null) {
      selected = myPrefix + selected;
    }
    return selected;
  }

  private String findTabNameByComponent(JComponent c) {
    for (Pair<String, JComponent> tab : myTabs) {
      if (tab.second == c) {
        return tab.first;
      }
    }
    return null;
  }

  @Override
  public List<Pair<String, JComponent>> getTabs() {
    return Collections.unmodifiableList(myTabs);
  }

  @Override
  public String getTitlePrefix() {
    return myPrefix;
  }

  @Override
  public void setTitlePrefix(String titlePrefix) {
    myPrefix = titlePrefix;
  }
}

@@ -783,16 +783,35 @@
 wing.*;%0A
+import java.awt.*;%0A
 import j
@@ -1482,103 +1482,430 @@
     
-myTabs.add(Pair.create(name, content));%0A    if (selectTab) %7B%0A      setComponent(content);%0A    %7D
+Pair%3CString, JComponent%3E tab = Pair.create(name, content);%0A    if (!myTabs.contains(tab)) %7B%0A      myTabs.add(tab);%0A    %7D%0A    if (selectTab && getComponent() != content) %7B%0A      setComponent(content);%0A    %7D%0A  %7D%0A%0A  @Override%0A  public void setComponent(JComponent component) %7B%0A    Container parent = getComponent().getParent();%0A    parent.remove(getComponent());%0A    parent.add(component);%0A%0A    super.setComponent(component);
 %0A  %7D

change component in toolwindow tab on selectTab