Coverage details for ui.dialog.ServerLoginDialog

LineHitsSource
1 /*******************************************************************************
2  * InternetCafe is a software solution that helps the management of Cybercafes
3  * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4  * Copyright (C) 2006 Guido Angelo Ingenito
5  
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *******************************************************************************/
20 package ui.dialog;
21  
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.GridLayout;
25 import java.awt.Toolkit;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28  
29 import javax.swing.ImageIcon;
30 import javax.swing.JButton;
31 import javax.swing.JDialog;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.JPasswordField;
35 import javax.swing.JTextField;
36 import javax.swing.border.TitledBorder;
37  
38 import org.apache.log4j.Logger;
39  
40 import ui.Messages;
41 import base.Control;
42 import base.InternetCafeManager;
430import base.user.User;
440 
450@SuppressWarnings("serial") //$NON-NLS-1$
460public class ServerLoginDialog extends JDialog {
470 
480    private static final transient Logger logger = Logger
49             .getLogger(ServerLoginDialog.class.getName());
50  
51     private JPanel contentPanel;
52  
53     private JPanel nicknamePanel;
54  
55     private JPanel passwordPanel;
56  
57     private JPanel buttonPanel;
58  
59     private JTextField nicknameTextField;
60  
61     private JPasswordField passwordField;
62  
63     private JButton loginButton;
640 
650    private JButton clearButton;
660    
670    private User loggedInUser;
680 
690    public ServerLoginDialog() {
700        initialize();
710    }
720 
730    protected void initialize() {
740        this.setResizable(false);
750        this.setSize(300, 200);
760        // Center the dialog
770        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
780        Dimension frameSize = this.getSize();
790        if (frameSize.height > screenSize.height) {
800            frameSize.height = screenSize.height;
810        }
820        if (frameSize.width > screenSize.width) {
830            frameSize.width = screenSize.width;
840        }
850        this.setLocation((screenSize.width - frameSize.width) / 2,
860                (screenSize.height - frameSize.height) / 2);
870 
880        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
890        this.setContentPane(getContentPanel());
900    }
910 
920    /**
930     * @return Returns the contentPanel.
940     */
950    protected JPanel getContentPanel() {
960        if (contentPanel == null) {
970            contentPanel = new JPanel();
980            contentPanel.setLayout(new GridLayout(3, 1));
990            contentPanel.add(getNicknamePanel(), null);
1000            contentPanel.add(getPasswordPanel(), null);
1010            contentPanel.add(getButtonPanel(), null);
102         }
1030        return contentPanel;
104     }
1050 
1060    /**
1070     * @return Returns the buttonPanel.
1080     */
1090    protected JPanel getButtonPanel() {
1100        if (buttonPanel == null) {
1110            buttonPanel = new JPanel();
1120            TitledBorder titledBorder = new TitledBorder(Messages.getString("dialog.availableactions")); //$NON-NLS-1$
1130            buttonPanel.setBorder(titledBorder);
1140            buttonPanel.setLayout(new GridLayout(1, 2));
1150            buttonPanel.add(getLoginButton(), null);
1160            buttonPanel.add(getClearButton(), null);
117         }
1180        return buttonPanel;
119     }
1200 
1210    /**
1220     * @return Returns the clearButton.
1230     */
1240    protected JButton getClearButton() {
1250        if (clearButton == null) {
1260            clearButton = new JButton(Messages.getString("button.clear")); //$NON-NLS-1$
1270            clearButton.setIcon(new ImageIcon(this.getClass().getResource(
128                     "/icon/16x16/actions/view-refresh.png"))); //$NON-NLS-1$
129  
1300            clearButton.addActionListener(new ActionListener() {
131                 public void actionPerformed(ActionEvent arg0) {
132                     logger.debug("actionPerformed clearButton"); //$NON-NLS-1$
1330                    getNicknameTextField().setText(""); //$NON-NLS-1$
1340                    getPasswordField().setText(""); //$NON-NLS-1$
1350                }
136             });
137         }
1380        return clearButton;
139     }
1400 
1410    /**
1420     * @return Returns the loginButton.
1430     */
1440    protected JButton getLoginButton() {
1450        if (loginButton == null) {
1460            loginButton = new JButton(Messages.getString("button.login")); //$NON-NLS-1$
1470            loginButton.setIcon(new ImageIcon(this.getClass().getResource(
148                     "/icon/16x16/apps/preferences-system-session.png"))); //$NON-NLS-1$
149  
1500            loginButton.addActionListener(new ActionListener() {
151                 private int authenticationCounter = 0;
152                 
153  
154                 public void actionPerformed(ActionEvent arg0) {
155                     logger.debug("actionPerformed loginButton"); //$NON-NLS-1$
156                     // Authentication code here
157                     if (authenticationCounter > 3) {
158                         // Lets him wait for a while...
159                         getLoginButton().setEnabled(false);
160                         getClearButton().setEnabled(false);
161                         try {
162                             Thread.sleep(5000);
163                             authenticationCounter = 0;
164                         } catch (InterruptedException ex) {
165                             // TODO Auto-generated catch block
166                             logger.error(ex.getMessage());
167                             ex.printStackTrace();
168                         }
169                         getLoginButton().setEnabled(true);
170                         getClearButton().setEnabled(true);
171  
172                     }
173                     if (!Control.areValidAdministratorCredentials(
174                             getUserNickname(), getUserPassword())) {
175                         authenticationCounter++;
176                         JOptionPane
177                                 .showMessageDialog(
178                                         null,
179                                         Messages.getString("dialog.serverlogindialog.message1"), //$NON-NLS-1$
180                                         Messages.getString("message.loginfailed"), //$NON-NLS-1$
181                                         JOptionPane.WARNING_MESSAGE);
182                         return;
183                     }
184                     authenticationCounter = 0;
1850                    logger.info(Messages.getString("message.loginsuccess")); //$NON-NLS-1$
1860                    getPasswordField().setText(""); //$NON-NLS-1$
1870                    //This ensures to have a reference to the logged in administrator user.
188                     setLoggedInUser(InternetCafeManager.getInstance().getUserByNickname(getUserNickname()));
189                     setVisible(false);
190                 }
191             });
1920        }
1930        return loginButton;
1940    }
1950 
1960    /**
1970     * @return Returns the passwordField.
198      */
199     protected JPasswordField getPasswordField() {
2000        if (passwordField == null) {
2010            passwordField = new JPasswordField();
2020        }
2030        return passwordField;
2040    }
2050 
2060    /**
2070     * @return Returns the nicknamePanel.
2080     */
2090    protected JPanel getNicknamePanel() {
2100        if (nicknamePanel == null) {
2110            nicknamePanel = new JPanel();
2120            TitledBorder titledBorder = new TitledBorder(Messages.getString("common.nickname")); //$NON-NLS-1$
2130            nicknamePanel.setBorder(titledBorder);
2140            nicknamePanel.setLayout(new BorderLayout());
2150            nicknamePanel.add(getNicknameTextField(), BorderLayout.CENTER);
2160        }
2170        return nicknamePanel;
2180    }
2190 
2200    /**
2210     * @return Returns the passwordPanel.
2220     */
2230    protected JPanel getPasswordPanel() {
2240        if (passwordPanel == null) {
2250            passwordPanel = new JPanel();
2260            TitledBorder titledBorder = new TitledBorder(Messages.getString("common.password")); //$NON-NLS-1$
2270            passwordPanel.setBorder(titledBorder);
2280            passwordPanel.setLayout(new BorderLayout());
2290            passwordPanel.add(getPasswordField(), BorderLayout.CENTER);
2300        }
2310        return passwordPanel;
2320    }
2330 
2340    /**
2350     * @return Returns the nicknameTextField.
236      */
2370    protected JTextField getNicknameTextField() {
2380        if (nicknameTextField == null) {
2390            nicknameTextField = new JTextField();
240         }
2410        return nicknameTextField;
2420    }
2430 
244     public String getUserNickname() {
2450        return this.getNicknameTextField().getText();
246     }
247  
248     public String getUserPassword() {
2490        return new String(getPasswordField().getPassword());
250     }
251  
252     /**
253      * @return Returns the loggedInUser.
254      */
255     public User getLoggedInUser() {
2560        return loggedInUser;
257     }
258  
259     /**
260      * @param loggedInUser The loggedInUser to set.
261      */
262     private void setLoggedInUser(User loggedInUser) {
2630        this.loggedInUser = loggedInUser;
2640    }
265  
266 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.