Skip to content

Commit

Permalink
Merge 7c4fb33 into 6116d3a
Browse files Browse the repository at this point in the history
  • Loading branch information
shitian007 committed Oct 19, 2017
2 parents 6116d3a + 7c4fb33 commit d1ee8d3
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
89 changes: 89 additions & 0 deletions src/main/java/seedu/address/logic/AutoComplete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package seedu.address.logic;

import java.util.ArrayList;

import seedu.address.model.Model;
import seedu.address.model.person.ReadOnlyPerson;


/**
* AutoComplete class integrated into LogicManager to keep track of current set
* of autocomplete suggestions
*/
public class AutoComplete {

private final String[] baseCommands = { "add", "edit", "select", "delete", "clear",
"backup", "find", "list", "history", "exit", "help", "undo", "redo"
};
private ArrayList<String> personsStringArray;
private String[] autoCompleteList;
private Model model;

public AutoComplete(Model model) {
this.model = model;
autoCompleteList = baseCommands;
personsStringArray = new ArrayList<String>();
this.updatePersonsArray();
}

/**
* Updates AutoComplete suggestions according to user typed input
* @param userInput
*/
public void updateAutoCompleteList(String userInput) {
switch (userInput) {
case "":
this.resetAutocompleteList();
break;
case "find":
this.autoCompleteList = getConcatPersonsArray("find");
break;
case "edit":
this.autoCompleteList = getConcatPersonsArray("edit");
break;
case "delete":
this.autoCompleteList = getConcatPersonsArray("delete");
break;
default:
return;
}
}

// Concatenate Persons to suggestions when command typed
private String[] getConcatPersonsArray(String command) {
String[] newAutoCompleteList = new String[personsStringArray.size()];
for (int i = 0; i < personsStringArray.size(); i++) {
if (command.equals("find")) {
newAutoCompleteList[i] = command + " " + personsStringArray.get(i);
} else {
newAutoCompleteList[i] = command + " " + (i + 1);
}
}
return newAutoCompleteList;
}

/**
* Reset autocomplete suggestions to base commands
*/
public void resetAutocompleteList() {
this.autoCompleteList = baseCommands;
}

/**
* Update array of persons suggestions when list modified
*/
public void updatePersonsArray() {
personsStringArray.clear();
for (ReadOnlyPerson p: model.getFilteredPersonList()) {
personsStringArray.add(p.getName().toString());
}
}

/**
* Getter for auto-complete list suggestions
*/
// Update array of persons suggestions when list modified
public String[] getAutoCompleteList() {
return autoCompleteList;
}
}
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public interface Logic {
/** Returns an unmodifiable view of the filtered list of persons */
ObservableList<ReadOnlyPerson> getFilteredPersonList();

/** Updates and gets list of Auto-complete Strings */
void updateAutoCompleteList(String userInput);
String[] getAutoCompleteList();


/** Returns the list of input entered by the user, encapsulated in a {@code ListElementPointer} object */
ListElementPointer getHistorySnapshot();
}
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ public class LogicManager extends ComponentManager implements Logic {
private final CommandHistory history;
private final AddressBookParser addressBookParser;
private final UndoRedoStack undoRedoStack;
private AutoComplete autoCompleteList;

public LogicManager(Model model) {
this.model = model;
this.history = new CommandHistory();
this.addressBookParser = new AddressBookParser();
this.undoRedoStack = new UndoRedoStack();
this.autoCompleteList = new AutoComplete(model);
}

@Override
public void updateAutoCompleteList(String userInput) {
autoCompleteList.updateAutoCompleteList(userInput);
}

@Override
public String[] getAutoCompleteList() {
return autoCompleteList.getAutoCompleteList();
}

@Override
Expand All @@ -39,6 +51,8 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
command.setData(model, history, undoRedoStack);
CommandResult result = command.execute();
undoRedoStack.push(command);
autoCompleteList.resetAutocompleteList();
autoCompleteList.updatePersonsArray();
return result;
} finally {
history.add(commandText);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public CommandResult(String feedbackToUser) {
this.feedbackToUser = requireNonNull(feedbackToUser);
}

public CommandResult(String firstFeedBackToUser, String SecondFeedBackToUser) {
this.feedbackToUser = requireNonNull(firstFeedBackToUser + "\n" + SecondFeedBackToUser);
public CommandResult(String firstFeedBackToUser, String secondFeedBackToUser) {
this.feedbackToUser = requireNonNull(firstFeedBackToUser + "\n" + secondFeedBackToUser);
}
}
12 changes: 11 additions & 1 deletion src/main/java/seedu/address/ui/CommandBox.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package seedu.address.ui;

import java.util.Arrays;
import java.util.logging.Logger;

import org.controlsfx.control.textfield.TextFields;

import impl.org.controlsfx.autocompletion.SuggestionProvider;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
Expand Down Expand Up @@ -29,13 +33,16 @@ public class CommandBox extends UiPart<Region> {

@FXML
private TextField commandTextField;
private SuggestionProvider suggestions;

public CommandBox(Logic logic) {
super(FXML);
this.logic = logic;
// calls #setStyleToDefault() whenever there is a change to the text of the command box.
commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
historySnapshot = logic.getHistorySnapshot();
suggestions = SuggestionProvider.create((Arrays.asList(logic.getAutoCompleteList())));
TextFields.bindAutoCompletion(commandTextField, suggestions);
}

/**
Expand All @@ -56,7 +63,10 @@ private void handleKeyPress(KeyEvent keyEvent) {
navigateToNextInput();
break;
default:
// let JavaFx handle the keypress
// Update textfield autocomplete options
logic.updateAutoCompleteList(commandTextField.getText());
suggestions.clearSuggestions();
suggestions.addPossibleSuggestions(Arrays.asList(logic.getAutoCompleteList()));
}
}

Expand Down

0 comments on commit d1ee8d3

Please sign in to comment.