-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewScheduleEntriesController.java
More file actions
194 lines (159 loc) · 6.6 KB
/
ViewScheduleEntriesController.java
File metadata and controls
194 lines (159 loc) · 6.6 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
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
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 java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import s25.cs151.application.model.DataManager;
import s25.cs151.application.model.ScheduleEntry;
import s25.cs151.application.model.*;
public class ViewScheduleEntriesController {
@FXML
private TableView<ScheduleEntry> entriesTable;
@FXML
private TableColumn<ScheduleEntry, String> studentNameColumn;
@FXML
private TableColumn<ScheduleEntry, String> dateColumn;
@FXML
private TableColumn<ScheduleEntry, String> timeSlotColumn;
@FXML
private TableColumn<ScheduleEntry, String> courseColumn;
@FXML
private TableColumn<ScheduleEntry, String> reasonColumn;
@FXML
private TableColumn<ScheduleEntry, String> commentColumn;
@FXML
private Button deleteButton;
@FXML
private TextField searchField;
@FXML
private Button searchButton;
@FXML
private Button clearSearchButton;
private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
private ObservableList<ScheduleEntry> allEntries;
@FXML
private void initialize() {
// Configure table columns
studentNameColumn.setCellValueFactory(new PropertyValueFactory<>("studentName"));
dateColumn.setCellValueFactory(cellData ->
cellData.getValue().getScheduleDate() != null ?
javafx.beans.binding.Bindings.createStringBinding(
() -> cellData.getValue().getScheduleDate().format(dateFormatter)
) : null
);
timeSlotColumn.setCellValueFactory(new PropertyValueFactory<>("timeSlot"));
courseColumn.setCellValueFactory(new PropertyValueFactory<>("course"));
reasonColumn.setCellValueFactory(new PropertyValueFactory<>("reason"));
commentColumn.setCellValueFactory(new PropertyValueFactory<>("comment"));
// Load and display sorted data
loadSortedEntries();
}
private void loadSortedEntries() {
// Get all entries
List<ScheduleEntry> entries = DataManager.loadScheduleEntries();
// Sort entries by date and then by timeslot
entries.sort(Comparator
.comparing(ScheduleEntry::getScheduleDate) // First sort by date
.thenComparing(entry -> { // Then sort by timeslot
String timeSlot = entry.getTimeSlot();
// Extract start time for comparison
return timeSlot.split(" - ")[0];
})
);
// Store all entries for filtering
allEntries = FXCollections.observableArrayList(entries);
entriesTable.setItems(allEntries);
}
@FXML
private void onSearch() {
String searchText = searchField.getText().trim().toLowerCase();
if (searchText.isEmpty()) {
entriesTable.setItems(allEntries);
return;
}
ObservableList<ScheduleEntry> filteredEntries = allEntries.filtered(entry ->
entry.getStudentName().toLowerCase().contains(searchText)
);
entriesTable.setItems(filteredEntries);
}
@FXML
private void onClearSearch() {
searchField.clear();
entriesTable.setItems(allEntries);
}
@FXML
private void onClear() {
Alert confirmAlert = new Alert(Alert.AlertType.CONFIRMATION);
confirmAlert.setTitle("Clear All Schedule Entries");
confirmAlert.setHeaderText("Are you sure?");
confirmAlert.setContentText("This will permanently delete all saved schedule entries.");
confirmAlert.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
// Clear the data
DataManager.clearAllScheduleEntries();
// Show success message
Alert successAlert = new Alert(Alert.AlertType.INFORMATION);
successAlert.setTitle("Success");
successAlert.setHeaderText(null);
successAlert.setContentText("All schedule entries have been cleared.");
successAlert.show();
// Refresh the table
loadSortedEntries();
}
});
}
@FXML
private void onClose() {
Stage stage = (Stage) entriesTable.getScene().getWindow();
stage.close();
}
@FXML
private void onDelete() {
ScheduleEntry selectedEntry = entriesTable.getSelectionModel().getSelectedItem();
if (selectedEntry == null) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("No Selection");
alert.setHeaderText(null);
alert.setContentText("Please select an entry to delete.");
alert.showAndWait();
return;
}
Alert confirmAlert = new Alert(Alert.AlertType.CONFIRMATION);
confirmAlert.setTitle("Delete Schedule Entry");
confirmAlert.setHeaderText("Are you sure?");
confirmAlert.setContentText("This will permanently delete the selected schedule entry.");
confirmAlert.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
try {
// Remove the selected entry from allEntries
allEntries.remove(selectedEntry);
// Save the updated list to file
DataManager.saveScheduleEntries(new ArrayList<>(allEntries));
// Show success message
Alert successAlert = new Alert(Alert.AlertType.INFORMATION);
successAlert.setTitle("Success");
successAlert.setHeaderText(null);
successAlert.setContentText("Schedule entry has been deleted successfully.");
successAlert.show();
// Refresh the table view
entriesTable.setItems(allEntries);
} catch (Exception e) {
Alert errorAlert = new Alert(Alert.AlertType.ERROR);
errorAlert.setTitle("Error");
errorAlert.setHeaderText(null);
errorAlert.setContentText("Failed to delete entry: " + e.getMessage());
errorAlert.show();
}
}
});
}
public void refreshTable() {
loadSortedEntries();
}
}