/*
 * @(#)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.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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;
import com.standbysoft.component.date.swing.plaf.DateUI;
import com.standbysoft.component.date.swing.plaf.basic.DefaultMonthUI;
import com.standbysoft.component.date.swing.plaf.basic.DefaultMonthViewUI;
import com.standbysoft.demo.date.plaf.MonthViewNoButtonsUI;
import com.standbysoft.demo.date.plaf.MonthROTitleUI;
import com.standbysoft.demo.date.plaf.MonthViewYearButtonsUI;
import com.standbysoft.demo.date.plaf.MonthWindowsDateTimeUI;

/**
 * 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 = createMonthView();

        setLayout(new GridBagLayout());
        add(createGeneralPanel()new GridBagConstraints(00111.00.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(8505)00));
        add(createGridPanel()new GridBagConstraints(10111.00.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(8505)00));
        add(monthView, new GridBagConstraints(01211.01.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(10505)00));
        add(new JLabel()new GridBagConstraints(03410.00.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0555)00));
    }

    /**
     * Creates the month view component used by this demo.
     
     @return month view component used by this demo.
     */
    private JMonthView createMonthView() {
        JMonthView monthView = new JMonthView() {
            /**
             * Resets the UI delegate for the internal JMonth components of the 
             * JMonthView. This method override is particular for this demo; you
             * don't have to do the same for a usual application.
             */
            public void updateUI() {
                putClientProperty("JMonthView.monthUI"null);
                super.updateUI();
            }
        };
        monthView.setColumns(3);
        monthView.setRows(2);
        monthView.setScrollingDelta(6);
        monthView.setStatusVisible(false);
        
        return monthView;
    }
    
    /**
     * 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) {
                monthView.setTodayButtonVisible(displayTodayCheckBox.isSelected());
            }
        });
        displayTodayCheckBox.setSelected(monthView.isTodayButtonVisible());
        
        final JCheckBox displayNoneCheckBox = new JCheckBox("Display None Button");
        displayNoneCheckBox.setMnemonic('n');
        displayNoneCheckBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                monthView.setNoneButtonVisible(displayNoneCheckBox.isSelected());
            }
        });
        displayNoneCheckBox.setSelected(monthView.isNoneButtonVisible());
        
        final JCheckBox showStatusBar = new JCheckBox("Show Status Bar");
        showStatusBar.setMnemonic('s');
        showStatusBar.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                monthView.setStatusVisible(showStatusBar.isSelected());
                displayTodayCheckBox.setEnabled(showStatusBar.isSelected());
                displayNoneCheckBox.setEnabled(showStatusBar.isSelected());
            }
        });
        showStatusBar.setSelected(monthView.isStatusVisible());
        displayTodayCheckBox.setEnabled(showStatusBar.isSelected());
        displayNoneCheckBox.setEnabled(showStatusBar.isSelected());
        
        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());
        
        JLabel monthViewUIDelegateLabel = new JLabel("Custom UI:");
        final JComboBox monthViewUIDelegateComboBox = new JComboBox(new String[] {"Default""Windows Date/Time""Read Only Title""Image Scroll Buttons"}) {
            /**
             * We need to override this to reselect the default UI for 
             * the JMonthView when the Look and Feel is changed.
             */
            public void updateUI() {
                super.updateUI();
                setSelectedIndex(0);
            }
        };
        monthViewUIDelegateLabel.setFont(monthViewUIDelegateLabel.getFont().deriveFont(Font.BOLD));
        monthViewUIDelegateLabel.setDisplayedMnemonic('u');
        monthViewUIDelegateLabel.setLabelFor(monthViewUIDelegateComboBox);

        monthViewUIDelegateComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (monthViewUIDelegateComboBox.getSelectedIndex() == 0) {
                    monthView.putClientProperty("JMonthView.monthUI", DefaultMonthUI.class.getName());
                    monthView.setUI((DateUI)DefaultMonthViewUI.createUI(monthView));
                    monthView.invalidate();
                else if (monthViewUIDelegateComboBox.getSelectedIndex() == 1) {
                    monthView.putClientProperty("JMonthView.monthUI", MonthWindowsDateTimeUI.class.getName());
                    monthView.setUI((DateUI)MonthViewNoButtonsUI.createUI(monthView));
                    monthView.invalidate();
                else if (monthViewUIDelegateComboBox.getSelectedIndex() == 2) {
                    monthView.putClientProperty("JMonthView.monthUI", MonthROTitleUI.class.getName());
                    monthView.setUI((DateUI)DefaultMonthViewUI.createUI(monthView));
                    monthView.invalidate();
                else if (monthViewUIDelegateComboBox.getSelectedIndex() == 3) {
                    monthView.putClientProperty("JMonthView.monthUI", DefaultMonthUI.class.getName());
                    monthView.setUI((DateUI)MonthViewYearButtonsUI.createUI(monthView));
                    monthView.invalidate();
                }
            }
        });

        generalPanel.add(monthViewUIDelegateLabel, new GridBagConstraints(10110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        generalPanel.add(monthViewUIDelegateComboBox, new GridBagConstraints(20110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        generalPanel.add(localeLabel, new GridBagConstraints(11110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        generalPanel.add(localeComboBox, new GridBagConstraints(21110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        generalPanel.add(showStatusBar, new GridBagConstraints(00110.00.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0555)00));
        generalPanel.add(displayTodayCheckBox, new GridBagConstraints(01110.00.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0555)00));
        generalPanel.add(displayNoneCheckBox, new GridBagConstraints(02110.00.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0555)00));
        generalPanel.add(emptySelectionCheckBox, new GridBagConstraints(03110.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));
        
        return generalPanel;
    }

    private JComponent createGridPanel() {
        JPanel gridPanel = new JPanel();
        gridPanel.setBorder(new TitledBorder("Grid"));
        gridPanel.setLayout(new GridBagLayout());
                
        JLabel rowsLabel = new JLabel("Rows:");
        rowsLabel.setDisplayedMnemonic('r');
        
        final JComboBox rowsComboBox = new JComboBox(new Integer[] {new Integer(1)new Integer(2)new Integer(3)new Integer(4)});
        rowsLabel.setLabelFor(rowsComboBox);

        rowsComboBox.setSelectedItem(new Integer(monthView.getRows()));
        rowsComboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                if (evt.getStateChange() == ItemEvent.SELECTED) {
                    Integer rows = (IntegerrowsComboBox.getSelectedItem();
                    monthView.setRows(rows.intValue());
                }
            }
        });
        
        JLabel columnsLabel = new JLabel("Columns:");
        columnsLabel.setDisplayedMnemonic('c');
        
        final JComboBox columnsComboBox = new JComboBox(new Integer[] {new Integer(1)new Integer(2)new Integer(3)new Integer(4)});
        columnsLabel.setLabelFor(columnsComboBox);

        columnsComboBox.setSelectedItem(new Integer(monthView.getColumns()));
        columnsComboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                if (evt.getStateChange() == ItemEvent.SELECTED) {
                    Integer columns = (IntegercolumnsComboBox.getSelectedItem();
                    monthView.setColumns(columns.intValue());
                }
            }
        });
        
        JLabel scrollingDeltaLabel = new JLabel("Scrolling Delta:");
        scrollingDeltaLabel.setDisplayedMnemonic('s');
        
        final JComboBox scrollingDeltaComboBox = new JComboBox(new Integer[] {new Integer(1)new Integer(2)new Integer(3)new Integer(4)new Integer(5)new Integer(6)new Integer(7)new Integer(8)new Integer(9)new Integer(10)new Integer(11)new Integer(12)});
        scrollingDeltaComboBox.setMaximumRowCount(12);
        scrollingDeltaLabel.setLabelFor(scrollingDeltaComboBox);

        scrollingDeltaComboBox.setSelectedItem(new Integer(monthView.getScrollingDelta()));
        scrollingDeltaComboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                if (evt.getStateChange() == ItemEvent.SELECTED) {
                    Integer delta = (IntegerscrollingDeltaComboBox.getSelectedItem();
                    monthView.setScrollingDelta(delta.intValue());
                }
            }
        });
        
        gridPanel.add(rowsLabel, new GridBagConstraints(00110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        gridPanel.add(rowsComboBox, new GridBagConstraints(10110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        gridPanel.add(columnsLabel, new GridBagConstraints(01110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        gridPanel.add(columnsComboBox, new GridBagConstraints(11110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        gridPanel.add(scrollingDeltaLabel, new GridBagConstraints(02110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        gridPanel.add(scrollingDeltaComboBox, new GridBagConstraints(12110.00.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0555)00));
        gridPanel.add(new JLabel()new GridBagConstraints(04312.01.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0555)00));

        return gridPanel;
    }
    
    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();
            }
        });
    }
}