/*
 * @(#)Main.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.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import com.standbysoft.component.date.swing.plaf.basic.BasicDatePickerUI;
import com.standbysoft.component.date.swing.plaf.basic.DefaultMonthUI;
import com.standbysoft.component.date.swing.plaf.basic.DefaultMonthViewUI;
import com.standbysoft.demo.date.plaf.ComboBoxTitleMonthUI;
import com.standbysoft.demo.date.plaf.CustomMonthViewUI;
import com.standbysoft.demo.date.plaf.KunststoffComboBoxUIExt;
import com.standbysoft.demo.date.plaf.MetouiaComboBoxUIExt;
import com.standbysoft.demo.date.plaf.NoButtonsMonthViewUI;
import com.standbysoft.demo.date.plaf.NoTitleMonthUI;
import com.standbysoft.demo.date.plaf.SimpleTitleMonthUI;
import com.standbysoft.demo.date.plaf.SpinnerTitleMonthUI;
import com.standbysoft.demo.date.plaf.TightLayoutMonthUI;

import edu.stanford.ejalbert.BrowserLauncher;

/**
 * The entry point for the Java Date Picker demo application. It assembles all other demos.
 */
public class Main {
    /**
     * Name of the product.
     */
    public static final String NAME = "Java Date Picker";

    /**
     * Product tag line.
     */
    public static final String MESSAGE = "Professional date components for Swing";
    
    /**
     * Current version.
     */
    public static final String VERSION = "3.1";
    
    private JFrame frame;
    
    private JTabbedPane panel;
    
    private Splash splashScreen;

    public Main(Splash asplashScreen) {
        this.splashScreen = asplashScreen;

        frame = new JFrame(NAME + " - " + MESSAGE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initComponents();
        
        registerDemos();

        frame.pack();
        center(frame);
        frame.setVisible(true);
    }
    
    private void initComponents() {
        final JComboBox lfComboBox = new JComboBox(UIManager.getInstalledLookAndFeels());
        JLabel lfLabel = new JLabel("Look and Feel:");
        lfLabel.setDisplayedMnemonic('f');
        lfLabel.setLabelFor(lfComboBox);

        lfComboBox.setRenderer(new DefaultListCellRenderer() {
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

                if (value != null && value instanceof UIManager.LookAndFeelInfo) {
                    UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfovalue;
                    setText(info.getName());
                }

                return c;
            }
        });
        lfComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Runnable changelf = new Runnable() {
                    public void run() {
                        try {
                            UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfolfComboBox.getSelectedItem();
                            if (info != null) {
                                UIManager.setLookAndFeel(info.getClassName());
                                SwingUtilities.updateComponentTreeUI(frame);
                                frame.pack();
                            }
                        catch (Exception ex) {
                            ex.printStackTrace();
                            JOptionPane.showOptionDialog(frame, "The following error occurred while changing the look and feel:\n" + ex.getMessage()"Look And Feel Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }null);
                        }
                    }
                };
                SwingUtilities.invokeLater(changelf);
            }
        });
                
        JLabel monthUIDelegateLabel = new JLabel("MonthUI Delegate:");
        final JComboBox monthUIDelegateComboBox = new JComboBox(new String[] {"Default""Tight Layout""Read-Only Title""Combo Box Title""No Title""Spinner Title"});
        monthUIDelegateLabel.setDisplayedMnemonic('u');
        monthUIDelegateLabel.setLabelFor(monthUIDelegateComboBox);

        monthUIDelegateComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (monthUIDelegateComboBox.getSelectedIndex() == 0) {
                    UIManager.put("MonthUI", DefaultMonthUI.class.getName());
                else if (monthUIDelegateComboBox.getSelectedIndex() == 1) {
                    UIManager.put("MonthUI", TightLayoutMonthUI.class.getName());
                else if (monthUIDelegateComboBox.getSelectedIndex() == 2) {
                    UIManager.put("MonthUI", SimpleTitleMonthUI.class.getName());
                else if (monthUIDelegateComboBox.getSelectedIndex() == 3) {
                    UIManager.put("MonthUI", ComboBoxTitleMonthUI.class.getName());
                else if (monthUIDelegateComboBox.getSelectedIndex() == 4) {
                    UIManager.put("MonthUI", NoTitleMonthUI.class.getName());
                else if (monthUIDelegateComboBox.getSelectedIndex() == 5) {
                    UIManager.put("MonthUI", SpinnerTitleMonthUI.class.getName());
                }
                SwingUtilities.updateComponentTreeUI(frame);
                frame.pack();
            }
        });

        JLabel monthViewUIDelegateLabel = new JLabel("MonthViewUI Delegate:");
        final JComboBox monthViewUIDelegateComboBox = new JComboBox(new String[] {"Default""Image Buttons""No Buttons"});
        monthViewUIDelegateLabel.setDisplayedMnemonic('u');
        monthViewUIDelegateLabel.setLabelFor(monthViewUIDelegateComboBox);

        monthViewUIDelegateComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (monthViewUIDelegateComboBox.getSelectedIndex() == 0) {
                    UIManager.put("MonthViewUI", DefaultMonthViewUI.class.getName());
                else if (monthViewUIDelegateComboBox.getSelectedIndex() == 1) {
                    UIManager.put("MonthViewUI", CustomMonthViewUI.class.getName());
                else if (monthViewUIDelegateComboBox.getSelectedIndex() == 2) {
                    UIManager.put("MonthViewUI", NoButtonsMonthViewUI.class.getName());
                }
                SwingUtilities.updateComponentTreeUI(frame);
                frame.pack();
            }
        });
        
        JPanel lfPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        lfPanel.add(lfLabel);
        lfPanel.add(lfComboBox);
        lfPanel.add(monthUIDelegateLabel);
        lfPanel.add(monthUIDelegateComboBox);
        lfPanel.add(monthViewUIDelegateLabel);
        lfPanel.add(monthViewUIDelegateComboBox);
        
        lfPanel.setBorder(new TitledBorder("Application Settings"));

        panel = new JTabbedPane();
        
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.getContentPane().add(lfPanel, BorderLayout.NORTH);
        
        splashScreen.increaseStep();
    }

    private static void center(JFrame frame) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension size = frame.getSize();
        screenSize.height = screenSize.height / 2;
        screenSize.width = screenSize.width / 2;
        size.height = size.height / 2;
        size.width = size.width / 2;
        int y = screenSize.height - size.height;
        int x = screenSize.width - size.width;
        frame.setLocation(x, y);
    }
    
    private void registerDemos() {
        panel.addTab("Welcome", null, new HtmlPagePanel("welcome.html")"An introduction to what this application is about");
        splashScreen.increaseStep();
        
        panel.addTab("Date Picker", null, new DatePickerDemo()"JDatePicker specific features");
        splashScreen.increaseStep();
        
        panel.addTab("Date Time Editing", null, new DateEditComponentDemo()"JDateField and JDatePicker common features");
        splashScreen.increaseStep();
        
        panel.addTab("Date Restriction", null, new DateComponentDemo()"Basic features common to all date components");
        splashScreen.increaseStep();
        
        panel.addTab("Month Customization", null, new MonthDemo()"JMonth and JMonthView common features");
        splashScreen.increaseStep();
        
        panel.addTab("Calendar Customization", null, new MonthViewDemo()"JMonthView specific features");
        splashScreen.increaseStep();
        
        panel.addTab("About", null, new HtmlPagePanel("about.html")"What is Java Date Picker");
        splashScreen.increaseStep();
    }

    public static void main(String[] args) {
        final Splash splashScreen = new Splash("com/standbysoft/demo/date/logo.jpg"11);
        splashScreen.showSplash();
        
        String osname = System.getProperty("os.name");
        
        if (osname.toLowerCase().indexOf("windows"!= -1) {
            BasicDatePickerUI.registerDateComboBoxUI("com.incors.plaf.kunststoff.KunststoffLookAndFeel", KunststoffComboBoxUIExt.class);
            UIManager.installLookAndFeel("Kunststoff""com.incors.plaf.kunststoff.KunststoffLookAndFeel");

            BasicDatePickerUI.registerDateComboBoxUI("net.sourceforge.mlf.metouia.MetouiaLookAndFeel", MetouiaComboBoxUIExt.class);
            UIManager.installLookAndFeel("Metouia""net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
        }
        splashScreen.increaseStep();
        
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        catch (Exception ex) {
        }
        splashScreen.increaseStep(2);
        
        new Main(splashScreen);

        splashScreen.hideSplash();
    }
}

/**
 * Displays information from a specified HTML file.
 */
class HtmlPagePanel extends JPanel {
    public HtmlPagePanel(String page) {
        JTextPane message = new JTextPane();
        message.setContentType("text/html");
        try {
            message.setPage(getClass().getResource(page));
        catch (Exception e) {
            message.setText("error");
        }
        
        message.setEditable(false);
        message.setAutoscrolls(true);
        
        message.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent evt) {
                if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    try {
                        BrowserLauncher.openURL(evt.getURL().toExternalForm());
                    catch (IOException e) {
                    }
                }
            }
        });

        setLayout(new BorderLayout());
        JScrollPane pane = new JScrollPane(message);
        pane.getViewport().setPreferredSize(new Dimension(100100));
        add(pane, BorderLayout.CENTER);
    }
}