forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MainWindow.java
397 lines (344 loc) · 12.3 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package seedu.address.ui;
import java.util.logging.Logger;
import com.google.common.eventbus.Subscribe;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextInputControl;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import seedu.address.commons.core.Config;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.events.model.ListJioCommandEvent;
import seedu.address.commons.events.model.ListingDebtCommandEvent;
import seedu.address.commons.events.model.ListingFriendCommandEvent;
import seedu.address.commons.events.model.ListingGroupCommandEvent;
import seedu.address.commons.events.ui.ExitAppRequestEvent;
import seedu.address.commons.events.ui.ShowHelpRequestEvent;
import seedu.address.logic.Logic;
import seedu.address.model.UserPrefs;
import seedu.address.model.accounting.Debt;
import seedu.address.model.friend.Friendship;
import seedu.address.model.group.Group;
import seedu.address.model.jio.Jio;
/**
* 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 Logic logic;
// Independent Ui parts residing in this Ui container
private BrowserPanel browserPanel;
private RestaurantListPanel restaurantListPanel;
private ListPanel<Jio> jioListPanel;
private ListPanel<Group> groupListPanel;
private ListPanel<Debt> debtListPanel;
private ListPanel<Friendship> friendRequestListPanel;
private ListPanel<Friendship> friendListPanel;
private Config config;
private UserPrefs prefs;
private HelpWindow helpWindow;
@FXML
private StackPane browserPlaceholder;
@FXML
private StackPane commandBoxPlaceholder;
@FXML
private MenuItem helpMenuItem;
@FXML
private StackPane restaurantListPanelPlaceholder;
@FXML
private StackPane featuresListPanelPlaceholder;
@FXML
private StackPane resultDisplayPlaceholder;
@FXML
private StackPane statusbarPlaceholder;
public MainWindow(Stage primaryStage, Config config, UserPrefs prefs, Logic logic) {
super(FXML, primaryStage);
// Set dependencies
this.primaryStage = primaryStage;
this.logic = logic;
this.config = config;
this.prefs = prefs;
// Configure the UI
setTitle(config.getAppTitle());
setWindowDefaultSize(prefs);
setAccelerators();
registerAsAnEventHandler(this);
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() {
browserPanel = new BrowserPanel();
browserPlaceholder.getChildren().add(browserPanel.getRoot());
restaurantListPanel = new RestaurantListPanel(logic.getFilteredRestaurantList());
restaurantListPanelPlaceholder.getChildren().add(restaurantListPanel.getRoot());
ResultDisplay resultDisplay = new ResultDisplay();
resultDisplayPlaceholder.getChildren().add(resultDisplay.getRoot());
StatusBarFooter statusBarFooter = new StatusBarFooter(prefs.getAddressBookFilePath());
statusbarPlaceholder.getChildren().add(statusBarFooter.getRoot());
CommandBox commandBox = new CommandBox(logic);
commandBoxPlaceholder.getChildren().add(commandBox.getRoot());
}
void hide() {
primaryStage.hide();
}
private void setTitle(String appTitle) {
primaryStage.setTitle(appTitle);
}
/**
* Sets the default size based on user preferences.
*/
private void setWindowDefaultSize(UserPrefs prefs) {
primaryStage.setHeight(prefs.getGuiSettings().getWindowHeight());
primaryStage.setWidth(prefs.getGuiSettings().getWindowWidth());
if (prefs.getGuiSettings().getWindowCoordinates() != null) {
primaryStage.setX(prefs.getGuiSettings().getWindowCoordinates().getX());
primaryStage.setY(prefs.getGuiSettings().getWindowCoordinates().getY());
}
}
/**
* Returns the current size and the position of the main Window.
*/
GuiSettings getCurrentGuiSetting() {
return new GuiSettings(primaryStage.getWidth(), primaryStage.getHeight(),
(int) primaryStage.getX(), (int) primaryStage.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() {
raise(new ExitAppRequestEvent());
}
public RestaurantListPanel getRestaurantListPanel() {
return restaurantListPanel;
}
void releaseResources() {
browserPanel.freeResources();
}
@Subscribe
private void handleShowHelpEvent(ShowHelpRequestEvent event) {
logger.info(LogsCenter.getEventHandlingLogMessage(event));
handleHelp();
}
/**
* Updates list panel with jios.
*/
@FXML
public void handleJio() {
jioListPanel = new ListPanel<>(logic.getJioList());
featuresListPanelPlaceholder.getChildren().add(jioListPanel.getRoot());
}
/**
* Updates list panel with groups.
*/
@FXML
public void handleGroup() {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
groupListPanel = new ListPanel<>(logic.getGroupList());
featuresListPanelPlaceholder.getChildren().add(groupListPanel.getRoot());
}
}
/**
* Updates list panel with group requests.
*/
@FXML
public void handleGroupRequest() {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
groupListPanel = new ListPanel<>(logic.getGroupRequestList());
featuresListPanelPlaceholder.getChildren().add(groupListPanel.getRoot());
}
}
/**
* Updates list panel with login user's debts.
*/
@FXML
public void handleAllDebt() {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
debtListPanel = new ListPanel<>(logic.getDebtList());
featuresListPanelPlaceholder.getChildren().add(debtListPanel.getRoot());
}
}
/**
* Updates list panel with login user's creditors.
*/
@FXML
public void handleCreditor() {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
debtListPanel = new ListPanel<>(logic.getCreditorList());
featuresListPanelPlaceholder.getChildren().add(debtListPanel.getRoot());
}
}
/**
* Updates list panel with login user's debtors.
*/
@FXML
public void handleDebtor() {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
debtListPanel = new ListPanel<>(logic.getDebtorList());
featuresListPanelPlaceholder.getChildren().add(debtListPanel.getRoot());
}
}
/**
* Updates list panel with login user's received request.
*/
@FXML
public void handleDebtRequestReceived() {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
debtListPanel = new ListPanel<>(logic.getDebtRequestReceived());
featuresListPanelPlaceholder.getChildren().add(debtListPanel.getRoot());
}
}
/**
* Updates list panel with login user's received sent.
*/
@FXML
public void handleDebtRequestSent() {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
debtListPanel = new ListPanel<>(logic.getDebtRequestSent());
featuresListPanelPlaceholder.getChildren().add(debtListPanel.getRoot());
}
}
/**
* Updates list panel with listing command.
*/
@Subscribe
@FXML
void handleListingDebtCommandEvent(ListingDebtCommandEvent event) {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
debtListPanel = new ListPanel<>(event.listingItem);
featuresListPanelPlaceholder.getChildren().add(debtListPanel.getRoot());
}
}
/**
* Updates list panel with friend listing command.
*/
@Subscribe
@FXML
void handleListingFriendCommandEvent(ListingFriendCommandEvent event) {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
friendListPanel = new ListPanel<>(event.listingItem);
featuresListPanelPlaceholder.getChildren().add(friendListPanel.getRoot());
}
}
/**
* Updates list panel with group listing command.
*/
@Subscribe
@FXML
void handleListingGroupCommandEvent(ListingGroupCommandEvent event) {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
groupListPanel = new ListPanel<>(event.listingItem);
featuresListPanelPlaceholder.getChildren().add(groupListPanel.getRoot());
}
}
/**
* Updates list panel with login user's received request.
*/
@FXML
public void handleFriendRequests() {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
friendRequestListPanel = new ListPanel<>(logic.getFriendRequestsList());
featuresListPanelPlaceholder.getChildren().add(friendRequestListPanel.getRoot());
}
}
/**
* Updates list panel with login user's friends.
*/
@FXML
public void handleFriends() {
if (!logic.isCurrentlyLoggedIn()) {
browserPanel.loadNotLoggedInPage();
} else {
friendListPanel = new ListPanel<>(logic.getFriendsList());
featuresListPanelPlaceholder.getChildren().add(friendListPanel.getRoot());
}
}
/**
* Updates list panel with listing command.
*/
@Subscribe
@FXML
void handleListJioCommandEvent(ListJioCommandEvent event) {
jioListPanel = new ListPanel<>(event.listingItem);
featuresListPanelPlaceholder.getChildren().add(jioListPanel.getRoot());
}
}