forked from nus-cs2103-AY1617S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainApp.java
222 lines (181 loc) · 7.68 KB
/
MainApp.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
package seedu.taskell;
import com.google.common.eventbus.Subscribe;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import seedu.taskell.commons.core.Config;
import seedu.taskell.commons.core.EventsCenter;
import seedu.taskell.commons.core.LogsCenter;
import seedu.taskell.commons.core.Version;
import seedu.taskell.commons.events.storage.StorageLocationChangedEvent;
import seedu.taskell.commons.events.ui.ExitAppRequestEvent;
import seedu.taskell.commons.exceptions.DataConversionException;
import seedu.taskell.commons.util.ConfigUtil;
import seedu.taskell.commons.util.StringUtil;
import seedu.taskell.history.HistoryManager;
import seedu.taskell.logic.Logic;
import seedu.taskell.logic.LogicManager;
import seedu.taskell.logic.commands.ClearCommand;
import seedu.taskell.logic.commands.SaveStorageLocationCommand;
import seedu.taskell.logic.commands.ViewHistoryCommand;
import seedu.taskell.model.*;
import seedu.taskell.storage.Storage;
import seedu.taskell.storage.StorageManager;
import seedu.taskell.ui.CalendarView;
import seedu.taskell.ui.Ui;
import seedu.taskell.ui.UiManager;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
/**
* The main entry point to the application.
*/
public class MainApp extends Application {
private static final Logger logger = LogsCenter.getLogger(MainApp.class);
public static final Version VERSION = new Version(1, 0, 0, true);
protected Ui ui;
protected Logic logic;
protected Storage storage;
protected Model model;
protected Config config;
protected UserPrefs userPrefs;
public MainApp() {}
@Override
public void init() throws Exception {
logger.info("=============================[ Initializing Task Manager ]===========================");
super.init();
config = initConfig(getApplicationParameter("config"));
storage = new StorageManager(config.getTaskManagerFilePath(), config.getUserPrefsFilePath());
userPrefs = initPrefs(config);
initLogging(config);
model = initModelManager(storage, userPrefs);
logic = new LogicManager(model, storage);
ui = new UiManager(logic, config, userPrefs);
initEventsCenter();
setDataForClasses();
initSaveStorageLocationCommand();
}
private void initSaveStorageLocationCommand() {
SaveStorageLocationCommand.setConfig(config);
SaveStorageLocationCommand.setStorage(storage);
}
private void setDataForClasses() {
HistoryManager.setModel(model);
ViewHistoryCommand.getInstance().setData(model);
ClearCommand.getInstance().setData(model);
CalendarView.setData(model);
}
private String getApplicationParameter(String parameterName){
Map<String, String> applicationParameters = getParameters().getNamed();
return applicationParameters.get(parameterName);
}
private Model initModelManager(Storage storage, UserPrefs userPrefs) {
Optional<ReadOnlyTaskManager> taskManagerOptional;
ReadOnlyTaskManager initialData;
try {
taskManagerOptional = storage.readTaskManager();
if(!taskManagerOptional.isPresent()){
logger.info("Data file not found. Will be starting with an empty TaskManager");
}
initialData = taskManagerOptional.orElse(new TaskManager());
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty TaskManager");
initialData = new TaskManager();
} catch (IOException e) {
logger.warning("Problem while reading from the file. . Will be starting with an empty TaskManager");
initialData = new TaskManager();
}
return new ModelManager(initialData, userPrefs);
}
private void initLogging(Config config) {
LogsCenter.init(config);
}
protected Config initConfig(String configFilePath) {
Config initializedConfig;
String configFilePathUsed;
configFilePathUsed = Config.DEFAULT_CONFIG_FILE;
if(configFilePath != null) {
logger.info("Custom Config file specified " + configFilePath);
configFilePathUsed = configFilePath;
}
logger.info("Using config file : " + configFilePathUsed);
try {
Optional<Config> configOptional = ConfigUtil.readConfig(configFilePathUsed);
initializedConfig = configOptional.orElse(new Config());
} catch (DataConversionException e) {
logger.warning("Config file at " + configFilePathUsed + " is not in the correct format. " +
"Using default config properties");
initializedConfig = new Config();
}
//Update config file in case it was missing to begin with or there are new/unused fields
try {
ConfigUtil.saveConfig(initializedConfig, configFilePathUsed);
} catch (IOException e) {
logger.warning("Failed to save config file : " + StringUtil.getDetails(e));
}
return initializedConfig;
}
protected UserPrefs initPrefs(Config config) {
assert config != null;
String prefsFilePath = config.getUserPrefsFilePath();
logger.info("Using prefs file : " + prefsFilePath);
UserPrefs initializedPrefs;
try {
Optional<UserPrefs> prefsOptional = storage.readUserPrefs();
initializedPrefs = prefsOptional.orElse(new UserPrefs());
} catch (DataConversionException e) {
logger.warning("UserPrefs file at " + prefsFilePath + " is not in the correct format. " +
"Using default user prefs");
initializedPrefs = new UserPrefs();
} catch (IOException e) {
logger.warning("Problem while reading from the file. . Will be starting with an empty TaskManager");
initializedPrefs = new UserPrefs();
}
//Update prefs file in case it was missing to begin with or there are new/unused fields
try {
storage.saveUserPrefs(initializedPrefs);
} catch (IOException e) {
logger.warning("Failed to save config file : " + StringUtil.getDetails(e));
}
return initializedPrefs;
}
private void initEventsCenter() {
EventsCenter.getInstance().registerHandler(this);
}
@Override
public void start(Stage primaryStage) {
logger.info("Starting TaskManager " + MainApp.VERSION);
ui.start(primaryStage);
}
@Override
public void stop() {
logger.info("============================ [ Stopping Task Manager ] =============================");
ui.stop();
try {
storage.saveUserPrefs(userPrefs);
} catch (IOException e) {
logger.severe("Failed to save preferences " + StringUtil.getDetails(e));
}
Platform.exit();
System.exit(0);
}
@Subscribe
public void handleExitAppRequestEvent(ExitAppRequestEvent event) {
logger.info(LogsCenter.getEventHandlingLogMessage(event));
this.stop();
}
/** @@author A0142130A **/
@Subscribe
private void handleStorageLocationChangedEvent(StorageLocationChangedEvent event) {
logger.info("saving new filepath to storage");
config = event.getConfig();
storage.clearTaskManager();
storage = new StorageManager(config.getTaskManagerFilePath(), config.getUserPrefsFilePath());
SaveStorageLocationCommand.setStorage(storage);
}
/** @@author **/
public static void main(String[] args) {
launch(args);
}
}