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

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import de.javasoft.swing.DateComboBox;

/**
 * A simple DateComboBox application used to demonstrate and explain  
 * essential functionality.
 */
@SuppressWarnings("serial")
public class SimpleDateComboBox extends JFrame
{
  public SimpleDateComboBox()
  {
    super("Simple DateComboBox");
    
//@highlight
    //create and add the combobox panel and its components
    JPanel panel = new JPanel();
    createAndAddComponents(panel);
    add(panel);
//@/highlight
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);        //center
    setVisible(true);
  }

  /**
   * Adds labels and comboboxes to the panel - using GridBagLayout.
   */
  private void createAndAddComponents(JPanel panel)
  {
    final JLabel dateLabel = new JLabel("---"); 
    
    panel.setLayout(new GridBagLayout());    
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.weightx = 1.0;
    gbc.insets = new Insets(5,5,5,5);
    gbc.fill = GridBagConstraints.HORIZONTAL;

    gbc.gridx = 0;
    gbc.gridy = 0;
    panel.add(new JLabel("1. Non-editable, Format (MM/dd/yy)"), gbc);
    gbc.gridx = 1;
//@highlight
    DateComboBox dateCombo1 = new DateComboBox();
    dateCombo1.setDateFormat(new SimpleDateFormat("MM/dd/yy"));
    //enable year selection
    dateCombo1.getPopup().getMonthView().setZoomable(true);
    panel.add(dateCombo1, gbc);
//@/highlight

    gbc.gridx = 0;
    gbc.gridy = 1;
    panel.add(new JLabel("2. Non-editable, Format (dd.MM.yyyy)"), gbc);
    gbc.gridx = 1;
//@highlight
    DateComboBox dateCombo2 = new DateComboBox();
    dateCombo2.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
    panel.add(dateCombo2, gbc);
//@/highlight
    
    gbc.gridx = 0;
    gbc.gridy = 2;
    panel.add(new JLabel("3. Non-editable, Format (DateFormat.LONG)"), gbc);
    gbc.gridx = 1;
//@highlight
    DateComboBox dateCombo3 = new DateComboBox();
    dateCombo3.setDateFormat(DateFormat.getDateInstance(DateFormat.LONG));
    panel.add(dateCombo3, gbc);
//@/highlight

    gbc.gridx = 0;
    gbc.gridy = 3;
    panel.add(new JLabel("4. Editable, Format (DateFormat.MEDIUM)"), gbc);
    gbc.gridx = 1;
//@highlight
    DateComboBox dateCombo4 = new DateComboBox();
    dateCombo4.setEditable(true);
    dateCombo4.setDateFormat(DateFormat.getDateInstance(DateFormat.MEDIUM));
    panel.add(dateCombo4, gbc);
//@/highlight

    gbc.gridx = 0;
    gbc.gridy = 4;
    panel.add(new JLabel("<html>5. Editable, accepts multiple formats<br>(EEE MM/dd/yy, MMddyy, MM/dd/yyyy)</html>"), gbc);
    gbc.gridx = 1;
//@highlight
    DateComboBox dateCombo5 = new DateComboBox();
    dateCombo5.setEditable(true);
    DateFormat[] dateFormats = new DateFormat[]{new SimpleDateFormat("EEE MM/dd/yy"),
                                                new SimpleDateFormat("MMddyy"),
                                                new SimpleDateFormat("MM/dd/yyyy")};
    dateCombo5.setDateFormats(dateFormats);
    dateCombo5.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
        DateComboBox combo = (DateComboBox)evt.getSource();
        if (combo.getDate() != null)
          dateLabel.setText(combo.getDate().toString());
      }
    });
    panel.add(dateCombo5, gbc);
//@/highlight
    
    gbc.insets = new Insets(25,5,5,5);
    gbc.gridx = 0;
    gbc.gridy = 5;
    panel.add(new JLabel("Non-editable JComboBox"), gbc);
    gbc.gridx = 1;
    JComboBox combo1 = new JComboBox(new Object[]{"Item 1", "Item 2", "Another Item"});
    panel.add(combo1, gbc);

    gbc.insets = new Insets(5,5,5,5);
    gbc.gridx = 0;
    gbc.gridy = 6;
    panel.add(new JLabel("Editable JComboBox"), gbc);
    gbc.gridx = 1;
    JComboBox combo2 = new JComboBox(new Object[]{"Item 1", "Item 2", "Another Item"});
    combo2.setEditable(true);
    panel.add(combo2, gbc);

    gbc.insets = new Insets(15,5,5,5);
    gbc.gridx = 0;
    gbc.gridy = 7;
    gbc.gridwidth = 2;
    JPanel p = new JPanel();
    p.setBorder(new TitledBorder("Selected date (5-th comboBox)"));
    p.add(dateLabel);
    panel.add(p, gbc);    
    
    
    panel.setBorder(new EmptyBorder(10,10,10,10));    
  }

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