forked from ndraiman/SQL-JDBC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlightsPanel.java
278 lines (212 loc) · 7.92 KB
/
FlightsPanel.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
package ex5;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.DefaultCaret;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
public class FlightsPanel extends JPanel {
private FlightsDatabase fd;
private JPanel _toolbar;
private JButton cmdCreateTable, cmdDropTable, cmdInsert, cmdAddTrigger, cmdCheapestFlight, cmdClear;
private JLabel lblFno, lblSource, lblDest, lblCost, lblInsert, lblCheapestFlight;
private JTextField txtFno, txtSource, txtDest, txtCost;
private JTextArea console;
private JScrollPane scroll;
/*******************************************************/
/******************** Constructor **********************/
/*******************************************************/
public FlightsPanel(String login, String password, int option) {
/*** Initialize JPanel Components ***/
//Buttons
cmdCreateTable = new JButton("Create Table");
cmdDropTable = new JButton("Drop Table");
cmdInsert = new JButton("Insert");
cmdAddTrigger = new JButton("Add Trigger");
cmdCheapestFlight = new JButton("Cheapest Flight");
cmdClear = new JButton("Clear Console");
//Labels
lblFno = new JLabel("Fno: (Max 3 Digits)");
lblSource = new JLabel("Source: (Max 30 Chars)");
lblDest = new JLabel("Destination: (Max 30 Chars)");
lblCost = new JLabel("Cost: (Max 4 Digits)");
lblInsert = new JLabel("Insert Values");
lblCheapestFlight = new JLabel("Cheapest Flight");
//Text Fields
txtFno = new JTextField(3);
txtSource = new JTextField(30);
txtDest = new JTextField(30);
txtCost = new JTextField(4);
//Set Button State
cmdCreateTable.setEnabled(true);
cmdDropTable.setEnabled(false);
cmdInsert.setEnabled(false);
cmdAddTrigger.setEnabled(false);
cmdCheapestFlight.setEnabled(false);
cmdClear.setEnabled(true);
//Assign Button Listener
ButtonListener buttonLis = new ButtonListener();
cmdCreateTable.addActionListener(buttonLis);
cmdDropTable.addActionListener(buttonLis);
cmdInsert.addActionListener(buttonLis);
cmdAddTrigger.addActionListener(buttonLis);
cmdCheapestFlight.addActionListener(buttonLis);
cmdClear.addActionListener(buttonLis);
/*** Define Layout ***/
//General Layout
setLayout(new BorderLayout());
setBackground(Color.white);
_toolbar = new JPanel();
_toolbar.setLayout(new GridLayout(2, 3, 10, 10));
_toolbar.add(cmdInsert);
_toolbar.add(cmdAddTrigger);
_toolbar.add(cmdCheapestFlight);
_toolbar.add(cmdCreateTable);
_toolbar.add(cmdDropTable);
_toolbar.add(cmdClear);
//Text Area for Console output
console = new JTextArea(5, 20);
console.setEditable(false);
console.setBackground(Color.WHITE);
console.setLineWrap(true);
console.setBounds(0, 0, getWidth(), getHeight()/2);
//Set update policy to always scroll to bottom of console
DefaultCaret caret = (DefaultCaret)console.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
//Add scroll bar
scroll = new JScrollPane (console,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//Add Components to JPanel
add(_toolbar, BorderLayout.SOUTH);
add(scroll, BorderLayout.CENTER);
//Redirect Output according to option
if(option == JOptionPane.YES_OPTION) {
PrintStream printStream = new PrintStream(new CustomOutputStream(console));
System.setOut(printStream);
System.setErr(printStream);
}
//Initialize Database
fd = new FlightsDatabase(login, password);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
repaint();
}
/*******************************************************/
/****************** Button Listener ********************/
/*******************************************************/
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == cmdClear) {
console.setText("");
}
else if(e.getSource() == cmdCreateTable) {
fd.create();
cmdCreateTable.setEnabled(false);
cmdDropTable.setEnabled(true);
cmdInsert.setEnabled(true);
cmdAddTrigger.setEnabled(true);
cmdCheapestFlight.setEnabled(true);
}
else if (e.getSource() == cmdDropTable) {
fd.clean();
cmdCreateTable.setEnabled(true);
cmdDropTable.setEnabled(false);
cmdInsert.setEnabled(false);
cmdAddTrigger.setEnabled(false);
cmdCheapestFlight.setEnabled(false);
}
else if (e.getSource() == cmdAddTrigger) {
fd.addTrigger();
}
else if (e.getSource() == cmdInsert) {
String source = "", dest = "";
int fno = 0, cost = 0;
while(true) {
//Open Dialog to get Insert Values
Object[] arr = {lblFno, txtFno, lblSource, txtSource, lblDest, txtDest, lblCost, txtCost};
int option = JOptionPane.showConfirmDialog(null, arr, lblInsert.getText(), JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.OK_OPTION) {
//check Values legality
if(txtFno.getText().trim().equals("") || txtCost.getText().trim().equals("") ||
txtSource.getText().trim().equals("") || txtDest.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "some text fields are Empty! Please Input Data in all of them!",
"ERROR", JOptionPane.ERROR_MESSAGE);
continue;
}
}
else
return;
source = txtSource.getText().trim();
dest = txtDest.getText().trim();
try {
fno = Integer.parseInt(txtFno.getText().trim());
cost = Integer.parseInt(txtCost.getText().trim());
} catch (NumberFormatException err) {
JOptionPane.showMessageDialog(null, "Text was inserted into a Number field! please insert a number!",
"ERROR", JOptionPane.ERROR_MESSAGE);
continue;
}
break;
}
fd.insert(fno, source, dest, cost);
//Reset Text Fields
txtFno.setText("");
txtSource.setText("");
txtDest.setText("");
txtCost.setText("");
}
else if (e.getSource() == cmdCheapestFlight) {
while(true) {
Object[] arr = {lblSource, txtSource, lblDest, txtDest};
int option = JOptionPane.showConfirmDialog(null, arr, lblCheapestFlight.getText(), JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.OK_OPTION) {
//check Values legality
if(txtSource.getText().trim().equals("") || txtDest.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "some text fields are Empty! Please Input Data in all of them!",
"ERROR", JOptionPane.ERROR_MESSAGE);
continue;
}
}
else
return;
break;
}
String source = txtSource.getText().trim();
String dest = txtDest.getText().trim();
System.out.println("Cheapest Flight from " + source + " to " + dest + " costs " + fd.CheapestFlight(source, dest));
//Reset Text Fields
txtSource.setText("");
txtDest.setText("");
}
}
}
/*******************************************************/
/***************** CustomOutputStream ******************/
/*******************************************************/
public class CustomOutputStream extends OutputStream {
private JTextArea output;
public CustomOutputStream(JTextArea textArea) {
output = textArea;
}
@Override
public void write(int b) throws IOException {
// redirects data to the text area
output.append(String.valueOf((char)b));
// scrolls the text area to the end of data
output.setCaretPosition(output.getDocument().getLength());
}
}
}