From fe2636b7485128bdb319fe5777abc2174e1523ee Mon Sep 17 00:00:00 2001 From: Jacqueline Cheong Date: Sun, 15 Apr 2018 10:24:58 +0800 Subject: [PATCH 1/2] Minor code update --- src/main/java/seedu/address/logic/Logic.java | 3 + .../seedu/address/logic/LogicManager.java | 8 +- .../address/logic/commands/AddCommand.java | 12 +-- .../seedu/address/logic/parser/CliSyntax.java | 8 ++ .../java/seedu/address/ui/Autocomplete.java | 91 ++++--------------- .../java/seedu/address/ui/CommandBox.java | 11 +-- 6 files changed, 44 insertions(+), 89 deletions(-) diff --git a/src/main/java/seedu/address/logic/Logic.java b/src/main/java/seedu/address/logic/Logic.java index ba2b5c5e5652..633078e03b97 100644 --- a/src/main/java/seedu/address/logic/Logic.java +++ b/src/main/java/seedu/address/logic/Logic.java @@ -47,6 +47,9 @@ public interface Logic { /** Returns a set of all command words. */ Set getAllCommandWords(); + /** Returns a set of command words that require prefixes and options in syntax */ + Set getCommandWordsWithOptionPrefix(); + /** Returns a set of all prefixes. */ Set getAllPrefixes(); diff --git a/src/main/java/seedu/address/logic/LogicManager.java b/src/main/java/seedu/address/logic/LogicManager.java index fff2fccfdc1b..58a6512cd284 100644 --- a/src/main/java/seedu/address/logic/LogicManager.java +++ b/src/main/java/seedu/address/logic/LogicManager.java @@ -35,7 +35,6 @@ public class LogicManager extends ComponentManager implements Logic { // person details private Set nricInModel; - private Set phoneNumbersInModel; private Set personTagsInModel; // pet patient details @@ -96,6 +95,11 @@ public Set getAllCommandWords() { return cliSyntax.getCommandWords(); } + @Override + public Set getCommandWordsWithOptionPrefix() { + return cliSyntax.getCommandWordsWithOptionPrefix(); + } + @Override public Set getAllPrefixes() { return cliSyntax.getPrefixes(); @@ -163,12 +167,10 @@ public Set getAllAppointmentTags() { @Override public void setAttributesForPersonObjects() { nricInModel = new HashSet<>(); - phoneNumbersInModel = new HashSet<>(); personTagsInModel = new HashSet<>(); for (Person p : model.getAddressBook().getPersonList()) { nricInModel.add(p.getNric().toString()); - phoneNumbersInModel.add(p.getPhone().toString()); personTagsInModel.addAll(p.getTags()); } } diff --git a/src/main/java/seedu/address/logic/commands/AddCommand.java b/src/main/java/seedu/address/logic/commands/AddCommand.java index 342794b3834b..5afedb78e6e1 100644 --- a/src/main/java/seedu/address/logic/commands/AddCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddCommand.java @@ -48,18 +48,18 @@ public class AddCommand extends UndoableCommand { + PREFIX_NRIC + "NRIC " + "[" + PREFIX_TAG + "TAG]...\n" + "To add a new pet patient: " - + COMMAND_WORD + " " + OPTION_PETPATIENT + " " + PREFIX_NAME + "NAME " + + COMMAND_WORD + " " + OPTION_PETPATIENT + " " + PREFIX_NAME + "PET_NAME " + PREFIX_SPECIES + "SPECIES " + PREFIX_BREED + "BREED " + PREFIX_COLOUR + "COLOUR " + PREFIX_BLOODTYPE + "BLOOD_TYPE " - + "[" + PREFIX_TAG + "TAG]... " + OPTION_OWNER + " " + PREFIX_NRIC + "OWNER_NRIC\n" + + "[" + PREFIX_TAG + "TAG]... " + OPTION_OWNER + " " + PREFIX_NRIC + "NRIC\n" + "To add a new appointment: " + COMMAND_WORD + " " + OPTION_APPOINTMENT + " " + PREFIX_DATE + "DATE " + PREFIX_REMARK + "REMARK " - + PREFIX_TAG + "TYPE OF APPOINTMENT... " + OPTION_OWNER + " " + PREFIX_NRIC + "OWNER_NRIC " + + PREFIX_TAG + "TYPE OF APPOINTMENT... " + OPTION_OWNER + " " + PREFIX_NRIC + "NRIC " + OPTION_PETPATIENT + " " + PREFIX_NAME + " PET_NAME\n" - + "To add all new: " + COMMAND_WORD + " " + OPTION_OWNER + " " + PREFIX_NAME + "OWNER_NAME " + + "To add all new: " + COMMAND_WORD + " " + OPTION_OWNER + " " + PREFIX_NAME + "NAME " + PREFIX_PHONE + "PHONE " + PREFIX_EMAIL + "EMAIL " + PREFIX_ADDRESS + "ADDRESS " @@ -102,7 +102,7 @@ public class AddCommand extends UndoableCommand { public static final String MESSAGE_ERROR_PETPATIENT = "option " + OPTION_PETPATIENT + "\n" + "Parameters: " - + PREFIX_NAME + "NAME " + + PREFIX_NAME + "PET_NAME " + PREFIX_SPECIES + "SPECIES " + PREFIX_BREED + "BREED " + PREFIX_COLOUR + "COLOUR " @@ -126,7 +126,7 @@ public class AddCommand extends UndoableCommand { + "New pet patient added: %2$s\n" + "New appointment made: %3$s"; public static final String MESSAGE_DUPLICATE_PERSON = "This contact already exists in Medeina."; - public static final String MESSAGE_DUPLICATE_NRIC = "This is already a contact with this NRIC."; + public static final String MESSAGE_DUPLICATE_NRIC = "There is already a contact with this NRIC."; public static final String MESSAGE_DUPLICATE_APPOINTMENT = "This particular appointment already exists in Medeina."; public static final String MESSAGE_DUPLICATE_DATETIME = "This date time is already taken by another appointment."; public static final String MESSAGE_DUPLICATE_PET_PATIENT = "This pet patient already exists in Medeina"; diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index 3abb29930d50..b94dfd91c610 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -83,6 +83,10 @@ public class CliSyntax { PREFIX_REMARK_DESC, PREFIX_TAG_DESC) .collect(Collectors.toSet()); + private static final Set commandWordsWithOptionPrefix = Stream.of( + AddCommand.COMMAND_WORD, EditCommand.COMMAND_WORD, DeleteCommand.COMMAND_WORD, FindCommand.COMMAND_WORD, + ListAppointmentCommand.COMMAND_WORD).collect(Collectors.toSet()); + private static final Set commandWords = Stream.of( AddCommand.COMMAND_WORD, EditCommand.COMMAND_WORD, DeleteCommand.COMMAND_WORD, ListCommand.COMMAND_WORD, FindCommand.COMMAND_WORD, ChangeThemeCommand.COMMAND_WORD, ClearCommand.COMMAND_WORD, @@ -107,6 +111,10 @@ public Set getCommandWords() { return CliSyntax.commandWords; } + public Set getCommandWordsWithOptionPrefix() { + return CliSyntax.commandWordsWithOptionPrefix; + } + public Set getPrefixes() { return CliSyntax.prefixes; } diff --git a/src/main/java/seedu/address/ui/Autocomplete.java b/src/main/java/seedu/address/ui/Autocomplete.java index 4a867415897a..32c720d29ade 100644 --- a/src/main/java/seedu/address/ui/Autocomplete.java +++ b/src/main/java/seedu/address/ui/Autocomplete.java @@ -15,6 +15,8 @@ import java.util.List; import java.util.Set; import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import com.google.common.eventbus.Subscribe; @@ -26,15 +28,8 @@ import seedu.address.commons.util.StringUtil; import seedu.address.logic.Logic; import seedu.address.logic.commands.AddCommand; -import seedu.address.logic.commands.ClearCommand; 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.CliSyntax; //@@author aquarinte @@ -93,7 +88,7 @@ public List getSuggestions(TextField commandTextField) { return getCommandWordSuggestions(); } - if (!targetWord.equals("")) { + if (!targetWord.equals("") && hasOptionsAndPrefixes()) { if (hasAddCommandReferNric() || hasEditCommandReferNric() || hasFindCommandReferNric()) { return getNricSuggestions(); } @@ -122,13 +117,11 @@ public List getSuggestions(TextField commandTextField) { return getTagSuggestions(); } - if (hasOptionsAndPrefixes() && targetWord.startsWith("-")) { + if (targetWord.startsWith("-")) { return getOptionSuggestions(); } - if (hasOptionsAndPrefixes()) { - return getPrefixSuggestions(); - } + return getPrefixSuggestions(); } else { @@ -144,35 +137,11 @@ public List getSuggestions(TextField commandTextField) { * Returns false if the command is one that does not require any options or prefixes in its syntax. */ private boolean hasOptionsAndPrefixes() { - if (commandWord.equals(ClearCommand.COMMAND_WORD)) { - return false; - } - - if (commandWord.equals(ListCommand.COMMAND_WORD)) { - return false; - } - - if (commandWord.equals(ExitCommand.COMMAND_WORD)) { - return false; - } - - if (commandWord.equals(UndoCommand.COMMAND_WORD)) { - return false; - } - - if (commandWord.equals(RedoCommand.COMMAND_WORD)) { - return false; - } - - if (commandWord.equals(HelpCommand.COMMAND_WORD)) { - return false; - } - - if (commandWord.equals(HistoryCommand.COMMAND_WORD)) { - return false; + if (logic.getCommandWordsWithOptionPrefix().contains(commandWord)) { + return true; } - return true; + return false; } /** @@ -226,40 +195,14 @@ private boolean hasFindCommandReferNric() { } /** - * Checks if command input {@code trimmedCommandInputArray} has reference to existing pet patients' names, and - * determine if autocomplete for pet patient names is necessary. - * - * Returns false if it is an add new pet patient command. - * Returns false if it is an add new owner, new pet patient & new appointment command. - * Returns false if it is an edit command. - * Returns false if it is a find command. + *Returns true if command input {@code trimmedCommandInput} is the syntax for adding a new appointment. */ private boolean hasReferenceToExistingPetPatientNames() { - // Add new pet patient - if (commandWord.equals(AddCommand.COMMAND_WORD) - && trimmedCommandInputArray[2].equals(OPTION_PETPATIENT) - && targetWord.startsWith(PREFIX_NAME.toString())) { - return false; - } - - // Add new owner, new pet patient & new appointment - if (commandWord.equals(AddCommand.COMMAND_WORD) - && trimmedCommandInputArray[2].equals(OPTION_OWNER) - && trimmedCommandInputArray[trimmedCommandInputArray.length - 3].equals(OPTION_PETPATIENT) - && targetWord.startsWith(PREFIX_NAME.toString())) { - return false; - } - - if (commandWord.equals(EditCommand.COMMAND_WORD)) { - return false; - } - - if (commandWord.equals(FindCommand.COMMAND_WORD)) { - return false; - } + final Pattern ADD_NEW_APPOINTMENT = Pattern.compile(AddCommand.COMMAND_WORD + " -(a)+(?.*)" + + "-(o)+(?.*)" + "-(p)+(?.*)"); + final Matcher matcherForNewAppt = ADD_NEW_APPOINTMENT.matcher(trimmedCommandInput); - if (trimmedCommandInputArray[trimmedCommandInputArray.length - 3].equals(OPTION_PETPATIENT) - && targetWord.startsWith(PREFIX_NAME.toString())) { + if (matcherForNewAppt.matches()) { return true; } @@ -272,9 +215,9 @@ private boolean hasReferenceToExistingPetPatientNames() { private void setOption() { option = "nil"; - int o = trimmedCommandInput.lastIndexOf("-o"); - int p = trimmedCommandInput.lastIndexOf("-p"); - int a = trimmedCommandInput.lastIndexOf("-a"); + int o = trimmedCommandInput.lastIndexOf(OPTION_OWNER); + int p = trimmedCommandInput.lastIndexOf(OPTION_PETPATIENT); + int a = trimmedCommandInput.lastIndexOf(OPTION_APPOINTMENT); int index = (a > p) ? a : p; index = (index > o) ? index : o; @@ -535,7 +478,7 @@ private List getCommandWordSuggestions() { @Subscribe public void handleAddressBookChangedEvent(AddressBookChangedEvent a) { init(this.logic); - logger.info(LogsCenter.getEventHandlingLogMessage(a, "Local data changed," + logger.info(LogsCenter.getEventHandlingLogMessage(a, "Local data has changed," + " update autocomplete data")); } diff --git a/src/main/java/seedu/address/ui/CommandBox.java b/src/main/java/seedu/address/ui/CommandBox.java index 6832c9817175..ba9ce9654572 100644 --- a/src/main/java/seedu/address/ui/CommandBox.java +++ b/src/main/java/seedu/address/ui/CommandBox.java @@ -43,15 +43,14 @@ public class CommandBox extends UiPart { private ContextMenu suggestionBox; private boolean isAutocompleting; - private Autocomplete autocompleteLogic = Autocomplete.getInstance(); - - private List suggestions; + private Autocomplete autocompleteLogic; public CommandBox(Logic logic) { super(FXML); this.logic = logic; suggestionBox = new ContextMenu(); commandTextField.setContextMenu(suggestionBox); + autocompleteLogic = Autocomplete.getInstance(); autocompleteLogic.init(logic); historySnapshot = logic.getHistorySnapshot(); // calls #setStyleToDefault() whenever there is a change to the text of the command box. @@ -221,10 +220,10 @@ private void triggerAutocomplete(String newValue) { if (!newValue.equals("")) { - suggestions = autocompleteLogic.getSuggestions(commandTextField); + List suggestions = autocompleteLogic.getSuggestions(commandTextField); if (!suggestions.isEmpty()) { - setContextMenu(); + setContextMenu(suggestions); } } } @@ -232,7 +231,7 @@ private void triggerAutocomplete(String newValue) { /** * Sets the context menu {@code suggestionBox} with autocomplete suggestions. */ - private void setContextMenu() { + private void setContextMenu(List suggestions) { for (String s : suggestions) { MenuItem m = new MenuItem(s); String autocompleteValue = StringUtil.removeDescription(s); From c4762d7fe442adfadbbd3868065f6fb244f22f5e Mon Sep 17 00:00:00 2001 From: Jacqueline Cheong Date: Sun, 15 Apr 2018 10:33:17 +0800 Subject: [PATCH 2/2] Fix checkstyle --- src/main/java/seedu/address/ui/Autocomplete.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/ui/Autocomplete.java b/src/main/java/seedu/address/ui/Autocomplete.java index 32c720d29ade..79ddca8618bb 100644 --- a/src/main/java/seedu/address/ui/Autocomplete.java +++ b/src/main/java/seedu/address/ui/Autocomplete.java @@ -198,9 +198,9 @@ private boolean hasFindCommandReferNric() { *Returns true if command input {@code trimmedCommandInput} is the syntax for adding a new appointment. */ private boolean hasReferenceToExistingPetPatientNames() { - final Pattern ADD_NEW_APPOINTMENT = Pattern.compile(AddCommand.COMMAND_WORD + " -(a)+(?.*)" + final Pattern addNewAppointment = Pattern.compile(AddCommand.COMMAND_WORD + " -(a)+(?.*)" + "-(o)+(?.*)" + "-(p)+(?.*)"); - final Matcher matcherForNewAppt = ADD_NEW_APPOINTMENT.matcher(trimmedCommandInput); + final Matcher matcherForNewAppt = addNewAppointment.matcher(trimmedCommandInput); if (matcherForNewAppt.matches()) { return true;