Skip to content

Commit

Permalink
Merge branch 'add_tags'
Browse files Browse the repository at this point in the history
  • Loading branch information
jingrui-who committed Oct 19, 2016
2 parents a489bc8 + 107e96d commit cbcedfa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class EditCommand extends Command{
public static final String START_WORD = "start";
public static final String END_WORD = "end";
public static final String TAG_WORD = "tag";
public static final String ADD_WORD = "add";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits an existing task in Simply. "
+"Parameters: INDEX <section to delete> <edited information>\n"
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/seedu/address/logic/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public class Parser {
+ "(des|date|start|end|tag) "
+ ".+");

private static final Pattern ADD_TAGS_ARGS_FORMAT = // '/' forward slashes are reserved for delimiter prefixes
Pattern.compile("[E|D|T]\\d+"
+ "(?<tagArguments>(?: #[^#]+)*)");// variable number of tags

public Parser() {}

/**
Expand All @@ -73,6 +77,8 @@ public Command parseCommand(String userInput) {
return prepareEvent(arguments);
else if (DEADLINE_DATA_ARGS_FORMAT.matcher(userInput).find())
return prepareDeadline(arguments);
else if (ADD_TAGS_ARGS_FORMAT.matcher(userInput).find())
return prepareAddTags(arguments);
else if (TODO_DATA_ARGS_FORMAT.matcher(userInput).find())
return prepareToDo(arguments);
}
Expand Down Expand Up @@ -107,6 +113,28 @@ else if (TODO_DATA_ARGS_FORMAT.matcher(userInput).find())
}
}

private Command prepareAddTags(String args) {
final Matcher matcher = ADD_TAGS_ARGS_FORMAT.matcher(args.trim());
if (!matcher.matches()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

args = args.trim();

char category = args.charAt(0);
Optional<Integer> index = parseIndex(Character.toString(args.charAt(1)));
args = args.substring(args.indexOf(' ') + 1);

if(!index.isPresent()) {
return new IncorrectCommand(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE));
}

Integer pass = index.get();
args = "add ".concat(args);
return new EditCommand(pass, args, category);
}

/**
* Parses arguments in the context of the add todo command.
*
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ public void setTaskCategory(int taskCat) {
public void setTags(UniqueTagList replacement) {
tags.setTags(replacement);
}
/**
* increase this task's tags with the tags in the argument tag list.
*/
public void addTags(UniqueTagList addOn) {
tags.mergeFrom(addOn);
}


/**
* Replaces the specific tag found in this task's tags with the tags in the argument.
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/seedu/address/model/task/UniqueTaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Expand Down Expand Up @@ -145,7 +147,7 @@ public boolean edit(ReadOnlyTask key, String args) throws IllegalValueException
//Task toEdit = new Task(internalList.get(editIndex));

if (args.contains(">")){
String[] beforeAndAfter = args.replaceAll(" ","").split(">");
String[] beforeAndAfter = args.replaceAll(" ","").split(">");
if (!toEdit.setTags(beforeAndAfter[0], beforeAndAfter[beforeAndAfter.length-1])){
assert false: "The target tag cannot be missing";
}
Expand All @@ -155,8 +157,20 @@ public boolean edit(ReadOnlyTask key, String args) throws IllegalValueException
}

internalList.set(editIndex, toEdit);
return true;
return true;

} else if (keyword.equals(EditCommand.ADD_WORD)) {
String[] newTag = args.replaceAll(" ", "").replaceFirst("#", "").split("#");
final Set<Tag> tagSet = new HashSet<>();
for (int i = 0; i < newTag.length; i++) {
tagSet.add(new Tag(newTag[i]));
}
UniqueTagList addTagList = new UniqueTagList(tagSet);
toEdit.addTags(addTagList);
internalList.set(editIndex, toEdit);
return true;
}

else {
return false;
}
Expand Down

0 comments on commit cbcedfa

Please sign in to comment.