From 7f1896211442f860b51050d5cf2ea1dd6c9fe68a Mon Sep 17 00:00:00 2001 From: flyingsalsa <107453007+flyingsalsa@users.noreply.github.com> Date: Sat, 19 Oct 2024 00:53:22 +0800 Subject: [PATCH 1/3] Create CommandHistory.java --- .../java/seedu/address/ui/CommandHistory.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/seedu/address/ui/CommandHistory.java diff --git a/src/main/java/seedu/address/ui/CommandHistory.java b/src/main/java/seedu/address/ui/CommandHistory.java new file mode 100644 index 00000000000..912dc8bc999 --- /dev/null +++ b/src/main/java/seedu/address/ui/CommandHistory.java @@ -0,0 +1,32 @@ +package seedu.address.ui; +import java.util.ArrayList; + + +/** + * Container for previous commands + */ +public class CommandHistory { + private ArrayList commandHistory = new ArrayList<>(); + private int currentHistoryIndex = -1; + + public void addCommand(String command) { + commandHistory.add(command); + currentHistoryIndex = commandHistory.size(); // Reset index to point after the latest command + } + + public String getPreviousCommand() { + if (currentHistoryIndex > 0) { + currentHistoryIndex--; + return commandHistory.get(currentHistoryIndex); + } + return null; // No previous command + } + + public String getNextCommand() { + if (currentHistoryIndex < commandHistory.size() - 1) { + currentHistoryIndex++; + return commandHistory.get(currentHistoryIndex); + } + return null; // No next command + } +} \ No newline at end of file From 6fdc539d385895694dab5795aec4bd296396387e Mon Sep 17 00:00:00 2001 From: flyingsalsa <107453007+flyingsalsa@users.noreply.github.com> Date: Sat, 19 Oct 2024 01:29:52 +0800 Subject: [PATCH 2/3] Implement CommandHistory and testcases --- .../java/seedu/address/ui/CommandBox.java | 29 ++++++++ .../java/seedu/address/ui/CommandHistory.java | 12 +++- .../java/seedu/address/ui/MainWindow.java | 2 + .../seedu/address/ui/CommandHistoryTest.java | 67 +++++++++++++++++++ 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 src/test/java/seedu/address/ui/CommandHistoryTest.java diff --git a/src/main/java/seedu/address/ui/CommandBox.java b/src/main/java/seedu/address/ui/CommandBox.java index 9e75478664b..4c82e89bcf9 100644 --- a/src/main/java/seedu/address/ui/CommandBox.java +++ b/src/main/java/seedu/address/ui/CommandBox.java @@ -3,6 +3,7 @@ import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.TextField; +import javafx.scene.input.KeyCode; import javafx.scene.layout.Region; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; @@ -18,9 +19,13 @@ public class CommandBox extends UiPart { private final CommandExecutor commandExecutor; + private CommandHistory commandHistory = new CommandHistory(); + @FXML private TextField commandTextField; + + /** * Creates a {@code CommandBox} with the given {@code CommandExecutor}. */ @@ -31,6 +36,28 @@ public CommandBox(CommandExecutor commandExecutor) { commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault()); } + /** + * initialize() method from javaFx, used to check for arrow inputs inside the commandTextField + */ + @FXML + public void initialize() { + commandTextField.setOnKeyPressed(event -> { + if (event.getCode() == KeyCode.UP) { + String previousCommand = commandHistory.getPreviousCommand(); + if (previousCommand != null) { + commandTextField.setText(previousCommand); + commandTextField.positionCaret(previousCommand.length()); // Move cursor to end + } + } else if (event.getCode() == KeyCode.DOWN) { + String nextCommand = commandHistory.getNextCommand(); + if (nextCommand != null) { + commandTextField.setText(nextCommand); + commandTextField.positionCaret(nextCommand.length()); // Move cursor to end + } + } + }); + } + /** * Handles the Enter button pressed event. */ @@ -46,7 +73,9 @@ private void handleCommandEntered() { commandTextField.setText(""); } catch (CommandException | ParseException e) { setStyleToIndicateCommandFailure(); + return; } + commandHistory.addCommand(commandText); } /** diff --git a/src/main/java/seedu/address/ui/CommandHistory.java b/src/main/java/seedu/address/ui/CommandHistory.java index 912dc8bc999..d6652e81663 100644 --- a/src/main/java/seedu/address/ui/CommandHistory.java +++ b/src/main/java/seedu/address/ui/CommandHistory.java @@ -1,7 +1,6 @@ package seedu.address.ui; import java.util.ArrayList; - /** * Container for previous commands */ @@ -9,11 +8,17 @@ public class CommandHistory { private ArrayList commandHistory = new ArrayList<>(); private int currentHistoryIndex = -1; + /** + * Adds a command to the ArrayList + */ public void addCommand(String command) { commandHistory.add(command); currentHistoryIndex = commandHistory.size(); // Reset index to point after the latest command } + /** + * Retrieves the previous command from the ArrayList, outputs null if already at the front of the arraylist + */ public String getPreviousCommand() { if (currentHistoryIndex > 0) { currentHistoryIndex--; @@ -22,6 +27,9 @@ public String getPreviousCommand() { return null; // No previous command } + /** + * Retrieves the next command from the ArrayList, outputs null if already at the end of the arraylist + */ public String getNextCommand() { if (currentHistoryIndex < commandHistory.size() - 1) { currentHistoryIndex++; @@ -29,4 +37,4 @@ public String getNextCommand() { } return null; // No next command } -} \ No newline at end of file +} diff --git a/src/main/java/seedu/address/ui/MainWindow.java b/src/main/java/seedu/address/ui/MainWindow.java index 79e74ef37c0..c35cdf4db81 100644 --- a/src/main/java/seedu/address/ui/MainWindow.java +++ b/src/main/java/seedu/address/ui/MainWindow.java @@ -121,6 +121,8 @@ void fillInnerParts() { CommandBox commandBox = new CommandBox(this::executeCommand); commandBoxPlaceholder.getChildren().add(commandBox.getRoot()); + + } /** diff --git a/src/test/java/seedu/address/ui/CommandHistoryTest.java b/src/test/java/seedu/address/ui/CommandHistoryTest.java new file mode 100644 index 00000000000..9dabfa14062 --- /dev/null +++ b/src/test/java/seedu/address/ui/CommandHistoryTest.java @@ -0,0 +1,67 @@ +package seedu.address.ui; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class CommandHistoryTest { + + private CommandHistory commandHistory; + + @BeforeEach + public void setUp() { + commandHistory = new CommandHistory(); + } + + @Test + public void testAddCommand() { + commandHistory.addCommand("first command"); + commandHistory.addCommand("second command"); + + // Moving back in history + assertEquals("second command", commandHistory.getPreviousCommand()); + assertEquals("first command", commandHistory.getPreviousCommand()); + + // Should return null as there's no more previous command + assertNull(commandHistory.getPreviousCommand()); + } + + @Test + public void testNextCommand() { + commandHistory.addCommand("first command"); + commandHistory.addCommand("second command"); + + // Go back twice to previous commands + commandHistory.getPreviousCommand(); + commandHistory.getPreviousCommand(); + + // Moving forward in history + assertEquals("second command", commandHistory.getNextCommand()); + assertNull(commandHistory.getNextCommand()); // No further next command after latest + } + + @Test + public void testEmptyHistory() { + // Initially, history should return null when getting previous/next command + assertNull(commandHistory.getPreviousCommand()); + assertNull(commandHistory.getNextCommand()); + } + + @Test + public void testMixedNavigation() { + commandHistory.addCommand("first command"); + commandHistory.addCommand("second command"); + commandHistory.addCommand("third command"); + + // Navigate through commands + assertEquals("third command", commandHistory.getPreviousCommand()); + assertEquals("second command", commandHistory.getPreviousCommand()); + + // Moving forward should bring back "third command" + assertEquals("third command", commandHistory.getNextCommand()); + + // After the latest command, there should be no more next command + assertNull(commandHistory.getNextCommand()); + } +} From 5b1034ec6eb63c58442ecdbd542346c054a99ab3 Mon Sep 17 00:00:00 2001 From: flyingsalsa <107453007+flyingsalsa@users.noreply.github.com> Date: Sat, 19 Oct 2024 01:35:02 +0800 Subject: [PATCH 3/3] fix Checkstyle --- src/test/java/seedu/address/ui/CommandHistoryTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/seedu/address/ui/CommandHistoryTest.java b/src/test/java/seedu/address/ui/CommandHistoryTest.java index 9dabfa14062..f10858a560b 100644 --- a/src/test/java/seedu/address/ui/CommandHistoryTest.java +++ b/src/test/java/seedu/address/ui/CommandHistoryTest.java @@ -1,9 +1,12 @@ package seedu.address.ui; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; + public class CommandHistoryTest {