diff --git a/src/main/java/seedu/address/logic/commands/AutoCompleteCommandHelper.java b/src/main/java/seedu/address/logic/commands/AutoCompleteCommandHelper.java new file mode 100644 index 000000000000..eef9eb7c40bd --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/AutoCompleteCommandHelper.java @@ -0,0 +1,35 @@ +package seedu.address.logic.commands; + +/** + * Helper class to auto complete commands typed into command box + */ +public class AutoCompleteCommandHelper { + private static String[] commandWordList = { + AddCommand.COMMAND_WORD, + ClearCommand.COMMAND_WORD, + DeleteCommand.COMMAND_WORD, + EditCommand.COMMAND_WORD, + ExitCommand.COMMAND_WORD, + FindCommand.COMMAND_WORD, + HelpCommand.COMMAND_WORD, + HistoryCommand.COMMAND_WORD, + ListCommand.COMMAND_WORD, + RedoCommand.COMMAND_WORD, + SelectCommand.COMMAND_WORD, + UndoCommand.COMMAND_WORD + }; + + /** + * This method predicts the command the user is entering. + * @param partialWord The current characters available in command box. + * @return The predicted command. + */ + public static String autoCompleteWord(String partialWord) { + for (String s : commandWordList) { + if (s.startsWith(partialWord)) { + return s; + } + } + return null; + } +} diff --git a/src/main/java/seedu/address/ui/CommandBox.java b/src/main/java/seedu/address/ui/CommandBox.java index 3d7aaded5640..2760b983430a 100644 --- a/src/main/java/seedu/address/ui/CommandBox.java +++ b/src/main/java/seedu/address/ui/CommandBox.java @@ -2,6 +2,7 @@ import java.util.logging.Logger; +import javafx.application.Platform; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.TextField; @@ -11,6 +12,7 @@ import seedu.address.commons.events.ui.NewResultAvailableEvent; import seedu.address.logic.ListElementPointer; import seedu.address.logic.Logic; +import seedu.address.logic.commands.AutoCompleteCommandHelper; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.logic.parser.exceptions.ParseException; @@ -33,6 +35,16 @@ public class CommandBox extends UiPart { public CommandBox(Logic logic) { super(FXML); this.logic = logic; + + //Calls #autoCompleteWord() when new character is entered to text of command box + commandTextField.textProperty().addListener((observable, oldValue, newValue) -> { + //Only perform auto complete if adding new characters. + int oldValueLengthWithoutAutoComplete = oldValue.length() - commandTextField.getSelection().getLength(); + if (oldValueLengthWithoutAutoComplete < newValue.length()) { + autoCompleteWord(); + } + }); + // calls #setStyleToDefault() whenever there is a change to the text of the command box. commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault()); historySnapshot = logic.getHistorySnapshot(); @@ -148,4 +160,23 @@ private void setStyleToIndicateCommandFailure() { styleClass.add(ERROR_STYLE_CLASS); } + /** + * Predict command user is inputting and type it in the command box. + */ + private void autoCompleteWord() { + String prefix = commandTextField.getCharacters().toString(); + String completeWord = AutoCompleteCommandHelper.autoCompleteWord(prefix); + //Return if there is no difference between prefix and completed word. + if (prefix.equals(completeWord)) { + return; + } + + if (completeWord != null && prefix.length() > 0) { + //String strToInsert = completeWord.substring(prefix.length()); + commandTextField.setText(completeWord); + Platform.runLater(() -> commandTextField.selectRange(prefix.length(), completeWord.length())); + } + } + + }