forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MainWindow.java
301 lines (243 loc) · 10.2 KB
/
MainWindow.java
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
package seedu.address.ui;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextInputControl;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import seedu.address.appmanager.AppManager;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.UiLogicHelper;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.commands.gamecommands.GameCommandResult;
import seedu.address.logic.parser.exceptions.ParseException;
/**
* The Main Window. Provides the basic application layout containing
* a menu bar and space where other JavaFX elements can be placed.
*/
public class MainWindow extends UiPart<Stage> {
private static final String FXML = "MainWindow.fxml";
private final Logger logger = LogsCenter.getLogger(getClass());
private Stage primaryStage;
private AppManager appManager;
private UiLogicHelper uiLogicHelper;
//Secondary parser for updating the Ui.
private UpdateUi updateUi;
// Independent Ui parts residing in this Ui container
private TimerDisplay timerDisplay;
private ResultDisplay resultDisplay;
private ModularDisplay modularDisplay;
private CurrentModeFooter currentModeFooter;
private HelpWindow helpWindow;
private CommandBox commandBox;
@FXML
private Scene scene;
@FXML
private StackPane commandBoxPlaceholder;
@FXML
private MenuItem helpMenuItem;
@FXML
private StackPane resultDisplayPlaceholder;
//One size fits all stackpane
@FXML
private StackPane modularDisplayPlaceholder;
//TimerDisplay placeholder
@FXML
private AnchorPane timerDisplayPlaceholder;
@FXML
private StackPane statusbarPlaceholder;
@FXML
private StackPane currentModePlaceholder;
public MainWindow(Stage primaryStage, AppManager appManager) {
super(FXML, primaryStage);
// Set dependencies
this.primaryStage = primaryStage;
this.appManager = appManager;
this.modularDisplay = new ModularDisplay(appManager);
this.uiLogicHelper = appManager.getLogic();
// Configure the UI
setWindowDefaultSize(appManager.getGuiSettings());
setAccelerators();
helpWindow = new HelpWindow();
}
public Stage getPrimaryStage() {
return primaryStage;
}
private void setAccelerators() {
setAccelerator(helpMenuItem, KeyCombination.valueOf("F1"));
}
/**
* Sets the accelerator of a MenuItem.
*
* @param keyCombination the KeyCombination value of the accelerator
*/
private void setAccelerator(MenuItem menuItem, KeyCombination keyCombination) {
menuItem.setAccelerator(keyCombination);
/*
* TODO: the code below can be removed once the bug reported here
* https://bugs.openjdk.java.net/browse/JDK-8131666
* is fixed in later version of SDK.
*
* According to the bug report, TextInputControl (TextField, TextArea) will
* consume function-key events. Because CommandBox contains a TextField, and
* ResultDisplay contains a TextArea, thus some accelerators (e.g F1) will
* not work when the focus is in them because the key event is consumed by
* the TextInputControl(s).
*
* For now, we add following event filter to capture such key events and open
* help window purposely so to support accelerators even when focus is
* in CommandBox or ResultDisplay.
*/
getRoot().addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.getTarget() instanceof TextInputControl && keyCombination.match(event)) {
menuItem.getOnAction().handle(new ActionEvent());
event.consume();
}
});
}
/**
* Fills up all the placeholders of this window.
*/
void fillInnerParts() {
modularDisplay.swapToLoadDisplay(modularDisplayPlaceholder);
resultDisplay = new ResultDisplay();
resultDisplayPlaceholder.getChildren().add(resultDisplay.getRoot());
//Set up timer display
timerDisplay = new TimerDisplay();
timerDisplayPlaceholder.getChildren().add(timerDisplay.getRoot());
//Set up callback function in GameManager to update TimerDisplay
appManager.setTimerDisplayCallBack(this::updateTimerDisplay);
//Set up callback function in GameManager to update ResultDisplay
appManager.setHintDisplayCallBack(this::updateHintDisplay);
//Set up callback function in GameManager to call MainWindow's executeCommand
appManager.setMainWindowExecuteCallBack(this::executeCommand);
StatusBarFooter statusBarFooter = new StatusBarFooter(appManager.getAddressBookFilePath());
statusbarPlaceholder.getChildren().add(statusBarFooter.getRoot());
//Displays the current mode. Starts in "load" mode.
currentModeFooter = new CurrentModeFooter();
currentModePlaceholder.getChildren().add(currentModeFooter.getRoot());
//Set up command box
commandBox = new CommandBox(this::executeCommand, uiLogicHelper);
commandBoxPlaceholder.getChildren().add(commandBox.getRoot());
//Assigns only after initialisation.
this.updateUi = new UpdateUi(modularDisplay, currentModeFooter);
updateUi.setTheme(appManager.getAppSettings().getDefaultTheme(), scene);
}
/**
* Sets the default size based on {@code guiSettings}.
*/
private void setWindowDefaultSize(GuiSettings guiSettings) {
primaryStage.setHeight(guiSettings.getWindowHeight());
primaryStage.setWidth(guiSettings.getWindowWidth());
if (guiSettings.getWindowCoordinates() != null) {
primaryStage.setX(guiSettings.getWindowCoordinates().getX());
primaryStage.setY(guiSettings.getWindowCoordinates().getY());
}
}
/**
* Opens the help window or focuses on it if it's already opened.
*/
@FXML
public void handleHelp() {
if (!helpWindow.isShowing()) {
helpWindow.show();
} else {
helpWindow.focus();
}
}
void show() {
primaryStage.show();
}
/**
* Closes the application.
*/
@FXML
private void handleExit() {
GuiSettings guiSettings = new GuiSettings(primaryStage.getWidth(), primaryStage.getHeight(),
(int) primaryStage.getX(), (int) primaryStage.getY());
appManager.setGuiSettings(guiSettings);
helpWindow.hide();
primaryStage.hide();
//Platform.exit();
//System.exit(0);
}
/**
* Opens the result stats window when the game is finished.
*/
private void handleFinishedGame() {
if (appManager.getGameStatistics() == null) {
throw new IllegalStateException("gameStatistics in gameManager should not be null when game"
+ "is finished");
}
modularDisplay.swapToGameResult(modularDisplayPlaceholder, appManager.getGameStatistics(),
appManager.getActiveWordBankStatistics());
}
/**
* Executes the command and returns the result.
*
* @see seedu.address.logic.Logic#execute(String)
*/
private CommandResult executeCommand(String commandText) throws CommandException, ParseException {
try {
CommandResult commandResult = appManager.execute(commandText);
logger.info("Result: " + commandResult.getFeedbackToUser());
resultDisplay.setFeedbackToUser(commandResult.getFeedbackToUser());
//Updates the Ui.
updateUi.updateModularDisplay(commandText, uiLogicHelper.getMode(), modularDisplayPlaceholder);
updateUi.setTheme(appManager.getAppSettings().getDefaultTheme(), scene);
if (commandResult.isShowHelp()) {
handleHelp();
}
if (commandResult.isExit()) {
handleExit();
}
if (commandResult.isPromptingGuess()) {
commandBox.setGuessTextAndCaret();
} else {
commandBox.clearCommandBox();
}
if (commandResult instanceof GameCommandResult) {
GameCommandResult gameCommandResult = (GameCommandResult) commandResult;
if (gameCommandResult.isFinishedGame()) {
handleFinishedGame();
}
}
return commandResult;
} catch (CommandException | ParseException e) {
logger.info("Invalid command: " + commandText);
resultDisplay.setFeedbackToUser(e.getMessage());
throw e;
}
}
/**
* Updates the timerDisplay module of MainWindow to be called from GameTimer.
* @param timerMessage Message to be displayed on the TimerDisplay.
* @param timeLeft Time in milliseconds that is left in the current timer.
*/
private void updateTimerDisplay(String timerMessage, long timeLeft, long totalTimeGiven) {
double percentageTimeLeft = (timeLeft * 1.0) / totalTimeGiven;
if (percentageTimeLeft <= 0.5) { // when time left is <= half of time given, switch to alert colour
this.timerDisplay.setAlertTextColour();
} else {
this.timerDisplay.setNormalTextColour();
}
timerDisplay.setProgressBarProgress(percentageTimeLeft);
timerDisplay.setFeedbackToUser(timerMessage);
}
/**
* Updates the HintDisplay section of the UI with the {@code resultDisplayMessage} string.
*/
private void updateHintDisplay(String resultDisplayMessage) {
// Todo: implement HintDisplay window and show hints there instead
String[] tokens = resultDisplay.getCurrentTextOnDisplay().split("\n");
String toDisplay = tokens[0] + "\n" + tokens[1] + "\n" + resultDisplayMessage;
resultDisplay.setFeedbackToUser(toDisplay);
}
}