forked from nus-cs2103-AY1718S2/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nus-cs2103-AY1718S2#99 from Aquarinte/v1.3
Add support for basic autocomplete
- Loading branch information
Showing
10 changed files
with
434 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package seedu.address.logic; | ||
|
||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_BLOODTYPE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_BREED; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_COLOUR; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SPECIES; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.AddCommand; | ||
import seedu.address.logic.commands.ChangeThemeCommand; | ||
import seedu.address.logic.commands.ClearCommand; | ||
import seedu.address.logic.commands.DeleteCommand; | ||
import seedu.address.logic.commands.EditCommand; | ||
import seedu.address.logic.commands.ExitCommand; | ||
import seedu.address.logic.commands.FindCommand; | ||
import seedu.address.logic.commands.HelpCommand; | ||
import seedu.address.logic.commands.HistoryCommand; | ||
import seedu.address.logic.commands.ListCommand; | ||
import seedu.address.logic.commands.RedoCommand; | ||
import seedu.address.logic.commands.UndoCommand; | ||
import seedu.address.logic.parser.Prefix; | ||
|
||
//@@author aquarinte | ||
/** | ||
* Stores all command syntax used in Medeina: command words, prefixes and options. | ||
*/ | ||
public class CommandSyntaxWords { | ||
|
||
private static CommandSyntaxWords instance; | ||
|
||
private static final Set<String> commandWords = Stream.of( | ||
AddCommand.COMMAND_WORD, EditCommand.COMMAND_WORD, DeleteCommand.COMMAND_WORD, ListCommand.COMMAND_WORD, | ||
FindCommand.COMMAND_WORD, ChangeThemeCommand.COMMAND_WORD, ClearCommand.COMMAND_WORD, | ||
HelpCommand.COMMAND_WORD, ExitCommand.COMMAND_WORD, RedoCommand.COMMAND_WORD, UndoCommand.COMMAND_WORD, | ||
HistoryCommand.COMMAND_WORD).collect(Collectors.toSet()); | ||
|
||
private static final Set<Prefix> prefixes = Stream.of( | ||
PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_NRIC, PREFIX_BREED, PREFIX_SPECIES, | ||
PREFIX_COLOUR, PREFIX_BLOODTYPE, PREFIX_DATE, PREFIX_REMARK, PREFIX_TAG).collect(Collectors.toSet()); | ||
|
||
private static final Set<String> options = Stream.of("-o", "-p", "-a").collect(Collectors.toSet()); | ||
|
||
public static CommandSyntaxWords getInstance() { | ||
if (instance == null) { | ||
instance = new CommandSyntaxWords(); | ||
} | ||
return instance; | ||
} | ||
|
||
public Set<String> getCommandWords() { | ||
return commandWords; | ||
} | ||
|
||
public Set<Prefix> getPrefixes() { | ||
return prefixes; | ||
} | ||
|
||
public Set<String> getOptions() { | ||
return options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package seedu.address.ui; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
//import java.util.regex.Pattern; | ||
|
||
import javafx.scene.control.TextField; | ||
import seedu.address.logic.Logic; | ||
import seedu.address.logic.parser.Prefix; | ||
|
||
//@@author aquarinte | ||
/** | ||
* Handles autocompletion of command input such as command word, options and prefixes. | ||
*/ | ||
public class Autocomplete { | ||
|
||
/*private static final Pattern ADD_COMMAND1 = Pattern.compile("add -(a)+(?<apptInfo>.*)-(o)(?<ownerNric>.*)" | ||
+ "-(p)+(?<petName>.*)"); | ||
private static final Pattern ADD_COMMAND2 = Pattern.compile("add -(p)+(?<petInfo>.*)-(o)+(?<ownerNric>.*)"); | ||
private static final Pattern ADD_COMMAND3 = Pattern.compile("add -(o)+(?<ownerInfo>.*)-(p)+(?<petInfo>.*)" | ||
+ "-(a)+(?<apptInfo>.*)"); | ||
private static final Pattern ADD_COMMAND4 = Pattern.compile("add -(o)+(?<ownerInfo>.*)"); | ||
*/ | ||
private static Autocomplete instance; | ||
private Set<String> suggestions; | ||
|
||
public static Autocomplete getInstance() { | ||
if (instance == null) { | ||
instance = new Autocomplete(); | ||
} | ||
return instance; | ||
} | ||
|
||
/** | ||
* Find suggestions for current user-input in commandTextField. | ||
*/ | ||
public Set<String> getSuggestions(Logic logic, TextField commandTextField) { | ||
suggestions = new HashSet<>(); | ||
String userInput = commandTextField.getText().trim(); | ||
String[] words = userInput.split(" "); | ||
//for (String s : words) { | ||
//System.out.println("Word: " + s); | ||
//} | ||
//The string to auto-complete: | ||
String target = words[words.length - 1]; | ||
|
||
if (words.length == 1) { | ||
suggestCommandWords(logic, words[0]); | ||
} else { | ||
|
||
if (words[words.length - 2].equals("-o") && target.equals("nr/")) { | ||
for (String nric : logic.getAllNric()) { | ||
suggestions.add(nric); | ||
} | ||
} | ||
|
||
if (words[words.length - 2].equals("-p") && target.equals("n/")) { | ||
for (String petName : logic.getAllPetPatientNames()) { | ||
suggestions.add(petName); | ||
} | ||
} | ||
suggestOptions(logic, target); | ||
suggestPrefixes(logic, target); | ||
} | ||
return suggestions; | ||
} | ||
|
||
/** | ||
* Find suggestions for prefixes. | ||
*/ | ||
private void suggestPrefixes(Logic logic, String target) { | ||
for (Prefix p : logic.getAllPrefixes()) { | ||
if (p.toString().startsWith(target) && !p.toString().equals(target)) { | ||
suggestions.add(p.toString()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Find suggestions for options. | ||
*/ | ||
private void suggestOptions(Logic logic, String target) { | ||
for (String option : logic.getAllOptions()) { | ||
if (option.startsWith(target) && !option.equals(target)) { | ||
suggestions.add(option); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Find suggestions for command word. | ||
*/ | ||
private void suggestCommandWords(Logic logic, String word) { | ||
for (String command : logic.getAllCommandWords()) { | ||
if (command.startsWith(word) && !command.equals(word)) { | ||
suggestions.add(command); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.