forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserInterface_GUI.java
2301 lines (2018 loc) · 82.3 KB
/
UserInterface_GUI.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
public class UserInterface_GUI extends JFrame implements ActionListener
{
private Container con;
private Controller_GUI rcController;
private String currentUserName;
// components for menu
private JMenuBar menuBar;
private JMenu mnFile;
private JMenuItem mntm1, mntm2;
//-------- Master panel --------------
//Main content panel(CENTER)
private JPanel mainPanel;
//Head panel (North)
private JPanel headPanel;
private JLabel headTitle;
private JButton headBtnLogin;
private JButton headBtnLogout;
//Main button panel(WEST)
private JPanel mainBtnsPanel;
// Main buttons
private JButton mainBtnShowMenu;
private JButton mainBtnManageOrder;
// Main buttons for management
private JButton mainBtnManageEmployee;
private JButton mainBtnManageMenuItem;
private JButton mainBtnShowTotalSales;
private JButton mainBtnShowPayment;
//Information panel(SOUTH)
private JPanel infoPanel;
private JLabel labelLoginUserName;
private JButton btnClockOut;
private JTextArea taMessage;
//-------- Contents panel --------------
// components for home panel
private JPanel homePanel;
private JLabel homeImage;
private LoginPanel cLoginPanel;
private MenuListPanel cMenuListPanel;
private OrderListPanel cOrderListPanel;
private OrderDetailPanel cOrderDetailPanel;
private EmployeeListPanel cEmployeeListPanel;
private EditEmployeePanel cEditEmployeePanel;
private MenuManagementPanel cMenuManagementPanel;
private EditMenuItemPanel cEditMenuItemPanel;
private TotalSalesPanel cTotalSalesPanel;
private PaymentPanel cPaymentPanel;
private final static int WINDOW_X = 100;
private final static int WINDOW_Y = 100;
private final static int WINDOW_WIDTH = 900;
private final static int WINDOW_HEIGHT = 600;
/**
* Constructor for objects of class UserInterface_GUI
*/
public UserInterface_GUI(Controller_GUI rController)
{
this.rcController = rController;
this.con = getContentPane();
// Set frame
setTitle("Valentino Restaurant Management System");
setBounds(WINDOW_X, WINDOW_Y, WINDOW_WIDTH, WINDOW_HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMasterPanelConpornents();
currentUserName = "";
setLoginUserName(currentUserName);
//------- Create main content panels
//Home panel
homePanel = new JPanel();
homeImage = new JLabel();
//Random generator = new Random();
int i = new Random().nextInt(4)+1;
homeImage.setHorizontalAlignment(SwingConstants.CENTER);
homeImage.setVerticalAlignment(SwingConstants.CENTER);
homeImage.setIcon(new ImageIcon("images/home" + i + ".jpg"));
homePanel.add(homeImage);
homePanel.setBackground(Color.WHITE);
mainPanel.add("Home", homePanel);
cLoginPanel = new LoginPanel();
mainPanel.add("Login", cLoginPanel);
cMenuListPanel = new MenuListPanel();
mainPanel.add("MenuList", cMenuListPanel);
cOrderListPanel = new OrderListPanel();
mainPanel.add("OrderList", cOrderListPanel);
cOrderDetailPanel = new OrderDetailPanel();
mainPanel.add("OrderDetail", cOrderDetailPanel);
cEmployeeListPanel = new EmployeeListPanel();
mainPanel.add("EmployeeList", cEmployeeListPanel);
cEditEmployeePanel = new EditEmployeePanel();
mainPanel.add("EditEmployee", cEditEmployeePanel);
cMenuManagementPanel = new MenuManagementPanel();
mainPanel.add("MenuManagement", cMenuManagementPanel);
cEditMenuItemPanel = new EditMenuItemPanel();
mainPanel.add("EditMenuItem", cEditMenuItemPanel);
cTotalSalesPanel = new TotalSalesPanel();
mainPanel.add("TotalSalesPanel", cTotalSalesPanel);
cPaymentPanel = new PaymentPanel();
mainPanel.add("PaymentPanel", cPaymentPanel);
changeMode(MODE_ANONYMOUS);
}
private void createMasterPanelConpornents()
{
// Add menu to frame
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mnFile = new JMenu("File");
menuBar.add(mnFile);
mntm1 = new JMenuItem("[1] Login");
mnFile.add(mntm1);
mntm1.addActionListener(this);
mntm2 = new JMenuItem("[2] Exit");
mnFile.add(mntm2);
mntm2.addActionListener(this);
//----------- Create main panels ------------
con.setLayout(new BorderLayout());
//head panel
headPanel = new JPanel();
headPanel.setBackground(Color.BLACK);
headPanel.setLayout(new FlowLayout());
headTitle = new JLabel("Valentino Restaurant Management System");
headTitle.setForeground(Color.WHITE);
headTitle.setPreferredSize(new Dimension(500, 30));
headTitle.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 24));
headBtnLogin = new JButton("Login");
headBtnLogin.addActionListener(this);
headBtnLogout = new JButton("Logout");
headBtnLogout.addActionListener(this);
headPanel.add(headTitle);
headPanel.add(headBtnLogin);
headPanel.add(headBtnLogout);
con.add(headPanel, BorderLayout.NORTH);
//main content panel
mainPanel = new JPanel();
mainPanel.setOpaque(true);
mainPanel.setLayout(new CardLayout());
con.add(mainPanel, BorderLayout.CENTER);
//main operate buttons panel
mainBtnsPanel = new JPanel();
mainBtnsPanel.setLayout(new GridLayout(0,1));
mainBtnShowMenu = new JButton("Show menu");
mainBtnShowMenu.addActionListener(this);
mainBtnsPanel.add(mainBtnShowMenu);
mainBtnManageOrder = new JButton("Order management");
mainBtnManageOrder.addActionListener(this);
mainBtnsPanel.add(mainBtnManageOrder);
mainBtnManageEmployee = new JButton("Manage employees");
mainBtnManageEmployee.addActionListener(this);
mainBtnsPanel.add(mainBtnManageEmployee);
mainBtnManageMenuItem = new JButton("Manage menu items");
mainBtnManageMenuItem.addActionListener(this);
mainBtnsPanel.add(mainBtnManageMenuItem);
mainBtnShowTotalSales = new JButton("Show total sales");
mainBtnShowTotalSales.addActionListener(this);
mainBtnsPanel.add(mainBtnShowTotalSales);
mainBtnShowPayment = new JButton("Show payments");
mainBtnShowPayment.addActionListener(this);
mainBtnsPanel.add(mainBtnShowPayment);
con.add(mainBtnsPanel, BorderLayout.WEST);
//Information panel
infoPanel = new JPanel();
infoPanel.setLayout(new FlowLayout());
labelLoginUserName = new JLabel();
labelLoginUserName.setPreferredSize(new Dimension(150, 50));
taMessage = new JTextArea(3,50);
taMessage.setEditable(false);
taMessage.setText("Wellcome!!");
taMessage.setOpaque(true);
btnClockOut = new JButton("Clock out");
btnClockOut.setEnabled(false);
btnClockOut.addActionListener(this);
LineBorder border = new LineBorder(Color.BLACK, 3, true);
taMessage.setBorder(border);
taMessage.setBackground(Color.WHITE);
infoPanel.add(labelLoginUserName);
infoPanel.add(btnClockOut);
infoPanel.add(taMessage);
con.add(infoPanel, BorderLayout.SOUTH);
}
public void setLoginUserName(String newName)
{
currentUserName = newName;
if(newName == "")
{
labelLoginUserName.setText("Please login first.");
}
else
{
labelLoginUserName.setText("<html>Login user<br>" + newName + "</html>");
}
}
public final static byte MODE_ANONYMOUS = 0;
public final static byte MODE_EMPLOYEE = 1;
public final static byte MODE_MANAGER = 2;
public void changeMode(byte state)
{
switch(state)
{
case MODE_ANONYMOUS:
headBtnLogout.setEnabled(false);
mainBtnShowMenu.setEnabled(false);
mainBtnManageOrder.setEnabled(false);
mainBtnManageEmployee.setEnabled(false);
mainBtnManageMenuItem.setEnabled(false);
mainBtnShowTotalSales.setEnabled(false);
mainBtnShowPayment.setEnabled(false);
break;
case MODE_EMPLOYEE:
headBtnLogout.setEnabled(true);
mainBtnShowMenu.setEnabled(true);
mainBtnManageOrder.setEnabled(true);
mainBtnManageEmployee.setEnabled(false);
mainBtnManageMenuItem.setEnabled(false);
mainBtnShowTotalSales.setEnabled(false);
mainBtnShowPayment.setEnabled(false);
break;
case MODE_MANAGER:
headBtnLogout.setEnabled(true);
mainBtnShowMenu.setEnabled(true);
mainBtnManageOrder.setEnabled(true);
mainBtnManageEmployee.setEnabled(true);
mainBtnManageMenuItem.setEnabled(true);
mainBtnShowTotalSales.setEnabled(true);
mainBtnShowPayment.setEnabled(true);
break;
}
}
public void setTodaysDate(String today)
{
////
}
void setClockOutButton()
{
if(rcController.checkIfUserClockedOut())
btnClockOut.setEnabled(true);
else
btnClockOut.setEnabled(false);
}
//--------------------------------------------------------
// Display message on an information panel
//--------------------------------------------------------
public void displayMessage(String message)
{
taMessage.setForeground(Color.BLACK);
taMessage.setText(message);
}
public void displayErrorMessage(String message)
{
taMessage.setForeground(Color.RED);
taMessage.setText(message);
}
//========================================================
// Show dialog message
//========================================================
final static int DIALOG_YES = JOptionPane.YES_OPTION;
final static int DIALOG_NO = JOptionPane.NO_OPTION;
final static int DIALOG_CANCEL = JOptionPane.CANCEL_OPTION;
public int showYesNoDialog(String title, String message)
{
int option = JOptionPane.showConfirmDialog(this, message, title, JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
return option;
}
public int showYesNoCancelDiaglog(String title, String message)
{
int option = JOptionPane.showConfirmDialog(this, message, title, JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
return option;
}
public void showErrorDialog(String title, String message)
{
JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE);
}
public void showConfirmDialog(String title, String message)
{
JOptionPane.showMessageDialog(this, message, title, JOptionPane.PLAIN_MESSAGE);
}
private int getIDfromString(String stringLine, int length)
{
int index = stringLine.indexOf("ID:"); //Search string of "ID:"
if(index == -1)
{
showErrorDialog("Error", "String 'ID:' is not found!!");
return -1;
}
try
{
String strID = stringLine.substring(index + 3, index + 3 + length);
int id = Integer.parseInt(strID.trim());
return id;
}
catch(Exception e)
{
showErrorDialog("Error", "Parse error");
return -1;
}
}
//========================================================
// Master panel action
//========================================================
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == mntm2)
{
System.exit(0);
}
else if (ae.getSource() == mainBtnShowMenu)
{
//((CardLayout) mainPanel.getLayout()).show( mainPanel, "MenuList");
changeMainPanel("MenuList");
cMenuListPanel.init();
}
else if (ae.getSource() == mainBtnManageOrder)
{
//((CardLayout) mainPanel.getLayout()).show( mainPanel, "OrderList");
changeMainPanel("OrderList");
cOrderListPanel.init();
}
else if (ae.getSource() == mainBtnManageEmployee)
{
changeMainPanel("EmployeeList");
cEmployeeListPanel.init();
}
else if (ae.getSource() == mainBtnManageMenuItem)
{
changeMainPanel("MenuManagement");
cMenuManagementPanel.init();
}
else if (ae.getSource() == mainBtnShowTotalSales)
{
changeMainPanel("TotalSalesPanel");
cTotalSalesPanel.init();
}
else if (ae.getSource() == mainBtnShowPayment)
{
changeMainPanel("PaymentPanel");
cPaymentPanel.init();
}
else if (ae.getSource() == headBtnLogin || ae.getSource() == mntm1) {
changeMainPanel("Login");
cLoginPanel.init();
displayMessage("Enter your login ID and password.");
}
else if (ae.getSource() == headBtnLogout) {
if( showYesNoDialog("Logout","Are you sure to logout?") == DIALOG_YES)
{
rcController.userLogout();
changeMainPanel("Home");
changeMode(MODE_ANONYMOUS);
setClockOutButton();
}
}
else if (ae.getSource() == btnClockOut){
if( showYesNoDialog("Clock out","Are you sure to clock out?") == DIALOG_YES)
{
rcController.clockOut();
setClockOutButton();
}
}
}
/****************************************************************
* Login panel
*****************************************************************/
private class LoginPanel extends JPanel implements ActionListener
{
// components for login panel
//private JPanel loginPanel;
private JLabel lblUserID;
private JTextField tfUserID;
private JLabel lblPassword;
private JPasswordField pwPassword;
private JCheckBox chbIsManager;
private JButton btnLoginOK;
public LoginPanel()
{
//loginPanel = new JPanel();
GridBagLayout gbLayout = new GridBagLayout();
this.setLayout( gbLayout);
GridBagConstraints gbc = new GridBagConstraints();
lblUserID = new JLabel("UserID:");
lblUserID.setPreferredSize(new Dimension(100, 30));
gbc.gridx = 0;
gbc.gridy = 0;
gbLayout.setConstraints(lblUserID, gbc);
this.add(lblUserID);
tfUserID = new JTextField(20);
tfUserID.setInputVerifier(new IntegerInputVerifier(0));
gbc.gridx = 1;
gbc.gridy = 0;
gbLayout.setConstraints(tfUserID, gbc);
this.add(tfUserID);
lblPassword = new JLabel("Password:");
lblPassword.setPreferredSize(new Dimension(100, 30));
gbc.gridx = 0;
gbc.gridy = 1;
gbLayout.setConstraints(lblPassword, gbc);
this.add(lblPassword);
pwPassword = new JPasswordField(20);
gbc.gridx = 1;
gbc.gridy = 1;
gbLayout.setConstraints(pwPassword, gbc);
this.add(pwPassword);
chbIsManager = new JCheckBox("Login as manager");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbLayout.setConstraints(chbIsManager, gbc);
this.add(chbIsManager);
btnLoginOK = new JButton("Login");
btnLoginOK.addActionListener(this);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbLayout.setConstraints(btnLoginOK, gbc);
this.add(btnLoginOK);
}
private void setUserID(String id)
{
tfUserID.setText(id);
}
private void setPassword(String password)
{
pwPassword.setText(password);
}
public void init()
{
setUserID("");
setPassword("");
tfUserID.setBackground( UIManager.getColor( "TextField.background" ) );
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnLoginOK)
{
//Check whether current focuced compornent have to verify their value
if (btnLoginOK.getVerifyInputWhenFocusTarget()) {
//Try to get focus
btnLoginOK.requestFocusInWindow();
if (!btnLoginOK.hasFocus()) { //Can not get focus ?Ë the compornent have not been verified
return;
}
}
//if(!inputVerified)
// return;
char[] password;
boolean isManager = chbIsManager.isSelected();
byte state = -1;
String inputID = tfUserID.getText();
if(inputID.equals(""))
{
displayErrorMessage("Enter user ID");
return;
}
password= pwPassword.getPassword();
String inputPassword = new String(password);
if(inputPassword.equals(""))
{
displayErrorMessage("Enter password");
return;
}
if( rcController.loginCheck(Integer.parseInt(inputID), inputPassword, isManager))
{
showConfirmDialog("Message", "Login success!!");
displayMessage("Wellcome, " + currentUserName);
tfUserID.setText("");
pwPassword.setText("");
changeMainPanel("Home");
setClockOutButton();
}
else
{
displayErrorMessage(rcController.getErrorMessage());
}
}
}
}
private void changeMainPanel(String panelName)
{
((CardLayout) mainPanel.getLayout()).show( mainPanel, panelName);
displayMessage("Main paanel change :" + panelName);
}
/****************************************************************
* Menu list panel
*****************************************************************/
private class MenuListPanel extends JPanel implements ActionListener
{
private JScrollPane scrollPanel;
private JTextArea displayArea;
private JPanel btnPanel;
private JButton btnAll;
private JButton btnMain;
private JButton btnDrink;
private JButton btnAlcohol;
private JButton btnDessert;
public MenuListPanel()
{
this.setLayout( new BorderLayout());
displayArea = new JTextArea();
displayArea.setFont(new Font(Font.MONOSPACED,Font.PLAIN,16));
displayArea.setEditable(false);
displayArea.setMargin(new Insets(5, 5, 5, 5));
scrollPanel = new JScrollPane(displayArea);
scrollPanel.setPreferredSize(new Dimension(200, 400));
add(scrollPanel, BorderLayout.CENTER);
btnPanel = new JPanel();
btnPanel.setLayout( new FlowLayout());
btnAll = new JButton("All");
btnAll.addActionListener(this);
btnMain = new JButton("Main");
btnMain.addActionListener(this);
btnDrink = new JButton("Drink");
btnDrink.addActionListener(this);
btnAlcohol = new JButton("Alcohol");
btnAlcohol.addActionListener(this);
btnDessert = new JButton("Dessert");
btnDessert.addActionListener(this);
btnPanel.add(btnAll);
btnPanel.add(btnMain);
btnPanel.add(btnDrink);
btnPanel.add(btnAlcohol);
btnPanel.add(btnDessert);
add(btnPanel, BorderLayout.SOUTH);
}
public void init()
{
showMenuList(0);
//displayArea.setText(str);
//showAllMenuList(displayArea);
}
private void showMenuList(int menuType)
{
displayArea.setText("");
ArrayList<String> menuList = rcController.createMenuList(menuType);
for(int i = 0; i < menuList.size(); i++)
displayArea.append(menuList.get(i) + "\n");
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnAll)
{
showMenuList(0);
//showAllMenuList(displayArea);
}
else if (ae.getSource() == btnMain)
{
showMenuList(MenuItem.MAIN);
//showParticularMenuList(MenuItem.MAIN, displayArea);
}
else if (ae.getSource() == btnDrink)
{
showMenuList(MenuItem.DRINK);
//showParticularMenuList(MenuItem.DRINK, displayArea);
}
else if (ae.getSource() == btnAlcohol)
{
showMenuList(MenuItem.ALCOHOL);
//showParticularMenuList(MenuItem.ALCOHOL, displayArea);
}
else if (ae.getSource() == btnDessert)
{
showMenuList(MenuItem.DESSERT);
//showParticularMenuList(MenuItem.DESSERT, displayArea);
}
}
}
/****************************************************************
* MenuManagementPanel
*****************************************************************/
private class MenuManagementPanel extends JPanel implements ActionListener
{
private JScrollPane scrollPanel;
private JList displayList;
private JButton btnAddNewMenuItem;
private JButton btnEditMenuItem;
private JButton btnDeleteMenuItem;
public MenuManagementPanel()
{
GridBagLayout gbLayout = new GridBagLayout();
this.setLayout( gbLayout);
GridBagConstraints gbc = new GridBagConstraints();
scrollPanel = new JScrollPane();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridwidth = 3;
gbLayout.setConstraints(scrollPanel, gbc);
this.add(scrollPanel);
btnAddNewMenuItem = new JButton("Add new menu item");
btnAddNewMenuItem.addActionListener(this);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weighty = 0;
gbc.weightx = 0.5;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbLayout.setConstraints(btnAddNewMenuItem, gbc);
this.add(btnAddNewMenuItem);
btnEditMenuItem = new JButton("Edit menu item");
btnEditMenuItem.addActionListener(this);
gbc.gridx = 1;
gbc.gridy = 1;
gbLayout.setConstraints(btnEditMenuItem, gbc);
this.add(btnEditMenuItem);
btnDeleteMenuItem = new JButton("Delete menu item");
btnDeleteMenuItem.addActionListener(this);
gbc.gridx = 2;
gbc.gridy = 1;
gbLayout.setConstraints(btnDeleteMenuItem, gbc);
this.add(btnDeleteMenuItem);
displayList = new JList();
displayList.setFont(new Font(Font.MONOSPACED,Font.PLAIN,16));
displayList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
public void init()
{
showMenuList();
}
private void showMenuList()
{
displayList.setListData(rcController.createMenuList(0).toArray());
scrollPanel.getViewport().setView(displayList);
}
private int getSelectedMenuID()
{
String orderLine = (String)displayList.getSelectedValue();
if (orderLine == null)
return -1;
return getIDfromString( orderLine, 4);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnAddNewMenuItem)
{
cEditMenuItemPanel.init(0);
changeMainPanel("EditMenuItem");
}
else if (ae.getSource() == btnEditMenuItem)
{
int menuID = getSelectedMenuID();
if( menuID == -1) return;
cEditMenuItemPanel.init(menuID);
changeMainPanel("EditMenuItem");
}
else if (ae.getSource() == btnDeleteMenuItem)
{
int deleteMenuID = getSelectedMenuID();
if( deleteMenuID == -1) return;
if( showYesNoDialog("", "Are you sure to delete the menu item?") == DIALOG_YES)
{
if(!rcController.deleteMenuItem(deleteMenuID))
{
showErrorDialog("Error", rcController.getErrorMessage());
}
else
{
displayMessage("Deleted.");
init();
}
}
}
}
}
/****************************************************************
* Edit menu item panel
*****************************************************************/
private class EditMenuItemPanel extends JPanel implements ActionListener
{
private JLabel lblMenuItemID;
private JTextField tbMenuItemID;
private JLabel lblName;
private JTextField tbName;
private JLabel lblPrice;
private JTextField tbPrice;
private JLabel lblType;
private JComboBox cbType;
private JButton btnOK;
private boolean isUpdate;
public EditMenuItemPanel()
{
GridBagLayout gbLayout = new GridBagLayout();
this.setLayout( gbLayout);
GridBagConstraints gbc = new GridBagConstraints();
lblMenuItemID = new JLabel("Menu item ID:");
lblMenuItemID.setPreferredSize(new Dimension(100, 30));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbLayout.setConstraints(lblMenuItemID, gbc);
this.add(lblMenuItemID);
tbMenuItemID = new JTextField(4);
tbMenuItemID.setInputVerifier(new IntegerInputVerifier(1,10000));
gbc.gridx = 1;
gbc.gridy = 0;
gbLayout.setConstraints(tbMenuItemID, gbc);
this.add(tbMenuItemID);
lblName = new JLabel("Menu item name:");
lblName.setPreferredSize(new Dimension(100, 30));
gbc.gridx = 0;
gbc.gridy = 1;
gbLayout.setConstraints(lblName, gbc);
this.add(lblName);
tbName = new JTextField(20);
gbc.gridx = 1;
gbc.gridy = 1;
gbLayout.setConstraints(tbName, gbc);
this.add(tbName);
lblPrice = new JLabel("Menu item price:");
lblPrice.setPreferredSize(new Dimension(100, 30));
gbc.gridx = 0;
gbc.gridy = 2;
gbLayout.setConstraints(lblPrice, gbc);
this.add(lblPrice);
tbPrice = new JTextField(10);
tbPrice.setInputVerifier(new DoubleInputVerifier(1,10000));
gbc.gridx = 1;
gbc.gridy = 2;
gbLayout.setConstraints(tbPrice, gbc);
this.add(tbPrice);
lblType = new JLabel("Menu item type:");
lblType.setPreferredSize(new Dimension(100, 30));
gbc.gridx = 0;
gbc.gridy = 3;
gbLayout.setConstraints(lblType, gbc);
this.add(lblType);
String[] combodata = {"Main", "Drink", "Alcohol", "Dessert"};
cbType = new JComboBox(combodata);
gbc.gridx = 1;
gbc.gridy = 3;
gbLayout.setConstraints(cbType, gbc);
this.add(cbType);
btnOK = new JButton("OK");
btnOK.addActionListener(this);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbLayout.setConstraints(btnOK, gbc);
this.add(btnOK);
}
private void setMenuID(String id)
{
tbMenuItemID.setText(id);
}
private void setItemName(String name)
{
tbName.setText(name);
}
private void setPrice(String price)
{
tbPrice.setText(price);
}
private void setType(String type)
{
cbType.setSelectedItem(type);
}
public void init(int menuItemID)
{
//------------- Add new menu item ------------
if( menuItemID == 0)
{
setMenuID("");
tbMenuItemID.setEditable(true);
setItemName("");
setPrice("");
setType("Main");
isUpdate = false;
return;
}
//------------- Update menu item ------------
MenuItem rMenuItem = rcController.getMenuItemData(menuItemID);
isUpdate = true;
if( rMenuItem == null)
{
showErrorDialog("Error", "Get menu item data failed.");
setItemName("");
setPrice("");
setType("Main");
return;
}
setMenuID(Integer.toString(rMenuItem.getID()));
setItemName(rMenuItem.getName());
setPrice(Double.toString(rMenuItem.getPrice()));
tbPrice.setBackground( UIManager.getColor( "TextField.background" ) );
switch( rMenuItem.getType())
{
case MenuItem.MAIN:
setType("Main");
break;
case MenuItem.DRINK:
setType("Drink");
break;
case MenuItem.ALCOHOL:
setType("Alcohol");
break;
case MenuItem.DESSERT:
setType("Dessert");
break;
}
tbMenuItemID.setEditable(false);
tbMenuItemID.setBackground( UIManager.getColor( "TextField.background" ) );
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnOK)
{
//if( !inputVerified) return;
//Check whether current focuced compornent have to verify their value
if (btnOK.getVerifyInputWhenFocusTarget()) {
//Try to get focus
btnOK.requestFocusInWindow();
if (!btnOK.hasFocus()) { //Can not get focus ?Ë the compornent have not been verified
return;
}
}
if( tbMenuItemID.getText().equals("") || tbName.getText().equals("") || tbPrice.getText().equals(""))
{
displayErrorMessage("Fill all form!!");
return;
}
int menuItemID = Integer.parseInt(tbMenuItemID.getText());
String strMenuType = (String)cbType.getSelectedItem();
byte menuType;
if( strMenuType.equals("Main"))
{
menuType = MenuItem.MAIN;
}
else if( strMenuType.equals("Drink"))
{
menuType = MenuItem.DRINK;
}
else if( strMenuType.equals("Alcohol"))
{
menuType = MenuItem.ALCOHOL;
}
else //Dessert
{
menuType = MenuItem.DESSERT;
}
if(isUpdate)
{
if(! rcController.updateMenuItem(menuItemID , tbName.getText(), Double.parseDouble(tbPrice.getText()), menuType))
{
showErrorDialog("Error", rcController.getErrorMessage());
return;
}
showConfirmDialog("Message", "Update successful!!");
}
else
{
if(! rcController.addNewMenuItem(menuItemID , tbName.getText(), Double.parseDouble(tbPrice.getText()), menuType))
{
showErrorDialog("Error", rcController.getErrorMessage());
return;
}
showConfirmDialog("Message", "New menu item is added!!");
}
init(menuItemID);
}
}
}
/****************************************************************
* Employee list panel
*****************************************************************/
private class EmployeeListPanel extends JPanel implements ActionListener
{
private JScrollPane scrollPanel;
private JList displayList;
//private JPanel btnPanel;
private JButton btnAddStaff;
private JButton btnEditStaff;
private JButton btnDeleteStaff;
private JButton btnClockOut;
public EmployeeListPanel()
{
GridBagLayout gbLayout = new GridBagLayout();
this.setLayout( gbLayout);