/*
 * @(#)MonthViewDemo.java
 *
 * Copyright (c) 2003-2004 Stand By Soft, Ltd. All rights reserved.
 *
 * This software is the proprietary information of Stand By Soft, Ltd.  
 * Use is subject to license terms.
 */
package com.standbysoft.demo.date;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Locale;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import com.standbysoft.component.date.swing.JMonthView;

/**
 * Shows the operations that can be performed on a <code>JMonthView</code> component.
 */
public class MonthViewDemo extends JPanel {
    /**
     * JMonthView component used to show the features.
     */
    private JMonthView monthView;

    public MonthViewDemo() {
        monthView = new JMonthView();

        setLayout(new GridBagLayout());
        add(monthView, new GridBagConstraints(00110.00.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(8505)00));
        add(createGeneralPanel()new GridBagConstraints(10111.00.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(05, -25)00));
        add(new JLabel()new GridBagConstraints(03410.02.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0555)00));
    }

    /**
     * Creates a panel that controls JMonthView specific features.
     
     @return actual panel
     */
    private JComponent createGeneralPanel() {
        JPanel generalPanel = new JPanel();
        generalPanel.setBorder(new TitledBorder("General"));
        generalPanel.setLayout(new GridBagLayout());
        
        JLabel localeLabel = new JLabel("Locale:");
        localeLabel.setDisplayedMnemonic('l');
        
        final JComboBox localeComboBox = new JComboBox(Locale.getAvailableLocales());
        localeLabel.setLabelFor(localeComboBox);

        localeComboBox.setSelectedItem(monthView.getLocale());
        localeComboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                if (evt.getStateChange() == ItemEvent.SELECTED) {
                    Locale locale = (LocalelocaleComboBox.getSelectedItem();
                    monthView.setLocale(locale);
                }
            }
        });
        localeComboBox.setRenderer(new DefaultListCellRenderer() {
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                JLabel l = (JLabelsuper.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                Locale locale = (Localevalue;
                l.setText(locale.getDisplayName());

                return l;
            }
        });
        
        final JCheckBox displayTodayCheckBox = new JCheckBox("Display Today Button");
        displayTodayCheckBox.setMnemonic('t');
        displayTodayCheckBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                try {
                    monthView.setDisplayToday(displayTodayCheckBox.isSelected());
                catch (Exception e) {
                    displayTodayCheckBox.setSelected(monthView.isDisplayToday());
                    JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(MonthViewDemo.this)"You can switch off empty selection only if at least one date is selected.""Empty Selection Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }null);
                }
            }
        });
        displayTodayCheckBox.setSelected(monthView.isDisplayToday());
        
        final JCheckBox showStatusBar = new JCheckBox("Show Status Bar");
        showStatusBar.setMnemonic('s');
        showStatusBar.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                try {
                    monthView.setStatusVisible(showStatusBar.isSelected());
                catch (Exception e) {
                    showStatusBar.setSelected(monthView.isStatusVisible());
                    JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(MonthViewDemo.this)"You can switch off empty selection only if at least one date is selected.""Empty Selection Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }null);
                }
            }
        });
        showStatusBar.setSelected(monthView.isStatusVisible());
        
        final JCheckBox emptySelectionCheckBox = new JCheckBox("Allow Empty Selection");
        emptySelectionCheckBox.setMnemonic('e');
        emptySelectionCheckBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                try {
                    monthView.setEmptySelectionAllowed(emptySelectionCheckBox.isSelected());
                catch (Exception e) {
                    emptySelectionCheckBox.setSelected(monthView.isEmptySelectionAllowed());
                    JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(MonthViewDemo.this)"You can switch off empty selection only if at least one date is selected.""Empty Selection Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }null);
                }
            }
        });
        emptySelectionCheckBox.setSelected(monthView.isEmptySelectionAllowed());

        generalPanel.add(displayTodayCheckBox, new GridBagConstraints(01210.00.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0555)00));
        generalPanel.add(showStatusBar, new GridBagConstraints(02210.00.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0555)00));
        generalPanel.add(emptySelectionCheckBox, new GridBagConstraints(03210.00.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0555)00));
        generalPanel.add(new JLabel()new GridBagConstraints(04312.01.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0555)00));
        generalPanel.add(localeLabel, new GridBagConstraints(05110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        generalPanel.add(localeComboBox, new GridBagConstraints(15110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        
        return generalPanel;
    }
    
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("JMonthView Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        MonthViewDemo newContentPane = new MonthViewDemo();
        newContentPane.setOpaque(true)//content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}