Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#77 from Ma-Yueran/branch-repla…
Browse files Browse the repository at this point in the history
…ce-ui

Branch replace ui
  • Loading branch information
Ma-Yueran authored Oct 7, 2020
2 parents 78effbe + 0827f29 commit 4a8fcfc
Show file tree
Hide file tree
Showing 11 changed files with 701 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
*/
public class Main {
public static void main(String[] args) {
Application.launch(MainApp.class, args);
Application.launch(NewMainApp.class, args);
}
}
183 changes: 183 additions & 0 deletions src/main/java/seedu/address/NewMainApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package seedu.address;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
import java.util.logging.Logger;

import javafx.application.Application;
import javafx.stage.Stage;
import seedu.address.commons.core.Config;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.Version;
import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.commons.util.ConfigUtil;
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.Logic;
import seedu.address.logic.LogicManager;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.UserPrefs;
import seedu.address.model.util.SampleDataUtil;
import seedu.address.storage.AddressBookStorage;
import seedu.address.storage.JsonAddressBookStorage;
import seedu.address.storage.JsonUserPrefsStorage;
import seedu.address.storage.Storage;
import seedu.address.storage.StorageManager;
import seedu.address.storage.UserPrefsStorage;
import seedu.address.ui.NewUiManager;
import seedu.address.ui.Ui;

/**
* Runs the application.
*/
public class NewMainApp extends Application {

public static final Version VERSION = new Version(0, 6, 0, true);

private static final Logger logger = LogsCenter.getLogger(MainApp.class);

protected Ui ui;
protected Logic logic;
protected Storage storage;
protected Model model;
protected Config config;

@Override
public void init() throws Exception {
logger.info("=============================[ Initializing AddressBook ]===========================");
super.init();

AppParameters appParameters = AppParameters.parse(getParameters());
config = initConfig(appParameters.getConfigPath());

UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
storage = new StorageManager(addressBookStorage, userPrefsStorage);

initLogging(config);

model = initModelManager(storage, userPrefs);

logic = new LogicManager(model, storage);

ui = new NewUiManager(logic);
}

/**
* Returns a {@code ModelManager} with the data from {@code storage}'s address book and {@code userPrefs}. <br>
* The data from the sample address book will be used instead if {@code storage}'s address book is not found,
* or an empty address book will be used instead if errors occur when reading {@code storage}'s address book.
*/
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
Optional<ReadOnlyAddressBook> addressBookOptional;
ReadOnlyAddressBook initialData;
try {
addressBookOptional = storage.readAddressBook();
if (!addressBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample AddressBook");
}
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
initialData = new AddressBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
initialData = new AddressBook();
}

return new ModelManager(initialData, userPrefs);
}

private void initLogging(Config config) {
LogsCenter.init(config);
}

/**
* Returns a {@code Config} using the file at {@code configFilePath}. <br>
* The default file path {@code Config#DEFAULT_CONFIG_FILE} will be used instead
* if {@code configFilePath} is null.
*/
protected Config initConfig(Path configFilePath) {
Config initializedConfig;
Path 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;
}

/**
* Returns a {@code UserPrefs} using the file at {@code storage}'s user prefs file path,
* or a new {@code UserPrefs} with default configuration if errors occur when
* reading from the file.
*/
protected UserPrefs initPrefs(UserPrefsStorage storage) {
Path prefsFilePath = storage.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 AddressBook");
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;
}

@Override
public void start(Stage primaryStage) {
logger.info("Starting AddressBook " + MainApp.VERSION);
ui.start(primaryStage);
}

@Override
public void stop() {
logger.info("============================ [ Stopping Address Book ] =============================");
try {
storage.saveUserPrefs(model.getUserPrefs());
} catch (IOException e) {
logger.severe("Failed to save preferences " + StringUtil.getDetails(e));
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/seedu/address/ui/FooterBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package seedu.address.ui;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.Region;

/**
* A ui for the footer bar that is displayed at the footer of the application.
*/
public class FooterBar extends UiPart<Region> {

private static final String FXML = "FooterBar.fxml";

@FXML
private Label versionNumber;

/**
* Creates a {@code StatusBarFooter} with the given {@code Path}.
*/
public FooterBar(String versionNum) {
super(FXML);
versionNumber.setText(versionNum);
}
}
28 changes: 28 additions & 0 deletions src/main/java/seedu/address/ui/LastInputDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.address.ui;

import static java.util.Objects.requireNonNull;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Region;

/**
* A ui for the status bar that is displayed at the header of the application.
*/
public class LastInputDisplay extends UiPart<Region> {

private static final String FXML = "ResultDisplay.fxml";

@FXML
private TextArea resultDisplay;

public LastInputDisplay() {
super(FXML);
}

public void setLastInput(String feedbackToUser) {
requireNonNull(feedbackToUser);
resultDisplay.setText(feedbackToUser);
}

}
81 changes: 81 additions & 0 deletions src/main/java/seedu/address/ui/NewCommandBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package seedu.address.ui;

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.layout.Region;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* The UI component that is responsible for receiving user command inputs.
*/
public class NewCommandBox extends UiPart<Region> {

public static final String ERROR_STYLE_CLASS = "error";
private static final String FXML = "CommandBox.fxml";

private final CommandExecutor commandExecutor;

@FXML
private TextField commandTextField;

/**
* Creates a {@code CommandBox} with the given {@code CommandExecutor}.
*/
public NewCommandBox(CommandExecutor commandExecutor) {
super(FXML);
this.commandExecutor = commandExecutor;
// calls #setStyleToDefault() whenever there is a change to the text of the command box.
commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
}

/**
* Handles the Enter button pressed event.
*/
@FXML
private void handleCommandEntered() {
try {
commandExecutor.execute(commandTextField.getText());
} catch (CommandException | ParseException e) {
// setStyleToIndicateCommandFailure();
}

commandTextField.setText("");
}

/**
* Sets the command box style to use the default style.
*/
private void setStyleToDefault() {
commandTextField.getStyleClass().remove(ERROR_STYLE_CLASS);
}

/**
* Sets the command box style to indicate a failed command.
*/
private void setStyleToIndicateCommandFailure() {
ObservableList<String> styleClass = commandTextField.getStyleClass();

if (styleClass.contains(ERROR_STYLE_CLASS)) {
return;
}

styleClass.add(ERROR_STYLE_CLASS);
}

/**
* Represents a function that can execute commands.
*/
@FunctionalInterface
public interface CommandExecutor {
/**
* Executes the command and returns the result.
*
* @see seedu.address.logic.Logic#execute(String)
*/
CommandResult execute(String commandText) throws CommandException, ParseException;
}

}
Loading

0 comments on commit 4a8fcfc

Please sign in to comment.