1   package org.wcb.gui.dialog;
2   
3   import org.wcb.gui.forms.auth.JLoginPanel;
4   import org.wcb.gui.util.UIHelper;
5   import org.wcb.model.util.SpringUtil;
6   import org.wcb.model.service.IServicesConstants;
7   import org.apache.commons.dbcp.BasicDataSource;
8   
9   import javax.swing.*;
10  import java.awt.*;
11  import java.awt.event.ActionListener;
12  import java.awt.event.ActionEvent;
13  import java.awt.event.KeyListener;
14  import java.awt.event.KeyEvent;
15  import java.sql.Connection;
16  import java.sql.Statement;
17  import java.sql.SQLException;
18  import java.util.logging.Logger;
19  import java.util.logging.Level;
20  
21  /**
22   * <small>
23   * <p/>
24   * Copyright (c)  2006  wbogaardt.
25   * This library is free software; you can redistribute it and/or
26   * modify it under the terms of the GNU Lesser General Public
27   * License as published by the Free Software Foundation; either
28   * version 2.1 of the License, or (at your option) any later version.
29   *
30   * This library is distributed in the hope that it will be useful,
31   * but WITHOUT ANY WARRANTY; without even the implied warranty of
32   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
33   * Lesser General Public License for more details.
34   *
35   * You should have received a copy of the GNU Lesser General Public
36   * License along with this library; if not, write to the Free Software
37   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
38   * <p/>
39   * $File:  $ <br>
40   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 27, 2006 11:43:42 AM $ <br>
41   * </small>
42   *
43   * @author wbogaardt
44   *         This displays a login dialog box which should tie into
45   * user name and password.
46   */
47  public class JLoginDialog extends JDialog implements ActionListener, KeyListener {
48      
49      private Logger LOG = Logger.getLogger(JLoginDialog.class.getName());
50      private JLoginPanel panel;
51      private JButton loginButton;
52      private JButton cancelButton;
53  
54      public JLoginDialog() {
55          super();
56          init();
57      }
58  
59      public JLoginDialog(Frame owner) {
60          super(owner);
61          init();
62      }
63  
64      public JLoginDialog(Frame owner, boolean modal) throws HeadlessException {
65          super(owner, modal);
66          init();
67      }
68  
69      public JLoginDialog(Frame owner, String title) throws HeadlessException {
70          super(owner, title);
71          init();
72      }
73  
74      public JLoginDialog(Frame owner, String title, boolean modal)
75          throws HeadlessException {
76          super(owner, title, modal);
77          init();
78      }
79  
80      protected void init() {
81          setLayout(new BorderLayout());
82          setPanel(new JLoginPanel());
83          add(createButtonPanel(), BorderLayout.SOUTH);
84          pack();
85      }
86  
87      private JPanel createButtonPanel() {
88          loginButton = new JButton("Login");
89          cancelButton = new JButton("Cancel");
90          loginButton.addKeyListener(this);
91          loginButton.addActionListener(this);
92          cancelButton.addActionListener(this);
93          JPanel buttonPanel = new JPanel(new GridBagLayout());
94          buttonPanel.add(loginButton, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(17, 12, 11, 5), 0, 0));
95          buttonPanel.add(cancelButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(17, 0, 11, 11), 0, 0));
96          return buttonPanel;
97      }
98  
99      public JLoginPanel getPanel() {
100         return panel;
101     }
102 
103     public void setPanel(JLoginPanel panel) {
104         this.panel = panel;
105         add(this.panel, BorderLayout.NORTH);
106         UIHelper.centerDialogToScreen(this);
107     }
108 
109 
110     public void actionPerformed(ActionEvent evt) {
111         Object src = evt.getSource();
112         if (src.equals(loginButton)) {
113             boolean show = getPanel().startLogin();
114             if (show) {
115                 super.getOwner().setVisible(show);
116                 this.dispose();
117             }
118         }
119         if (src.equals(cancelButton)) {
120              boolean show = getPanel().startLogin();
121             if (!show) {
122                 shutdownDatabase();
123                 System.exit(0);
124             }
125             setVisible(false);
126             this.dispose();
127         }
128 
129     }
130 
131     public void keyTyped(KeyEvent evt) {
132         Object src = evt.getSource();
133         if (src.equals(loginButton)) {
134              boolean show = getPanel().startLogin();
135             if (show) {
136                 super.getOwner().setVisible(show);
137                 this.dispose();
138             }
139         }
140     }
141     public void keyPressed(KeyEvent evt) {}
142     public void keyReleased(KeyEvent evt) {}
143 
144     private void shutdownDatabase() {
145         BasicDataSource dao = (BasicDataSource) SpringUtil.getApplicationContext().getBean(IServicesConstants.DATA_SOURCE);
146         try {
147             Connection conn = dao.getConnection();
148             Statement st = conn.createStatement();
149             st.execute("SHUTDOWN");
150             conn.close();
151         } catch (SQLException sqle) {
152             LOG.log(Level.WARNING, "Unable to shutdown database properly", sqle);
153         }
154     }
155 
156 }