-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsController.java
More file actions
196 lines (178 loc) · 5.69 KB
/
SettingsController.java
File metadata and controls
196 lines (178 loc) · 5.69 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
package cs3500.pa05.controller;
import cs3500.pa05.model.json.ThemeJson;
import cs3500.pa05.model.json.WeekJson;
import cs3500.pa05.view.BujoView;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/**
* Represents the controller for the game window that generates weekly settings.
*/
public class SettingsController implements ControllerInterface {
private WeekJson bujoWeek;
private Stage stage;
@FXML
private TextField nameField;
@FXML
private TextField eventMaxField;
@FXML
private TextField taskMaxField;
@FXML
private ComboBox<String> themeChoiceBox;
@FXML
private Button saveButton;
private Dialog<String> warning;
private Dialog<String> customTheme;
private String selectedTheme;
private String selectedName;
private int selectedTaskMax;
private int selectedEventMax;
private String path;
/**
* Instantiates a controller to handle user settings.
*
* @param w the week the user is currently editing
* @param s the stage to place the scene
* @param p the string representation of the .bujo path
*/
public SettingsController(WeekJson w, Stage s, String p) {
this.path = p;
this.bujoWeek = w;
this.stage = s;
this.nameField = new TextField();
this.eventMaxField = new TextField();
this.taskMaxField = new TextField();
this.saveButton = new Button("Save Settings");
this.warning = new Dialog<>();
this.themeChoiceBox = new ComboBox<String>();
this.customTheme = new Dialog<>();
}
/**
* Sets up dialog to generate custom theme.
*/
private void setCustomTheme() {
ButtonType ok = new ButtonType("Ok", ButtonBar.ButtonData.OK_DONE);
customTheme.setTitle("Custom Theme");
customTheme.getDialogPane().getButtonTypes().add(ok);
ColorPicker colorPicker = new ColorPicker();
colorPicker.setOnAction(event -> {
Color selectedColor = colorPicker.getValue();
selectedTheme = String.valueOf(selectedColor);
bujoWeek.allThemes().add(new ThemeJson(selectedTheme));
});
customTheme.getDialogPane().setContent(colorPicker);
}
/**
* Sets up warning dialog to handle incorrect paths.
*/
private void setWarning() {
ButtonType ok = new ButtonType("Ok", ButtonBar.ButtonData.OK_DONE);
warning.setTitle("Warning");
warning.getDialogPane().getButtonTypes().add(ok);
}
/**
* Displays a warning popup with given message.
*
* @param message warning message
*/
private void displayWarning(String message) {
warning.setContentText(message);
}
/**
* Checks user input for week settings.
*
* @return whether the settings are valid
*/
private boolean validSettings() {
String name = nameField.getText();
String eventMax = eventMaxField.getText();
String taskMax = taskMaxField.getText();
// handles cases with incorrect user input by displaying a warning
if (name.isEmpty()) {
selectedName = bujoWeek.name();
} else {
selectedName = name;
}
// handles event max by attempting to parse
if (eventMax.isEmpty()) {
selectedEventMax = bujoWeek.eventMax();
} else {
try {
selectedEventMax = Integer.parseInt(eventMax);
} catch (NumberFormatException e) {
displayWarning("Event max must be an integer.");
warning.showAndWait();
return false;
}
}
// handles task max by attempting to parse
if (taskMax.isEmpty()) {
selectedTaskMax = bujoWeek.taskMax();
} else {
try {
selectedTaskMax = Integer.parseInt(taskMax);
} catch (NumberFormatException e) {
displayWarning("Task max must be an integer.");
warning.showAndWait();
return false;
}
}
// handles the user creating a custom theme
if (themeChoiceBox.getValue() == "add custom theme") {
customTheme.setContentText("Choose a background color");
customTheme.showAndWait();
} else {
selectedTheme = themeChoiceBox.getValue();
}
return true;
}
/**
* Closes the scene by delegating to the WeekController to display the
* next part of the program.
*/
private void closeScene() {
if (validSettings()) {
WeekJson weekCopy = new WeekJson(selectedName, selectedTaskMax, selectedEventMax,
bujoWeek.allThemes(), new ThemeJson(selectedTheme), bujoWeek.days(), bujoWeek.taskQueue(),
bujoWeek.note());
WeekController contr = new WeekController(weekCopy, stage, path);
BujoView view = new BujoView(contr, "week.fxml");
try {
stage.setScene(view.load());
contr.run();
stage.show();
} catch (IllegalStateException e) {
System.err.println("Can't load new task");
}
}
}
/**
* Adds all theme choices to the comboBox.
*/
private void addThemeChoices() {
for (ThemeJson theme : bujoWeek.allThemes()) {
this.themeChoiceBox.getItems().add(theme.name());
}
if (bujoWeek.allThemes().size() < 7) {
this.themeChoiceBox.getItems().add("add custom theme");
}
this.themeChoiceBox.setValue(bujoWeek.allThemes().get(0).name());
}
/**
* Runs the controller.
*/
@Override
public void run() {
addThemeChoices();
setCustomTheme();
setWarning();
saveButton.setOnAction(event -> closeScene());
}
}