Skip to content

Commit

Permalink
Merge pull request #97 from CS2103AUG2016-W11-C3/feature/redo-command
Browse files Browse the repository at this point in the history
Feature/redo command
  • Loading branch information
Wong Kang Fei authored Oct 17, 2016
2 parents 1559bbf + 089d541 commit cf76305
Show file tree
Hide file tree
Showing 14 changed files with 434 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package harmony.mastermind.commons.exceptions;

public class TaskAlreadyMarkedException extends Exception {

//@@author A0138862W
public TaskAlreadyMarkedException(){
super();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package harmony.mastermind.commons.exceptions;

public class TaskAlreadyUnmarkedException extends Exception{

public TaskAlreadyUnmarkedException(){
super();
}
}
35 changes: 33 additions & 2 deletions src/main/java/harmony/mastermind/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
import harmony.mastermind.model.tag.Tag;
import harmony.mastermind.model.tag.UniqueTagList;
import harmony.mastermind.model.task.*;
import harmony.mastermind.model.task.UniqueTaskList.DuplicateTaskException;

/**
* Adds a task to the task manager.
*
*/
// @@author A0138862W
public class AddCommand extends Command implements Undoable {
public class AddCommand extends Command implements Undoable, Redoable {

public static final String COMMAND_KEYWORD_ADD = "add";
public static final String COMMAND_KEYWORD_DO = "do";
Expand Down Expand Up @@ -65,6 +66,7 @@ public class AddCommand extends Command implements Undoable {

public static final String MESSAGE_SUCCESS = "New task added: %1$s";
public static final String MESSAGE_UNDO_SUCCESS = "[Undo Add Command] Task deleted: %1$s";
public static final String MESSAGE_REDO_SUCCESS = "[Redo Add Command] Task added: %1$s";
public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in Mastermind";

private final Task toAdd;
Expand Down Expand Up @@ -113,8 +115,14 @@ public AddCommand(String name, Set<String> tags) throws IllegalValueException, P
public CommandResult execute() {
assert model != null;
try {
model.addTask(toAdd);
executeAdd();

// push this command into undoHistory
model.pushToUndoHistory(this);

// this is a new command entered by user (not undo/redo)
// need to clear the redoHistory Stack
model.clearRedoHistory();

return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
} catch (UniqueTaskList.DuplicateTaskException e) {
Expand All @@ -130,11 +138,34 @@ public CommandResult undo() {
try {
// remove the task that's previously added.
model.deleteTask(toAdd);

model.pushToRedoHistory(this);

return new CommandResult(String.format(MESSAGE_UNDO_SUCCESS, toAdd));
} catch (UniqueTaskList.TaskNotFoundException pne) {
return new CommandResult(Messages.MESSAGE_TASK_NOT_IN_MASTERMIND);
}
}

@Override
/** action to perform when ModelManager requested to redo this command**/
// @@author A0138862W
public CommandResult redo() {
assert model != null;
try {
executeAdd();

model.pushToUndoHistory(this);

return new CommandResult(String.format(MESSAGE_REDO_SUCCESS, toAdd));
} catch (UniqueTaskList.DuplicateTaskException e) {
return new CommandResult(MESSAGE_DUPLICATE_TASK);
}
}

/** extract method since it's reusable for execute() and redo()**/
private void executeAdd() throws DuplicateTaskException {
model.addTask(toAdd);
}

}
84 changes: 60 additions & 24 deletions src/main/java/harmony/mastermind/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,104 @@
import harmony.mastermind.model.task.UniqueTaskList.TaskNotFoundException;

/**
* Deletes a task identified using it's last displayed index from the task manager.
* Deletes a task identified using it's last displayed index from the task
* manager.
*/
public class DeleteCommand extends Command implements Undoable{
public class DeleteCommand extends Command implements Undoable, Redoable {

public static final String COMMAND_WORD = "delete";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the task identified by the index number used in the last task listing.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";
+ ": Deletes the task identified by the index number used in the last task listing.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: "
+ COMMAND_WORD
+ " 1";

public static final String MESSAGE_DELETE_TASK_SUCCESS = "Deleted Task: %1$s";
public static final String COMMAND_SUMMARY = "Deleting a task:"
+ "\n" + COMMAND_WORD + " INDEX";

+ "\n"
+ COMMAND_WORD
+ " INDEX";

public static final String MESSAGE_UNDO_SUCCESS = "[Undo Delete Command] Task added: %1$s";
public static final String MESSAGE_REDO_SUCCESS = "[Redo Delete Command] Deleted Task: %1$s";

public final int targetIndex;

private ReadOnlyTask toDelete;

public DeleteCommand(int targetIndex) {
this.targetIndex = targetIndex;
}


@Override
public CommandResult execute() {

UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();
try {
executeDelete();

if (lastShownList.size() < targetIndex) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}
model.pushToUndoHistory(this);

toDelete = lastShownList.get(targetIndex - 1);
model.clearRedoHistory();

try {
model.deleteTask(toDelete);
model.pushToUndoHistory(this);
} catch (TaskNotFoundException | IndexOutOfBoundsException tnfe) {
// by A0138862W:
// uncommented this line because it makes the DeleteCommandTest fail at line 33
// should return invalid message for UI to display otherwise it'll display null
//assert false : "The target task cannot be missing";

} catch (TaskNotFoundException tnfe) {
assert false : "The target task cannot be missing";
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

return new CommandResult(String.format(MESSAGE_DELETE_TASK_SUCCESS, toDelete));
}


@Override
/** action to perform when ModelManager requested to undo this command**/
//@@author A0138862W
/** action to perform when ModelManager requested to undo this command **/
// @@author A0138862W
public CommandResult undo() {
try {
// add back the task that's previously added.
model.addTask((Task)toDelete);
model.addTask((Task) toDelete);

model.pushToRedoHistory(this);

return new CommandResult(String.format(MESSAGE_UNDO_SUCCESS, toDelete));
} catch (DuplicateTaskException e) {
return new CommandResult(AddCommand.MESSAGE_DUPLICATE_TASK);
}
}

@Override
// @@author A0138862W
public CommandResult redo() {
try {
executeDelete();

model.pushToUndoHistory(this);

} catch (TaskNotFoundException | IndexOutOfBoundsException tnfe) {
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

return new CommandResult(String.format(MESSAGE_REDO_SUCCESS, toDelete));
}

private void executeDelete() throws TaskNotFoundException, IndexOutOfBoundsException {
UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();

if (lastShownList.size() < targetIndex) {
indicateAttemptToExecuteIncorrectCommand();
throw new IndexOutOfBoundsException();
}

if (toDelete == null) {
toDelete = lastShownList.get(targetIndex
- 1);
}

model.deleteTask(toDelete);
}

}
106 changes: 68 additions & 38 deletions src/main/java/harmony/mastermind/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Edits a task in task manager
*
*/
public class EditCommand extends Command implements Undoable{
public class EditCommand extends Command implements Undoable, Redoable {

public static final String COMMAND_KEYWORD_EDIT = "edit";
public static final String COMMAND_KEYWORD_UPDATE = "update";
Expand Down Expand Up @@ -55,8 +55,9 @@ public class EditCommand extends Command implements Undoable{

public static final String MESSAGE_EDIT_TASK_PROMPT = "Edit the following task: %1$s";
public static final String MESSAGE_EDIT_TASK_SUCCESS = "Task successfully edited: %1$s";

public static final String MESSAGE_UNDO_SUCCESS = "[Undo Edit Command] Task reverted: %1$s";
public static final String MESSAGE_REDO_SUCCESS = "[Redo Edit Command] Edit the following task: %1$s";

// private MainWindow window;
private ReadOnlyTask originalTask;
Expand All @@ -79,15 +80,72 @@ public EditCommand(int targetIndex, Optional<String> name, Optional<String> star
@Override
public CommandResult execute() {

// grabbing the origin task (before edit)
try {
// grabbing the origin task (before edit)
executeEdit();

model.pushToUndoHistory(this);

// this is a new command entered by user (not undo/redo)
// need to clear the redoHistory Stack
model.clearRedoHistory();

return new CommandResult(String.format(MESSAGE_EDIT_TASK_PROMPT, originalTask));

} catch (TaskNotFoundException | DuplicateTaskException | IndexOutOfBoundsException ie) {
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

}

@Override
// @@author A0138862W
public CommandResult undo() {

try {
// remove the task that's previously edited
model.deleteTask(editedTask);

// add back the original task
model.addTask((Task) originalTask);

model.pushToRedoHistory(this);

return new CommandResult(String.format(MESSAGE_UNDO_SUCCESS, originalTask));
} catch (UniqueTaskList.TaskNotFoundException pne) {
return new CommandResult(Messages.MESSAGE_TASK_NOT_IN_MASTERMIND);
} catch (DuplicateTaskException e) {
return new CommandResult(AddCommand.MESSAGE_DUPLICATE_TASK);
}
}

@Override
// @@author A0138862W
public CommandResult redo() {

try {
executeEdit();

model.pushToUndoHistory(this);

return new CommandResult(String.format(MESSAGE_REDO_SUCCESS, originalTask));
} catch (TaskNotFoundException | DuplicateTaskException | IndexOutOfBoundsException ie) {
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}
}

private void executeEdit() throws TaskNotFoundException, DuplicateTaskException, IndexOutOfBoundsException {
UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();

if (lastShownList.size() < targetIndex) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
throw new IndexOutOfBoundsException();
}

if (originalTask == null) {
originalTask = lastShownList.get(targetIndex
- 1);
}
originalTask = lastShownList.get(targetIndex
- 1);

// if user provides explicit field and value, we change them
// otherwise, all user omitted field are preserve from the original
Expand All @@ -108,40 +166,12 @@ public CommandResult execute() {
}).orElse(originalTask.getTags().toSet()));

// initialize the new task with edited values
this.editedTask = new Task(toEditName, toEditStartDate, toEditEndDate, toEditTags);

try {
model.deleteTask(originalTask);
model.addTask(editedTask);

model.pushToUndoHistory(this);
//
return new CommandResult(String.format(MESSAGE_EDIT_TASK_PROMPT, originalTask));

} catch (TaskNotFoundException | DuplicateTaskException ie) {
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
if (editedTask == null) {
editedTask = new Task(toEditName, toEditStartDate, toEditEndDate, toEditTags);
}

model.deleteTask(originalTask);
model.addTask(editedTask);
}

@Override
//@@author A0138862W
public CommandResult undo() {

try {
// remove the task that's previously edited
model.deleteTask(editedTask);

// add back the original task
model.addTask((Task)originalTask);

return new CommandResult(String.format(MESSAGE_UNDO_SUCCESS, originalTask));
} catch (UniqueTaskList.TaskNotFoundException pne) {
return new CommandResult(Messages.MESSAGE_TASK_NOT_IN_MASTERMIND);
} catch (DuplicateTaskException e) {
return new CommandResult(AddCommand.MESSAGE_DUPLICATE_TASK);
}
}


}
Loading

0 comments on commit cf76305

Please sign in to comment.