-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlightStatusPanel.java
100 lines (81 loc) · 3.28 KB
/
FlightStatusPanel.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
package flightscheduleryjw5018.gui;
import flightscheduleryjw5018.data.Booking;
import flightscheduleryjw5018.data.Flight;
import flightscheduleryjw5018.services.DateUtils;
import flightscheduleryjw5018.services.SchedulingService;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class FlightStatusPanel extends JPanel implements FlightUpdateListener, DayUpdateListener {
private SchedulingService schedulingService;
private final JComboBox flightList = new JComboBox();
private final JComboBox dayList = new JComboBox();
private final JTable flightStatus = new JTable();
public FlightStatusPanel() {
}
public void init() {
setLayout(new BorderLayout());
updateFlightList();
updateDayList();
JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Date"));
topPanel.add(dayList);
topPanel.add(new JLabel("Flight"));
topPanel.add(flightList);
JButton getStatusButton = new JButton("Get Status");
getStatusButton.addActionListener((ActionEvent e) -> {
String flight = (String) flightList.getSelectedItem();
Date day = DateUtils.parseDate((String) dayList.getSelectedItem());
flightStatus.removeAll();
//flightStatus.setco
Collection<Booking> bookings = schedulingService.getFlightStatus(flight, day);
if (bookings != null) {
DefaultTableModel model = (DefaultTableModel) flightStatus.getModel();
// Clear the table
model.setRowCount(0);
// Add new rows
for (Booking booking : bookings) {
model.addRow(new Object[]{booking.getFlight(), booking.getDate(), booking.getCustomer(), DateUtils.formatTimestamp(booking.getEntered())});
}
}
});
topPanel.add(getStatusButton);
add(topPanel, BorderLayout.NORTH);
flightStatus.setModel(new DefaultTableModel(new Object[]{"Flight", "Date", "Customer", "Booked On"}, 0));
add(new JScrollPane(flightStatus), BorderLayout.CENTER);
}
public void setSchedulingService(SchedulingService schedulingService) {
this.schedulingService = schedulingService;
}
public void updateFlightList() {
flightList.removeAllItems();
Collection<Flight> flights = schedulingService.findFlights();
for (Flight flight : flights) {
flightList.addItem(flight.getName());
}
}
public void updateDayList() {
dayList.removeAllItems();
Collection<Date> days = schedulingService.getDays();
for (Date day : days) {
String d = DateUtils.formatDate(day);
dayList.addItem(d);
}
}
@Override
public void flightUpdated() {
updateFlightList();
}
@Override
public void dayUpdated() {
updateDayList();
}
}