-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerMain.java
More file actions
307 lines (257 loc) · 9.41 KB
/
ControllerMain.java
File metadata and controls
307 lines (257 loc) · 9.41 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.Pane;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Button;
import java.net.URL;
import java.util.ResourceBundle;
import java.io.IOException;
import javafx.scene.control.DatePicker;
import java.time.LocalDate;
import java.util.NoSuchElementException;
import java.util.Observable;
import java.util.Observer;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.layout.AnchorPane; // Assuming rightPane is of type AnchorPane
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.time.format.DateTimeFormatter;
import javafx.scene.control.Alert;
/**
* This class is the main ControllerMain for the whole program. It will deal with the
* loading of panels (Switching from panel to panel), and saving of the user's
* inputted dates.
*/
public class ControllerMain implements Observer {
// Singleton pattern
private static ControllerMain instance;
// Declare other controllers
ControllerAbout controllerAbout;
ControllerStatistic controllerStatistic;
ControllerMap controllerMap;
// Declare the models (Source of data)
ModelMain modelMain;
// Panels
@FXML
private Pane rootLayout;
@FXML
private Pane rightPane;
@FXML
private Pane statsPane;
@FXML
private Pane leftPane;
// Declaration of leftside buttons
@FXML
Button btnAbout;
@FXML
Button btnMap;
@FXML
Button btnStat;
@FXML
Button btnFour;
// Date picker
@FXML
private DatePicker startDatePicker;
@FXML
private DatePicker endDatePicker;
private LocalDate startDate;
private LocalDate endDate;
// Buttons (Fix this)
@FXML
private Button aboutButton;
// Date variables
LocalDate minDate;
LocalDate maxDate;
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private boolean dateSet = false;
public ControllerMain() { initialize(); }
public void initialize() {
modelMain = modelMain.getInstance();
LocalDate minDate = modelMain.findMinDate();
LocalDate maxDate = modelMain.findMaxDate();
}
@Override
public void update(Observable o, Object arg) {
updatePanelOne();
updatePanelThree();
}
public void updatePanelOne() {
this.modelMain = modelMain.getInstance();
if (modelMain.getIndex() == 0) {
controllerAbout.updateDate(modelMain.getStartDate(), modelMain.getEndDate());
}
}
public void updatePanelThree() {
this.modelMain = modelMain.getInstance();
if (modelMain.getIndex() == 2) {
controllerStatistic = (ControllerStatistic) loadPanel("ViewStatistics.fxml",rightPane);
controllerStatistic.updateCurrentPanel();
}
}
private void panelCycler(int counter) {
if(counter == 0) {handlePanelOne(null);}
else if(counter==1) {handlePanelTwo(null);}
else if(counter==2) {handlePanelThree(null);}
}
@FXML
private void incrementPanel(ActionEvent event) {
if(dateSet){
modelMain.increaseIndex();
if(modelMain.getIndex()>2){
modelMain.resetIndex();
}
panelCycler(modelMain.getIndex());
}
}
@FXML
private void decrementPanel(ActionEvent event) {
if(dateSet){
modelMain.decreaseIndex();
if(modelMain.getIndex()<0){
modelMain.setIndex(2);
}
panelCycler(modelMain.getIndex());
}
}
@FXML
private void handlePanelOne(ActionEvent event) {
System.out.println("Loading ViewAbout.fxml");
controllerAbout = (ControllerAbout) loadPanel("ViewAbout.fxml",rightPane);
this.modelMain = modelMain.getInstance();
modelMain.setIndex(0);
updatePanelOne();
// Set button styles
btnAbout.getStyleClass().clear();
btnAbout.getStyleClass().add("main__selected-transparent-button");
btnMap.getStyleClass().clear();
btnMap.getStyleClass().add("main__unselected-transparent-button");
btnStat.getStyleClass().clear();
btnStat.getStyleClass().add("main__unselected-transparent-button");
btnFour.getStyleClass().clear();
btnFour.getStyleClass().add("main__unselected-transparent-button");
}
@FXML
private void handlePanelTwo(ActionEvent event) {
if(dateSet){
System.out.println("Loading ViewMap.fxml");
loadPanel("ViewMap.fxml",rightPane);
controllerMap = (ControllerMap) loadPanel("ViewMap.fxml",rightPane);
this.modelMain = modelMain.getInstance();
modelMain.setIndex(1);
btnAbout.getStyleClass().clear();
btnAbout.getStyleClass().add("main__unselected-transparent-button");
btnMap.getStyleClass().clear();
btnMap.getStyleClass().add("main__selected-transparent-button");
btnStat.getStyleClass().clear();
btnStat.getStyleClass().add("main__unselected-transparent-button");
btnFour.getStyleClass().clear();
btnFour.getStyleClass().add("main__unselected-transparent-button");
}
}
@FXML
private void handlePanelThree(ActionEvent event) {
if(dateSet){
System.out.println("Loading ViewStatistics.fxml");
loadPanel("ViewStatistics.fxml",rightPane);
controllerStatistic = (ControllerStatistic) loadPanel("ViewStatistics.fxml",rightPane);
this.modelMain = modelMain.getInstance();
modelMain.setIndex(2);
updatePanelThree();
btnAbout.getStyleClass().clear();
btnAbout.getStyleClass().add("main__unselected-transparent-button");
btnMap.getStyleClass().clear();
btnMap.getStyleClass().add("main__unselected-transparent-button");
btnStat.getStyleClass().clear();
btnStat.getStyleClass().add("main__selected-transparent-button");
btnFour.getStyleClass().clear();
btnFour.getStyleClass().add("main__unselected-transparent-button");
}
}
@FXML
private void handleStartDatePicker() {
// Create instance of model for holding data
this.modelMain = modelMain.getInstance();
minDate = modelMain.findMinDate();
maxDate = modelMain.findMaxDate();
startDate = startDatePicker.getValue();
if(this.startDate != null){
modelMain.setStartDate(startDate);
System.out.println("start date set to: " + startDate);
}
checkValidDate(minDate,maxDate);
}
@FXML
private void handleEndDatePicker() {
// Create instance of model for holding data
this.modelMain = modelMain.getInstance();
minDate = modelMain.findMinDate();
maxDate = modelMain.findMaxDate();
endDate = endDatePicker.getValue();
if(this.endDate != null){
modelMain.setEndDate(endDate);
System.out.println("end date set to: " + endDate);
}
checkValidDate(minDate,maxDate);
}
private void checkValidDate(LocalDate minDate,LocalDate maxDate){
if (this.startDate != null && this.endDate != null) {
if( (endDate.isAfter(minDate)&& endDate.isBefore(maxDate)) || (endDate.isEqual(minDate) || endDate.isEqual(maxDate)) ){
if( (startDate.isAfter(minDate)&& startDate.isBefore(maxDate)) || (startDate.isEqual(minDate) || startDate.isEqual(maxDate)) ){
if(endDate.isAfter(startDate)){
dateSet = true;
}
else{showAlert("Invalid date","please select a valid date range from"+(""+minDate)+" to "+(""+maxDate));
dateSet = false;
}
}
else{showAlert("Invalid date","please select a valid date range from"+(""+minDate)+" to "+(""+maxDate));
dateSet = false;
}
}
else{showAlert("Invalid date","please select a valid date range from"+(""+minDate)+" to "+(""+maxDate));
dateSet = false;
}
}
else {
dateSet = false;
System.out.println("Please select both start and end dates.");
}
}
private void showAlert(String title, String content) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(content);
alert.showAndWait();
}
/**
* This sets the chosen pane and makes it load the chosen fxmlFile
*/
private <T> T loadPanel(String fxmlFile, Pane pane) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFile));
Node panel = loader.load();
pane.getChildren().setAll(panel);
return loader.getController();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public LocalDate getStartDate() {
return startDate;
}
public LocalDate getEndDate() {
return endDate;
}
public void doThis() {
loadPanel("ViewAbout.fxml",rightPane);
}
@FXML
void exitProgram(ActionEvent event) {
}
}