-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewOfficeHoursController.java
More file actions
144 lines (117 loc) · 4.54 KB
/
ViewOfficeHoursController.java
File metadata and controls
144 lines (117 loc) · 4.54 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
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
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.scene.control.cell.PropertyValueFactory;
import s25.cs151.application.model.DataManager;
import s25.cs151.application.model.SemesterOfficeHours;
import java.util.*;
import s25.cs151.application.model.*;
public class ViewOfficeHoursController {
@FXML
private TableView<OfficeHourRow> officeHoursTable;
@FXML
private TableColumn<OfficeHourRow, String> semesterColumn;
@FXML
private TableColumn<OfficeHourRow, String> yearColumn;
@FXML
private TableColumn<OfficeHourRow, String> daysColumn;
@FXML
private Button closeButton;
// Define semester order
private static final Map<String, Integer> SEMESTER_ORDER = new HashMap<>() {{
put("Spring", 0);
put("Summer", 1);
put("Fall", 2);
put("Winter", 3);
}};
@FXML
private void initialize() {
// Configure table columns
semesterColumn.setCellValueFactory(new PropertyValueFactory<>("semester"));
yearColumn.setCellValueFactory(new PropertyValueFactory<>("year"));
daysColumn.setCellValueFactory(new PropertyValueFactory<>("days"));
// Load and display sorted data
loadOfficeHours();
}
private void loadOfficeHours() {
ObservableList<OfficeHourRow> tableData = FXCollections.observableArrayList();
// Load saved office hours
List<SemesterOfficeHours> savedHours = DataManager.loadAllOfficeHours();
// Convert each SemesterOfficeHours to a display row
for (SemesterOfficeHours hour : savedHours) {
tableData.add(new OfficeHourRow(hour));
}
// Sort the data
tableData.sort((row1, row2) -> {
// First, compare by year (descending)
int yearCompare = row2.getYear().compareTo(row1.getYear());
if (yearCompare != 0) return yearCompare;
// Then, compare by semester
return Integer.compare(
SEMESTER_ORDER.get(row1.getSemester()),
SEMESTER_ORDER.get(row2.getSemester())
);
});
// Set the sorted data to the table
officeHoursTable.setItems(tableData);
}
@FXML
private void onClose() {
Stage stage = (Stage) closeButton.getScene().getWindow();
stage.close();
}
@FXML
private void onClear() {
// Show confirmation dialog
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Clear All Entries");
alert.setHeaderText("Are you sure?");
alert.setContentText("This will permanently delete all saved office hours entries.");
// Wait for user response
alert.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
// Clear the data
DataManager.clearAllOfficeHours();
// Refresh the table
loadOfficeHours();
// Show success message
Alert success = new Alert(Alert.AlertType.INFORMATION);
success.setTitle("Success");
success.setHeaderText(null);
success.setContentText("All entries have been cleared.");
success.show();
}
});
}
// Helper class for table display
public static class OfficeHourRow {
private final String semester;
private final String year;
private final String days;
public OfficeHourRow(SemesterOfficeHours hours) {
this.semester = hours.getSemester();
this.year = hours.getYear();
this.days = convertDaysToString(hours.getDaysSelected());
}
private String convertDaysToString(boolean[] daysSelected) {
String[] dayNames = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
StringBuilder days = new StringBuilder();
for (int i = 0; i < daysSelected.length; i++) {
if (daysSelected[i]) {
if (days.length() > 0) {
days.append(", ");
}
days.append(dayNames[i]);
}
}
return days.toString();
}
// Getters required for TableView
public String getSemester() { return semester; }
public String getYear() { return year; }
public String getDays() { return days; }
}
}