Skip to content

Commit

Permalink
Add welcome message on chatbot startup
Browse files Browse the repository at this point in the history
  • Loading branch information
C5hives committed Sep 18, 2024
1 parent d31873c commit d900777
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/main/java/lawrence/app/Lawrence.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ public Response getResponse(String input) {
return new Response(CommandType.INVALID, message, true);
}
}

public String getWelcomeMessage() {
return String.format("Hello! I'm %s and I'm here to establish another GST hike.%n"
+ "What can I do for you?", NAME);
}
}
3 changes: 3 additions & 0 deletions src/main/java/lawrence/ui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ 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);
stage.setTitle("Lawrence Chatbot");

fxmlLoader.<MainWindow>getController().setLawrence(lawrence);
fxmlLoader.<MainWindow>getController().showWelcomeMessage();
stage.show();
} catch (IOException e) {
e.printStackTrace();
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/lawrence/ui/components/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javafx.scene.layout.VBox;
import lawrence.app.Lawrence;
import lawrence.app.Response;
import lawrence.command.CommandType;

/**
* Controller for the main GUI.
Expand All @@ -34,15 +35,7 @@ public class MainWindow extends AnchorPane {
*/
@FXML
public void initialize() {
// add a listener to detect scrolling on the dialogContainer
dialogContainer.setOnScroll(event -> {
// get change in scroll direction
double deltaY = event.getDeltaY();
double scrollAmount = scrollPane.getVvalue() - deltaY
/ scrollPane.getContent().getBoundsInLocal().getHeight();

scrollPane.setVvalue(scrollAmount);
});
setScrollListener();
}

/**
Expand Down Expand Up @@ -76,6 +69,31 @@ private void handleUserInput() {
scrollToBottom();
}

/**
* Displays a welcome message.
*/
public void showWelcomeMessage() {
Response welcomeResponse = new Response(CommandType.INVALID,
lawrence.getWelcomeMessage(),
true);
dialogContainer.getChildren().add(
DialogBox.getBotDialog(welcomeResponse, botImage));
}

/**
* Initialises a listener to handle scroll events.
*/
private void setScrollListener() {
dialogContainer.setOnScroll(event -> {
// get change in scroll direction
double deltaY = event.getDeltaY();
double scrollAmount = scrollPane.getVvalue() - deltaY
/ scrollPane.getContent().getBoundsInLocal().getHeight();

scrollPane.setVvalue(scrollAmount);
});
}

/**
* Sets the vertical value of the scroll pane to emulate scrolling to the bottom of the dialog box.
*/
Expand Down

0 comments on commit d900777

Please sign in to comment.