package com.intellij.ui;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CustomShortcutSet;
import com.intellij.util.ui.UIUtil;

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

/**
 * @author evgeny zakrevsky
 */
public class HideableTitledPanel extends JPanel {
  private final static Icon OFF_ICON = AllIcons.General.ComboArrow;
  private final static Icon ON_ICON = AllIcons.General.ComboUpPassive;

  private TitledSeparatorWithMnemonic myTitledSeparator;
  private boolean myOn;
  private final JComponent myContent;
  private Dimension myPreviousContentSize;
  private boolean myMnemonicActionRegistered = false;

  public HideableTitledPanel(String title, JComponent content, boolean on) {
    super(new BorderLayout());
    myContent = content;
    add(myContent, BorderLayout.CENTER);
    myTitledSeparator = new TitledSeparatorWithMnemonic("", null);
    add(myTitledSeparator, BorderLayout.NORTH);
    myTitledSeparator.getLabel().setIcon(OFF_ICON);

    myTitledSeparator.getLabel().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    myTitledSeparator.getLabel().addMouseListener(new MouseAdapter() {
      @Override
      public void mouseReleased(MouseEvent e) {
        if (myOn) {
          off();
        }
        else {
          on();
        }
      }
    });

    setOn(on);
    setTitle(title);

    addHierarchyListener(new HierarchyListener() {
      @Override
      public void hierarchyChanged(HierarchyEvent e) {
        final JComponent rootPane = getRootPane();
        if (rootPane != null && !myMnemonicActionRegistered) {
          final int mnemonicIndex = UIUtil.getDisplayMnemonicIndex(getTitle());
          if (mnemonicIndex != -1) {
            AnAction action = new AnAction() {
              public void actionPerformed(AnActionEvent e) {
                if (myOn) {
                  off();
                }
                else {
                  on();
                }
              }
            };
            final Character mnemonicCharacter = UIUtil.removeMnemonic(getTitle()).toLowerCase().charAt(mnemonicIndex);
            action.registerCustomShortcutSet(
              new CustomShortcutSet(KeyStroke.getKeyStroke(mnemonicCharacter, InputEvent.ALT_MASK)), rootPane);
            myMnemonicActionRegistered = true;
          }
        }
      }
    });
  }

  public void setOn(boolean on) {
    myOn = on;
    if (myOn) {
      on();
    }
    else {
      off();
    }
  }

  public void setTitle(String title) {
    myTitledSeparator.setText(title);
  }

  public String getTitle() {
    return myTitledSeparator.getText();
  }

  protected void on() {
    myOn = true;
    myTitledSeparator.getLabel().setIcon(ON_ICON);
    myContent.setVisible(true);
    adjustWindow();
    invalidate();
    repaint();
  }

  protected void off() {
    myOn = false;
    myTitledSeparator.getLabel().setIcon(OFF_ICON);
    myContent.setVisible(false);
    myPreviousContentSize = myContent.getSize();
    adjustWindow();
    invalidate();
    repaint();
  }

  private void adjustWindow() {
    final Window window = SwingUtilities.getWindowAncestor(this);
    if (window == null) return;
    final Dimension size = window.getSize();
    final Dimension contentSize = myPreviousContentSize != null && myPreviousContentSize.width > 0 && myPreviousContentSize.height > 0
                                  ? myPreviousContentSize
                                  : myContent.getPreferredSize();
    final Dimension newSize;
    if (myOn) {
      newSize = new Dimension(Math.max(size.width, myContent.getSize().width), size.height + contentSize.height);
    }
    else {
      newSize = new Dimension(size.width, size.height - contentSize.height);
    }
    if (!newSize.equals(size)) {
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          if (window.isShowing()) {
            window.setSize(newSize);
          }
        }
      });
    }
  }

  @Override
  public void setEnabled(boolean enabled) {
    myTitledSeparator.myLabel.setForeground(enabled ? UIUtil.getActiveTextColor() : UIUtil.getInactiveTextColor());
    myContent.setEnabled(enabled);
  }
}

@@ -59,175 +59,8 @@
 ns;%0A
-import com.intellij.openapi.actionSystem.AnAction;%0Aimport com.intellij.openapi.actionSystem.AnActionEvent;%0Aimport com.intellij.openapi.actionSystem.CustomShortcutSet;%0A
 impo
@@ -91,16 +91,16 @@
 UIUtil;%0A
+
 %0Aimport 
@@ -550,62 +550,8 @@
 ize;
-%0A  private boolean myMnemonicActionRegistered = false;
 %0A%0A  
@@ -1261,999 +1261,8 @@
 le);
-%0A%0A    addHierarchyListener(new HierarchyListener() %7B%0A      @Override%0A      public void hierarchyChanged(HierarchyEvent e) %7B%0A        final JComponent rootPane = getRootPane();%0A        if (rootPane != null && !myMnemonicActionRegistered) %7B%0A          final int mnemonicIndex = UIUtil.getDisplayMnemonicIndex(getTitle());%0A          if (mnemonicIndex != -1) %7B%0A            AnAction action = new AnAction() %7B%0A              public void actionPerformed(AnActionEvent e) %7B%0A                if (myOn) %7B%0A                  off();%0A                %7D%0A                else %7B%0A                  on();%0A                %7D%0A              %7D%0A            %7D;%0A            final Character mnemonicCharacter = UIUtil.removeMnemonic(getTitle()).toLowerCase().charAt(mnemonicIndex);%0A            action.registerCustomShortcutSet(%0A              new CustomShortcutSet(KeyStroke.getKeyStroke(mnemonicCharacter, InputEvent.ALT_MASK)), rootPane);%0A            myMnemonicActionRegistered = true;%0A          %7D%0A        %7D%0A      %7D%0A    %7D);
 %0A  %7D
@@ -3042,24 +3042,24 @@
 xtColor());%0A
-
     myConten
@@ -3084,11 +3084,672 @@
 ed);%0A  %7D
+%0A%0A  @Override%0A  public void addNotify() %7B%0A    super.addNotify();%0A    final int mnemonicIndex = UIUtil.getDisplayMnemonicIndex(getTitle());%0A    if (mnemonicIndex != -1) %7B%0A      getActionMap().put(%22tt%22, new AbstractAction() %7B%0A        @Override%0A        public void actionPerformed(ActionEvent e) %7B%0A          if (myOn) %7B%0A            off();%0A          %7D%0A          else %7B%0A            on();%0A          %7D%0A        %7D%0A      %7D);%0A      final Character mnemonicCharacter = UIUtil.removeMnemonic(getTitle()).toUpperCase().charAt(mnemonicIndex);%0A      getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(mnemonicCharacter, InputEvent.ALT_MASK, false), %22tt%22);%0A    %7D%0A  %7D
 %0A%7D%0A

fix mnemonic in hideable titled panel