-
Notifications
You must be signed in to change notification settings - Fork 1
/
GUIController.java
191 lines (164 loc) · 5.15 KB
/
GUIController.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
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* Controller for GUI. This handles action responses as well as populating the reserved menu space.
* @author Chae Jubb
* @version 1.0
*
*/
public class GUIController implements ActionListener {
private final GUI gui;
private Order order;
private IOHandler io;
/**
* Constructor for GUI Controller
*
* @param gui
* GUI which this controller controls
*/
public GUIController(GUI gui) {
this.gui = gui;
this.order = new Order();
this.io = new IOHandler();
}
/**
* Handles responses to button clicks (clear, submit, menu item) and directs
* them to appropriate sub-methods
*
* @param arg0
* Action Event triggering this method
*/
@Override
public void actionPerformed(ActionEvent arg0) {
JTextArea textArea = this.gui.getTextArea();
if (arg0.getSource().equals(this.gui.getClearButton())) {
this.clear(textArea);
} else if (arg0.getSource().equals(this.gui.getSubmitButton())) {
this.submit(textArea);
} else {
// find menu item corresponding to button text
JButton btnSrc = (JButton) arg0.getSource();
Item itmSrc = Menu.findItem(new Item(btnSrc.getText()));
// keeps GUI and order object up to date
textArea.append(itmSrc.toString() + "\n");
this.order.addItem(itmSrc);
this.updateRunningTotal();
}
}
/**
* Resets GUI and sub-objects and prepares for another order to be placed
*
* @param textArea
* Text Area in the GUI to be cleared
*/
private void clear(JTextArea textArea) {
textArea.setText("");
this.order.clearItems();
this.updateRunningTotal();
}
/**
* Submits order by recording to log file and resetting for another order to
* be placed.
*
* @param textArea
*/
private void submit(JTextArea textArea) {
this.io.writeToLog(this.order.logWrite());
JOptionPane.showMessageDialog(null, "Transaction recorded in logfile!",
"Success!", JOptionPane.INFORMATION_MESSAGE);
this.clear(textArea);
}
/**
* Updates running value of total cost. To be called after each button
* action.
*/
private void updateRunningTotal() {
double totalPrice = this.order.getTotalPrice();
NumberFormat formatter = NumberFormat.getCurrencyInstance();
this.gui.getRunningTotalArea().setText(
"TOTAL: " + formatter.format(totalPrice));
}
/**
* Creates menu formatting and fills menu skeleton with menu item buttons
*
* @param fileLocation
* Location of menu source file
* @param menuArea
* JPanel area which is to be filled
* @return Menu-filled JPanel
*/
public JPanel loadMenu(String fileLocation, JPanel menuArea) {
Menu menuList = Menu.getMenu(fileLocation);
int itemCount = menuList.itemList.length;
menuArea = new JPanel();
// creates different arrangements based on number of menu items
if (itemCount == 1) {
menuArea.setLayout(new GridLayout(1, 1));
} else if (itemCount == 2) {
menuArea.setLayout(new GridLayout(2, 1));
} else if (itemCount == 3) {
menuArea.setLayout(new GridLayout(3, 1));
} else if (itemCount == 4) {
menuArea.setLayout(new GridLayout(2, 2));
} else if (itemCount == 5 || itemCount == 6) {
menuArea.setLayout(new GridLayout(3, 2));
} else if (itemCount == 7 || itemCount == 8 || itemCount == 9) {
menuArea.setLayout(new GridLayout(3, 3));
} else if ((itemCount >= 10) && (itemCount <= 12)) {
menuArea.setLayout(new GridLayout(4, 3));
} else if ((itemCount >= 13) && (itemCount <= 14)) {
menuArea.setLayout(new GridLayout(5, 3));
} else if ((itemCount >= 15) && (itemCount <= 18)) {
menuArea.setLayout(new GridLayout(6, 3));
} else if ((itemCount >= 19) && (itemCount <= 21)) {
menuArea.setLayout(new GridLayout(7, 3));
} else {
// If >21 menuItems, set up scroll bar
int menuHeight = (int) itemCount / 3;
if ((itemCount % 3) != 0) {
menuHeight++;
}
JPanel innerMenu = new JPanel(new GridLayout(menuHeight, 3));
this.populateMenu(innerMenu, itemCount);
JScrollPane scroll = new JScrollPane(innerMenu,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
menuArea.setLayout(new BorderLayout());
menuArea.add(scroll, BorderLayout.CENTER);
}
// already done otherwise
if (itemCount <= 21) {
this.populateMenu(menuArea, itemCount);
}
return menuArea;
}
/**
* Fills menu skeleton with buttons for each menu item
*
* @param panel
* JPanel to be populated
* @param itemCount
* Number of buttons to create in panel
*/
private void populateMenu(JPanel panel, int itemCount) {
String itemName;
JButton btn;
for (int i = 0; i < itemCount; i++) {
// short names go on buttons to help avoid overflow
itemName = Menu.getMenu(null).itemList[i].getShortName();
btn = new JButton(itemName);
btn.setPreferredSize(new Dimension(0, 100));
btn.addActionListener(this);
panel.add(btn);
}
}
}