Skip to content

Commit 4a8fcfc

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#77 from Ma-Yueran/branch-replace-ui
Branch replace ui
2 parents 78effbe + 0827f29 commit 4a8fcfc

File tree

11 files changed

+701
-7
lines changed

11 files changed

+701
-7
lines changed

src/main/java/seedu/address/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
*/
2121
public class Main {
2222
public static void main(String[] args) {
23-
Application.launch(MainApp.class, args);
23+
Application.launch(NewMainApp.class, args);
2424
}
2525
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package seedu.address;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.util.Optional;
6+
import java.util.logging.Logger;
7+
8+
import javafx.application.Application;
9+
import javafx.stage.Stage;
10+
import seedu.address.commons.core.Config;
11+
import seedu.address.commons.core.LogsCenter;
12+
import seedu.address.commons.core.Version;
13+
import seedu.address.commons.exceptions.DataConversionException;
14+
import seedu.address.commons.util.ConfigUtil;
15+
import seedu.address.commons.util.StringUtil;
16+
import seedu.address.logic.Logic;
17+
import seedu.address.logic.LogicManager;
18+
import seedu.address.model.AddressBook;
19+
import seedu.address.model.Model;
20+
import seedu.address.model.ModelManager;
21+
import seedu.address.model.ReadOnlyAddressBook;
22+
import seedu.address.model.ReadOnlyUserPrefs;
23+
import seedu.address.model.UserPrefs;
24+
import seedu.address.model.util.SampleDataUtil;
25+
import seedu.address.storage.AddressBookStorage;
26+
import seedu.address.storage.JsonAddressBookStorage;
27+
import seedu.address.storage.JsonUserPrefsStorage;
28+
import seedu.address.storage.Storage;
29+
import seedu.address.storage.StorageManager;
30+
import seedu.address.storage.UserPrefsStorage;
31+
import seedu.address.ui.NewUiManager;
32+
import seedu.address.ui.Ui;
33+
34+
/**
35+
* Runs the application.
36+
*/
37+
public class NewMainApp extends Application {
38+
39+
public static final Version VERSION = new Version(0, 6, 0, true);
40+
41+
private static final Logger logger = LogsCenter.getLogger(MainApp.class);
42+
43+
protected Ui ui;
44+
protected Logic logic;
45+
protected Storage storage;
46+
protected Model model;
47+
protected Config config;
48+
49+
@Override
50+
public void init() throws Exception {
51+
logger.info("=============================[ Initializing AddressBook ]===========================");
52+
super.init();
53+
54+
AppParameters appParameters = AppParameters.parse(getParameters());
55+
config = initConfig(appParameters.getConfigPath());
56+
57+
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
58+
UserPrefs userPrefs = initPrefs(userPrefsStorage);
59+
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
60+
storage = new StorageManager(addressBookStorage, userPrefsStorage);
61+
62+
initLogging(config);
63+
64+
model = initModelManager(storage, userPrefs);
65+
66+
logic = new LogicManager(model, storage);
67+
68+
ui = new NewUiManager(logic);
69+
}
70+
71+
/**
72+
* Returns a {@code ModelManager} with the data from {@code storage}'s address book and {@code userPrefs}. <br>
73+
* The data from the sample address book will be used instead if {@code storage}'s address book is not found,
74+
* or an empty address book will be used instead if errors occur when reading {@code storage}'s address book.
75+
*/
76+
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
77+
Optional<ReadOnlyAddressBook> addressBookOptional;
78+
ReadOnlyAddressBook initialData;
79+
try {
80+
addressBookOptional = storage.readAddressBook();
81+
if (!addressBookOptional.isPresent()) {
82+
logger.info("Data file not found. Will be starting with a sample AddressBook");
83+
}
84+
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
85+
} catch (DataConversionException e) {
86+
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
87+
initialData = new AddressBook();
88+
} catch (IOException e) {
89+
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
90+
initialData = new AddressBook();
91+
}
92+
93+
return new ModelManager(initialData, userPrefs);
94+
}
95+
96+
private void initLogging(Config config) {
97+
LogsCenter.init(config);
98+
}
99+
100+
/**
101+
* Returns a {@code Config} using the file at {@code configFilePath}. <br>
102+
* The default file path {@code Config#DEFAULT_CONFIG_FILE} will be used instead
103+
* if {@code configFilePath} is null.
104+
*/
105+
protected Config initConfig(Path configFilePath) {
106+
Config initializedConfig;
107+
Path configFilePathUsed;
108+
109+
configFilePathUsed = Config.DEFAULT_CONFIG_FILE;
110+
111+
if (configFilePath != null) {
112+
logger.info("Custom Config file specified " + configFilePath);
113+
configFilePathUsed = configFilePath;
114+
}
115+
116+
logger.info("Using config file : " + configFilePathUsed);
117+
118+
try {
119+
Optional<Config> configOptional = ConfigUtil.readConfig(configFilePathUsed);
120+
initializedConfig = configOptional.orElse(new Config());
121+
} catch (DataConversionException e) {
122+
logger.warning("Config file at " + configFilePathUsed + " is not in the correct format. "
123+
+ "Using default config properties");
124+
initializedConfig = new Config();
125+
}
126+
127+
//Update config file in case it was missing to begin with or there are new/unused fields
128+
try {
129+
ConfigUtil.saveConfig(initializedConfig, configFilePathUsed);
130+
} catch (IOException e) {
131+
logger.warning("Failed to save config file : " + StringUtil.getDetails(e));
132+
}
133+
return initializedConfig;
134+
}
135+
136+
/**
137+
* Returns a {@code UserPrefs} using the file at {@code storage}'s user prefs file path,
138+
* or a new {@code UserPrefs} with default configuration if errors occur when
139+
* reading from the file.
140+
*/
141+
protected UserPrefs initPrefs(UserPrefsStorage storage) {
142+
Path prefsFilePath = storage.getUserPrefsFilePath();
143+
logger.info("Using prefs file : " + prefsFilePath);
144+
145+
UserPrefs initializedPrefs;
146+
try {
147+
Optional<UserPrefs> prefsOptional = storage.readUserPrefs();
148+
initializedPrefs = prefsOptional.orElse(new UserPrefs());
149+
} catch (DataConversionException e) {
150+
logger.warning("UserPrefs file at " + prefsFilePath + " is not in the correct format. "
151+
+ "Using default user prefs");
152+
initializedPrefs = new UserPrefs();
153+
} catch (IOException e) {
154+
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
155+
initializedPrefs = new UserPrefs();
156+
}
157+
158+
//Update prefs file in case it was missing to begin with or there are new/unused fields
159+
try {
160+
storage.saveUserPrefs(initializedPrefs);
161+
} catch (IOException e) {
162+
logger.warning("Failed to save config file : " + StringUtil.getDetails(e));
163+
}
164+
165+
return initializedPrefs;
166+
}
167+
168+
@Override
169+
public void start(Stage primaryStage) {
170+
logger.info("Starting AddressBook " + MainApp.VERSION);
171+
ui.start(primaryStage);
172+
}
173+
174+
@Override
175+
public void stop() {
176+
logger.info("============================ [ Stopping Address Book ] =============================");
177+
try {
178+
storage.saveUserPrefs(model.getUserPrefs());
179+
} catch (IOException e) {
180+
logger.severe("Failed to save preferences " + StringUtil.getDetails(e));
181+
}
182+
}
183+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package seedu.address.ui;
2+
3+
import javafx.fxml.FXML;
4+
import javafx.scene.control.Label;
5+
import javafx.scene.layout.Region;
6+
7+
/**
8+
* A ui for the footer bar that is displayed at the footer of the application.
9+
*/
10+
public class FooterBar extends UiPart<Region> {
11+
12+
private static final String FXML = "FooterBar.fxml";
13+
14+
@FXML
15+
private Label versionNumber;
16+
17+
/**
18+
* Creates a {@code StatusBarFooter} with the given {@code Path}.
19+
*/
20+
public FooterBar(String versionNum) {
21+
super(FXML);
22+
versionNumber.setText(versionNum);
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package seedu.address.ui;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import javafx.fxml.FXML;
6+
import javafx.scene.control.TextArea;
7+
import javafx.scene.layout.Region;
8+
9+
/**
10+
* A ui for the status bar that is displayed at the header of the application.
11+
*/
12+
public class LastInputDisplay extends UiPart<Region> {
13+
14+
private static final String FXML = "ResultDisplay.fxml";
15+
16+
@FXML
17+
private TextArea resultDisplay;
18+
19+
public LastInputDisplay() {
20+
super(FXML);
21+
}
22+
23+
public void setLastInput(String feedbackToUser) {
24+
requireNonNull(feedbackToUser);
25+
resultDisplay.setText(feedbackToUser);
26+
}
27+
28+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package seedu.address.ui;
2+
3+
import javafx.collections.ObservableList;
4+
import javafx.fxml.FXML;
5+
import javafx.scene.control.TextField;
6+
import javafx.scene.layout.Region;
7+
import seedu.address.logic.commands.CommandResult;
8+
import seedu.address.logic.commands.exceptions.CommandException;
9+
import seedu.address.logic.parser.exceptions.ParseException;
10+
11+
/**
12+
* The UI component that is responsible for receiving user command inputs.
13+
*/
14+
public class NewCommandBox extends UiPart<Region> {
15+
16+
public static final String ERROR_STYLE_CLASS = "error";
17+
private static final String FXML = "CommandBox.fxml";
18+
19+
private final CommandExecutor commandExecutor;
20+
21+
@FXML
22+
private TextField commandTextField;
23+
24+
/**
25+
* Creates a {@code CommandBox} with the given {@code CommandExecutor}.
26+
*/
27+
public NewCommandBox(CommandExecutor commandExecutor) {
28+
super(FXML);
29+
this.commandExecutor = commandExecutor;
30+
// calls #setStyleToDefault() whenever there is a change to the text of the command box.
31+
commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
32+
}
33+
34+
/**
35+
* Handles the Enter button pressed event.
36+
*/
37+
@FXML
38+
private void handleCommandEntered() {
39+
try {
40+
commandExecutor.execute(commandTextField.getText());
41+
} catch (CommandException | ParseException e) {
42+
// setStyleToIndicateCommandFailure();
43+
}
44+
45+
commandTextField.setText("");
46+
}
47+
48+
/**
49+
* Sets the command box style to use the default style.
50+
*/
51+
private void setStyleToDefault() {
52+
commandTextField.getStyleClass().remove(ERROR_STYLE_CLASS);
53+
}
54+
55+
/**
56+
* Sets the command box style to indicate a failed command.
57+
*/
58+
private void setStyleToIndicateCommandFailure() {
59+
ObservableList<String> styleClass = commandTextField.getStyleClass();
60+
61+
if (styleClass.contains(ERROR_STYLE_CLASS)) {
62+
return;
63+
}
64+
65+
styleClass.add(ERROR_STYLE_CLASS);
66+
}
67+
68+
/**
69+
* Represents a function that can execute commands.
70+
*/
71+
@FunctionalInterface
72+
public interface CommandExecutor {
73+
/**
74+
* Executes the command and returns the result.
75+
*
76+
* @see seedu.address.logic.Logic#execute(String)
77+
*/
78+
CommandResult execute(String commandText) throws CommandException, ParseException;
79+
}
80+
81+
}

0 commit comments

Comments
 (0)