Skip to content

Commit

Permalink
Add Level 10 (basic GUI functionality; to be polished)
Browse files Browse the repository at this point in the history
  • Loading branch information
Illio Suardi committed Sep 7, 2020
1 parent 6963ea0 commit dcd857d
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 181 deletions.
156 changes: 36 additions & 120 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,27 @@
import duke.resource.Parser;
import duke.resource.TaskList;
import duke.util.DukeException;
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;

/**
* Duke is a bot that functions as a user's task manager.
*/

public class Duke extends Application {
public class Duke {

private static final String FILEPATH = "./src/main/data/duke.txt";

private Image user = new Image(this.getClass().getResourceAsStream("/images/DaUser.png"));
private Image duke = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png"));
private ScrollPane scrollPane;
private VBox dialogContainer;
private TextField userInput;
private Button sendButton;
private Scene scene;
private final Ui ui;
private TaskList tasks;
private final Storage storage;

/**
* Overloaded constructor that creates a Duke object, so Launcher can be ran.
* Constructor that creates a Duke object, so Launcher can be ran.
*/

public Duke() {
this.ui = new Ui();
this.storage = new Storage("./src/main/data/duke.txt");
}

/**
* Overloaded constructor that creates a Duke object with the path the text
* file will be saved in.
*
* @param filePath the filepath task sessions will be saved in
*/

public Duke(String filePath) {
this.ui = new Ui();
this.storage = new Storage(filePath);
ui.welcome();
this.storage = new Storage(FILEPATH);
ui.printWelcome();
try {
tasks = TaskList.parse(this.storage.load());
ui.printLoaded(tasks);
Expand All @@ -66,96 +37,42 @@ public Duke(String filePath) {
* Runs Duke's user input scanning that only terminates when a "bye" command is given.
*/

public void run() {
ui.start();
boolean isExit = false;
while (!isExit) {
try {
String command = ui.read();
Command c = Parser.parse(command);
c.execute(tasks, ui, storage);
isExit = c.shouldExit();
} catch (DukeException e) {
ui.printError(e);
}
// public void run() {
// ui.start();
// boolean isExit = false;
// while (!isExit) {
// try {
// String command = ui.read();
// Command c = Parser.parse(command);
// c.execute(tasks, ui, storage);
// isExit = c.shouldExit();
// } catch (DukeException e) {
// ui.printError(e);
// }
// }
// }

public String welcome() {
String ret = ui.welcome();
try {
tasks = TaskList.parse(this.storage.load());
ret += ui.printLoaded(tasks);
} catch (DukeException e) {
ret += ui.showError(e);
}
return ret;
}

private Label getDialogLabel(String text) {
Label textToAdd = new Label(text);
textToAdd.setWrapText(true);

return textToAdd;
}

private void handleUserInput() {
Label userText = new Label(userInput.getText());
Label dukeText = new Label(getResponse(userInput.getText()));
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(userText, new ImageView(user)),
DialogBox.getDukeDialog(dukeText, new ImageView(duke))
);
userInput.clear();
public Ui getUi() {
return this.ui;
}

String getResponse(String input) {
return "Duke heard: " + input;
public TaskList getTaskList() {
return this.tasks;
}

@Override
public void start(Stage stage) {
scrollPane = new ScrollPane();
dialogContainer = new VBox();
scrollPane.setContent(dialogContainer);

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

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

scene = new Scene(mainLayout);

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

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);

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);

sendButton.setOnMouseClicked(event -> {
handleUserInput();
});

userInput.setOnAction(event -> {
handleUserInput();
});

dialogContainer.heightProperty().addListener(observable -> scrollPane.setVvalue(1.0));
public Storage getStorage() {
return this.storage;
}

/**
Expand All @@ -165,7 +82,6 @@ public void start(Stage stage) {
*/

public static void main(String[] args) {
// change the filePath below to save elsewhere
new Duke("./src/main/data/duke.txt").run();
// new Duke().run();
}
}
123 changes: 123 additions & 0 deletions src/main/java/DukeGui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import javafx.application.Application;
import javafx.application.Platform;
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;

import duke.Command;
import duke.resource.Parser;

import java.util.concurrent.CompletableFuture;

public class DukeGui extends Application {

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

private Label getDialogLabel(String text) {
Label textToAdd = new Label(text);
textToAdd.setWrapText(true);

return textToAdd;
}

private void displayIntro() {
Label introText = new Label(duke.welcome());
dialogContainer.getChildren().addAll(
DialogBox.getDukeDialog(introText, new ImageView(dukeImage)));
}

private void handleUserInput() {
Label userText = new Label(userInput.getText());
Command c = Parser.parse(userInput.getText());
Label dukeText = new Label(c.parse(duke.getTaskList(), duke.getUi(), duke.getStorage()));
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(userText, new ImageView(userImage)),
DialogBox.getDukeDialog(dukeText, new ImageView(dukeImage))
);
if (c.shouldExit()) {
CompletableFuture.runAsync(Platform::exit);
}
userInput.clear();
}

@Override
public void start(Stage stage) {
scrollPane = new ScrollPane();
dialogContainer = new VBox();
scrollPane.setContent(dialogContainer);

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

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

scene = new Scene(mainLayout);

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

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);

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);

sendButton.setOnMouseClicked(event -> {
handleUserInput();
});

userInput.setOnAction(event -> {
handleUserInput();
});

dialogContainer.heightProperty().addListener(observable -> scrollPane.setVvalue(1.0));

duke = new Duke();

displayIntro();

}

public static void main(String[] args) {
Application.launch(args);
}

}
9 changes: 0 additions & 9 deletions src/main/java/Launcher.java

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Main {

public static void main(String[] args) {
DukeGui.main(args);
}

}
44 changes: 0 additions & 44 deletions src/main/java/MainWindow.java

This file was deleted.

0 comments on commit dcd857d

Please sign in to comment.