Skip to content

Commit

Permalink
Fixed JavaFX
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvinnyk committed Sep 10, 2019
1 parent f1c7507 commit 7919b52
Showing 1 changed file with 106 additions and 156 deletions.
262 changes: 106 additions & 156 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
import Storage.Storage;
import 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.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.nio.file.Paths;

Expand All @@ -15,203 +25,143 @@ public class Duke extends Application {

private final String INPUT_DELIMITER = " ";

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;

public Duke(){}

@Override
public void start(Stage stage) {
//The container for the content of the chat to scroll.
//Label helloWorld = new Label("Hello World!"); // Creating a new Label control
//Scene scene = new Scene(helloWorld); // Setting the scene to be our Label

//stage.setScene(scene); // Setting the stage to show our screen
//stage.show(); // Render the stage.
}


public static void main(String[] args){

new Duke("duke.txt").run();
}

public Duke(String filepath){
ui = new UI();
storage = new Storage(Paths.get(filepath));
tasks = storage.load();

}

public void run() {
ui.printLogo();
ui.printData("Hello! I'm Duke\n" +
"What can I do for you?\n");

boolean isExit = false;
CommandParser commandParser = new CommandParser(INPUT_DELIMITER);

while(!isExit){
String userInput = ui.nextLine();
Command command = commandParser.parseCommand(userInput);
command.execute(tasks, ui, storage);
isExit = command.isExit();
}
scrollPane = new ScrollPane();
dialogContainer = new VBox();
scrollPane.setContent(dialogContainer);

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

/*
do{
String input = ui.nextLine();
AnchorPane mainLayout = new AnchorPane();
mainLayout.getChildren().addAll(scrollPane, userInput, sendButton);

if(input.equals("bye")){
ui.printData("Bye. Hope to see you again soon!");
break;
} else if(input.equals("list")) {
int i;
scene = new Scene(mainLayout);

String content = "";
//stage.setScene(scene);
//stage.show();

for(i = 0; i < tasks.size(); i++){
content = content.concat((i + 1) + ". ");
content = content.concat("[" + tasks.get(i).getSymbol() + "]");
content = content.concat("[" + tasks.get(i).getIsDoneSymbol() + "]");
content = content.concat(" " + tasks.get(i).getDescription());
if(tasks.get(i).getSymbol() == 'D'){
content = content.concat(" (by: " + tasks.get(i).getDetails() + ")");
} else if(tasks.get(i).getSymbol() == 'E'){
content = content.concat(" (at: " + tasks.get(i).getDetails() + ")");
}
content = content.concat("\n");
}
//Step 2. Formatting the window to look as expected
stage.setTitle("Duke");
stage.setResizable(false);
stage.setMinHeight(600.0);
stage.setMinWidth(400.0);

ui.printData(content);
storage.save(tasks);
mainLayout.setPrefSize(400.0, 600.0);

} else if(input.startsWith("done")) {
String[] sp = input.split(" ", 2);
int index = Integer.parseInt(sp[1]);
scrollPane.setPrefSize(385, 535);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

tasks.get(index - 1).markAsDone();
scrollPane.setVvalue(1.0);
scrollPane.setFitToWidth(true);

String content = "Nice! I've marked this task as done:\n" +
"[" + tasks.get(index - 1).getIsDoneSymbol() + "] " + tasks.get(index - 1).getDescription() +"\n";
// You will need to import `javafx.scene.layout.Region` for this.
dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE);

ui.printData(content);
userInput.setPrefWidth(325.0);

} else if(input.startsWith("delete")){
String[] sp = input.split(" ", 2);
int index = Integer.parseInt(sp[1]);
sendButton.setPrefWidth(55.0);

String content = "";
content = content.concat("Noted. I've removed this task:\n");
content = content.concat("[" + tasks.get(index - 1).getSymbol() + "][" + tasks.get(index - 1).getIsDoneSymbol() + "] " + tasks.get(index - 1).getDescription());
if(tasks.get(index - 1).getSymbol() == 'D'){
content = content.concat(" (by: " + tasks.get(index - 1).getDetails() + ")");
} else if (tasks.get(index - 1).getSymbol() == 'E'){
content = content.concat(" (at: " + tasks.get(index - 1).getDetails() + ")");
}
content = content.concat("\n");
AnchorPane.setTopAnchor(scrollPane, 1.0);

tasks.remove(index - 1);
AnchorPane.setBottomAnchor(sendButton, 1.0);
AnchorPane.setRightAnchor(sendButton, 1.0);

content = content.concat("You now have " + tasks.size() + " tasks in this list\n");
ui.printData(content);
AnchorPane.setLeftAnchor(userInput , 1.0);
AnchorPane.setBottomAnchor(userInput, 1.0);

//Step 3. Add functionality to handle user input.
sendButton.setOnMouseClicked((event) -> {
dialogContainer.getChildren().add(getDialogLabel(userInput.getText()));
userInput.clear();
});

} else if(input.startsWith("todo")) {
String[] sp = input.split(" ", 2);
userInput.setOnAction((event) -> {
dialogContainer.getChildren().add(getDialogLabel(userInput.getText()));
userInput.clear();
});

if(sp.length < 2){
ui.printData("OOPS! The description of a todo cannot be empty.\n");
continue;
}
dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0));

tasks.add(new todo(sp[1]));
//Part 3. Add functionality to handle user input.
sendButton.setOnMouseClicked((event) -> {
handleUserInput();
});

String content = "";
userInput.setOnAction((event) -> {
handleUserInput();
});

content = content.concat("Got it. I've added this task:\n");
content = content.concat("[T][✗] " + sp[1] +'\n');
content = content.concat("Now you have " + tasks.size() + " tasks in this list\n");

ui.printData(content);
stage.setScene(scene);
stage.show();

} else if(input.startsWith("deadline")) {
String[] sp = input.split(" ", 2);
if(sp.length < 2){
ui.printData("OOPS! The description of a deadline cannot be empty.\n");
continue;
}
String[] sp2 = sp[1].split(" /by ", 2);
tasks.add(new deadline(sp2[0], sp2[1]));
String content = "";
content = content.concat("Got it. I've added this task:\n");
content = content.concat("[D][✗] " + sp2[0] + " (by: " + sp2[1] + ")\n");
content = content.concat("Now you have " + tasks.size() + " tasks in this list\n");
ui.printData(content);
} else if(input.startsWith("event")) {
String[] sp = input.split(" ", 2);
}

if(sp.length < 2){
ui.printData("OOPS!!! The description of a event cannot be empty.");
continue;
}
private Label getDialogLabel(String text) {
// You will need to import `javafx.scene.control.Label`.
Label textToAdd = new Label(text);
textToAdd.setWrapText(true);

String[] sp2 = sp[1].split(" /at ", 2);
return textToAdd;
}

tasks.add(new event(sp2[0], sp2[1]));
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();
}

String content = "";
private String getResponse(String input) {
return "Duke heard: " + input;
}

content = content.concat("Got it. I've added this task:");
content = content.concat("[E][✗] " + sp2[0] + " (at: " + sp2[1] + ")");
content = content.concat("Now you have " + tasks.size() + " tasks in this list");

ui.printData(content);

} else if(input.startsWith("find")) {

String[] sp = input.split(" ", 2);
public static void main(String[] args){

if(sp.length < 2){
ui.printData("OOPS!!! The description of a find cannot be empty.");
continue;
}
new Duke("duke.txt").run();
}

ArrayList<Integer> indexes = new ArrayList<Integer>();
int i;
for(i = 0; i < tasks.size(); i++) {
if(tasks.get(i).getDescription().contains(sp[1])) {
indexes.add(i);
}
}
String content = "";
for(i = 0; i < indexes.size(); i++) {
content = content.concat((i + 1) + ". ");
content = content.concat("[" + tasks.get(indexes.get(i)).getSymbol() + "]");
content = content.concat("[" + tasks.get(indexes.get(i)).getIsDoneSymbol() + "]");
content = content.concat(" " + tasks.get(indexes.get(i)).getDescription());
if(tasks.get(indexes.get(i)).getSymbol() == 'D'){
content = content.concat(" (by: " + tasks.get(indexes.get(i)).getDetails() + ")");
} else if(tasks.get(indexes.get(i)).getSymbol() == 'E'){
content = content.concat(" (at: " + tasks.get(indexes.get(i)).getDetails() + ")");
}
content = content.concat("\n");
}
ui.printData(content);
public Duke(String filepath){
ui = new UI();
storage = new Storage(Paths.get(filepath));
tasks = storage.load();

}

} else {
public void run() {
ui.printLogo();
ui.printData("Hello! I'm Duke\n" +
"What can I do for you?\n");

ui.printData("OOPS!!! I'm sorry, but I don't know what that means :-(\n");
}
boolean isExit = false;
CommandParser commandParser = new CommandParser(INPUT_DELIMITER);

} while(true);
while(!isExit){
String userInput = ui.nextLine();
Command command = commandParser.parseCommand(userInput);
command.execute(tasks, ui, storage);
isExit = command.isExit();
}

*/
}
}

0 comments on commit 7919b52

Please sign in to comment.