package utilities;

import java.awt.*;
import java.awt.event.*;

        /** Just handle WINDOW_CLOSING events.
         * The constructor's argument determines whether to exit
         * the program when the window is closed.
         */
public class WindowCloser extends WindowAdapter {
        /** Exit when the window is closed.
         */
    public WindowCloser(){
        // Exit by default.
        this(true);
    }

        /** Exit when the window is closed if the argument is true.
         *  Otherwise, just free the window's resources.
         */
    public WindowCloser(boolean exitOnClose){
        setExitOnClose(exitOnClose);
    }

        /** A WINDOW_CLOSING event has been received.
         *  Take the appropriate action, disposing of this
         *  window's resources.
         */
    public void windowClosing(WindowEvent e){
        // Find out which window has been closed.
        Window w = e.getWindow();
        // Hide it.
        w.setVisible(false);
        // Free its resources.
        w.dispose();
        if(exitOnClose()){
            System.exit(0);
        }
    }

    protected boolean exitOnClose(){
        return this.exitOnClose;
    }

    protected void setExitOnClose(boolean b){
        exitOnClose = b;
    }

    private boolean exitOnClose = false;
}

