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

import com.intellij.ide.actions.CloseTabToolbarAction;
import com.intellij.openapi.actionSystem.*;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import com.intellij.ui.content.ContentManagerAdapter;
import com.intellij.ui.content.ContentManagerEvent;
import com.intellij.ui.tabs.impl.JBTabsImpl;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NonNls;

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


public abstract class PanelWithActionsAndCloseButton extends JPanel implements DataProvider {
  protected final ContentManager myContentManager;
  private final String myHelpId;
  private final boolean myVerticalToolbar;
  private boolean myCloseEnabled;
  private final DefaultActionGroup myToolbarGroup = new DefaultActionGroup(null, false);

  public PanelWithActionsAndCloseButton(ContentManager contentManager, @NonNls String helpId) {
    this(contentManager, helpId, true);
  }

  public PanelWithActionsAndCloseButton(ContentManager contentManager, @NonNls String helpId, final boolean verticalToolbar) {
    super(new BorderLayout());
    myContentManager = contentManager;
    myHelpId = helpId;
    myVerticalToolbar = verticalToolbar;
    myCloseEnabled = true;

    if (myContentManager != null) {
      myContentManager.addContentManagerListener(new ContentManagerAdapter(){
        public void contentRemoved(ContentManagerEvent event) {
          if (event.getContent().getComponent() == PanelWithActionsAndCloseButton.this) {
            dispose();
            myContentManager.removeContentManagerListener(this);
          }
        }
      });
    }

  }

  public String getHelpId() {
    return myHelpId;
  }

  protected void disableClose() {
    myCloseEnabled = false;
  }

  protected void init(){
    addActionsTo(myToolbarGroup);
    myToolbarGroup.add(new MyCloseAction());
    myToolbarGroup.add(ActionManager.getInstance().getAction(IdeActions.ACTION_CONTEXT_HELP));


    ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.FILEHISTORY_VIEW_TOOLBAR, myToolbarGroup, ! myVerticalToolbar);
    JComponent centerPanel = createCenterPanel();
    toolbar.setTargetComponent(centerPanel);

    add(centerPanel, BorderLayout.CENTER);
    if (myVerticalToolbar) {
      add(toolbar.getComponent(), BorderLayout.WEST);
    } else {
      add(toolbar.getComponent(), BorderLayout.NORTH);
    }
  }

  public Object getData(String dataId) {
    if (PlatformDataKeys.HELP_ID.is(dataId)){
      return myHelpId;
    }
    return null;
  }

  protected abstract JComponent createCenterPanel();

  protected void addActionsTo(DefaultActionGroup group) {}

  protected void dispose() {}

  private class MyCloseAction extends CloseTabToolbarAction {
    @Override
    public void update(AnActionEvent e) {
      super.update(e);
      e.getPresentation().setVisible(myCloseEnabled);
    }

    public void actionPerformed(AnActionEvent e) {
      if (myContentManager != null) {
        Content content = myContentManager.getContent(PanelWithActionsAndCloseButton.this);
        if (content != null) {
          myContentManager.removeContent(content, true);
        } else {
          final JBTabsImpl tabs = UIUtil.getParentOfType(JBTabsImpl.class, PanelWithActionsAndCloseButton.this);
          if (tabs != null && tabs.getSelectedInfo() != null) {
            tabs.removeTab(tabs.getSelectedInfo());
            if (tabs.getTabCount() == 0) {
              Content tabbedContent = myContentManager.getContent(tabs);
              if (tabbedContent == null) tabbedContent = myContentManager.getContent(tabs.getComponent()); // one more try for wrappers
              if (tabbedContent != null) {
                myContentManager.removeContent(tabbedContent, true);
              }
            }
          }
        }
      }
    }
  }
}

@@ -762,249 +762,9 @@
 ent.
-Content;%0Aimport com.intellij.ui.content.ContentManager;%0Aimport com.intellij.ui.content.ContentManagerAdapter;%0Aimport com.intellij.ui.content.ContentManagerEvent;%0Aimport com.intellij.ui.tabs.impl.JBTabsImpl;%0Aimport com.intellij.util.ui.UIUtil
+*
 ;%0Aim
@@ -3510,72 +3510,99 @@
     
-myC
+if (c
 ontent
-Manager.removeContent(content, true);%0A        %7D else
+ instanceof TabbedContent && ((TabbedContent)content).getTabs().size() %3E 1)
  %7B%0A
+  
     
@@ -3617,334 +3617,124 @@
 nal 
-JB
 Tab
-sImpl tabs = UIUtil.getParentOfType(JBTabsImpl.class, PanelWithActionsAndCloseButton.this);%0A          if (tabs != null && tabs.getSelectedInfo() != null) %7B%0A            tabs.removeTab(tabs.getSelectedInfo());%0A            if (tabs.getTabCount() == 0) %7B%0A              C
+bedContent tabbedContent = (TabbedContent)content;%0A            final JComp
 on
-t
 ent 
-tabbedC
+comp
 on
-t
 ent = 
-myC
+c
 ontent
-Manager.getContent(tabs
+.getComponent(
 );%0A 
@@ -3736,38 +3736,32 @@
 ();%0A            
-  if (
 tabbedContent ==
@@ -3761,33 +3761,46 @@
 tent
- == null) tabbedContent =
+.removeContent(component);%0A           
  myC
@@ -3817,117 +3817,99 @@
 ger.
-getContent(tabs.getComponent()); // one more try for wrappers%0A              if (tabbedContent != null) %7B%0A    
+setSelectedContent(content, true, true); //we should request focus here%0A          %7D else %7B%0A
     
@@ -3947,23 +3947,17 @@
 Content(
-tabbedC
+c
 ontent, 
@@ -3967,38 +3967,8 @@
 e);%0A
-              %7D%0A            %7D%0A
     

make tool window Close button work with tabbed content