Skip to content

Commit

Permalink
Add ViewCommand and ViewExitCommand
Browse files Browse the repository at this point in the history
Add ViewCommand and ViewExitCommand for enhancements of editing functionality.
  • Loading branch information
brandon-nam committed Oct 13, 2023
1 parent 54c428d commit e4b07ff
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 5 deletions.
28 changes: 26 additions & 2 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.ViewCommand;
import seedu.address.logic.commands.ViewExitCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.AddressBookParser;
import seedu.address.logic.parser.ViewModeParser;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
Expand All @@ -26,12 +29,17 @@ public class LogicManager implements Logic {

public static final String FILE_OPS_PERMISSION_ERROR_FORMAT =
"Could not save data to file %s due to insufficient permissions to write to the file or the folder.";
public static final String WINDOW_IN_VIEW_MODE_ERROR =
"You cannot run other commands while in view profile mode.";

private final Logger logger = LogsCenter.getLogger(LogicManager.class);

private final Model model;
private final Storage storage;
private final AddressBookParser addressBookParser;
private final ViewModeParser viewModeParser;

private boolean isInViewMode = false;

/**
* Constructs a {@code LogicManager} with the given {@code Model} and {@code Storage}.
Expand All @@ -40,16 +48,32 @@ public LogicManager(Model model, Storage storage) {
this.model = model;
this.storage = storage;
addressBookParser = new AddressBookParser();
viewModeParser = new ViewModeParser();
}

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
logger.info("----------------[USER COMMAND][" + commandText + "]");

Command command;
CommandResult commandResult;
Command command = addressBookParser.parseCommand(commandText);

if (!isInViewMode) {
command = addressBookParser.parseCommand(commandText);

} else {
command = viewModeParser.parseCommand(commandText);
}

commandResult = command.execute(model);

if (command instanceof ViewCommand) {
isInViewMode = true;
}

if (command instanceof ViewExitCommand) {
isInViewMode = false;
}

try {
storage.saveAddressBook(model.getAddressBook());
} catch (AccessDeniedException e) {
Expand Down
38 changes: 35 additions & 3 deletions src/main/java/seedu/address/logic/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import java.util.Objects;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.person.Person;

/**
* Represents the result of a command execution.
Expand All @@ -19,13 +21,32 @@ public class CommandResult {
/** The application should exit. */
private final boolean exit;

private final boolean showView;

private final Person personToView;

private final boolean viewExit;

public CommandResult(
String feedbackToUser,
boolean showHelp,
boolean exit,
boolean showView,
Person personToView,
boolean viewExit) {
this.feedbackToUser = requireNonNull(feedbackToUser);
this.showHelp = showHelp;
this.exit = exit;
this.showView = showView;
this.personToView = personToView;
this.viewExit = viewExit;
}

/**
* Constructs a {@code CommandResult} with the specified fields.
*/
public CommandResult(String feedbackToUser, boolean showHelp, boolean exit) {
this.feedbackToUser = requireNonNull(feedbackToUser);
this.showHelp = showHelp;
this.exit = exit;
this(requireNonNull(feedbackToUser), showHelp, exit, false, null, false);
}

/**
Expand All @@ -48,6 +69,17 @@ public boolean isExit() {
return exit;
}

public boolean isShowView() {
return showView;
}

public boolean isViewExit() {
return viewExit;
}

public Person getPersonToView() {
return personToView;
}
@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/seedu/address/logic/commands/ViewCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;

public class ViewCommand extends Command {
public static final String COMMAND_WORD = "view";

private final Index targetIndex;
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Shows the profile of the user.\n"
+ "Example: " + COMMAND_WORD + " 1";
public static final String VIEWING_PROFILE_SUCCESS = "Viewing Person: %1$s";
public ViewCommand(Index index) {
this.targetIndex = index;
}
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToView = lastShownList.get(targetIndex.getZeroBased());
return new CommandResult(
String.format(VIEWING_PROFILE_SUCCESS, Messages.format(personToView)),
false,
false,
true,
personToView,
false);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof ViewCommand)) {
return false;
}

ViewCommand e = (ViewCommand) other;
return targetIndex.equals(e.targetIndex);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", targetIndex)
.toString();
}
}
21 changes: 21 additions & 0 deletions src/main/java/seedu/address/logic/commands/ViewExitCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package seedu.address.logic.commands;

import seedu.address.model.Model;

public class ViewExitCommand extends Command {
public static final String COMMAND_WORD = "exit";

public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting view mode as requested ...";

@Override
public CommandResult execute(Model model) {
return new CommandResult(
MESSAGE_EXIT_ACKNOWLEDGEMENT,
false,
false,
false,
null,
true
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ViewCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -71,6 +72,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ListCommand.COMMAND_WORD:
return new ListCommand();

case ViewCommand.COMMAND_WORD:
return new ViewCommandParser().parse(arguments);

case ExitCommand.COMMAND_WORD:
return new ExitCommand();

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/seedu/address/logic/parser/ViewCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.ViewCommand;
import seedu.address.logic.parser.exceptions.ParseException;

public class ViewCommandParser {
public ViewCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new ViewCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE), pe);
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/seedu/address/logic/parser/ViewModeParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ViewExitCommand;
import seedu.address.logic.parser.exceptions.ParseException;

public class ViewModeParser {
/**
* Used for initial separation of command word and args.
*/
private static final Pattern BASIC_COMMAND_FORMAT = Pattern.compile("(?<commandWord>\\S+)(?<arguments>.*)");
private static final Logger logger = LogsCenter.getLogger(seedu.address.logic.parser.AddressBookParser.class);

/**
* Parses user input into command for execution.
*
* @param userInput full user input string
* @return the command based on the user input
* @throws ParseException if the user input does not conform the expected format
*/
public Command parseCommand(String userInput) throws ParseException {
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE));
}

final String commandWord = matcher.group("commandWord");
final String arguments = matcher.group("arguments");

// Note to developers: Change the log level in config.json to enable lower level (i.e., FINE, FINER and lower)
// log messages such as the one below.
// Lower level log messages are used sparingly to minimize noise in the code.
logger.fine("Command word: " + commandWord + "; Arguments: " + arguments);

switch (commandWord) {

case ViewExitCommand.COMMAND_WORD:
return new ViewExitCommand();

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
}

}
26 changes: 26 additions & 0 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Person;

/**
* The Main Window. Provides the basic application layout containing
Expand All @@ -32,6 +33,8 @@ public class MainWindow extends UiPart<Stage> {

// Independent Ui parts residing in this Ui container
private PersonListPanel personListPanel;

private PersonProfile personProfile;
private ResultDisplay resultDisplay;
private HelpWindow helpWindow;

Expand Down Expand Up @@ -66,6 +69,7 @@ public MainWindow(Stage primaryStage, Logic logic) {
setAccelerators();

helpWindow = new HelpWindow();

}

public Stage getPrimaryStage() {
Expand Down Expand Up @@ -167,6 +171,19 @@ public PersonListPanel getPersonListPanel() {
return personListPanel;
}

@FXML
private void handleView(Person personToView) {
personProfile = new PersonProfile(personToView);
personListPanelPlaceholder.getChildren().remove(personListPanel.getRoot());
personListPanelPlaceholder.getChildren().add(personProfile.getRoot());
}
@FXML
private void handleViewExit() {
personListPanel = new PersonListPanel(logic.getFilteredPersonList());
personListPanelPlaceholder.getChildren().remove(personProfile.getRoot());
personListPanelPlaceholder.getChildren().add(personListPanel.getRoot());
}

/**
* Executes the command and returns the result.
*
Expand All @@ -186,11 +203,20 @@ private CommandResult executeCommand(String commandText) throws CommandException
handleExit();
}

if (commandResult.isShowView()) {
handleView(commandResult.getPersonToView());
}

if (commandResult.isViewExit()) {
handleViewExit();
}

return commandResult;
} catch (CommandException | ParseException e) {
logger.info("An error occurred while executing command: " + commandText);
resultDisplay.setFeedbackToUser(e.getMessage());
throw e;
}
}

}
Loading

0 comments on commit e4b07ff

Please sign in to comment.