package de.javasoft.synthetica.democenter.examples.jytabbedpane;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import de.javasoft.swing.JYTabbedPane;
import de.javasoft.swing.JYTabbedPane.CloseButtonStrategy;
import de.javasoft.swing.plaf.jytabbedpane.Tab;

/**
 * Simple JYTabbedPane demo with closeable tabs and customized close action.  
 */
@SuppressWarnings("serial")
public class CloseableTabsTabbedPane extends JFrame
{
  public CloseableTabsTabbedPane()
  {
    super("CloseButton JYTabbedPane");
    
    final JYTabbedPane tabbedPane = new JYTabbedPane();
//@highlight
    tabbedPane.setCloseButtonStrategy(CloseButtonStrategy.ALL_TABS);
//@/highlight

    //we want to control the close action by ourselves
//@highlight
    tabbedPane.getActionMap().put("closeTab", new AbstractAction()
    {
      public void actionPerformed(ActionEvent evt)
      {
        Tab tab = (Tab)SwingUtilities.getAncestorOfClass(Tab.class, (Component) evt.getSource());
        int result = JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(tab), "Really close tab number #" + (tab.getTabIndex()+1) + " ?", "Confirmation", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION)
          tabbedPane.removeTabAt(tab.getTabIndex());
      }
    });
//@/highlight

    tabbedPane.addTab("Closeable Tab", new JScrollPane(new JTextArea("Component A")));
    tabbedPane.addTab("Closeable Tab", new JScrollPane(new JTextArea("Component B")));
    add(tabbedPane);
    
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(400, 300);
    setLocationRelativeTo(null);        //center
    setVisible(true);
  }

  /**
   * Static main method for application startup. 
   */
  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel");
          new CloseableTabsTabbedPane();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
}
