package net.kenevans.dilbert;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import com.toedter.calendar.JCalendar;

/*
 * Created on Jan 6, 2012
 * By Kenneth Evans, Jr.
 */

public class JCalendarDialog extends JDialog
{
    private static final long serialVersionUID = 1L;
    private JPanel jContentPane;
    private JPanel mainPanel = null;
    private JButton okButton = null;
    private JButton cancelButton = null;
    private JPanel buttonPanel = null;
    private JCalendar jCal;

    private Date date;
    private Boolean canceled = false;

    /**
     * Constructor
     */
    public JCalendarDialog(Component parent, Date date) {
        super();
        this.date = date;
        setLocationRelativeTo(parent);
        initialize();
        // Locate it on the screen
        // this.setLocation(425, 490);
    }

    /**
     * This method initializes this dialog
     * 
     * @return void
     */
    private void initialize() {
        this.setModal(true);
        this.setTitle("Select Date");
        this.setContentPane(getJContentPane());
        pack();
    }

    /**
     * This method returns the jContentPane, creating it the first time.
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if(jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getMainPanel(), java.awt.BorderLayout.CENTER);
        }
        return jContentPane;
    }

    /**
     * This method returns the mainPanel, creating it the first time.
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getMainPanel() {
        if(mainPanel == null) {
            mainPanel = new JPanel();
            mainPanel
                .setLayout(new BoxLayout(getMainPanel(), BoxLayout.Y_AXIS));
            mainPanel.add(getJCalendar(), null);
            mainPanel.add(getButtonPanel(), null);
        }
        return mainPanel;
    }

    /**
     * This method initializes useFiltersCheckBox, creating it the first time.
     * 
     * @return javax.swing.JCheckBox
     */
    private JCalendar getJCalendar() {
        if(jCal == null) {
            jCal = new JCalendar(date);
        }
        return jCal;
    }

    /**
     * This method returns the buttonPanel, creating it the first time.
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getButtonPanel() {
        if(buttonPanel == null) {
            buttonPanel = new JPanel();
            buttonPanel.add(getOkButton(), null);
            buttonPanel.add(getCancelButton(), null);
        }
        return buttonPanel;
    }

    /**
     * This method returns the okButton, creating it the first time.
     * 
     * @return javax.swing.JButton
     */
    private JButton getOkButton() {
        if(okButton == null) {
            okButton = new JButton();
            okButton.setText("OK");
            okButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if(jCal != null) {
                        date = jCal.getDate();
                    } else {
                        date = null;
                    }
                    canceled = false;
                    setVisible(false);
                }
            });
        }
        return okButton;
    }

    /**
     * This method returns the cancelButton, creating it the first time.
     * 
     * @return javax.swing.JButton
     */
    private JButton getCancelButton() {
        if(cancelButton == null) {
            cancelButton = new JButton();
            cancelButton.setText("Cancel");
            cancelButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    canceled = true;
                    setVisible(false);
                }
            });
        }
        return cancelButton;
    }

    /**
     * @return The value of canceled.
     */
    public Boolean getCanceled() {
        return canceled;
    }

    /**
     * @return The value of date.
     */
    public Date getDate() {
        return date;
    }

    /**
     * @param date The new value for date.
     */
    public void setDate(Date date) {
        this.date = date;
    }

}
