Skip to content

Commit

Permalink
Merge f6e56ec into 7ab351e
Browse files Browse the repository at this point in the history
  • Loading branch information
tztzt committed Nov 8, 2018
2 parents 7ab351e + f6e56ec commit 090e3e9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 43 deletions.
36 changes: 26 additions & 10 deletions src/main/java/seedu/planner/ui/CustomSuggestionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
import impl.org.controlsfx.autocompletion.SuggestionProvider;
import seedu.planner.logic.commands.AddCommand;
import seedu.planner.logic.commands.AddLimitCommand;
import seedu.planner.logic.commands.AddMonthlyLimitCommand;
import seedu.planner.logic.commands.ArchiveCommand;
import seedu.planner.logic.commands.CheckLimitCommand;
import seedu.planner.logic.commands.ClearCommand;
import seedu.planner.logic.commands.DeleteByDateCommand;
import seedu.planner.logic.commands.DeleteCommand;
import seedu.planner.logic.commands.DeleteLimitCommand;
import seedu.planner.logic.commands.DeleteMonthlyLimitCommand;
import seedu.planner.logic.commands.EditCommand;
import seedu.planner.logic.commands.EditLimitCommand;
import seedu.planner.logic.commands.EditMonthlyLimitCommand;
import seedu.planner.logic.commands.ExitCommand;
import seedu.planner.logic.commands.ExportExcelCommand;
import seedu.planner.logic.commands.FindCommand;
Expand Down Expand Up @@ -50,10 +53,12 @@ public class CustomSuggestionProvider {
private static final Set<String> commandKeywordsSet =
new HashSet<>(Arrays.asList(
AddCommand.COMMAND_WORD, AddLimitCommand.COMMAND_WORD,
ArchiveCommand.COMMAND_WORD, CheckLimitCommand.COMMAND_WORD,
ClearCommand.COMMAND_WORD, DeleteByDateCommand.COMMAND_WORD,
DeleteCommand.COMMAND_WORD, DeleteLimitCommand.COMMAND_WORD,
AddMonthlyLimitCommand.COMMAND_WORD, ArchiveCommand.COMMAND_WORD,
CheckLimitCommand.COMMAND_WORD, ClearCommand.COMMAND_WORD,
DeleteByDateCommand.COMMAND_WORD, DeleteCommand.COMMAND_WORD,
DeleteLimitCommand.COMMAND_WORD, DeleteMonthlyLimitCommand.COMMAND_WORD,
EditCommand.COMMAND_WORD, EditLimitCommand.COMMAND_WORD,
EditMonthlyLimitCommand.COMMAND_WORD,
ExitCommand.COMMAND_WORD, ExportExcelCommand.COMMAND_WORD,
FindCommand.COMMAND_WORD, FindTagCommand.COMMAND_WORD,
HelpCommand.COMMAND_WORD, HistoryCommand.COMMAND_WORD,
Expand Down Expand Up @@ -86,6 +91,13 @@ public SuggestionProvider<String> getSuggestions() {
return suggestionProvider;
}

/**
* Clears the set of suggestions
*/
private void emptySuggestions() {
suggestionProvider.clearSuggestions();
}

/**
* Clears the current set of suggestions and insert the new set of suggestions
* @param suggestions is the complete list of strings that can be suggested to the user
Expand All @@ -108,7 +120,11 @@ public void updateSuggestions(String userInput, String prefix, String wordInput)
for (; strIndex < inputs.length && !inputs[strIndex].equals(wordInput); strIndex++);

if (strIndex == 0) {
updateSuggestions(commandKeywordsSet);
if (!commandKeywordsSet.contains(inputs[strIndex])) {
updateSuggestions(commandKeywordsSet);
} else {
emptySuggestions();
}
} else if (inputs[0].equals(SortCommand.COMMAND_WORD)) {
if (inputs.length == 2) {
updateSuggestions(sortKeywordsSet);
Expand All @@ -127,28 +143,28 @@ public void updateSuggestions(String userInput, String prefix, String wordInput)
} else if (SortCommand.CATEGORY_SET.contains(inputs[1])) {
updateSuggestions(SortCommand.ORDER_SET);
} else {
updateSuggestions(emptySet);
emptySuggestions();
}
}
} else {
updateSuggestions(emptySet);
emptySuggestions();
}
} else if (possibleTagKeywordsSet.contains(inputs[0])) {
if (wordInput.startsWith(PREFIX_TAG.getPrefix())) {
updateSuggestions(tagsSuggestionSet);
} else {
updateSuggestions(emptySet);
emptySuggestions();
}
} else if (inputs[0].equals(SummaryCommand.COMMAND_WORD)) {
if (inputs.length == 2) {
if (strIndex == 1) {
updateSuggestions(summaryKeywordsSet);
} else {
updateSuggestions(emptySet);
emptySuggestions();
}
} else if (inputs[0].equals(FindTagCommand.COMMAND_WORD)) {
updateSuggestions(tagsSuggestionSet);
} else {
updateSuggestions(emptySet);
emptySuggestions();
}
}

Expand Down
97 changes: 64 additions & 33 deletions src/main/java/seedu/planner/ui/NewAutoCompletionBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

/**
* This object creates the binding for the auto complete popup to the commandBox
* and handles whenever there is a change in the user input by updating the suggestions
* and inserts the new suggested text when selected by the user.
* and updates the list of suggestions for the commandbox
*/
public class NewAutoCompletionBinding<T> {

Expand All @@ -37,51 +36,28 @@ public class NewAutoCompletionBinding<T> {
private String suffixText = "";
private final StringConverter<T> converter = defaultStringConverter();

/** This listeners observes the position of the caret in the command box and updates the list
* of suggestions when there is a change detected. It passes the new input word to the other
* methods to compare and match for suggestions.
/**
* This listener observes the position of the caret in the command box and passed different String
* arguments to allow for accurate text suggestions.
*/
private final ChangeListener<Number> caretChangedListener = (obs, oldPosition, newPosition) -> {
String newText = getCompletionTarget().getText();
String inputText;
String prefix = "";
int startIndex = newPosition.intValue();
int endIndex = newPosition.intValue();
if (newPosition.intValue() == newText.length()) {
suffixText = "";
if (newPosition.intValue() > 0) {
startIndex--;
for (; startIndex > 0 && !Character.isWhitespace(newText.charAt(startIndex)); startIndex--);
}
inputText = newText.substring((startIndex == 0) ? startIndex : startIndex + 1);
prefixText = newText.substring(0, (startIndex == 0) ? startIndex : startIndex + 1);
inputText = caretAtEnd(newPosition, newText);
} else {
if (newPosition.intValue() == 0) {
prefixText = ""; // necessary to reset parameters
suffixText = "";
inputText = "";
inputText = resetTexts();
} else {
if (Character.isWhitespace(newText.charAt(newPosition.intValue()))
&& Character.isWhitespace(newText.charAt(newPosition.intValue() - 1))) {
prefixText = "";
suffixText = "";
inputText = "";
inputText = resetTexts();
} else {
if (Character.isWhitespace(newText.charAt(newPosition.intValue()))) {
startIndex--;
for (; startIndex > 0 && !Character.isWhitespace(newText.charAt(startIndex)); startIndex--);
prefixText = (startIndex == 0) ? "" : newText.substring(0, startIndex + 1);

for (; endIndex < newText.length()
&& !Character.isWhitespace(newText.charAt(endIndex)); endIndex++);
suffixText = (endIndex == newText.length()) ? "" : newText.substring(endIndex);

inputText = newText.substring((startIndex == 0) ? startIndex : startIndex + 1, (
endIndex == newText.length()) ? endIndex : endIndex);
inputText = caretInBetween(newPosition, newText);
} else {
prefixText = "";
suffixText = "";
inputText = "";
inputText = resetTexts();
}
}
}
Expand Down Expand Up @@ -136,6 +112,61 @@ public NewAutoCompletionBinding(Node completionTarget) {
init();
}

/**
* Splits the input text accordingly when the caret is found to be at the end of the command box
* @param newPosition is the current position of the caret
* @param newText is the entire input in the command box
* @return
*/
private String caretAtEnd(Number newPosition, String newText) {
int startIndex = newPosition.intValue();
String inputText;
suffixText = "";
if (newPosition.intValue() > 0) {
startIndex--;
for (; startIndex > 0 && !Character.isWhitespace(newText.charAt(startIndex)); startIndex--);
}
inputText = newText.substring((startIndex == 0) ? startIndex : startIndex + 1);
prefixText = newText.substring(0, (startIndex == 0) ? startIndex : startIndex + 1);
return inputText;
}

/**
* Splits the input text when the caret is found in the middle of the string in the command box
* and is found at the end of a word.
* @param newText
* @param newPosition
* @return
*/
private String caretInBetween(Number newPosition, String newText) {
int startIndex = newPosition.intValue();
int endIndex = newPosition.intValue();
String inputText;
startIndex--;
for (; startIndex > 0 && !Character.isWhitespace(newText.charAt(startIndex)); startIndex--);
prefixText = (startIndex == 0) ? "" : newText.substring(0, startIndex + 1);

for (; endIndex < newText.length()
&& !Character.isWhitespace(newText.charAt(endIndex)); endIndex++);
suffixText = (endIndex == newText.length()) ? "" : newText.substring(endIndex);

inputText = newText.substring((startIndex == 0) ? startIndex : startIndex + 1, (
endIndex == newText.length()) ? endIndex : endIndex);
return inputText;
}

/**
* Reset the text Strings to an empty String when there is no word to autocomplete
* @return the Word to be autocompleted
*/
private String resetTexts() {
String inputText;
prefixText = "";
suffixText = "";
inputText = "";
return inputText;
}

/**
* String converter to be used to convert suggestions to strings.
*/
Expand Down

0 comments on commit 090e3e9

Please sign in to comment.