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.SimpleDateFormat;

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;

/**
 * DateComboBox demo including time.
 */
@SuppressWarnings("serial")
public class DateTimeComboBox extends JFrame
{
  public DateTimeComboBox()
  {
    super("Date/Time DateComboBox");
    
    JPanel panel = new JPanel();
    createAndAddComponents(panel);
    add(panel);
    //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("Date, Time DateComboBox"), gbc);
    gbc.gridx++;
//@highlight
    DateComboBox dateCombo = new DateComboBox();
    dateCombo.setEditable(true);
    dateCombo.setDateFormat(new SimpleDateFormat("MM/dd/yyyy HH:mm"));
    dateCombo.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
        DateComboBox combo = (DateComboBox)evt.getSource();
        dateLabel.setText(combo.getDateTime().toString());
      }
    });
    panel.add(dateCombo, gbc);
//@/highlight
    
    gbc.insets = new Insets(15,5,5,5);
    gbc.gridx = 0;
    gbc.gridy++;
    gbc.gridwidth = 2;
    JPanel p = new JPanel();
    p.setBorder(new TitledBorder("Selected date/time"));
    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 DateTimeComboBox();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
}
