Skip to content

Commit

Permalink
Merge pull request #168 from Pluiexo/branch-SelectCommand
Browse files Browse the repository at this point in the history
Branch select command
  • Loading branch information
whitesnowx authored Apr 4, 2024
2 parents ec037cf + 0751fb2 commit 900c536
Show file tree
Hide file tree
Showing 16 changed files with 528 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.Objects.requireNonNull;
import static staffconnect.logic.parser.CliSyntax.PREFIX_MEETING_DESCRIPTION;
import static staffconnect.logic.parser.CliSyntax.PREFIX_MEETING_STARTDATE;
import static staffconnect.model.Model.PREDICATE_SHOW_ALL_PERSONS;
import static staffconnect.model.meeting.comparator.MeetingDateThenDescriptionComparator.MEETING_DATE_THEN_DESCRIPTION_COMPARATOR;

import java.util.List;
Expand Down Expand Up @@ -69,8 +68,9 @@ public CommandResult execute(Model model) throws CommandException {

//setPerson to force update the ui with the new items
model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));

return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)), editedPerson,
index.getZeroBased());
}


Expand Down
76 changes: 64 additions & 12 deletions src/main/java/staffconnect/logic/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static java.util.Objects.requireNonNull;

import java.util.Objects;
import java.util.Optional;

import staffconnect.commons.util.ToStringBuilder;
import staffconnect.model.person.Person;

/**
* Represents the result of a command execution.
Expand All @@ -13,27 +15,48 @@ public class CommandResult {

private final String feedbackToUser;

/** Help information should be shown to the user. */
/**
* Help information should be shown to the user.
*/
private final boolean showHelp;

/** The application should exit. */
/**
* The application should exit.
*/
private final boolean exit;

private final int index;

private final Person personToSwitch;

/**
* Constructs a {@code CommandResult} with the specified {@code feedbackToUser},
* and other fields set to their default value.
*/
public CommandResult(String feedbackToUser) {
this(feedbackToUser, false, false);
}

/**
* 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;
index = -999;
personToSwitch = null;
}

/**
* Constructs a {@code CommandResult} with the specified {@code feedbackToUser},
* and other fields set to their default value.
* Constructs a {@code CommandResult} with the specified person to store.
*/
public CommandResult(String feedbackToUser) {
this(feedbackToUser, false, false);
public CommandResult(String feedbackToUser, Person personToSwitch, int index) {
this.feedbackToUser = requireNonNull(feedbackToUser);
this.showHelp = false;
this.exit = false;
this.personToSwitch = personToSwitch;
this.index = index;
}

public String getFeedbackToUser() {
Expand All @@ -48,6 +71,34 @@ public boolean isExit() {
return exit;
}

/**
* Checks if there is a valid person and index to get in the result.
*
* @return
*/
public boolean hasPersonAndIndex() {
return personToSwitch != null && index != -999;
}

/**
* Returns a person from the result to send to the UI display.
*
* @return a person from the command result.
*/
public Optional<Person> getPersonToDisplay() {
return personToSwitch != null ? Optional.of(personToSwitch) : Optional.empty();
}

public int getIndex() {
return index;

}

@Override
public int hashCode() {
return Objects.hash(feedbackToUser, showHelp, exit, index, personToSwitch);
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -62,12 +113,11 @@ public boolean equals(Object other) {
CommandResult otherCommandResult = (CommandResult) other;
return feedbackToUser.equals(otherCommandResult.feedbackToUser)
&& showHelp == otherCommandResult.showHelp
&& exit == otherCommandResult.exit;
}

@Override
public int hashCode() {
return Objects.hash(feedbackToUser, showHelp, exit);
&& exit == otherCommandResult.exit
&& index == otherCommandResult.index
&& (personToSwitch != null
? personToSwitch.equals(otherCommandResult.personToSwitch)
: personToSwitch == otherCommandResult.personToSwitch);
}

@Override
Expand All @@ -76,6 +126,8 @@ public String toString() {
.add("feedbackToUser", feedbackToUser)
.add("showHelp", showHelp)
.add("exit", exit)
.add("index", index)
.add("person", personToSwitch)
.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public CommandResult execute(Model model) throws CommandException {

//force update the ui
model.setPerson(personToSelect, editedPerson);
return new CommandResult(String.format(MESSAGE_DELETE_MEETING_SUCCESS, Messages.format(meetingToSelect)));
return new CommandResult(String.format(MESSAGE_DELETE_MEETING_SUCCESS, Messages.format(meetingToSelect)),
editedPerson, targetPersonIndex.getZeroBased());
}

@Override
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/staffconnect/logic/commands/SelectCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package staffconnect.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.List;

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

/**
* Selects a person identified using it's displayed index from the staff book.
*/
public class SelectCommand extends Command {

public static final String COMMAND_WORD = "select";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Selects the person identified by the index number used in the displayed person list for display.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_SELECT_PERSON_SUCCESS = "Selected Person: %1$s";

private final Index targetIndex;

public SelectCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getSortedFilteredPersonList();

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

Person personToSelect = lastShownList.get(targetIndex.getZeroBased());

return new CommandResult(String.format(MESSAGE_SELECT_PERSON_SUCCESS, Messages.format(personToSelect)),
personToSelect, targetIndex.getZeroBased());
}

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

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

SelectCommand selectCommand = (SelectCommand) other;
return targetIndex.equals(selectCommand.targetIndex);
}

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

import static staffconnect.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import staffconnect.commons.core.index.Index;
import staffconnect.logic.commands.SelectCommand;
import staffconnect.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new SelectCommand object
*/
public class SelectCommandParser implements Parser<SelectCommand> {

/**
* Parses the given {@code String} of arguments in the context of the SelectCommand
* and returns a SelectCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public SelectCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new SelectCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SelectCommand.MESSAGE_USAGE), pe);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import staffconnect.logic.commands.HelpCommand;
import staffconnect.logic.commands.ListCommand;
import staffconnect.logic.commands.RefreshCommand;
import staffconnect.logic.commands.SelectCommand;
import staffconnect.logic.commands.SortCommand;
import staffconnect.logic.commands.UnfavCommand;
import staffconnect.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -105,6 +106,9 @@ public Command parseCommand(String userInput) throws ParseException {
case RefreshCommand.COMMAND_WORD:
return new RefreshCommand();

case SelectCommand.COMMAND_WORD:
return new SelectCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
44 changes: 38 additions & 6 deletions src/main/java/staffconnect/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public MainWindow(Stage primaryStage, Logic logic) {

// Configure the UI
setWindowDefaultSize(logic.getGuiSettings());

// The default divider is 0.43
personOnDisplay = logic.getFirstPersonIfExist()
.map(PersonCard::new).orElse(new PersonCard());
.map(person -> new PersonCard(person, 0.43)).orElse(new PersonCard());

helpWindow = new HelpWindow();
}
Expand All @@ -87,7 +87,8 @@ public Stage getPrimaryStage() {
*/
void fillInnerParts() {

personListPanel = new PersonListPanel(logic.getFilteredPersonList(), this::changePersonCard);
personListPanel =
new PersonListPanel(logic.getFilteredPersonList(), this::changePersonCard, this::getDividerPosition);
personListPanel.setListSelectedIndex(0);
personListPanelPlaceholder.getChildren().add(personListPanel.getRoot());

Expand All @@ -104,7 +105,6 @@ void fillInnerParts() {
personCardPanelPlaceholder.getChildren().add(personOnDisplay.getRoot());



}

private void changePersonCard(PersonCard personToUpdate) {
Expand All @@ -115,6 +115,15 @@ private void changePersonCard(PersonCard personToUpdate) {

}

/**
* Gets the current divider position, to be used by the UI only
*
* @return a double value of the divider position.
*/
public double getDividerPosition() {
return personOnDisplay.getCurrentDividerPosition();
}

/**
* Executes the command and returns the result.
*
Expand All @@ -136,6 +145,18 @@ private CommandResult executeCommand(String commandText) throws CommandException
handleExit();
}

if (commandResult.hasPersonAndIndex()) {

double dividerPosition = personOnDisplay.getCurrentDividerPosition();
int index = commandResult.getIndex();
commandResult.getPersonToDisplay()
.ifPresentOrElse(
person -> reloadPersonCardWithPerson(new PersonCard(person, dividerPosition), index),
this::reloadPersonCardWithRoot);
} else {
reloadPersonCardWithRoot();
}

return commandResult;
} catch (CommandException | ParseException e) {
logger.info("An error occurred while executing command: " + commandText);
Expand Down Expand Up @@ -168,8 +189,9 @@ public void handleHelp() {
}

private void reloadPersonCardWithRoot() {

personOnDisplay = logic.getFirstPersonIfExist().map(PersonCard::new).orElse(new PersonCard());
double previousDividerPosition = personOnDisplay.getCurrentDividerPosition();
personOnDisplay = logic.getFirstPersonIfExist()
.map(person -> new PersonCard(person, previousDividerPosition)).orElse(new PersonCard());

personCardPanelPlaceholder.getChildren().clear();
personCardPanelPlaceholder.getChildren().add(personOnDisplay.getRoot());
Expand All @@ -178,6 +200,16 @@ private void reloadPersonCardWithRoot() {

}

private void reloadPersonCardWithPerson(PersonCard person, int index) {

personOnDisplay = person;
personCardPanelPlaceholder.getChildren().clear();
personCardPanelPlaceholder.getChildren().add(personOnDisplay.getRoot());

personListPanel.setListSelectedIndex(index);

}

void show() {
primaryStage.show();
}
Expand Down
Loading

0 comments on commit 900c536

Please sign in to comment.