/*
 * [TestDialog.java]
 *
 * Summary: Example use of java.awt.Dialog, NON-MODAL with a stripped down about box.
 *
 * Copyright: (c) 2006-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 java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Example use of java.awt.Dialog, NON-MODAL with a stripped down about box.
 * <p/>
 * It is amazing how little this bulky code accomplishes.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2006-03-02
 * @since 2006-03-02
 */
@SuppressWarnings( { "UnusedDeclaration" } )
final class TestDialog extends Frame
    {
    /**
     * 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 = "Dialog Demo";

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

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

    /**
     * Demonstrate use of Dialog to do a simple About box.
     *
     * @param args command line arguments are ignored.
     */
    @SuppressWarnings( { "UnusedParameters" } )
    public static void main( String args[] )
        {
        // using AWT
        final Frame frame = new Frame( TITLE_STRING + " " + VERSION_STRING );
        frame.setSize( width, height );
        // Note that jframe.setBackground is almost useless.
        // You must set the background colour of the contentPane.
        frame.setBackground( Color.WHITE );
        frame.setForeground( Color.BLUE );
        frame.setLayout( new FlowLayout() );
        // set up a button then when you push is pops up our JDialog.
        Button about = new Button( "About" );
        about.addActionListener( new ActionListener()
            {
            public void actionPerformed( ActionEvent e )
                {
                // open about box dialog
                new AboutBox( frame,
                        TITLE_STRING,
                        VERSION_STRING,
                        "2006-03-02",
                        "2006",
                        "Roedy Green" );
                }
            } );
        frame.add( about );
        frame.addWindowListener( new WindowAdapter()
            {
            /**
             * Handle request to shutdown.
             * @param e event giving details of closing.
             */
            public void windowClosing( WindowEvent e )
                {
                System.exit( 0 );
                } // end WindowClosing
            } // end anonymous class
        );// end addWindowListener line
        frame.validate();
        frame.setVisible( true );
        } // end main
    } // end TestDialog

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

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

    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 Button _dismissButton;

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

    /**
     * copyright
     */
    private final Label _copyright;

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

    /**
     * date released yyyy-mm-dd
     */
    private final Label _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    Roedy Green, Canadian Mind Products
     */
    @SuppressWarnings( { "SameParameterValue" } )
    public AboutBox( Frame parent,
                     String progname,
                     String version,
                     String released,
                     String copyright,
                     String author )
        {
        super( parent, progname + " " + version, false );
        setSize( width, height );
        setLocation( 0, 0 );
        this.setBackground( white );
        this.setLayout( new FlowLayout() );
        _prognameVersion = new Label( progname + " " + version, Label.CENTER );
        _prognameVersion.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
        _prognameVersion.setForeground( red );
        _prognameVersion.setBackground( white );
        _released = new Label( "released: " + released, Label.CENTER );
        _released.setFont( new Font( "Dialog", Font.PLAIN, 11 ) );
        _released.setForeground( FOREGROUND_FOR_LABEL );
        _released.setBackground( white );
        _copyright =
                new Label( "copyright: \u00a9 " + copyright, Label.CENTER );
        _copyright.setFont( new Font( "Dialog", Font.ITALIC, 11 ) );
        _copyright.setForeground( DARK_GREEN );
        _copyright.setBackground( white );
        _author = new Label( "author: " + author, Label.CENTER );
        _author.setFont( new Font( "Dialog", Font.BOLD + Font.ITALIC, 11 ) );
        _author.setForeground( DARK_GREEN );
        _author.setBackground( white );
        _dismissButton = new Button( "Dismiss" );
        _dismissButton.setFont( new Font( "Dialog", Font.BOLD, 16 ) );
        // add components
        this.add( _prognameVersion );
        this.add( _released );
        this.add( _copyright );
        this.add( _author );
        this.add( _dismissButton );
        // 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();
        this.setVisible( true );
        } // end constructor

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