Skip to content

Commit

Permalink
Merge pull request #9 from rgonslayer/Level-10
Browse files Browse the repository at this point in the history
Level-10
  • Loading branch information
rgonslayer authored Sep 19, 2022
2 parents 421def4 + bc4794d commit 81f7c97
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 79 deletions.
15 changes: 15 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ checkstyle {
dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.0'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.0'

String javaFxVersion = '11'

implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux'
}

test {
Expand Down
1 change: 1 addition & 0 deletions data/taskList.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
T | 0 | read book
D | 0 | return book | 2022-09-20
T | 0 | gym
T | 0 | finish coding
2 changes: 1 addition & 1 deletion src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: jarvis.Jarvis
jarvis.Main-Class: jarvis.Jarvis

61 changes: 61 additions & 0 deletions src/main/java/jarvis/DialogBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package jarvis;

import java.io.IOException;
import java.util.Collections;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

/**
* An example of a custom control using FXML.
* This control represents a dialog box consisting of an ImageView to represent the speaker's face and a label
* containing text from the speaker.
*/
public class DialogBox extends HBox {
@FXML
private Label dialog;
@FXML
private ImageView displayPicture;

private DialogBox(String text, Image img) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("/view/DialogBox.fxml"));
fxmlLoader.setController(this);
fxmlLoader.setRoot(this);
fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}

dialog.setText(text);
displayPicture.setImage(img);
}

/**
* Flips the dialog box such that the ImageView is on the left and text on the right.
*/
private void flip() {
ObservableList<Node> tmp = FXCollections.observableArrayList(this.getChildren());
Collections.reverse(tmp);
getChildren().setAll(tmp);
setAlignment(Pos.TOP_LEFT);
}

public static DialogBox getUserDialog(String text, Image img) {
return new DialogBox(text, img);
}

public static DialogBox getDukeDialog(String text, Image img) {
var db = new DialogBox(text, img);
db.flip();
return db;
}
}
35 changes: 20 additions & 15 deletions src/main/java/jarvis/Jarvis.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import jarvis.task.TaskList;
import jarvis.exception.JarvisException;
import javafx.application.Application;

import java.io.IOException;

public class Jarvis {
public static void main(String[] args) throws JarvisException {

public class Jarvis{
static Parser parser;
public static void main(String[] args){

String filePath = "data/taskList.txt";

Expand All @@ -18,22 +21,24 @@ public static void main(String[] args) throws JarvisException {
} catch (IOException e) {
tasks = new TaskList();
}
parser = new Parser(tasks);

Parser parser = new Parser(tasks);

parser.introduction();
Application.launch(Main.class, args);

try {
parser.readCommand();
try {
storage.write(tasks);
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
parser.farewell();
} catch (JarvisException e) {
System.out.println((e));
storage.write(tasks);
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

/**
* You should have your own function to generate a response to user input.
* Replace this stub with your completed method.
*/
String getResponse(String input) throws JarvisException {
return parser.readCommand(input);
}

}
31 changes: 31 additions & 0 deletions src/main/java/jarvis/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package jarvis;

import java.io.IOException;

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

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

private Jarvis jarvis = new Jarvis();

@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().setJarvis(jarvis);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
52 changes: 52 additions & 0 deletions src/main/java/jarvis/MainWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package jarvis;

import jarvis.exception.JarvisException;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
/**
* Controller for MainWindow. Provides the layout for the other controls.
*/
public class MainWindow extends AnchorPane {
@FXML
private ScrollPane scrollPane;
@FXML
private VBox dialogContainer;
@FXML
private TextField userInput;
@FXML
private Button sendButton;

private Jarvis jarvis;

private Image userImage = new Image(this.getClass().getResourceAsStream("/images/user.png"));
private Image jarvisImage = new Image(this.getClass().getResourceAsStream("/images/jarvis.png"));

@FXML
public void initialize() {
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
}

public void setJarvis(Jarvis j) {
jarvis = j;
}

/**
* Creates two dialog boxes, one echoing user input and the other containing Jarvis's reply and then appends them to
* the dialog container. Clears the user input after processing.
*/
@FXML
private void handleUserInput() throws JarvisException {
String input = userInput.getText();
String response = jarvis.getResponse(input);
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, jarvisImage)
);
userInput.clear();
}
}
Loading

0 comments on commit 81f7c97

Please sign in to comment.