/*
 * [TestJDialog.java]
 *
 * Summary: Example use of javax.swing.JDialog, NON-MODAL with a stripped down about box.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2006-03-02
 */
package com.mindprod.example;

import com.mindprod.common18.JEButton;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Example use of javax.swing.JDialog, NON-MODAL with a stripped down about box.
 * <p/>
 * It is amazing how little bulky code accomplishes.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2006-03-02
 * @since 2006-03-02
 */
@SuppressWarnings( { "UnusedDeclaration" } )
final class TestJDialog
    {
    /**
     * height of frame in pixels
     */
    private static final int height = 100;

    /**
     * width of frame in pixels
     */
    private static final int width = 300;

    private static final String RELEASE_DATE = "2006-03-06";

    /**
     * title for frame
     */
    private static final String TITLE_STRING = "JDialog Demo";

    /**
     * program version
     */
    private static final String VERSION_STRING = "1.0";

    private static final Color FOREGROUND_FOR_LABEL = new Color( 0x0000b0 );

    /**
     * for titles
     */
    private static final Color FOREGROUND_FOR_TITLE = new Color( 0xdc143c );

    /**
     * for for titles and About buttons
     */
    private static final Font FONT_FOR_TITLE = new Font( "Dialog", Font.BOLD, 16 );

    /**
     * Demonstrate use of JDialog to do a simple About box.
     *
     * @param args command line arguments are ignored.
     */
    @SuppressWarnings( { "UnusedParameters" } )
    public static void main( String args[] )
        {
        // Invoke the run method on the Swing event dispatch thread
        // Sun now recommends you call ALL your GUI methods on the Swing
        // event thread, even the initial setup.
        // Could also use invokeAndWait and catch exceptions
        SwingUtilities.invokeLater( new Runnable()
            {
            /**
             * fire up a JFrame on the Swing thread
             */
            public void run()
                {
                JFrame.setDefaultLookAndFeelDecorated( true );
                final JFrame jframe =
                        new JFrame( TITLE_STRING + " " + VERSION_STRING );
                jframe.setSize( width, height );
                Container contentPane = jframe.getContentPane();
                // Note that jframe.setBackground is almost useless.
                // You must set the background colour of the contentPane.
                contentPane.setBackground( Color.WHITE );
                contentPane.setForeground( Color.BLUE );
                contentPane.setLayout( new FlowLayout() );
                jframe.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                // set up a button then when you push is pops up our JDialog.
                JButton about = new JEButton( "About" );
                about.setBorder( BorderFactory.createRaisedBevelBorder() );
                about.setFont( FONT_FOR_TITLE );
                about.setFocusPainted( false );
                about.setToolTipText( "About "
                                      + TITLE_STRING
                                      + " "
                                      + VERSION_STRING );
                about.addActionListener( new ActionListener()
                    {
                    public void actionPerformed( ActionEvent e )
                        {
                        // open about box dialog
                        new AboutJBox( jframe,
                                TITLE_STRING,
                                VERSION_STRING,
                                "2006-03-02",
                                "2006",
                                "Roedy Green" );
                        }
                    } );
                contentPane.add( about );
                // hide ugly focus rectangle
                jframe.validate();
                jframe.setVisible( true );
                }
            } );
        } // end main
    } // end TestJDialog

/**
 * Our custom JDialog Box, a very simple About Box. This is a stripped down version of
 * com.mindprod.common18.CMPAboutJBox available with source as part of the Canadian Tax package at
 * http://mindprod.com/products1.html#CANADIANTAX
 */
@SuppressWarnings( { "FieldCanBeLocal", "UnusedDeclaration" } )
final class AboutJBox extends JDialog
    {
    /**
     * height of Dialog box in pixels. Does not include surrounding frame.
     */
    private static final int height = 185;

    /**
     * Width of Dialog box in pixels.
     */
    private static final int width = 225;

    private static final Color DARK_GREEN = new Color( 0x008000 );

    private static final Color FOREGROUND_FOR_LABEL = new Color( 0x0000b0 );

    private static final Color red = Color.RED;

    private static final Color white = Color.WHITE;

    /**
     * trigger dismiss of about box
     */
    private final JButton _dismissButton;

    /**
     * program author
     */
    private final JLabel _author;

    /**
     * copyright
     */
    private final JLabel _copyright;

    /**
     * program name and version
     */
    private final JLabel _prognameVersion;

    /**
     * date released yyyy-mm-dd
     */
    private final JLabel _released;

    /**
     * Constructor. Create an about box.
     *
     * @param parent    enclosing frame for this about box.
     * @param progname  Program name
     * @param version   Program version e.g. "1.3"
     * @param released  Date released e.g. "1999-12-31"
     * @param copyright e.g. "1996-2005"
     * @param author    program author
     */
    @SuppressWarnings( { "SameParameterValue" } )
    public AboutJBox( Frame parent,
                      String progname,
                      String version,
                      String released,
                      String copyright,
                      String author )
        {
        super( parent, progname + " " + version, false/* not modal */ );
        setSize( width, height );
        setLocation( 0, 0 );
        Container contentPane = this.getContentPane();
        contentPane.setBackground( white );
        // set up vertical arrangement
        contentPane.setLayout( new BoxLayout( contentPane,
                BoxLayout.PAGE_AXIS ) );
        _prognameVersion =
                new JLabel( progname + " " + version, JLabel.CENTER );
        _prognameVersion.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
        _prognameVersion.setForeground( red );
        _prognameVersion.setBackground( white );
        _prognameVersion.setAlignmentX( .5F );
        _released = new JLabel( "released: " + released, JLabel.CENTER );
        _released.setFont( new Font( "Dialog", Font.PLAIN, 11 ) );
        _released.setForeground( FOREGROUND_FOR_LABEL );
        _released.setBackground( white );
        _released.setAlignmentX( .5F );
        _copyright =
                new JLabel( "copyright: \u00a9 " + copyright, JLabel.LEFT );
        _copyright.setFont( new Font( "Dialog", Font.ITALIC, 11 ) );
        _copyright.setForeground( DARK_GREEN );
        _copyright.setBackground( white );
        _copyright.setAlignmentX( .5F );
        _author = new JLabel( "author: " + author, JLabel.LEFT );
        _author.setFont( new Font( "Dialog", Font.BOLD + Font.ITALIC, 11 ) );
        _author.setForeground( DARK_GREEN );
        _author.setBackground( white );
        _author.setAlignmentX( .5F );
        _dismissButton = new JEButton( "Dismiss" );
        _dismissButton.setToolTipText( "Dismiss this dialog" );
        // add components:
        contentPane.add( _prognameVersion );
        contentPane.add( Box.createVerticalGlue() );
        contentPane.add( _released );
        contentPane.add( Box.createVerticalGlue() );
        contentPane.add( _copyright );
        contentPane.add( Box.createVerticalGlue() );
        contentPane.add( _author );
        contentPane.add( Box.createVerticalGlue() );
        contentPane.add( _dismissButton );
        contentPane.add( Box.createVerticalGlue() );
        // hook up About box listeners
        this.addWindowListener( new WindowAdapter()
            {
            /**
             * Handle request to close about box
             * @param e event giving details of closing.
             */
            public void windowClosing( WindowEvent e )
                {
                dismiss();
                } // end WindowClosing
            } // end anonymous class
        );// end addWindowListener line
        _dismissButton.addActionListener( new ActionListener()
            {
            /**
             * close down the About box when user clicks Dismiss
             */
            public void actionPerformed( ActionEvent event )
                {
                Object object = event.getSource();
                if ( object == _dismissButton )
                    {
                    dismiss();
                    } // end if
                } // end actionPerformed
            } // end anonymous class
        );// end addActionListener line
        this.validate();
        _dismissButton.requestFocus();
        _dismissButton.setFocusPainted( false );
        this.setVisible( true );
        } // end constructor

    /**
     * Shutdown the about box
     */
    void dismiss()
        {
        // dispose of this JDialog
        this.dispose();
        } // end dismiss
    }