View Javadoc

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;
43  import base.user.User;
44  
45  @SuppressWarnings("serial") //$NON-NLS-1$
46  public class ServerLoginDialog extends JDialog {
47  
48  	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;
64  
65  	private JButton clearButton;
66  	
67  	private User loggedInUser;
68  
69  	public ServerLoginDialog() {
70  		initialize();
71  	}
72  
73  	protected void initialize() {
74  		this.setResizable(false);
75  		this.setSize(300, 200);
76  		// Center the dialog
77  		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
78  		Dimension frameSize = this.getSize();
79  		if (frameSize.height > screenSize.height) {
80  			frameSize.height = screenSize.height;
81  		}
82  		if (frameSize.width > screenSize.width) {
83  			frameSize.width = screenSize.width;
84  		}
85  		this.setLocation((screenSize.width - frameSize.width) / 2,
86  				(screenSize.height - frameSize.height) / 2);
87  
88  		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
89  		this.setContentPane(getContentPanel());
90  	}
91  
92  	/***
93  	 * @return Returns the contentPanel.
94  	 */
95  	protected JPanel getContentPanel() {
96  		if (contentPanel == null) {
97  			contentPanel = new JPanel();
98  			contentPanel.setLayout(new GridLayout(3, 1));
99  			contentPanel.add(getNicknamePanel(), null);
100 			contentPanel.add(getPasswordPanel(), null);
101 			contentPanel.add(getButtonPanel(), null);
102 		}
103 		return contentPanel;
104 	}
105 
106 	/***
107 	 * @return Returns the buttonPanel.
108 	 */
109 	protected JPanel getButtonPanel() {
110 		if (buttonPanel == null) {
111 			buttonPanel = new JPanel();
112 			TitledBorder titledBorder = new TitledBorder(Messages.getString("dialog.availableactions")); //$NON-NLS-1$
113 			buttonPanel.setBorder(titledBorder);
114 			buttonPanel.setLayout(new GridLayout(1, 2));
115 			buttonPanel.add(getLoginButton(), null);
116 			buttonPanel.add(getClearButton(), null);
117 		}
118 		return buttonPanel;
119 	}
120 
121 	/***
122 	 * @return Returns the clearButton.
123 	 */
124 	protected JButton getClearButton() {
125 		if (clearButton == null) {
126 			clearButton = new JButton(Messages.getString("button.clear")); //$NON-NLS-1$
127 			clearButton.setIcon(new ImageIcon(this.getClass().getResource(
128 					"/icon/16x16/actions/view-refresh.png"))); //$NON-NLS-1$
129 
130 			clearButton.addActionListener(new ActionListener() {
131 				public void actionPerformed(ActionEvent arg0) {
132 					logger.debug("actionPerformed clearButton"); //$NON-NLS-1$
133 					getNicknameTextField().setText(""); //$NON-NLS-1$
134 					getPasswordField().setText(""); //$NON-NLS-1$
135 				}
136 			});
137 		}
138 		return clearButton;
139 	}
140 
141 	/***
142 	 * @return Returns the loginButton.
143 	 */
144 	protected JButton getLoginButton() {
145 		if (loginButton == null) {
146 			loginButton = new JButton(Messages.getString("button.login")); //$NON-NLS-1$
147 			loginButton.setIcon(new ImageIcon(this.getClass().getResource(
148 					"/icon/16x16/apps/preferences-system-session.png"))); //$NON-NLS-1$
149 
150 			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;
185 					logger.info(Messages.getString("message.loginsuccess")); //$NON-NLS-1$
186 					getPasswordField().setText(""); //$NON-NLS-1$
187 					//This ensures to have a reference to the logged in administrator user.
188 					setLoggedInUser(InternetCafeManager.getInstance().getUserByNickname(getUserNickname()));
189 					setVisible(false);
190 				}
191 			});
192 		}
193 		return loginButton;
194 	}
195 
196 	/***
197 	 * @return Returns the passwordField.
198 	 */
199 	protected JPasswordField getPasswordField() {
200 		if (passwordField == null) {
201 			passwordField = new JPasswordField();
202 		}
203 		return passwordField;
204 	}
205 
206 	/***
207 	 * @return Returns the nicknamePanel.
208 	 */
209 	protected JPanel getNicknamePanel() {
210 		if (nicknamePanel == null) {
211 			nicknamePanel = new JPanel();
212 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.nickname")); //$NON-NLS-1$
213 			nicknamePanel.setBorder(titledBorder);
214 			nicknamePanel.setLayout(new BorderLayout());
215 			nicknamePanel.add(getNicknameTextField(), BorderLayout.CENTER);
216 		}
217 		return nicknamePanel;
218 	}
219 
220 	/***
221 	 * @return Returns the passwordPanel.
222 	 */
223 	protected JPanel getPasswordPanel() {
224 		if (passwordPanel == null) {
225 			passwordPanel = new JPanel();
226 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.password")); //$NON-NLS-1$
227 			passwordPanel.setBorder(titledBorder);
228 			passwordPanel.setLayout(new BorderLayout());
229 			passwordPanel.add(getPasswordField(), BorderLayout.CENTER);
230 		}
231 		return passwordPanel;
232 	}
233 
234 	/***
235 	 * @return Returns the nicknameTextField.
236 	 */
237 	protected JTextField getNicknameTextField() {
238 		if (nicknameTextField == null) {
239 			nicknameTextField = new JTextField();
240 		}
241 		return nicknameTextField;
242 	}
243 
244 	public String getUserNickname() {
245 		return this.getNicknameTextField().getText();
246 	}
247 
248 	public String getUserPassword() {
249 		return new String(getPasswordField().getPassword());
250 	}
251 
252 	/***
253 	 * @return Returns the loggedInUser.
254 	 */
255 	public User getLoggedInUser() {
256 		return loggedInUser;
257 	}
258 
259 	/***
260 	 * @param loggedInUser The loggedInUser to set.
261 	 */
262 	private void setLoggedInUser(User loggedInUser) {
263 		this.loggedInUser = loggedInUser;
264 	}
265 
266 }