Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Copy Command #131

Merged
merged 8 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class Messages {

public static final String MESSAGE_SHOWING_HELP = "Opened help window.";
public static final String MESSAGE_EXITING = "Exiting Address Book as requested ...";
public static final String MESSAGE_COPIED = "Copied details to clipboard: %1$s";
public static final int MESSAGE_COPIED_LEN = "Copied details to clipboard:".length();

/**
* Returns an error message indicating the duplicate prefixes.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/logic/commands/CommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public Command createCommand(String arguments) throws IllegalArgumentException {
return AddCommand.of(arguments);
}
},
COPY {
@Override
public Command createCommand(String arguments) throws IllegalArgumentException {
return CopyCommand.of(arguments);
}
},
EDIT {
@Override
public Command createCommand(String arguments) throws IllegalArgumentException {
Expand Down
178 changes: 178 additions & 0 deletions src/main/java/seedu/address/logic/commands/CopyCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.model.person.fields.Address.PREFIX_ADDRESS;
import static seedu.address.model.person.fields.Assets.PREFIX_ASSET;
import static seedu.address.model.person.fields.Email.PREFIX_EMAIL;
import static seedu.address.model.person.fields.Name.PREFIX_NAME;
import static seedu.address.model.person.fields.Phone.PREFIX_PHONE;
import static seedu.address.model.person.fields.Tags.PREFIX_TAG;

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.logic.util.ArgumentMultimap;
import seedu.address.logic.util.ArgumentTokenizer;
import seedu.address.logic.util.ParserUtil;
import seedu.address.model.Model;
import seedu.address.model.person.Person;

/**
* Copies the details of a person in the address book.
*/
public class CopyCommand extends Command {

public static final String COMMAND_WORD = "copy";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Copies the details of the prefix identified\n"
+ "Parameters: "
+ "[" + PREFIX_NAME + "] "
+ "[" + PREFIX_PHONE + "] "
+ "[" + PREFIX_EMAIL + "] "
+ "[" + PREFIX_ADDRESS + "] "
+ "[" + PREFIX_TAG + "] "
+ "[" + PREFIX_ASSET + "]\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_NAME + " "
+ PREFIX_PHONE + " "
+ PREFIX_TAG + " "
+ PREFIX_ASSET + " ";

public static final String MESSAGE_NO_PARAM = "At least one field to copy must be provided.";

private final Index index;
private final boolean[] info;

/**
* @param index of the person in the filtered person list to copy
* @param info list of details to copy to clipboard
*/
public CopyCommand(Index index, boolean[] info) {
requireNonNull(index);

this.index = index;
this.info = info;
}

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

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

Person personToCopy = lastShownList.get(index.getZeroBased());
String personDetails = copyToClipboard(personToCopy, info);

return String.format(Messages.MESSAGE_COPIED, personDetails);
}

/**
* Copies the details of {@code personToCopy} to clipboard
*/
private static String copyToClipboard(Person personToCopy, boolean[] info) {
requireNonNull(personToCopy);

StringBuilder copiedMsg = new StringBuilder();

if (info[0]) {
copiedMsg.append("Name: ").append(personToCopy.getName()).append("; ");
}
if (info[1]) {
copiedMsg.append("Phone: ").append(personToCopy.getPhone()).append("; ");
}
if (info[2]) {
copiedMsg.append("Email: ").append(personToCopy.getEmail()).append("; ");
}
if (info[3]) {
copiedMsg.append("Address: ").append(personToCopy.getAddress()).append("; ");
}
if (info[4]) {
copiedMsg.append("Tags: ").append(personToCopy.getTags()).append("; ");
}
if (info[5]) {
copiedMsg.append("Assets: ").append(personToCopy.getAssets()).append("; ");
}

return copiedMsg.substring(0, copiedMsg.length() - 2);
}

/**
* Parses the given {@code String} of arguments in the context of the CopyCommand
* and returns an CopyCommand object for execution.
* @throws IllegalArgumentException if the user input does not conform the expected format
*/
public static CopyCommand of(String args) throws IllegalArgumentException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_TAG, PREFIX_ASSET);

Index index;

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (IllegalArgumentException ie) {
throw new IllegalArgumentException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE), ie);
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);

boolean[] info = new boolean[6];

if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
info[0] = true;
}
if (argMultimap.getValue(PREFIX_PHONE).isPresent()) {
info[1] = true;
}
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
info[2] = true;
}
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
info[3] = true;
}
if (argMultimap.getValue(PREFIX_TAG).isPresent()) {
info[4] = true;
}
if (argMultimap.getValue(PREFIX_ASSET).isPresent()) {
info[5] = true;
}

for (boolean b: info) {
if (b) {
return new CopyCommand(index, info);
}
}

throw new IllegalArgumentException(MESSAGE_NO_PARAM);
}

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

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

CopyCommand otherCopyCommand = (CopyCommand) other;
return index.equals(otherCopyCommand.index);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("index", index)
.toString();

Check warning on line 176 in src/main/java/seedu/address/logic/commands/CopyCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/CopyCommand.java#L174-L176

Added lines #L174 - L176 were not covered by tests
}
}
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import javafx.fxml.FXML;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextInputControl;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
Expand Down Expand Up @@ -164,6 +166,17 @@
primaryStage.hide();
}

/**
* Copies string to the clipboard
*/
@FXML
private void handleCopy(String detailsToCopy) {
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
content.putString(detailsToCopy);
clipboard.setContent(content);
}

Check warning on line 178 in src/main/java/seedu/address/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/MainWindow.java#L174-L178

Added lines #L174 - L178 were not covered by tests

public PersonListPanel getPersonListPanel() {
return personListPanel;
}
Expand All @@ -185,12 +198,17 @@
throw e;
}

// == used to prevent an edge case where a command may somehow return this exact string,
// but is not actually a help or exit command.
if (commandResult == Messages.MESSAGE_SHOWING_HELP) {
handleHelp();
}
if (commandResult == Messages.MESSAGE_EXITING) {
handleExit();
}
if (commandResult.startsWith(Messages.MESSAGE_COPIED.substring(0, Messages.MESSAGE_COPIED_LEN + 1))) {
handleCopy(commandResult.substring(Messages.MESSAGE_COPIED_LEN).trim());

Check warning on line 210 in src/main/java/seedu/address/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/MainWindow.java#L210

Added line #L210 was not covered by tests
}
return commandResult;
}
}
Loading