forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #124 from AY1920S1-CS2103T-W11-2/add-view-command
Add View command and UI to display details of Activity and Person
- Loading branch information
Showing
62 changed files
with
1,511 additions
and
198 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
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
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
98 changes: 98 additions & 0 deletions
98
src/main/java/seedu/address/logic/commands/ViewCommand.java
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,98 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ACTIVITY; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_CONTACT; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.CommandSubType; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Context; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.activity.Activity; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Updates the GUI to list all entries of a specified type to the user. | ||
*/ | ||
public class ViewCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "view"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Switches the current view to show the details of a contact or activity, " | ||
+ "identified by their display index (a positive integer) in the respective list.\n" | ||
+ "Parameters: " + PREFIX_CONTACT + "CONTACT_INDEX OR " + PREFIX_ACTIVITY + "ACTIVITY_INDEX\n" | ||
+ "Example: view " + PREFIX_CONTACT + "1"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Showing the details of %s %s"; | ||
public static final String MESSAGE_UNKNOWN_VIEW_TYPE = "View command has unknown type!"; | ||
|
||
private final Index targetIndex; | ||
private final CommandSubType type; | ||
|
||
public ViewCommand(CommandSubType type, Index targetIndex) { | ||
requireAllNonNull(type, targetIndex); | ||
this.type = type; | ||
this.targetIndex = targetIndex; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
// Contextual behaviour | ||
switch (this.type) { | ||
case CONTACT: | ||
List<Person> listedPersons = model.getFilteredPersonList(); | ||
|
||
if (targetIndex.getOneBased() > listedPersons.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAY_INDEX); | ||
} | ||
|
||
Person personToView = listedPersons.get(targetIndex.getZeroBased()); | ||
Context newContactContext = new Context(personToView); | ||
model.setContext(newContactContext); | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, "contact", personToView.getName()), | ||
newContactContext); | ||
case ACTIVITY: | ||
List<Activity> listedActivities = model.getFilteredActivityList(); | ||
|
||
if (targetIndex.getOneBased() > listedActivities.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_ACTIVITY_DISPLAY_INDEX); | ||
} | ||
|
||
Activity activityToView = listedActivities.get(targetIndex.getZeroBased()); | ||
Context newActivityContext = new Context(activityToView); | ||
model.setContext(newActivityContext); | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, "activity", activityToView.getTitle()), | ||
newActivityContext); | ||
default: | ||
throw new CommandException(MESSAGE_UNKNOWN_VIEW_TYPE); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ViewCommand)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
ViewCommand v = (ViewCommand) other; | ||
return type.equals(v.type) | ||
&& targetIndex.equals(v.targetIndex); | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
src/main/java/seedu/address/logic/parser/ViewCommandParser.java
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,65 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ACTIVITY; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_CONTACT; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.CommandSubType; | ||
import seedu.address.logic.commands.ViewCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new {@code ViewCommand} object | ||
*/ | ||
public class ViewCommandParser implements Parser<ViewCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of a {@code ViewCommand} | ||
* and returns a {@code ViewCommand} object for execution. | ||
* @throws ParseException if the user input does not conform to the expected format, | ||
* or has missing compulsory arguments. | ||
*/ | ||
public ViewCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_CONTACT, PREFIX_ACTIVITY); | ||
|
||
if (!onePrefixPresent(argMultimap, PREFIX_CONTACT, PREFIX_ACTIVITY) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
CommandSubType subType; | ||
String indexString; | ||
|
||
if (argMultimap.getValue(PREFIX_ACTIVITY).isPresent()) { | ||
subType = CommandSubType.ACTIVITY; | ||
indexString = argMultimap.getValue(PREFIX_ACTIVITY).get(); | ||
} else { | ||
assert argMultimap.getValue(PREFIX_CONTACT).isPresent(); | ||
subType = CommandSubType.CONTACT; | ||
indexString = argMultimap.getValue(PREFIX_CONTACT).get(); | ||
} | ||
|
||
try { | ||
Index index = ParserUtil.parseIndex(indexString); | ||
return new ViewCommand(subType, index); | ||
} catch (ParseException pe) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE), pe); | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if exactly one the prefixes contains a non-empty {@code Optional} value in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean onePrefixPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes) | ||
.filter(prefix -> argumentMultimap.getValue(prefix).isPresent()) | ||
.count() == 1; | ||
} | ||
} |
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
Oops, something went wrong.