001         package com.croftsoft.core.gui;
002    
003         import java.awt.*;
004         import java.awt.event.*;
005    
006         import com.croftsoft.core.lang.lifecycle.Destroyable;
007    
008         /*********************************************************************
009         * Performs a graceful shutdown of a program when the window is closed.
010         *
011         * <p>
012         * <ol>
013         * <li> Sets window visibility to false.</li>
014         * <li> Calls the destroy() method, in array order, of each of the
015         *      Destroyable instances passed via the constructor argument.
016         *      Any exceptions are caught, printed, and ignored.</li>
017         * <li> Calls the window dispose() method.</li>
018         * <li> Calls System.exit(0).</li>
019         * </ol>
020         * </p>
021         *
022         * <p>
023         * Example:
024         * <code>
025         * <pre>
026         * frame.addWindowListener (
027         *   new ShutdownWindowListener ( destroyables ) );
028         * </pre>
029         * </code>
030         * </p>
031         *
032         * <p>
033         * Java 1.1 compatible.
034         * </p>
035         *
036         * @version
037         *   2001-07-20
038         * @since
039         *   2001-03-06
040         * @author
041         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
042         *********************************************************************/
043    
044         public class  ShutdownWindowListener
045           extends WindowAdapter
046         //////////////////////////////////////////////////////////////////////
047         //////////////////////////////////////////////////////////////////////
048         {
049    
050         private Destroyable [ ]  destroyables;
051    
052         //////////////////////////////////////////////////////////////////////
053         //////////////////////////////////////////////////////////////////////
054    
055         /*********************************************************************
056         * Main constructor.
057         *
058         * @param  destroyables
059         *   May be null.
060         *********************************************************************/
061         public  ShutdownWindowListener ( Destroyable [ ]  destroyables )
062         //////////////////////////////////////////////////////////////////////
063         {
064           this.destroyables = destroyables;
065         }
066    
067         /*********************************************************************
068         * Convenience constructor.
069         *
070         * <code>
071         * <pre>
072         * this ( new Destroyable [ ] { destroyable } );
073         * </pre>
074         * </code>
075         *********************************************************************/
076         public  ShutdownWindowListener ( Destroyable  destroyable )
077         //////////////////////////////////////////////////////////////////////
078         {
079           this ( new Destroyable [ ] { destroyable } );
080         }
081    
082         /*********************************************************************
083         * Convenience constructor.
084         *
085         * <code>
086         * <pre>
087         * this ( ( Destroyable [ ] ) null );
088         * </pre>
089         * </code>
090         *********************************************************************/
091         public  ShutdownWindowListener ( )
092         //////////////////////////////////////////////////////////////////////
093         {
094           this ( ( Destroyable [ ] ) null );
095         }
096    
097         //////////////////////////////////////////////////////////////////////
098         //////////////////////////////////////////////////////////////////////
099    
100         public void  windowClosing ( WindowEvent  windowEvent )
101         //////////////////////////////////////////////////////////////////////
102         {
103           Window  window = windowEvent.getWindow ( );
104    
105           window.setVisible ( false );
106    
107           if ( destroyables != null )
108           {
109             for ( int  i = 0; i < destroyables.length; i++ )
110             {
111               try
112               {
113                 destroyables [ i ].destroy ( );
114               }
115               catch ( Exception  ex )
116               {
117                 ex.printStackTrace ( );
118               }
119             }
120           }
121    
122           window.dispose ( );
123    
124           System.exit ( 0 );
125         }
126    
127         //////////////////////////////////////////////////////////////////////
128         //////////////////////////////////////////////////////////////////////
129         }