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