Skip to content

Commit

Permalink
Merge pull request #11 from KokJianYu/Intellisense
Browse files Browse the repository at this point in the history
Auto complete functionality for commands
  • Loading branch information
elliottan authored Oct 3, 2018
2 parents d917127 + 97877fa commit 0bbddb2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
31 changes: 31 additions & 0 deletions src/main/java/seedu/address/ui/CommandBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -33,6 +35,16 @@ public class CommandBox extends UiPart<Region> {
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();
Expand Down Expand Up @@ -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()));
}
}


}

0 comments on commit 0bbddb2

Please sign in to comment.