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

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import de.javasoft.swing.DropDownButton;

/**
 * A simple DropDownButton application used to demonstrate and explain  
 * basic functionality.
 */
@SuppressWarnings("serial")
public class SimpleDropDownButton extends JFrame
{
  public SimpleDropDownButton()
  {
    super("Simple DropDownButton");

    //create and add the toolbar
    JToolBar toolBar = new JToolBar();
    createAndAddButtons(toolBar);
    add(toolBar, BorderLayout.NORTH);
    
    //create and add the buttonpanel
    JPanel panel = new JPanel();
    createAndAddButtons(panel);
    add(panel);
    
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    pack();
    //center
    setLocationRelativeTo(null);
    setVisible(true);
  }

  /**
   * Creates components and adds them to the container.
   */
  private void createAndAddButtons(Container container)
  {
    Icon icon = new ImageIcon(getClass().getResource("/de/javasoft/synthetica/democenter/examples/dropdownbutton/jyloo.png"));

    Action action = new AbstractAction()
    {
      public void actionPerformed(ActionEvent evt)
      {
        new JDialog(SimpleDropDownButton.this, true);        
        JOptionPane.showMessageDialog(SimpleDropDownButton.this, "You've pressed the button...");
      }
    };

//@highlight
    //create a DropDownButton with icon
    DropDownButton button1 = new DropDownButton(action);
    button1.setText("Small arrow");
    button1.setIcon(icon);
    addMenuItems(button1.getPopupMenu());

    //create a DropDownButton with icon and a large arrow icon
    DropDownButton button2 = new DropDownButton(action);
    button2.setText("Bigger arrow");
    button2.setIcon(icon);    
    addMenuItems(button2.getPopupMenu());
    button2.setUseSmallArrowIcon(false);
    button2.setHorizontalTextPosition(SwingConstants.CENTER);
    button2.setVerticalTextPosition(SwingConstants.BOTTOM);

    //create a DropDownButton with custom arrow icon and additional arrow icon space 
    DropDownButton button3 = new DropDownButton(action);
    button3.setText("Custom");
    button3.setIcon(icon);    
    addMenuItems(button3.getPopupMenu());
    Icon arrowIcon = new ImageIcon(getClass().getResource("/resources/icons/arrowDown.png"));
    button3.setArrowIcon(arrowIcon);
    button3.setArrowIconSpace(15);
//@/highlight
    
    container.add(button1);
    container.add(button2);
    container.add(button3);    
  }
  
  /**
   * Add menuItems to the specified popup menu. 
   */
  private void addMenuItems(JPopupMenu popupMenu)
  { 
    popupMenu.add(new JMenuItem("My first popup menu entry"));
    popupMenu.add(new JMenuItem("Second menu item"));
    popupMenu.add(new JMenuItem("MenuItem 3"));
  }
  
  /**
   * 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 SimpleDropDownButton();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
}
