From fd45baab0be8e7993ea701734f5938ee1ff59dea Mon Sep 17 00:00:00 2001 From: nixonwidjaja Date: Thu, 2 Nov 2023 19:24:42 +0800 Subject: [PATCH] Gets previous input --- .../java/seedu/address/ui/CommandBox.java | 113 ++++++++++++++++++ src/main/resources/view/CommandBox.fxml | 2 +- 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/ui/CommandBox.java b/src/main/java/seedu/address/ui/CommandBox.java index 9e75478664b..821caff1543 100644 --- a/src/main/java/seedu/address/ui/CommandBox.java +++ b/src/main/java/seedu/address/ui/CommandBox.java @@ -1,8 +1,12 @@ package seedu.address.ui; +import java.util.ArrayList; + import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.TextField; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; import javafx.scene.layout.Region; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; @@ -17,6 +21,7 @@ public class CommandBox extends UiPart { private static final String FXML = "CommandBox.fxml"; private final CommandExecutor commandExecutor; + private final InputHistory inputs; @FXML private TextField commandTextField; @@ -27,6 +32,7 @@ public class CommandBox extends UiPart { public CommandBox(CommandExecutor commandExecutor) { super(FXML); this.commandExecutor = commandExecutor; + this.inputs = new InputHistory(); // calls #setStyleToDefault() whenever there is a change to the text of the command box. commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault()); } @@ -42,6 +48,7 @@ private void handleCommandEntered() { } try { + inputs.add(commandText); commandExecutor.execute(commandText); commandTextField.setText(""); } catch (CommandException | ParseException e) { @@ -49,6 +56,45 @@ private void handleCommandEntered() { } } + /** + * Handles UP and DOWN key pressed event. + */ + @FXML + private void handleClick(KeyEvent keyEvent) { + KeyCode clickedKey = keyEvent.getCode(); + + if (clickedKey.equals(KeyCode.UP)) { + keyEvent.consume(); + getPreviousInput(); + } else if (clickedKey.equals(KeyCode.DOWN)) { + keyEvent.consume(); + getNextInput(); + } + } + + private void getPreviousInput() { + if (inputs.hasPreviousInput()) { + updateText(inputs.getPreviousInput()); + } + } + + private void getNextInput() { + if (inputs.hasNextInput()) { + updateText(inputs.getNextInput()); + } else { + updateText(""); + inputs.setLastIndex(); + } + } + + /** + * Change current text with the given input. + */ + private void updateText(String input) { + commandTextField.setText(input); + commandTextField.positionCaret(input.length()); + } + /** * Sets the command box style to use the default style. */ @@ -82,4 +128,71 @@ public interface CommandExecutor { CommandResult execute(String commandText) throws CommandException, ParseException; } + /** + * Represents the history of inputs given by the user. + */ + public class InputHistory extends ArrayList { + private int index; + + /** + * Constructs the InputHistory. + */ + public InputHistory() { + super(); + super.add(""); + index = 1; + } + + @Override + public boolean add(String input) { + if (!input.equals(super.get(super.size() - 1))) { + super.add(input); + } + index = super.size(); + return true; + } + + /** + * Sets the index to be equal to the list size. + */ + public void setLastIndex() { + index = super.size(); + } + + /** + * Checks whether the list has previous input. + * @return boolean + */ + public boolean hasPreviousInput() { + return index > 0; + } + + /** + * Checks whether the list has next input. + * @return boolean + */ + public boolean hasNextInput() { + return index < super.size() - 1; + } + + /** + * Gets the previous input + * @return the previous input + */ + public String getPreviousInput() { + assert index > 0; + index--; + return super.get(index); + } + + /** + * Gets the next input + * @return the next input + */ + public String getNextInput() { + assert index < super.size() - 1; + index++; + return super.get(index); + } + } } diff --git a/src/main/resources/view/CommandBox.fxml b/src/main/resources/view/CommandBox.fxml index 124283a392e..4191a8bdde1 100644 --- a/src/main/resources/view/CommandBox.fxml +++ b/src/main/resources/view/CommandBox.fxml @@ -4,6 +4,6 @@ - +