Skip to content

Commit

Permalink
Refactor code, add FXML
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeoliya committed Sep 15, 2020
1 parent a565754 commit ce3ee7d
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 195 deletions.
2 changes: 1 addition & 1 deletion data/duke.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
T~1~read book
E~0~f~2020-10-15
109 changes: 7 additions & 102 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,14 @@
import duke.parser.Parser;
import duke.storage.Storage;
import duke.task.TaskList;
import duke.ui.DialogBox;
import duke.ui.Ui;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Duke extends Application {
public class Duke {

private Storage storage;
private TaskList tasks;
private Ui ui;

private ScrollPane scrollPane;
private VBox dialogContainer;
private TextField userInput;
private Button sendButton;
private Scene scene;
private Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png"));
private Image dukeImage =new Image(this.getClass().getResourceAsStream("/images/DaDuke.png"));

// constructor
public Duke() {
this("data", "./data/duke.txt");
Expand All @@ -45,90 +23,17 @@ public Duke(String dataDirectory, String filePath) {
tasks = new TaskList(storage.load());
}

@Override
public void start(Stage stage) {
//Step 1. Setting up required components

//The container for the content of the chat to scroll.
scrollPane = new ScrollPane();
dialogContainer = new VBox();
Label greeting = new Label(ui.greet());
dialogContainer.getChildren().addAll(
DialogBox.getDukeDialog(greeting, new ImageView(dukeImage))
);
scrollPane.setContent(dialogContainer);

userInput = new TextField();
sendButton = new Button("Send");

AnchorPane mainLayout = new AnchorPane();
mainLayout.getChildren().addAll(scrollPane, userInput, sendButton);

scene = new Scene(mainLayout);

//Step 2. Formatting the window to look as expected
stage.setTitle("Duke");
stage.setResizable(false);
stage.setMinHeight(600.0);
stage.setMinWidth(400.0);

mainLayout.setPrefSize(400.0, 600.0);

scrollPane.setPrefSize(385, 535);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

scrollPane.setVvalue(1.0);
scrollPane.setFitToWidth(true);

// You will need to import `javafx.scene.layout.Region` for this.
dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE);

userInput.setPrefWidth(325.0);

sendButton.setPrefWidth(55.0);

AnchorPane.setTopAnchor(scrollPane, 1.0);

AnchorPane.setBottomAnchor(sendButton, 1.0);
AnchorPane.setRightAnchor(sendButton, 1.0);

AnchorPane.setLeftAnchor(userInput , 1.0);
AnchorPane.setBottomAnchor(userInput, 1.0);

stage.setScene(scene);
stage.show();

// Scroll down to the end every time dialogContainer's height changes
dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0));

// Add functionality to handle user input
sendButton.setOnMouseClicked((event) -> runGUI()); // for button
userInput.setOnAction((event) -> runGUI()); // for textfield
}

private void runGUI() {
String input = userInput.getText().trim();
String output;

// process user input
public String getResponse(String input) {
try {
Command command = Parser.parse(input);
output = command.execute(tasks, ui, storage);
return command.execute(tasks, ui, storage);
} catch (DukeException e) {
output = ui.format(e.getMessage());
return ui.format(e.getMessage());
}
assert !output.isEmpty() : "Output should not be empty";

// set labels and dialog container
Label userText = new Label(input);
Label dukeText = new Label(output);
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(userText, new ImageView(userImage)),
DialogBox.getDukeDialog(dukeText, new ImageView(dukeImage))
);
}

userInput.clear();
public Ui getUi() {
return ui;
}
}

3 changes: 3 additions & 0 deletions src/main/java/duke/ExceptionTypeEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ public enum ExceptionTypeEnum {
INCORRECT_LIST ("Did you mean to say 'list'?"),
MISSING_FIND_KEYWORD ("Please provide keyword(s) to look for"),
MISSING_SCHEDULE_DATE ("Please provide a date to look for"),
INCORRECT_SCHEDULE_DATE ("A date must follow ISO format: yyyy-mm-dd"),
MISSING_TODO_DESCRIPTION ("The description for a todo cannot be empty."),
MISSING_DEADLINE_DESCRIPTION ("The description for a deadline cannot be empty."),
MISSING_DEADLINE_DATE ("The date for a deadline cannot be empty."),
INCORRECT_DEADLINE_DATE ("The date for a deadline must follow ISO format: yyyy-mm-dd"),
MISSING_EVENT_DESCRIPTION ("The description for a task cannot be empty."),
MISSING_EVENT_DATE ("The date for an event cannot be empty."),
INCORRECT_EVENT_DATE ("The date for an event must follow ISO format: yyyy-mm-dd"),
MISSING_NOTE_NAME ("The name for a note cannot be empty."),
MISSING_NOTE_DESCRIPTION ("The description for a note cannot be empty."),
MISSING_DONE_ITEM ("Please specify an item number."),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
*/
public class Launcher {
public static void main(String[] args) {
Application.launch(Duke.class, args);
Application.launch(Main.class, args);
}
}
32 changes: 32 additions & 0 deletions src/main/java/duke/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package duke;

import duke.ui.MainWindow;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;

/**
* A GUI for Duke using FXML.
*/
public class Main extends Application {

private Duke duke = new Duke();

@Override
public void start(Stage stage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/view/MainWindow.fxml"));
AnchorPane ap = fxmlLoader.load();
Scene scene = new Scene(ap);
stage.setScene(scene);
fxmlLoader.<MainWindow>getController().setDuke(duke);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2 changes: 0 additions & 2 deletions src/main/java/duke/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public AddCommand(Task task) {
this.task = task;
}

public AddCommand() { }

/**
* Adds a task to the given list.
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/duke/command/ByeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import duke.ui.Ui;

public class ByeCommand implements Command {
static final String BYE_MESSAGE = "Bye. Hope to see you again soon!";
static final String EMPTY_STRING = "";

/**
* Prints the goodbye message to the user
Expand All @@ -15,6 +15,6 @@ public class ByeCommand implements Command {
* @param storage storage interface
*/
@Override
public String execute(TaskList tasks, Ui ui, Storage storage) { return ui.format(BYE_MESSAGE);
public String execute(TaskList tasks, Ui ui, Storage storage) { return ui.format(EMPTY_STRING);
}
}
3 changes: 0 additions & 3 deletions src/main/java/duke/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import duke.task.TaskList;
import duke.ui.Ui;

import java.util.Arrays;
import java.util.List;

public interface Command {
String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException;
}
1 change: 0 additions & 1 deletion src/main/java/duke/command/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package duke.command;

import duke.DukeException;
import duke.ExceptionTypeEnum;
import duke.storage.Storage;
import duke.task.Task;
import duke.task.TaskList;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/duke/command/DoneCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package duke.command;

import duke.DukeException;
import duke.ExceptionTypeEnum;
import duke.storage.Storage;
import duke.task.Task;
import duke.task.TaskList;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/duke/command/FindCommand.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package duke.command;

import duke.storage.Storage;
import duke.task.TaskList;
import duke.ui.Ui;
import duke.storage.Storage;
import duke.task.TaskList;
import duke.ui.Ui;

public class FindCommand implements Command {
String keyword;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/duke/command/ListCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package duke.command;

import duke.DukeException;
import duke.storage.Storage;
import duke.task.TaskList;
import duke.ui.Ui;
Expand Down

0 comments on commit ce3ee7d

Please sign in to comment.