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 all 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
180 changes: 180 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,180 @@
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 one detail 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 + " ";

public static final String MESSAGE_NO_PARAM = "One field to copy must be provided.";
public static final String MESSAGE_EXTRA_PARAM = "Only one field can be copied.";

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);

if (info[0]) {
return personToCopy.getName().toString();
}
if (info[1]) {
return personToCopy.getPhone().toString();
}
if (info[2]) {
return personToCopy.getEmail().toString();
}
if (info[3]) {
return personToCopy.getAddress().toString();
}
if (info[4]) {
return personToCopy.getTags().toString();
}
if (info[5]) {
return personToCopy.getAssets().toString();
}

throw new IllegalArgumentException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE));

Check warning on line 99 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#L99

Added line #L99 was not covered by tests
}

/**
* 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];
int totalParams = 0;

if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
info[0] = true;
totalParams++;
}
if (argMultimap.getValue(PREFIX_PHONE).isPresent()) {
info[1] = true;
totalParams++;
}
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
info[2] = true;
totalParams++;

Check warning on line 135 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#L134-L135

Added lines #L134 - L135 were not covered by tests
}
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
info[3] = true;
totalParams++;
}
if (argMultimap.getValue(PREFIX_TAG).isPresent()) {
info[4] = true;
totalParams++;
}
if (argMultimap.getValue(PREFIX_ASSET).isPresent()) {
info[5] = true;
totalParams++;
}

if (totalParams == 0) {
throw new IllegalArgumentException(MESSAGE_NO_PARAM);
} else if (totalParams > 1) {
throw new IllegalArgumentException(MESSAGE_EXTRA_PARAM);
}

return new CopyCommand(index, info);
}

@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 178 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#L176-L178

Added lines #L176 - L178 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 @@ -161,6 +163,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 175 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#L171-L175

Added lines #L171 - L175 were not covered by tests

/**
* Executes the command and returns the result.
*
Expand All @@ -178,12 +191,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 203 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#L203

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

Expand Down
Loading