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.JComponent;
import javax.swing.JFrame;
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;

/**
 * JYTabbedPane demo with plus button support.  
 */
@SuppressWarnings("serial")
public class PlusButtonTabbedPane extends JFrame
{
  public PlusButtonTabbedPane()
  {
    super("PlusButton JYTabbedPane");
    
    JYTabbedPane tabbedPane = new JYTabbedPane();
//@highlight
    tabbedPane.getActionMap().put("addTab", new AbstractAction()
    {
      public void actionPerformed(ActionEvent evt)
      {
        JYTabbedPane tabPane = (JYTabbedPane)(SwingUtilities.getAncestorOfClass(JYTabbedPane.class, (Component)evt.getSource()));
        String id = (tabPane.getTabCount()+1)+"";
        tabPane.addTab("New Tab " + id, createTabbedPaneContent(id));
        //select new tab
        if (tabPane.getTabCount() > 1)
          tabPane.setSelectedIndex(tabPane.getTabCount()-1, true);
      }
    });
//@/highlight

    tabbedPane.setCloseButtonStrategy(CloseButtonStrategy.ALL_TABS);
//@highlight
    //invoke after putting "addTab" action
    tabbedPane.setShowPlusButton(true);
//@/highlight

    tabbedPane.addTab("Tab 1", createTabbedPaneContent("1"));
    tabbedPane.addTab("Tab 2", createTabbedPaneContent("2"));
    add(tabbedPane);
    
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(400, 300);
    setLocationRelativeTo(null);
    setVisible(true);
  }
  
  private JComponent createTabbedPaneContent(String id)
  {
    return new JScrollPane(new JTextArea("Component " + id));    
  }

  /**
   * 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 PlusButtonTabbedPane();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
}
