forked from nus-cs2103-AY1920S1/addressbook-level3
/
MainWindow.java
220 lines (193 loc) · 8.62 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
package seedu.address.ui;
import java.util.logging.Logger;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.Logic;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.semester.Semester;
import seedu.address.model.studyplan.StudyPlan;
/**
* 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 static final String NO_ACTIVE_STUDY_PLAN = "No remaining study plans.";
private static final String STUDY_PLAN_ID = "(ID: %d)";
private final Logger logger = LogsCenter.getLogger(getClass());
private Stage primaryStage;
private Logic logic;
// Independent Ui parts residing in this Ui container
private SemesterListPanel semesterListPanel;
private ResultDisplay resultDisplay;
private CommandBox commandBox;
private StudyPlanTagPanel studyPlanTagPanel;
@FXML
private Label studyPlanId;
@FXML
private Label title;
@FXML
private Label mcCount;
@FXML
private StackPane commandBoxPlaceholder;
@FXML
private StackPane semesterListPanelPlaceholder;
@FXML
private StackPane resultDisplayPlaceholder;
@FXML
private StackPane statusbarPlaceholder;
@FXML
private StackPane studyPlanTagsPlaceholder;
public MainWindow(Stage primaryStage, Logic logic) {
super(FXML, primaryStage);
// Set dependencies
this.primaryStage = primaryStage;
this.logic = logic;
// Configure the UI
setWindowDefaultSize(logic.getGuiSettings());
initialiseMode(logic.getGuiSettings());
}
public Stage getPrimaryStage() {
return primaryStage;
}
/**
* Sets the accelerator of a MenuItem.
* @param keyCombination the KeyCombination value of the accelerator
*/
/**
* Fills up all the placeholders of this window.
*/
void fillInnerParts() {
logger.info("Filling Main Window.");
StudyPlan sp = logic.getActiveStudyPlan();
if (sp == null) {
logger.info("No active study plan present.");
NoActiveStudyPlanDisplay noActiveStudyPlanDisplay = new NoActiveStudyPlanDisplay();
semesterListPanelPlaceholder.getChildren().add(noActiveStudyPlanDisplay.getRoot());
studyPlanId.setText("");
title.setText(NO_ACTIVE_STUDY_PLAN);
mcCount.setText("");
} else {
logger.info("An active study plan is present.");
ObservableList<Semester> semesters = sp.getSemesters().asUnmodifiableObservableList();
semesterListPanel = new SemesterListPanel(semesters, sp.getCurrentSemester());
semesterListPanelPlaceholder.getChildren().add(semesterListPanel.getRoot());
studyPlanId.setText(String.format(STUDY_PLAN_ID, sp.getIndex()));
title.setText(sp.getTitle().toString());
mcCount.setText(sp.getMcCountString());
studyPlanTagPanel = new StudyPlanTagPanel(sp.getStudyPlanTags()
.asUnmodifiableObservableList());
studyPlanTagsPlaceholder.getChildren().add(studyPlanTagPanel.getRoot());
}
resultDisplay = new ResultDisplay();
resultDisplayPlaceholder.getChildren().add(resultDisplay.getRoot());
commandBox = new CommandBox(this::executeCommand, logic.getModulePlanner());
commandBoxPlaceholder.getChildren().add(commandBox.getRoot());
}
/**
* 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());
}
}
void show() {
primaryStage.show();
}
/**
* Closes the application.
*/
@FXML
private void handleExit() {
logger.info("Main Window closing.");
GuiSettings guiSettings = new GuiSettings(primaryStage.getWidth(), primaryStage.getHeight(),
(int) primaryStage.getX(), (int) primaryStage.getY(), logic.getGuiSettings().getGuiTheme());
logic.setGuiSettings(guiSettings);
primaryStage.hide();
}
/**
* Executes the command and returns the result.
*
* @see seedu.address.logic.Logic#execute(String)
*/
private CommandResult executeCommand(String commandText) throws CommandException, ParseException {
resultDisplay.removeResultView();
try {
CommandResult commandResult = logic.execute(commandText);
logger.info("Result: " + commandResult.getFeedbackToUser());
resultDisplay.setFeedbackToUser(commandResult.getFeedbackToUser());
semesterListPanel.refresh();
StudyPlan sp = logic.getActiveStudyPlan();
mcCount.setText(sp == null ? "" : sp.getMcCountString());
if (commandResult.isChangesActiveStudyPlan()) {
changeMode(logic.getGuiSettings());
logger.info("Study plan has been changed. Refreshing display.");
if (sp == null) {
NoActiveStudyPlanDisplay noActiveStudyPlanDisplay = new NoActiveStudyPlanDisplay();
if (semesterListPanelPlaceholder.getChildren().size() != 0) {
semesterListPanelPlaceholder.getChildren().remove(0);
}
semesterListPanelPlaceholder.getChildren().add(noActiveStudyPlanDisplay.getRoot());
studyPlanId.setText("");
if (studyPlanTagsPlaceholder.getChildren().size() != 0) {
studyPlanTagsPlaceholder.getChildren().remove(0);
}
title.setText(NO_ACTIVE_STUDY_PLAN);
} else {
ObservableList<Semester> semesters = sp.getSemesters().asUnmodifiableObservableList();
semesterListPanel = new SemesterListPanel(semesters, sp.getCurrentSemester());
if (semesterListPanelPlaceholder.getChildren().size() != 0) {
semesterListPanelPlaceholder.getChildren().remove(0);
}
semesterListPanelPlaceholder.getChildren().add(semesterListPanel.getRoot());
studyPlanTagPanel = new StudyPlanTagPanel(sp.getStudyPlanTags().asUnmodifiableObservableList());
if (studyPlanTagsPlaceholder.getChildren().size() != 0) {
studyPlanTagsPlaceholder.getChildren().remove(0);
}
studyPlanTagsPlaceholder.getChildren().add(studyPlanTagPanel.getRoot());
studyPlanId.setText(String.format(STUDY_PLAN_ID, sp.getIndex()));
title.setText(sp.getTitle().toString());
commandBox.handleChangeOfActiveStudyPlan();
}
}
if (commandResult.isExit()) {
handleExit();
}
ResultViewType resultViewType = commandResult.getResultViewType();
if (resultViewType != null) {
resultDisplay.handleResult(resultViewType, commandResult.getResultContent());
}
return commandResult;
} catch (CommandException | ParseException e) {
logger.info("Invalid command: " + commandText);
resultDisplay.setFeedbackToUser(e.getMessage());
throw e;
}
}
/**
* Sets the initialised GUI mode based on {@code guiSettings}.
*/
private void initialiseMode(GuiSettings guiSettings) {
ObservableList<String> styles = primaryStage.getScene().getStylesheets();
styles.add(guiSettings.getGuiTheme().getCssString());
}
/**
* Changes the current GUI mode based on {@code guiSettings}.
*/
private void changeMode(GuiSettings guiSettings) {
ObservableList<String> styles = primaryStage.getScene().getStylesheets();
styles.remove(styles.size() - 1);
styles.add(guiSettings.getGuiTheme().getCssString());
}
}