package app;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



/**
 * A JApplication is a swing application that looks and behaves like a
 * JApplet
 *
 * Specializations of this class must implement the init() method.
 * They may also implement the destroy(), start(), and stop() methods.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public abstract class      JApplication 
                implements RootPaneContainer, Runnable, WindowListener
{
    private   int           height, width;    
    protected JFrame        mainWindow;
    

    /**
     * Explicit Value Constructor
     *
     * @param width   The width of the content (in pixels)
     * @param height  The height of the content (in pixels)
     */
    public JApplication(int width, int height)
    {
       this.width  = width;
       this.height = height;       
    }
    

//[constructMainWindow1.

    /**
     * Construct the main window
     */
    private void constructMainWindow()
    {
       ImageIcon    icon;
       JPanel       contentPane;       
       String       lookAndFeel;
        
       try 
       {
          lookAndFeel = UIManager.getSystemLookAndFeelClassName();
          UIManager.setLookAndFeel(lookAndFeel);
       }
       catch (Exception e)
       {
          // Use the default look and feel
       }
        

       icon = null;
       icon = new ImageIcon(icondata);
//]constructMainWindow1.

//[constructMainWindow2.
       mainWindow = new JFrame();       
       mainWindow.setTitle("James Madison University");
       mainWindow.setResizable(false);       

       contentPane = (JPanel)mainWindow.getContentPane();       
       contentPane.setLayout(null);       
       contentPane.setDoubleBuffered(false);       
//]constructMainWindow2.

       mainWindow.setIconImage(icon.getImage());

//[constructMainWindow3.
       mainWindow.setDefaultCloseOperation(
                                 JFrame.DO_NOTHING_ON_CLOSE);      
       mainWindow.addWindowListener(this);
    }
//]constructMainWindow3.

    


    /**
     * This method is called just after the main window is
     * disposed.
     *
     * The default implementation does nothing.
     */
    public void destroy()
    {
    }
    

//[exit.

    /**
     * Exist this JApplication (after prompting the user
     * for a verification)
     */
    private void exit()
    {
       int        response;
       

       response = JOptionPane.showConfirmDialog(mainWindow,
                                      "Exit this application?",
                                      "Exit?", 
                                      JOptionPane.YES_NO_OPTION);

       if (response == JOptionPane.YES_OPTION)
       {
          mainWindow.setVisible(false);       
          stop();       
          mainWindow.dispose();          
       }
    }
//]exit.
 

    /**
     * Returns the content pane
     * (required by RootPaneContainer)
     */
    public Container getContentPane()
    {
       return mainWindow.getContentPane();       
    }
 

    /**
     * Returns the glass pane
     * (required by RootPaneContainer)
     */
    public Component getGlassPane()
    {
       return mainWindow.getGlassPane();       
    }
 

    /**
     * Returns the layered pane
     * (required by RootPaneContainer)
     */
    public JLayeredPane getLayeredPane()
    {
       return mainWindow.getLayeredPane();       
    }
 

    /**
     * Returns the root pane
     * (required by RootPaneContainer)
     */
    public JRootPane getRootPane()
    {
       return mainWindow.getRootPane();       
    }
    

//[init.

    /**
     * This method is called just before the main window
     * is first made visible
     */
    public abstract void init();
//]init.


//[run.
    
    /**
     * The actual entry point into this JApplication
     * (required by Runnable)
     *
     * This method is called in the event dispatch thread.
     */
    public final void run()
    {
       constructMainWindow();       
       
       init();

       mainWindow.setVisible(true);       
    }
//]run.


    /**
     * Resize the content (and the JFrame accordingly)
     * after the JFrame has been made visible
     */
    private void resize()
    {
       Insets        insets;
       JPanel        contentPane;
       
       contentPane = (JPanel)mainWindow.getContentPane();       
       
       insets = mainWindow.getInsets();
       contentPane.invalidate();       
       mainWindow.setSize(width+insets.left+insets.right, 
                          height+insets.top+insets.bottom);       
       contentPane.setSize(width, height);       
       contentPane.validate();       
    }


 

    /**
     * Set the content pane
     * (required by RootPaneContainer)
     */
    public void setContentPane(Container contentPane)
    {
       mainWindow.setContentPane(contentPane);       
    }
 

    /**
     * Set the glass pane
     * (required by RootPaneContainer)
     */
    public void setGlassPane(Component glassPane)
    {
       mainWindow.setGlassPane(glassPane);       
    }
 

    /**
     * Set the layered pane
     * (required by RootPaneContainer)
     */
    public void setLayeredPane(JLayeredPane layeredPane)
    {
       mainWindow.setLayeredPane(layeredPane);       
    }


    

    /**
     * This method is called when the main window is first
     * made visible and then each time it is de-iconified.
     *
     * The default implementation does nothing.
     */
    public void start()
    {
    }
    

    /**
     * This method is called each time the main window is iconified
     * and just before it is disposed.
     *
     * The default implementation does nothing.
     */
    public void stop()
    {
    }



//[windowActivated.

    /**
     * Handle windowActivated messages -- when the windows gains the focus
     * (required by WindowListener)
     *
     * @param event   The WindowEvent that generated the message
     */
    public void windowActivated(WindowEvent event)
    {
    }
//]windowActivated.

//[windowClosed.
    
    /**
     * Handle windowClosed messages -- when the window is disposed
     * (required by WindowListener)
     *
     * @param event   The WindowEvent that generated the message
     */
    public void windowClosed(WindowEvent event)
    {
       destroy();       
       System.exit(0);       
    }
//]windowClosed.


//[windowClosing.
    
    /**
     * Handle windowClosing messages 
     * (required by WindowListener)
     *
     * @param event   The WindowEvent that generated the message
     */
    public void windowClosing(WindowEvent event)
    {
       exit();
    }
//]windowClosing.


//[windowDeiconified.

    /**
     * Handle windowDeiconified messages -- when the window is maximized
     * (required by WindowListener)
     *
     * @param event   The WindowEvent that generated the message
     */
    public void windowDeiconified(WindowEvent event)
    {
       start();       
    }
//]windowDeiconified.


//[windowDeactivated.

    /**
     * Handle windowDeactivated messages -- when the windows loses the focus
     * (required by WindowListener)
     *
     * @param event   The WindowEvent that generated the message
     */
    public void windowDeactivated(WindowEvent event)
    {
       // Ignore
    }
//]windowDeactivated.


//[windowIconified.

    /**
     * Handle windowIconified messages -- when the window is minimized
     * (required by WindowListener)
     *
     * @param event   The WindowEvent that generated the message
     */
    public void windowIconified(WindowEvent event)
    {
       stop();       
    }
//]windowIconified.

//[windowOpened.

    /**
     * Handle windowOpened messages -- the first time the window is
     * made visible (required by WindowListener)
     *
     * @param event   The WindowEvent that generated the message
     */
    public void windowOpened(WindowEvent event)
    {
       resize();       
       start();       
    }
//]windowOpened.



    // The icon
    private static byte[] icondata = {	71,
                                        73,
                                        70,
                                        56,
                                        57,
                                        97,
                                        16,
                                        0,
                                        16,
                                        0,
                                        -77,
                                        0,
                                        0,
                                        100,
                                        2,
                                        4,
                                        -4,
                                        -50,
                                        100,
                                        52,
                                        -50,
                                        -4,
                                        -100,
                                        102,
                                        4,
                                        -4,
                                        -2,
                                        -4,
                                        100,
                                        50,
                                        52,
                                        -4,
                                        -50,
                                        -100,
                                        100,
                                        -50,
                                        -4,
                                        -100,
                                        102,
                                        52,
                                        0,
                                        0,
                                        0,
                                        0,
                                        0,
                                        0,
                                        0,
                                        0,
                                        0,
                                        -94,
                                        -8,
                                        -119,
                                        29,
                                        80,
                                        74,
                                        -25,
                                        97,
                                        -10,
                                        119,
                                        0,
                                        119,
                                        33,
                                        -7,
                                        4,
                                        0,
                                        0,
                                        0,
                                        0,
                                        0,
                                        44,
                                        0,
                                        0,
                                        0,
                                        0,
                                        16,
                                        0,
                                        16,
                                        0,
                                        3,
                                        4,
                                        89,
                                        80,
                                        -56,
                                        25,
                                        -86,
                                        61,
                                        51,
                                        -117,
                                        106,
                                        122,
                                        15,
                                        -102,
                                        20,
                                        120,
                                        68,
                                        73,
                                        -128,
                                        -38,
                                        104,
                                        32,
                                        70,
                                        48,
                                        12,
                                        106,
                                        -90,
                                        2,
                                        44,
                                        58,
                                        116,
                                        84,
                                        7,
                                        0,
                                        -73,
                                        -115,
                                        -117,
                                        6,
                                        -128,
                                        -128,
                                        -41,
                                        -110,
                                        -36,
                                        12,
                                        20,
                                        -31,
                                        -80,
                                        103,
                                        -4,
                                        9,
                                        6,
                                        5,
                                        -29,
                                        -47,
                                        -29,
                                        -124,
                                        62,
                                        93,
                                        6,
                                        83,
                                        -23,
                                        7,
                                        -123,
                                        17,
                                        93,
                                        -81,
                                        -40,
                                        -45,
                                        -5,
                                        -107,
                                        20,
                                        -114,
                                        -58,
                                        -128,
                                        -80,
                                        44,
                                        56,
                                        115,
                                        -107,
                                        108,
                                        55,
                                        -14,
                                        25,
                                        93,
                                        22,
                                        -37,
                                        104,
                                        -70,
                                        36,
                                        -114,
                                        -114,
                                        0,
                                        0,
                                        59};
    
}
