-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartUp.java
More file actions
93 lines (84 loc) · 2.89 KB
/
StartUp.java
File metadata and controls
93 lines (84 loc) · 2.89 KB
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
package GUI;
import Amazon.DataBase;
import Amazon.USER;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StartUp extends JDialog {
private JPanel contentPane;
private JButton buttonsignin;
private JButton signup;
private JButton cancelButton;
private JTextField username;
private JTextField pass;
private DataBase dataBase;
public StartUp(DataBase Data) {
dataBase = Data;
setContentPane(contentPane);
setModal(true);
setSize(200, 200);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
final int x = (screenSize.width - this.getWidth()) / 2;
final int y = (screenSize.height - this.getHeight()) / 2;
setLocation(x, y);
getRootPane().setDefaultButton(buttonsignin);
buttonsignin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onSignIn();
}
});
signup.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onSignUp();
}
});
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
onCancel();
}
});
}
private void onSignIn() {
boolean found = dataBase.search(username.getText(), pass.getText());
if (found){
USER user = dataBase.get_user(username.getText());
MainWindow mainWindow = new MainWindow(dataBase, username.getText(), dataBase.getName(username.getText()), user.getType());
mainWindow.setLocationRelativeTo(null);
mainWindow.pack();
mainWindow.setVisible(true);
this.dispose();
}
else {
MessageBox mb = new MessageBox();
mb.setLocationRelativeTo(null);
mb.setText("Wrong username or password");
mb.pack();
mb.setVisible(true);
}
}
private void onSignUp() {
// add your code here if necessary
SignUp signUp = new SignUp(dataBase);
signUp.setLocationRelativeTo(null);
signUp.pack();
signUp.setVisible(true);
}
private void onCancel() {
dataBase.close();
dispose();
}
}