-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWelcomeGUI.java
More file actions
225 lines (183 loc) · 8.26 KB
/
Copy pathWelcomeGUI.java
File metadata and controls
225 lines (183 loc) · 8.26 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
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
package lms;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
public class WelcomeGUI implements ActionListener {
//Add various components. Helps to add them here in case we want to use the information in our ActionListener later
/**
* radio button that signifies a patron is signing in
*/
public static JRadioButton patButton = new JRadioButton("Patron");
/**
* radio button that signifies a librarian is signing in
*/
public static JRadioButton libButton = new JRadioButton("Librarian");
/**
* Button group, which will ensure only one radio button is selected at a time
*/
public static ButtonGroup radioButtons = new ButtonGroup();
/**
* Text field to input login info
*/
public static JTextField loginInfo = new JTextField();
/**
* Button used to navigate to "create patron" window
*/
public static JButton createPatron = new JButton("Create Patron Account");
/**
* Constructor for the welcome window.
* Sets sizes, layouts, and action events/listeners for all elements of the page
*/
public WelcomeGUI(){
// Creating instance of JFrame
JFrame frame = new JFrame("Welcome to our humble library!");
// Set the width and height of frame
frame.setSize(800, 400);
//Create the panel that contains the "boxes" of content
JPanel boxes = new JPanel();
boxes.setLayout(new GridLayout(6,1,10,0));
//Create the Header for the Sign in section
JPanel box1 = new JPanel();
JLabel signInHeader = new JLabel("Sign In");
signInHeader.setHorizontalAlignment(SwingConstants.CENTER);
signInHeader.setFont(new Font("Arial", Font.BOLD, 18));
//signInHeader.setVerticalAlignment(SwingConstants.TOP);
box1.add(signInHeader);
//Create a box that holds a Patron/Librarian radio button and the sign in text field
JPanel box2 = new JPanel();
box2.setLayout(new BoxLayout(box2, BoxLayout.LINE_AXIS));
//Set a border, mainly to squish the components closer to the center
box2.setBorder(new EmptyBorder(10, 200, 10, 200));
// Add button to choose between librarian and patron sign in
//JRadioButton patButton = new JRadioButton("Patron");
patButton.setMnemonic(KeyEvent.VK_P);
patButton.setActionCommand("Patron");
//Set the patron button to be selected by default
patButton.setSelected(true);
//patButton.doClick();
//JRadioButton libButton = new JRadioButton("Librarian");
libButton.setMnemonic(KeyEvent.VK_L);
libButton.setActionCommand("Librarian");
//Add radio buttons to group
radioButtons.add(libButton);
radioButtons.add(patButton);
//Use a different panel for each group to better control proportions
JPanel rButtons = new JPanel();
rButtons.add(patButton);
rButtons.add(libButton);
box2.add(rButtons);
JPanel loginText = new JPanel();
loginText.setPreferredSize(new Dimension(200,25));
loginInfo.setPreferredSize(new Dimension(200,25));
loginInfo.setActionCommand("enter");
loginInfo.addActionListener(this);
loginText.add(loginInfo);
box2.add(loginText);
//Add instructions for signing in. Using a TextArea here and formatting it to look like a label so I could have more
//control over how the large amount of text wraps.
JPanel box3 = new JPanel();
JTextArea signInText = new JTextArea(2, 60);
signInText.setText("If you are a patron, sign in using your Patron ID. If you a librarian, sign in"
+ " using the super-secret password that may or may not be 'librarian'. Press enter to submit your response.");
signInText.setWrapStyleWord(true);
signInText.setLineWrap(true);
signInText.setOpaque(false);
signInText.setEditable(false);
signInText.setFocusable(false);
signInText.setBackground(UIManager.getColor("Label.background"));
signInText.setFont(UIManager.getFont("Label.font"));
signInText.setBorder(UIManager.getBorder("Label.border"));
box3.add(signInText);
//Create Header for Create Patron Account Section
JPanel box4 = new JPanel();
JLabel createHeader = new JLabel("Don't Have an Account?");
createHeader.setHorizontalAlignment(SwingConstants.CENTER);
createHeader.setFont(new Font("Arial", Font.BOLD, 18));
box4.add(createHeader);
//Add createPatron Button
JPanel box5 = new JPanel();
createPatron.setMnemonic(KeyEvent.VK_C);
createPatron.setActionCommand("createPatron");
createPatron.addActionListener(this);
box5.add(createPatron);
// Use an empty panel to add space between two main sections
JPanel spacer = new JPanel();
spacer.add(Box.createRigidArea(new Dimension(10, 50)));
boxes.add(box1);
boxes.add(box2);
boxes.add(box3);
boxes.add(spacer);
boxes.add(box4);
boxes.add(box5);
//Give entire window some padding
boxes.setBorder(new EmptyBorder(10, 0, 30, 0));
//Add boxes to frame
frame.add(boxes);
//Setting the frame visibility to true
frame.setVisible(true);
//Close application upon pressing x
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* Overriding the abstract actionPerformed method
* Writes logic to handle the actions for various buttons/fields in the window
* Detects login info/createPatron button clicks to navigate to correct page.
*/
@Override
public void actionPerformed(ActionEvent e) {
Connection connection = DatabaseConnection.openDatabase();
//Check when enter is pressed on text field
if ("enter".equals(e.getActionCommand())) {
//If patron radio button selected
if (patButton.isSelected()) {
//See if the submitted id is in the database
Boolean patronExists = DatabaseQueries.patronExists(connection, "patrons", loginInfo.getText());
//if the id is in database, create a new Patron with their id passed in, then run patron gui
if(!loginInfo.getText().isBlank() && patronExists){
Patron pat = new Patron(connection, loginInfo.getText());
pat.runPatron();
} else {
//print error message
JOptionPane.showMessageDialog(null, "Whoops! We can't find any Patrons with that id. Please try again or create a new Patron Account.", "Patron Doesn't Exist", JOptionPane.ERROR_MESSAGE);
}
//if librarian radio button selected
}else if (libButton.isSelected()) {
if (loginInfo.getText().equals("librarian")) {
new LibrarianGUI();
} else {
System.out.println(loginInfo.getText());
JOptionPane.showMessageDialog(null, "Sorry, that's the wrong login info for the Librarian Account (hint: try 'librarian')", "Incorrect Information", JOptionPane.INFORMATION_MESSAGE);
}
}
} else if ("createPatron".equals(e.getActionCommand())) {
new CreatePatronGUI();
}
DatabaseConnection.closeDatabase(connection);
}
}