Skip to content

Commit

Permalink
Merge pull request se-edu#239 from Aquarinte/v1.5
Browse files Browse the repository at this point in the history
Minor code update
  • Loading branch information
Robert-Peng authored Apr 15, 2018
2 parents f6601b6 + c4762d7 commit ca82af3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 89 deletions.
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public interface Logic {
/** Returns a set of all command words. */
Set<String> getAllCommandWords();

/** Returns a set of command words that require prefixes and options in syntax */
Set<String> getCommandWordsWithOptionPrefix();

/** Returns a set of all prefixes. */
Set<String> getAllPrefixes();

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class LogicManager extends ComponentManager implements Logic {

// person details
private Set<String> nricInModel;
private Set<String> phoneNumbersInModel;
private Set<Tag> personTagsInModel;

// pet patient details
Expand Down Expand Up @@ -96,6 +95,11 @@ public Set<String> getAllCommandWords() {
return cliSyntax.getCommandWords();
}

@Override
public Set<String> getCommandWordsWithOptionPrefix() {
return cliSyntax.getCommandWordsWithOptionPrefix();
}

@Override
public Set<String> getAllPrefixes() {
return cliSyntax.getPrefixes();
Expand Down Expand Up @@ -163,12 +167,10 @@ public Set<String> 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());
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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 "
Expand All @@ -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";
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public class CliSyntax {
PREFIX_REMARK_DESC, PREFIX_TAG_DESC)
.collect(Collectors.toSet());

private static final Set<String> 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<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,
Expand All @@ -107,6 +111,10 @@ public Set<String> getCommandWords() {
return CliSyntax.commandWords;
}

public Set<String> getCommandWordsWithOptionPrefix() {
return CliSyntax.commandWordsWithOptionPrefix;
}

public Set<String> getPrefixes() {
return CliSyntax.prefixes;
}
Expand Down
91 changes: 17 additions & 74 deletions src/main/java/seedu/address/ui/Autocomplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -93,7 +88,7 @@ public List<String> getSuggestions(TextField commandTextField) {
return getCommandWordSuggestions();
}

if (!targetWord.equals("")) {
if (!targetWord.equals("") && hasOptionsAndPrefixes()) {
if (hasAddCommandReferNric() || hasEditCommandReferNric() || hasFindCommandReferNric()) {
return getNricSuggestions();
}
Expand Down Expand Up @@ -122,13 +117,11 @@ public List<String> getSuggestions(TextField commandTextField) {
return getTagSuggestions();
}

if (hasOptionsAndPrefixes() && targetWord.startsWith("-")) {
if (targetWord.startsWith("-")) {
return getOptionSuggestions();
}

if (hasOptionsAndPrefixes()) {
return getPrefixSuggestions();
}
return getPrefixSuggestions();

} else {

Expand All @@ -144,35 +137,11 @@ public List<String> 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;
}

/**
Expand Down Expand Up @@ -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 addNewAppointment = Pattern.compile(AddCommand.COMMAND_WORD + " -(a)+(?<apptInfo>.*)"
+ "-(o)+(?<ownerNric>.*)" + "-(p)+(?<petName>.*)");
final Matcher matcherForNewAppt = addNewAppointment.matcher(trimmedCommandInput);

if (trimmedCommandInputArray[trimmedCommandInputArray.length - 3].equals(OPTION_PETPATIENT)
&& targetWord.startsWith(PREFIX_NAME.toString())) {
if (matcherForNewAppt.matches()) {
return true;
}

Expand All @@ -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;
Expand Down Expand Up @@ -535,7 +478,7 @@ private List<String> 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"));
}

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/seedu/address/ui/CommandBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ public class CommandBox extends UiPart<Region> {
private ContextMenu suggestionBox;

private boolean isAutocompleting;
private Autocomplete autocompleteLogic = Autocomplete.getInstance();

private List<String> 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.
Expand Down Expand Up @@ -221,18 +220,18 @@ private void triggerAutocomplete(String newValue) {

if (!newValue.equals("")) {

suggestions = autocompleteLogic.getSuggestions(commandTextField);
List<String> suggestions = autocompleteLogic.getSuggestions(commandTextField);

if (!suggestions.isEmpty()) {
setContextMenu();
setContextMenu(suggestions);
}
}
}

/**
* Sets the context menu {@code suggestionBox} with autocomplete suggestions.
*/
private void setContextMenu() {
private void setContextMenu(List<String> suggestions) {
for (String s : suggestions) {
MenuItem m = new MenuItem(s);
String autocompleteValue = StringUtil.removeDescription(s);
Expand Down

0 comments on commit ca82af3

Please sign in to comment.