-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddResidentDialog.java
More file actions
444 lines (388 loc) · 15.8 KB
/
AddResidentDialog.java
File metadata and controls
444 lines (388 loc) · 15.8 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* * * * * * * * * * *\
* AddResidentDialog.java
* Description: Allows user to enter resident information and save it, used for adding and editing residents.
* Will validate each field when user submits. Only the resident director will be able to add, edit or
* delete a resident.
*
* Date: 4/10/16
* @author Brandon Ballard & Hanif Mirza
\* * * * * * * * * * */
import java.awt.event.*;
import static javax.swing.GroupLayout.Alignment.*;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
class AddResidentDialog extends JDialog implements ActionListener,DocumentListener
{
JPanel buttonPanel, fieldPanel;
JButton cancelButton, addButton;
AdminGUI adminGUI;
Statement myStatement;
JTextField residentIDTF,roomNoTF, dormNameTF, firstNameTF, lastNameTF, emailTF, phoneTF,lockoutTF;
String residentID,roomNo, dormName, firstName, lastName, email, phone,gender,lockoutNo;
JLabel residentIDLabel,roomNoLabel, genderLabel, dormNameLabel, accountLabel, firstNameLabel, lastNameLabel, emailLabel, phoneLabel,lockoutLabel;
JComboBox genderCBox, dormNameCBox;
int row;
//CONSTRUCTOR FOR ADDING A RESIDENT BY BRANDON BALLARD
public AddResidentDialog(AdminGUI adminGUI,Statement myStatement)
{
setTitle("Add Resident");
this.myStatement = myStatement;
this.adminGUI = adminGUI;
//________________________________________________________________Create buttons and button panel
addButton = new JButton("Add Resident");
addButton.setBackground(Color.WHITE);
addButton.addActionListener(this);
addButton.setActionCommand("ADD");
addButton.setEnabled(false);
getRootPane().setDefaultButton(addButton);
cancelButton = new JButton("Cancel");
cancelButton.setBackground(Color.WHITE);
cancelButton.addActionListener(this);
cancelButton.setActionCommand("CANCEL");
buttonPanel = new JPanel(new FlowLayout());
buttonPanel.setBackground(Color.GRAY);
buttonPanel.add(cancelButton);
buttonPanel.add(addButton);
//________________________________________________________________Add text fields for user input
fieldPanel = setFields();
lockoutTF.setText("0");
//________________________________________________________________Add components to container
getContentPane().add(fieldPanel, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
setupMainFrame();
}
//CONSTRUCTOR FOR EDITING A RESIDENT BY BRANDON BALLARD
public AddResidentDialog(AdminGUI adminGUI,Statement myStatement,String residentID , int row)
{
setTitle("Edit Resident");
this.myStatement = myStatement;
this.adminGUI = adminGUI;
this.residentID = residentID;
this.row = row;
//________________________________________________________________Create buttons and button panel
addButton = new JButton("Update Resident");
addButton.setBackground(Color.WHITE);
addButton.addActionListener(this);
addButton.setActionCommand("UPDATE");
getRootPane().setDefaultButton(addButton);
cancelButton = new JButton("Cancel");
cancelButton.setBackground(Color.WHITE);
cancelButton.addActionListener(this);
cancelButton.setActionCommand("CANCEL");
buttonPanel = new JPanel(new FlowLayout());
buttonPanel.setBackground(Color.GRAY);
buttonPanel.add(cancelButton);
buttonPanel.add(addButton);
//________________________________________________________________Add text fields for user input
fieldPanel = setFields();
//________________________________________________________________Populate text fields with selected accounts info
populateAllFields();
getContentPane().add(fieldPanel, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
setupMainFrame();
}
//WRITTEN BY BRANDON BALLARD
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("ADD"))
{
doAddResident();
}
else if(e.getActionCommand().equals("UPDATE"))
{
doUpdateResident();
}
else if(e.getActionCommand().equals("CANCEL"))
{
this.dispose();
}
}
// Written by Hanif Mirza, this function will add a new resident to the database and also add it to the AdminGUI table
void doAddResident()
{
try
{
if(checkIfValuesExist("Resident","userID",residentID))
{
throw new MyException("F-number "+residentID+" is taken" + "\n" + "Please use a unique F-number");
}
validateFields();
// database statement to insert a new resident
String sql = "INSERT INTO Resident(userID,first_name,last_name,gender,email,phone,dorm_name,room_number,number_of_lockouts)"
+ " VALUES ("+ "'"+residentID+"'" +","+ "'"+firstName+"'" +","+ "'"+lastName+"'" +","+ "'"+gender+"'" +","+ "'"+email+"'" +","
+ "'"+phone+"'" +","+ "'"+dormName+"'" + ","+roomNo+","+lockoutNo+")";
int confirmationNo = myStatement.executeUpdate(sql);
Vector<Object> currentRow = new Vector<Object>();
currentRow.addElement(residentID);
currentRow.addElement(firstName);
currentRow.addElement(lastName);
currentRow.addElement(gender);
currentRow.addElement(email);
currentRow.addElement(phone);
currentRow.addElement(dormName);
currentRow.addElement(roomNo);
currentRow.addElement(lockoutNo);
adminGUI.tableModel.addRow(currentRow); // add the new resident to the table
if( confirmationNo > 0)
{
this.dispose();
JOptionPane.showMessageDialog(this, "New resident "+firstName +" "+ lastName+" is added ", "successful" , JOptionPane.INFORMATION_MESSAGE);
}
}
catch(MyException myEx)
{
JOptionPane.showMessageDialog(this,myEx.getMessage(), "Warning ",JOptionPane.WARNING_MESSAGE);
}
catch ( SQLException sqlException )
{
JOptionPane.showMessageDialog(this, sqlException.getMessage());
}
catch ( Exception exception )
{
JOptionPane.showMessageDialog(this, exception.getMessage());
}
}
// Written by Hanif Mirza, this function will validate all the fields, if any field is invalid then it will through exception
void validateFields() throws Exception
{
residentID = residentIDTF.getText().trim();
roomNo = roomNoTF.getText().trim();
dormName = dormNameCBox.getSelectedItem().toString();
firstName = firstNameTF.getText().trim();
lastName = lastNameTF.getText().trim();
email = emailTF.getText().trim();
phone = phoneTF.getText().trim();
gender = genderCBox.getSelectedItem().toString();
lockoutNo = lockoutTF.getText().trim();
if (!Validate.validateFirstName(firstName))
{
throw new MyException("Please enter a valid first name" + "\n" + "No special characters allowed (1@#$%&*)");
}
else if (!Validate.validateLastName(lastName))
{
throw new MyException("Please enter a valid last name" + "\n" + "No special characters allowed (1@#$%&*)");
}
else if (gender.equals("-Select-"))
{
throw new MyException("Please select a gender");
}
else if (dormName.equals("-Select-"))
{
throw new MyException("Please select a dorm name");
}
else if (!Validate.validateNumber(roomNo))
{
throw new MyException("Invalid room number");
}
else if (!Validate.validateEmail(email))
{
throw new MyException("Invalid email address");
}
else if (!Validate.validatePhoneNumber(phone))
{
throw new MyException("Invalid phone number"+ "\n" +"Phone number format xxx-xxx-xxxx ");
}
else if (!Validate.validateNumber(lockoutNo))
{
throw new MyException("Invalid lockout number");
}
}
//Written by Hanif Mirza, This function will use the information selected on table in AdminGUI.java to populate text fields with resident info
void populateAllFields()
{
residentIDTF.setText( adminGUI.tableModel.getValueAt(row,0).toString() );
residentIDTF.setEditable(false);
firstNameTF.setText( adminGUI.tableModel.getValueAt(row,1).toString() );
lastNameTF.setText( adminGUI.tableModel.getValueAt(row,2).toString() );
genderCBox.setSelectedItem( adminGUI.tableModel.getValueAt(row,3).toString() );
emailTF.setText( adminGUI.tableModel.getValueAt(row,4).toString() );
phoneTF.setText( adminGUI.tableModel.getValueAt(row,5).toString() );
dormNameCBox.setSelectedItem( adminGUI.tableModel.getValueAt(row,6).toString() );
roomNoTF.setText( adminGUI.tableModel.getValueAt(row,7).toString() );
lockoutTF.setText( adminGUI.tableModel.getValueAt(row,8).toString() );
}
// Written by Hanif Mirza, This function update the information of selected resident from the table
void doUpdateResident()
{
try
{
validateFields();
// database statement to update resident details
String sql = "UPDATE Resident "
+ "SET first_name = "+ "'"+firstName+"'" +","+ "last_name = "+ "'"+lastName +"'"+","
+ "gender = "+ "'"+gender+"'" +","+ "email = "+ "'"+email+"'" +","
+ "phone = "+ "'"+phone+"'" +","+ "dorm_name = "+ "'"+dormName+"'"+","+ "room_number = "+roomNo+ ","+ "number_of_lockouts = "+lockoutNo
+ " Where userID = "+ "'"+residentID+"'";
int confirmationNo = myStatement.executeUpdate(sql);
adminGUI.tableModel.setValueAt(firstName, row,1);
adminGUI.tableModel.setValueAt(lastName, row,2);
adminGUI.tableModel.setValueAt(gender, row,3);
adminGUI.tableModel.setValueAt(email, row,4);
adminGUI.tableModel.setValueAt(phone, row,5);
adminGUI.tableModel.setValueAt(dormName, row,6);
adminGUI.tableModel.setValueAt(roomNo, row,7);
adminGUI.tableModel.setValueAt(lockoutNo, row,8);
if( confirmationNo > 0)
{
this.dispose();
JOptionPane.showMessageDialog(this, "Resident "+firstName +" "+ lastName+" has been updated ", "successful" , JOptionPane.INFORMATION_MESSAGE);
}
}
catch(MyException myEx)
{
JOptionPane.showMessageDialog(this,myEx.getMessage(), "Warning ",JOptionPane.WARNING_MESSAGE);
}
catch ( SQLException sqlException )
{
JOptionPane.showMessageDialog(this, sqlException.getMessage());
}
catch ( Exception exception )
{
JOptionPane.showMessageDialog(this, exception.getMessage());
}
}
public void insertUpdate(DocumentEvent de)
{
residentID = residentIDTF.getText().trim();
roomNo = roomNoTF.getText().trim();
firstName = firstNameTF.getText().trim();
lastName = lastNameTF.getText().trim();
email = emailTF.getText().trim();
phone = phoneTF.getText().trim();
lockoutNo = lockoutTF.getText().trim();
if( residentID.equals("") || roomNo.equals("") || firstName.equals("")|| lastName.equals("") || email.equals("") || phone.equals("") || lockoutNo.equals(""))
{
addButton.setEnabled(false);
}
else
{
addButton.setEnabled(true);
}
}
public void removeUpdate(DocumentEvent de)
{
residentID = residentIDTF.getText().trim();
roomNo = roomNoTF.getText().trim();
firstName = firstNameTF.getText().trim();
lastName = lastNameTF.getText().trim();
email = emailTF.getText().trim();
phone = phoneTF.getText().trim();
lockoutNo = lockoutTF.getText().trim();
if( residentID.equals("") || roomNo.equals("") || firstName.equals("")|| lastName.equals("") || email.equals("") || phone.equals("")|| lockoutNo.equals("") )
{
addButton.setEnabled(false);
}
else
{
addButton.setEnabled(true);
}
}
public void changedUpdate(DocumentEvent de){}
//Written by Hanif Mirza, this function will return true if values exist in specific column of a database table
boolean checkIfValuesExist(String tableName,String columnName,String columnValue) throws Exception
{
String SQL_Query = "Select * "+"From "+tableName+" WHERE "+columnName+" LIKE "+ "'"+columnValue+"'";
ResultSet resultSet = myStatement.executeQuery(SQL_Query);// query database
if(!resultSet.next())
{
resultSet.close();
return false; // values don't exist, so return false
}
else
{
resultSet.close();
return true; // values exist, so return true
}
}
//Adds fields using a group layout, by BRANDON BALLARD
JPanel setFields()
{
GroupLayout layout;
JPanel p;
residentIDLabel = new JLabel("F-number:");
residentIDLabel.setForeground(Color.WHITE);
genderLabel = new JLabel("Gender:");
genderLabel.setForeground(Color.WHITE);
dormNameLabel = new JLabel("Location:");
dormNameLabel.setForeground(Color.WHITE);
genderCBox = new JComboBox();
genderCBox.addItem("-Select-");
genderCBox.addItem("Male");
genderCBox.addItem("Female");
dormNameCBox = new JComboBox();
dormNameCBox.addItem("-Select-");
dormNameCBox.addItem("Bryant Place");
dormNameCBox.addItem("Prichard Hall");
dormNameCBox.addItem("Morrow Hall");
dormNameCBox.addItem("Pence Hall");
firstNameLabel = new JLabel("First Name:");
firstNameLabel.setForeground(Color.WHITE);
lastNameLabel = new JLabel("Last Name:");
lastNameLabel.setForeground(Color.WHITE);
emailLabel = new JLabel("Email:");
emailLabel.setForeground(Color.WHITE);
phoneLabel = new JLabel("Phone Number:");
phoneLabel.setForeground(Color.WHITE);
lockoutLabel = new JLabel("Lockout Number:");
lockoutLabel.setForeground(Color.WHITE);
residentIDTF = new JTextField(30);
firstNameTF = new JTextField(30);
lastNameTF = new JTextField(30);
emailTF = new JTextField(30);
phoneTF = new JTextField(30);
lockoutTF = new JTextField(30);
roomNoTF = new JTextField(30);
roomNoLabel = new JLabel("Room Number:");
roomNoLabel.setForeground(Color.WHITE);
residentIDTF.getDocument().addDocumentListener(this);
emailTF.getDocument().addDocumentListener(this);
phoneTF.getDocument().addDocumentListener(this);
roomNoTF.getDocument().addDocumentListener(this);
lockoutTF.getDocument().addDocumentListener(this);
firstNameTF.getDocument().addDocumentListener(this);
lastNameTF.getDocument().addDocumentListener(this);
p = new JPanel();
p.setBackground(new Color(1, 0.1f, 0.1f).darker().darker());
layout = new GroupLayout(p);
p.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup().addComponent(residentIDLabel).addComponent(firstNameLabel).addComponent(lastNameLabel)
.addComponent(genderLabel).addComponent(dormNameLabel).addComponent(roomNoLabel)
.addComponent(emailLabel).addComponent(phoneLabel).addComponent(lockoutLabel));
hGroup.addGroup(layout.createParallelGroup().addComponent(residentIDTF).addComponent(firstNameTF).addComponent(lastNameTF)
.addComponent(genderCBox).addComponent(dormNameCBox).addComponent(roomNoTF)
.addComponent(emailTF).addComponent(phoneTF).addComponent(lockoutTF));
layout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(residentIDLabel).addComponent(residentIDTF));
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(firstNameLabel).addComponent(firstNameTF));
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(lastNameLabel).addComponent(lastNameTF));
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(genderLabel).addComponent(genderCBox));
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(dormNameLabel).addComponent(dormNameCBox));
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(roomNoLabel).addComponent(roomNoTF));
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(emailLabel).addComponent(emailTF));
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(phoneLabel).addComponent(phoneTF));
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(lockoutLabel).addComponent(lockoutTF));
layout.setVerticalGroup(vGroup);
layout.setVerticalGroup(vGroup);
return(p);
}
//BY BRANDON BALLARD
void setupMainFrame()
{
Dimension d;
Toolkit tk;
tk = Toolkit.getDefaultToolkit();
d = tk.getScreenSize();
setSize(d.width/3, 380);
setLocation(d.width/3, d.height/4 + d.height/20);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
setVisible(true);
}
}