-
Notifications
You must be signed in to change notification settings - Fork 1
/
logInScreen2.java
363 lines (286 loc) · 11.8 KB
/
logInScreen2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JLabel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JEditorPane;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import java.util.Stack;
public class logInScreen2 extends JFrame {
private static JLabel emailLabel;
private static JLabel passwordLabel;
private static JButton signInButton;
private static JButton signUpButton;
private static JPanel iconsPanel;
private static JTextField passwordField;
private static JPanel bottomPanel;
private static JPanel credentialsPanel;
private static JComboBox emailsFieldBox;
private static JEditorPane editorPane;
private static String enteredEmailAddress;
private static String enteredPassword;
private static String userName;
private static Stack passwordsStack;
private static Stack emailsInThe_DB;
private static RegistrationForm registrationForm;
private static InboxFrame2 inboxFrame = new InboxFrame2();
public logInScreen2() {
//SET FRAME PROPERTIES
setSize(500, 600);
setLocation(getLocation().x, getLocation().y);
setLayout(new GridLayout(2, 1, 1, 1));
setResizable(false);
setIconImage(new ImageIcon("anonimas.jpg").getImage());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(false);
registrationForm = new RegistrationForm();
emailsInThe_DB = new Stack();
passwordsStack = new Stack();
credentialsPanel = new JPanel();
bottomPanel = new JPanel();
editorPane = new JEditorPane();
passwordField = new JTextField();
emailLabel = new JLabel("Email Address");
passwordLabel = new JLabel("Password Label");
iconsPanel = new JPanel();
signInButton = new JButton("Sign In");
signUpButton = new JButton("Have no account ?\nSign Up");
//PREPARE TEXT FIELDS
iconsPanel.setBackground(Color.WHITE);
//PREPARE LABELS
//ICONS PANEL SPECS
//BOTTOM PANEL
bottomPanel.add(signInButton, BorderLayout.EAST);
bottomPanel.add(signUpButton, BorderLayout.WEST);
//ADD PHONE NUMBER VALUES TO PHONE NUMBER TEXT FIELD
emailsFieldBox = new JComboBox<>();
emailsFieldBox.setEditable(true);
//PREPARE BUTTONS
signInButton.setMnemonic(KeyEvent.VK_ENTER);
signInButton.setForeground(Color.WHITE);
signUpButton.setForeground(Color.WHITE);
signInButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
signUpButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
signInButton.setBackground(Color.blue);
signUpButton.setBackground(Color.blue);
signInButton.setEnabled(false);
signUpButton.setEnabled(true);
//CREDENTIAL PANEL SPECS
credentialsPanel.setBackground(Color.WHITE);
credentialsPanel.setLayout(new GridLayout(6, 1, 3, 3));
credentialsPanel.add(editorPane);
credentialsPanel.add(emailLabel);
credentialsPanel.add(emailsFieldBox);
credentialsPanel.add(passwordLabel);
credentialsPanel.add(passwordField);
credentialsPanel.add(bottomPanel);
//ADD PANELS TO THE FRAME
add(iconsPanel);
add(credentialsPanel);
//SET UP FOCUS LISTENERS FOR THE GUI COMPONENTS
signInButton.addFocusListener(
new FocusListener() {
@Override
public void focusLost(FocusEvent event) {
//signInButton.setBackground(Color.BLUE);
}
@Override
public void focusGained(FocusEvent event) {
signInButton.setBackground(Color.GREEN);
}
}
);
signUpButton.addFocusListener(
new FocusListener() {
@Override
public void focusGained(FocusEvent event) {
signUpButton.setBackground(Color.GREEN);
}
@Override
public void focusLost(FocusEvent event) {
signUpButton.setBackground(Color.BLUE);
}
}
);
passwordField.setCursor(new Cursor(Cursor.TEXT_CURSOR));
passwordField.addFocusListener(
new FocusListener() {
@Override
public void focusGained(FocusEvent event) {
passwordField.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLUE, Color.BLUE));
}
@Override
public void focusLost(FocusEvent event) {
if (passwordField.getText().toString().equals("")) {
Toolkit.getDefaultToolkit().beep();
passwordField.setBorder(new BevelBorder(BevelBorder.RAISED, Color.RED, Color.RED));
} else {
passwordField.setBorder(new BevelBorder(BevelBorder.RAISED, Color.lightGray, Color.lightGray));
}
}
}
);
//CHECK LOG IN SCREEN FIELD !NULL
passwordField.addKeyListener(
new KeyListener() {
@Override
public void keyPressed(KeyEvent event) {
}
@Override
public void keyReleased(KeyEvent event) {
ableToLogIn(true);
}
@Override
public void keyTyped(KeyEvent event) {
}
}
);
//Obtain log in credentials from user AND AUTHENTICATE USER.
signInButton.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
AuthenticateUser();
}
}
);
//OPEN REGISTRATION FROM ON REQUEST
signUpButton.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
setVisible(false);
registrationForm.setVisible(true);
}
}
);
}
//ALLOW LOG IN WHEN CREDENTIALS ARE ADDED
private void ableToLogIn(final boolean canLogIn) {
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
signInButton.setEnabled(canLogIn);
}
}
);
}
//Connect to the MySql database and obtain email addresses and passwords
public void AuthenticateUser() {
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
try {
//System.err.println("Authenticate method is executed...");
String url = "jdbc:mysql://localhost:3306/gmail";
enteredPassword = passwordField.getText().toString();
enteredEmailAddress = emailsFieldBox.getSelectedItem().toString();
/**
* System.out.println("The entered email address is " +
* enteredEmailAddress); System.out.println("entered EMAIL
* ADDRESS is" + enteredEmailAddress);
* System.out.println("enterd USER PASSWORD is" +
* enteredPassword);*
*/
Connection connection = DriverManager.getConnection(url, "root", "");
Statement sql_statement = connection.createStatement();
String SELECT_QUERY = "SELECT password FROM registration_form WHERE emailAddress = " + enteredEmailAddress;
ResultSet queryResults = sql_statement.executeQuery("SELECT password, first_name FROM registration_form WHERE emailAddress = " + "'" + enteredEmailAddress + "';");
while (queryResults.next()) {
//add all emails to the eamails stack
System.err.println("line 356 (while loop) is executed");
passwordsStack.push(queryResults.getString("password"));
userName = queryResults.getString("first_name");
System.err.println("line 359 (while loop) is executed");
}
//Compare entered password
if (passwordsStack.search(enteredPassword) == -1) {
setVisible(true);
passwordField.setBorder(new BevelBorder(BevelBorder.RAISED, Color.RED, Color.RED));
inboxFrame.setVisible(false);
editorPane.setForeground(Color.red);
Toolkit.getDefaultToolkit().beep();
showMessage("\t\tWRONG PASSWORD");
} else if (passwordsStack.search(enteredPassword) >= 0) {
showMessage("SUCCESSFULL");
passwordField.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLUE, Color.BLUE));
emailsFieldBox.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLUE, Color.BLUE));
JOptionPane.showMessageDialog(iconsPanel, "\tSuccessfull\nWelcom to your account");
remove(credentialsPanel);
setVisible(false);
inboxFrame.setVisible(true);
inboxFrame.setTitle(userName);
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "" + e.getMessage());
}
}
}
);
try {
String SELECT_QUERY = "SELECT phone_name,emailAddress FROM registration_form ";
String dbConnectionurl = "jdbc:mysql://localhost:3306/gmail";
Connection connection = DriverManager.getConnection(dbConnectionurl, "root", "");
Statement sqlStatement = connection.createStatement();
ResultSet resultSet = sqlStatement.executeQuery(SELECT_QUERY);
while (resultSet.next()) {
emailsInThe_DB.push(resultSet.getString("emailAddress"));
}
} catch (SQLException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(iconsPanel, e.getMessage());
} catch (SecurityException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "A security breach was detected in your System\n An automatic shut down is scheduled", "SECURITY BREACH WARNING", JOptionPane.WARNING_MESSAGE);
} catch (StringIndexOutOfBoundsException e) {
}
}
//UPDATE ERROR MESSAGE PANE DURING RUN TIME WHEN WRONG PASSWORD IS TYPED
private void showMessage(final String errorMessage) {
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
editorPane.setBackground(Color.white);
editorPane.setText(errorMessage);
}
}
);
}
/**
* public static void main(String[] args) {
*
* javax.swing.SwingUtilities.invokeLater( new Runnable() {
*
* @Override public void run() {
*
* new logInScreen2();
*
* }
* }
* ); }*
*/
}