Skip to content

Commit

Permalink
Merge pull request AY2324S1-CS2103-F13-2#126 from nixonwidjaja/naviga…
Browse files Browse the repository at this point in the history
…te-input

Gets previous input
  • Loading branch information
nixonwidjaja authored Nov 2, 2023
2 parents 4d52bbe + 1927ff0 commit 6f679c5
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 3 deletions.
6 changes: 4 additions & 2 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ HR Insight is a **desktop app for HR people, optimized for use via a Line Interf

- `exit` : Exits the app.

6. Refer to the [Features](#features) below for details of each command.
6. You can navigate through your previous commands using <button>&uarr;</button> or <button>&darr;</button> on your keyboard, just like your computer's CLI or terminal.

7. Refer to the [Features](#features) below for details of each command.

---

Expand Down Expand Up @@ -293,7 +295,7 @@ Format: `export [file_name]`
- The exported file_name.csv will be found in the Exported_CSVs folder.
- A maximum of one file_name has to be provided. File_name can comprise alphanumeric and special characters.

- Examples:
Examples:
- `list` followed by `export all_data` will download all employees' attributes into a csv file.


Expand Down
113 changes: 113 additions & 0 deletions src/main/java/seedu/address/ui/CommandBox.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,6 +21,7 @@ public class CommandBox extends UiPart<Region> {
private static final String FXML = "CommandBox.fxml";

private final CommandExecutor commandExecutor;
private final InputHistory inputs;

@FXML
private TextField commandTextField;
Expand All @@ -27,6 +32,7 @@ public class CommandBox extends UiPart<Region> {
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());
}
Expand All @@ -42,13 +48,53 @@ private void handleCommandEntered() {
}

try {
inputs.add(commandText);
commandExecutor.execute(commandText);
commandTextField.setText("");
} catch (CommandException | ParseException e) {
setStyleToIndicateCommandFailure();
}
}

/**
* 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.
*/
Expand Down Expand Up @@ -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<String> {
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);
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/view/CommandBox.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<?import javafx.scene.layout.StackPane?>

<StackPane styleClass="stack-pane" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
<TextField fx:id="commandTextField" onAction="#handleCommandEntered" promptText="Enter command here..."/>
<TextField fx:id="commandTextField" onAction="#handleCommandEntered" onKeyPressed="#handleClick" promptText="Enter command here..."/>
</StackPane>

0 comments on commit 6f679c5

Please sign in to comment.