1   package org.wcb.gui.event;
2   
3   import org.wcb.gui.LogbookFrame;
4   import org.wcb.gui.effect.SpinDissolver;
5   
6   import java.awt.event.WindowEvent;
7   import java.awt.event.WindowListener;
8   import java.awt.event.ActionListener;
9   import java.awt.event.ActionEvent;
10  import java.sql.Connection;
11  import java.sql.SQLException;
12  import java.sql.Statement;
13  import java.util.logging.Logger;
14  import java.util.logging.Level;
15  
16  import org.wcb.gui.util.WindowStateSaver;
17  import org.wcb.gui.util.ApplicationPreferences;
18  import org.wcb.gui.util.IPilotsLogApplicationPreferenceKeys;
19  import org.wcb.model.util.SpringUtil;
20  import org.wcb.model.service.IServicesConstants;
21  import org.apache.commons.dbcp.BasicDataSource;
22  
23  /**
24   * <small>
25   * Copyright (c)  2006  wbogaardt.
26   * This library is free software; you can redistribute it and/or
27   * modify it under the terms of the GNU Lesser General Public
28   * License as published by the Free Software Foundation; either
29   * version 2.1 of the License, or (at your option) any later version.
30   *
31   * This library is distributed in the hope that it will be useful,
32   * but WITHOUT ANY WARRANTY; without even the implied warranty of
33   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
34   * Lesser General Public License for more details.
35   *
36   * You should have received a copy of the GNU Lesser General Public
37   * License along with this library; if not, write to the Free Software
38   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
39   * <p/>
40   * </small>
41   *
42   * @author Owner
43   *         Date: May 21, 2006
44   *         Time: 2:33:20 PM
45   *         To change this template use File | Settings | File Templates.
46   */
47  
48  public class ApplicationCloseListener implements WindowListener, ActionListener {
49  
50      private LogbookFrame frame;
51      private Logger LOG = Logger.getLogger(ApplicationCloseListener.class.getName());
52  
53      public ApplicationCloseListener(LogbookFrame frame) {
54          this.frame = frame;
55      }
56  
57      public void actionPerformed(ActionEvent evt) {
58          WindowStateSaver.saveSettings(frame);
59          shutdownDatabase();
60          if (ApplicationPreferences.getInstance().getBoolean(IPilotsLogApplicationPreferenceKeys.ENABLE_EFFECTS_ON_CLOSING)) {
61              new SpinDissolver().dissolveExit(frame);
62          } else {
63              System.exit(0);
64          }
65      }
66  
67      public void windowOpened(WindowEvent evt) {
68      }
69  
70      public void windowClosing(WindowEvent evt) {
71          WindowStateSaver.saveSettings(frame);
72          shutdownDatabase();
73          //check if the user wanted to have the frame spin away some systems this looks ugly.
74          if (ApplicationPreferences.getInstance().getBoolean(IPilotsLogApplicationPreferenceKeys.ENABLE_EFFECTS_ON_CLOSING)) {
75              new SpinDissolver().dissolveExit(frame);
76          } else {
77              System.exit(0);
78          }
79      }
80  
81  
82      private void shutdownDatabase() {
83          BasicDataSource dao = (BasicDataSource) SpringUtil.getApplicationContext().getBean(IServicesConstants.DATA_SOURCE);
84          try {
85              Connection conn = dao.getConnection();
86              Statement st = conn.createStatement();
87              st.execute("SHUTDOWN");
88              conn.close();
89          } catch (SQLException sqle) {
90              LOG.log(Level.WARNING, "Unable to shutdown database properly", sqle);
91          }
92      }
93      
94      public void windowClosed(WindowEvent evt) {}
95      public void windowIconified(WindowEvent evt) {}
96      public void windowDeiconified(WindowEvent evt) {}
97      public void windowActivated(WindowEvent evt) {}
98      public void windowDeactivated(WindowEvent evt) {}
99  }