forked from nus-cs2103-AY1617S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
3,748 additions
and
98 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,298 @@ | ||
# A0135793W | ||
###### /java/seedu/taskitty/commons/util/AppUtil.java | ||
``` java | ||
public static UnmodifiableObservableList<ReadOnlyTask> getCorrectListBasedOnCategoryIndex(Model model, int categoryIndex) { | ||
assert categoryIndex == 0 || categoryIndex == 1 || categoryIndex == 2; | ||
if (categoryIndex == 0) { | ||
return model.getFilteredTodoList(); | ||
} | ||
else if (categoryIndex == 1) { | ||
return model.getFilteredDeadlineList(); | ||
} | ||
else { | ||
return model.getFilteredEventList(); | ||
} | ||
} | ||
|
||
} | ||
``` | ||
###### /java/seedu/taskitty/logic/commands/EditCommand.java | ||
``` java | ||
/** | ||
* Edits a task identified using it's last displayed index from the task manager. | ||
*/ | ||
|
||
public class EditCommand extends Command{ | ||
|
||
public static final String COMMAND_WORD = "edit"; | ||
|
||
public static final String MESSAGE_PARAMETER = COMMAND_WORD | ||
+ " [index] [name] [t/tag]..."; | ||
public static final String MESSAGE_USAGE = "This command edits a task in TasKitty, Meow!" | ||
+ "\n[index] is the index eg. t1, d1, e1."; | ||
|
||
public static final String MESSAGE_SUCCESS = "Task edited: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the task manager"; | ||
|
||
public final int categoryIndex; | ||
|
||
public final int targetIndex; | ||
|
||
private Task toEdit; | ||
private ReadOnlyTask taskToEdit; | ||
private String[] data; | ||
private final Set<Tag> tagSet; | ||
|
||
public EditCommand(String[] data, Set<String> tags, int targetIndex) | ||
throws IllegalValueException { | ||
this(data, tags, targetIndex, Task.DEFAULT_CATEGORY_INDEX); | ||
} | ||
/** | ||
* Convenience constructor using raw values. | ||
* | ||
* @throws IllegalValueException if any of the raw values are invalid | ||
*/ | ||
public EditCommand(String[] data, Set<String> tags, int targetIndex, int categoryIndex) | ||
throws IllegalValueException { | ||
|
||
assert categoryIndex >= 0 && categoryIndex < 3; | ||
|
||
this.targetIndex = targetIndex; | ||
this.categoryIndex = categoryIndex; | ||
this.data = data; | ||
tagSet = new HashSet<>(); | ||
for (String tagName : tags) { | ||
tagSet.add(new Tag(tagName)); | ||
} | ||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
assert categoryIndex >= 0 && categoryIndex < 3; | ||
|
||
UnmodifiableObservableList<ReadOnlyTask> lastShownList = AppUtil.getCorrectListBasedOnCategoryIndex(model,categoryIndex); | ||
if (lastShownList.size() < targetIndex) { | ||
indicateAttemptToExecuteIncorrectCommand(); | ||
model.removeUnchangedState(); | ||
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
taskToEdit = lastShownList.get(targetIndex - 1); | ||
|
||
try { | ||
Optional<CommandResult> result = updateToEditVariable(); | ||
if (result.isPresent()) { | ||
return result.get(); | ||
} | ||
model.editTask(taskToEdit, toEdit); | ||
} catch (UniqueTaskList.DuplicateTaskException e) { | ||
model.removeUnchangedState(); | ||
return new CommandResult(MESSAGE_DUPLICATE_TASK); | ||
} catch (TaskNotFoundException pnfe) { | ||
model.removeUnchangedState(); | ||
assert false : "The target task cannot be missing"; | ||
} catch (IllegalValueException ive) { | ||
return new CommandResult(ive.getMessage()); | ||
} | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, Task.CATEGORIES[categoryIndex], toEdit)); | ||
} | ||
|
||
/** | ||
* Ensure that toEdit variable has proper values before executing the command | ||
* @return | ||
* @throws IllegalValueException | ||
*/ | ||
private Optional<CommandResult> updateToEditVariable() throws IllegalValueException { | ||
if (data.length == Task.TASK_COMPONENT_COUNT) { | ||
this.toEdit = new Task( | ||
new Name(data[Task.TASK_COMPONENT_INDEX_NAME]), | ||
new TaskPeriod(), | ||
new UniqueTagList(tagSet) | ||
); | ||
} else if (data.length == Task.DEADLINE_COMPONENT_COUNT) { | ||
if (data[0].isEmpty()) { | ||
data[0] = taskToEdit.getName().toString(); | ||
} | ||
if (data[2] == null) { | ||
if (categoryIndex != 1) { | ||
return Optional.of(new CommandResult(String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE))); | ||
} | ||
data[2] = taskToEdit.getPeriod().getEndTime().toString(); | ||
} | ||
this.toEdit = new Task( | ||
new Name(data[Task.DEADLINE_COMPONENT_INDEX_NAME]), | ||
new TaskPeriod(new TaskDate(data[Task.DEADLINE_COMPONENT_INDEX_END_DATE]), | ||
new TaskTime(data[Task.DEADLINE_COMPONENT_INDEX_END_TIME])), | ||
new UniqueTagList(tagSet) | ||
); | ||
} else if (data.length == Task.EVENT_COMPONENT_COUNT) { | ||
if (data[0].isEmpty()) { | ||
data[0] = taskToEdit.getName().toString(); | ||
} | ||
if (data[2] == null) { | ||
if (categoryIndex != 2) { | ||
return Optional.of(new CommandResult(String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE))); | ||
} | ||
data[2] = taskToEdit.getPeriod().getStartTime().toString(); | ||
} | ||
if (data[4] == null) { | ||
data[4] = taskToEdit.getPeriod().getEndTime().toString(); | ||
} | ||
this.toEdit = new Task( | ||
new Name(data[Task.EVENT_COMPONENT_INDEX_NAME]), | ||
new TaskPeriod(new TaskDate(data[Task.EVENT_COMPONENT_INDEX_START_DATE]), | ||
new TaskTime(data[Task.EVENT_COMPONENT_INDEX_START_TIME]), | ||
new TaskDate(data[Task.EVENT_COMPONENT_INDEX_END_DATE]), | ||
new TaskTime(data[Task.EVENT_COMPONENT_INDEX_END_TIME])), | ||
new UniqueTagList(tagSet) | ||
); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public void saveStateIfNeeded(String commandText) { | ||
model.saveState(commandText); | ||
} | ||
} | ||
``` | ||
###### /java/seedu/taskitty/logic/commands/SaveCommand.java | ||
``` java | ||
/** | ||
* Saves TasKitty data to a folder specified by the user | ||
* @author JiaWern | ||
* | ||
*/ | ||
public class SaveCommand extends Command{ | ||
|
||
public static final String COMMAND_WORD = "save"; | ||
|
||
public static final String MESSAGE_PARAMETER = COMMAND_WORD | ||
+ " filepath"; | ||
public static final String MESSAGE_USAGE = "This command saves data in TasKitty to a location of your choice, Meow!\n"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Data saved to: %1$s"; | ||
public static final String MESSAGE_FAILED = "Failed to save data to: %1$s"; | ||
|
||
public final String filepath; | ||
|
||
public SaveCommand(String filepath) throws IllegalValueException{ | ||
if (filepath.toCharArray().length == 0) { | ||
throw new IllegalValueException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE)); | ||
} | ||
this.filepath = filepath; | ||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
Config config = new Config(); | ||
String configFile = Config.DEFAULT_CONFIG_FILE; | ||
|
||
try { | ||
config.setTaskManagerFilePath(filepath + "/" + config.getTaskManagerFilePath()); | ||
ConfigUtil.saveConfig(config, configFile); | ||
|
||
new StorageManager(config.getTaskManagerFilePath(), config.getUserPrefsFilePath()); | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, filepath)); | ||
} catch (IOException io) { | ||
return new CommandResult(MESSAGE_FAILED + StringUtil.getDetails(io)); | ||
} | ||
} | ||
|
||
@Override | ||
public void saveStateIfNeeded(String commandText) { | ||
// This constructor is intentionally empty, because something something | ||
} | ||
|
||
} | ||
``` | ||
###### /java/seedu/taskitty/logic/parser/CommandParser.java | ||
``` java | ||
private Command prepareSave(String argument) { | ||
try { | ||
return new SaveCommand(argument.trim()); | ||
} catch (IllegalValueException ive) { | ||
return new IncorrectCommand(ive.getMessage()); | ||
} | ||
} | ||
``` | ||
###### /java/seedu/taskitty/logic/parser/CommandParser.java | ||
``` java | ||
/** | ||
* Parses arguments in the context of the mark as done command. | ||
* | ||
* @param args full command args string | ||
* @return the prepared command | ||
*/ | ||
private Command prepareDone(String args) { | ||
|
||
args = args.trim(); | ||
String[] indexes = args.split("\\s"); | ||
Pair<Integer, Integer> categoryAndIndex = null; | ||
ArrayList<Pair<Integer, Integer>> listOfIndexes = new ArrayList<Pair<Integer, Integer>>(); | ||
|
||
for (String index: indexes) { | ||
categoryAndIndex= getCategoryAndIndex(index); | ||
if (categoryAndIndex == null) { | ||
return new IncorrectCommand( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DoneCommand.MESSAGE_USAGE)); | ||
} | ||
listOfIndexes.add(categoryAndIndex); | ||
} | ||
|
||
return new DoneCommand(listOfIndexes); | ||
} | ||
|
||
/** | ||
* Parses arguments in the context of the edit task command. | ||
* | ||
* @param args full command args string | ||
* @return the prepared command | ||
*/ | ||
private Command prepareEdit(String args) { | ||
String[] splitArgs = args.trim().split(" "); | ||
if (splitArgs.length < 2) { | ||
return new IncorrectCommand( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
Pair<Integer, Integer> categoryAndIndexPair = getCategoryAndIndex(splitArgs[0]); | ||
|
||
if (categoryAndIndexPair == null) { | ||
return new IncorrectCommand( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
try { | ||
String arguments = ""; | ||
for (int i = 1; i<splitArgs.length; i++){ | ||
arguments = arguments + splitArgs[i] + " "; | ||
} | ||
arguments.substring(0, arguments.length() - 1); | ||
String taskDetailArguments = getTaskDetailArguments(arguments); | ||
String tagArguments = getTagArguments(arguments); | ||
|
||
return new EditCommand( | ||
extractTaskDetailsNatty(taskDetailArguments), | ||
getTagsFromArgs(tagArguments), | ||
categoryAndIndexPair.getValue(), | ||
categoryAndIndexPair.getKey()); | ||
} catch (IllegalValueException ive) { | ||
return new IncorrectCommand(ive.getMessage()); | ||
} | ||
} | ||
|
||
``` | ||
###### /java/seedu/taskitty/model/ModelManager.java | ||
``` java | ||
@Override | ||
public synchronized void editTask(ReadOnlyTask target, Task task) throws UniqueTaskList.TaskNotFoundException, UniqueTaskList.DuplicateTaskException { | ||
taskManager.addTask(task); | ||
indicateTaskManagerChanged(); | ||
taskManager.removeTask(target); | ||
updateFilteredListToShowAll(); | ||
indicateTaskManagerChanged(); | ||
} | ||
``` |
Oops, something went wrong.