-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewTimeslotsController.java
More file actions
98 lines (80 loc) · 3.09 KB
/
ViewTimeslotsController.java
File metadata and controls
98 lines (80 loc) · 3.09 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
package s25.cs151.application.controller;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.beans.property.SimpleStringProperty;
import s25.cs151.application.model.DataManager;
import s25.cs151.application.model.Timeslot;
import s25.cs151.application.model.*;
public class ViewTimeslotsController {
@FXML
private TableView<Timeslot> timeslotsTable;
@FXML
private TableColumn<Timeslot, String> startTimeColumn;
@FXML
private TableColumn<Timeslot, String> endTimeColumn;
@FXML
private Button closeButton;
@FXML
private void initialize() {
// Configure table columns
startTimeColumn.setCellValueFactory(data ->
new SimpleStringProperty(data.getValue().getFormattedStartTime()));
endTimeColumn.setCellValueFactory(data ->
new SimpleStringProperty(data.getValue().getFormattedEndTime()));
// Load and display sorted data
loadTimeslots();
}
private void loadTimeslots() {
ObservableList<Timeslot> tableData = FXCollections.observableArrayList(
DataManager.loadAllTimeslots()
);
// Sort the timeslots
tableData.sort((t1, t2) -> {
// Convert times to comparable values (in minutes since midnight)
int time1 = convertToMinutes(t1.getStartHour(), t1.getStartMinute(), t1.getStartAmPm());
int time2 = convertToMinutes(t2.getStartHour(), t2.getStartMinute(), t2.getStartAmPm());
return Integer.compare(time1, time2);
});
timeslotsTable.setItems(tableData);
}
private int convertToMinutes(String hour, String minute, String amPm) {
int h = Integer.parseInt(hour);
int m = Integer.parseInt(minute);
// Convert to 24-hour format
if (amPm.equals("PM") && h != 12) {
h += 12;
} else if (amPm.equals("AM") && h == 12) {
h = 0;
}
return h * 60 + m;
}
@FXML
private void onClear() {
Alert confirmAlert = new Alert(Alert.AlertType.CONFIRMATION);
confirmAlert.setTitle("Clear All Timeslots");
confirmAlert.setHeaderText("Are you sure?");
confirmAlert.setContentText("This will permanently delete all saved timeslots.");
confirmAlert.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
// Clear the data
DataManager.clearAllTimeslots();
// Show success message
Alert successAlert = new Alert(Alert.AlertType.INFORMATION);
successAlert.setTitle("Success");
successAlert.setHeaderText(null);
successAlert.setContentText("All timeslots have been cleared.");
successAlert.show();
// Refresh the table
loadTimeslots();
}
});
}
@FXML
private void onClose() {
Stage stage = (Stage) closeButton.getScene().getWindow();
stage.close();
}
}