forked from nus-cs2103-AY2021S1/tp
-
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.
Merge pull request nus-cs2103-AY2021S1#91 from luo-git/Branch-OpenCom…
…mand Add OpenCommand and OpenCommandParser
- Loading branch information
Showing
25 changed files
with
584 additions
and
147 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/java/seedu/address/logic/commands/OpenCommand.java
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,72 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG_NAME; | ||
|
||
import java.awt.Desktop; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import javafx.collections.transformation.FilteredList; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.tag.Tag; | ||
import seedu.address.model.tag.TagName; | ||
|
||
public class OpenCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "open"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Opens the file specified in the filepath of a tag. " | ||
+ "\nParameters: " | ||
+ PREFIX_TAG_NAME + "TAG_NAME " | ||
+ "\nExample: " + COMMAND_WORD + " " | ||
+ PREFIX_TAG_NAME + "cs2103 "; | ||
|
||
public static final String MESSAGE_SUCCESS = "File opened! Tag: %1$s"; | ||
public static final String MESSAGE_TAG_NOT_FOUND = "Tag '%s' not found!"; | ||
public static final String MESSAGE_FILE_NOT_FOUND = "The file: %s doesn't exist."; | ||
|
||
private final TagName tagName; | ||
|
||
/** | ||
* Creates an OpenCommand to open the file specified in the {@code Tag} | ||
*/ | ||
public OpenCommand(TagName tagName) { | ||
this.tagName = tagName; | ||
} | ||
|
||
/** | ||
* Executes the command and opens the file specified by tagName. | ||
* | ||
* @param model {@code Model} which the command should operate on. | ||
* @throws CommandException if tagName cannot be found in model. | ||
*/ | ||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
// Check if tagName is in tag list | ||
FilteredList<Tag> tagList = | ||
model.getAddressBook().getTagList().filtered(x -> x.getTagName().equals(tagName)); | ||
if (tagList.isEmpty()) { | ||
throw new CommandException(String.format(MESSAGE_TAG_NOT_FOUND, tagName.toString())); | ||
} | ||
|
||
// Get tag and prepare file to be opened | ||
Tag tagToOpen = tagList.get(0); | ||
File file = new File(tagToOpen.getFileAddress().value); | ||
|
||
try { | ||
// Open file using java.awt.Desktop | ||
Desktop.getDesktop().open(file); | ||
} catch (IOException | IllegalArgumentException e) { | ||
throw new CommandException(e.getMessage(), e); | ||
} | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, tagToOpen.toString())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof OpenCommand // instanceof handles nulls | ||
&& tagName.equals(((OpenCommand) other).tagName)); | ||
} | ||
} |
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
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
41 changes: 41 additions & 0 deletions
41
src/main/java/seedu/address/logic/parser/OpenCommandParser.java
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,41 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG_NAME; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.OpenCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.tag.TagName; | ||
|
||
public class OpenCommandParser implements Parser<OpenCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the OpenCommand | ||
* and returns an OpenCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
@Override | ||
public OpenCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_TAG_NAME); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_TAG_NAME) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, OpenCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
TagName tagName = ParserUtil.parseTagName(argMultimap.getValue(PREFIX_TAG_NAME).get()); | ||
|
||
return new OpenCommand(tagName); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
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
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
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
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,92 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import javafx.collections.ObservableList; | ||
import seedu.address.commons.core.GuiSettings; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.ReadOnlyAddressBook; | ||
import seedu.address.model.ReadOnlyUserPrefs; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* A default model stub that have all of the methods failing. | ||
*/ | ||
class ModelStub implements Model { | ||
@Override | ||
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public ReadOnlyUserPrefs getUserPrefs() { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public GuiSettings getGuiSettings() { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public void setGuiSettings(GuiSettings guiSettings) { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public Path getAddressBookFilePath() { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public void setAddressBookFilePath(Path addressBookFilePath) { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public void addTag(Tag tag) { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public void setAddressBook(ReadOnlyAddressBook newData) { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public ReadOnlyAddressBook getAddressBook() { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public boolean hasTag(Tag tag) { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public void deleteTag(Tag target) { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public void setTag(Tag target, Tag editedTag) { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public ObservableList<Tag> getFilteredTagList() { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public void updateFilteredTagList(Predicate<Tag> predicate) { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
|
||
@Override | ||
public List<Tag> findFilteredTagList(Predicate<Tag> predicate) { | ||
throw new AssertionError("This method should not be called."); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/seedu/address/logic/commands/ModelStubAcceptingTagAdded.java
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,34 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.ArrayList; | ||
|
||
import seedu.address.model.AddressBook; | ||
import seedu.address.model.ReadOnlyAddressBook; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* A Model stub that always accept the tag being added. | ||
*/ | ||
@Deprecated | ||
class ModelStubAcceptingTagAdded extends ModelStub { | ||
final ArrayList<Tag> tagsAdded = new ArrayList<>(); | ||
|
||
@Override | ||
public boolean hasTag(Tag tag) { | ||
requireNonNull(tag); | ||
return tagsAdded.stream().anyMatch(tag::isSameTag); | ||
} | ||
|
||
@Override | ||
public void addTag(Tag tag) { | ||
requireNonNull(tag); | ||
tagsAdded.add(tag); | ||
} | ||
|
||
@Override | ||
public ReadOnlyAddressBook getAddressBook() { | ||
return new AddressBook(); | ||
} | ||
} |
Oops, something went wrong.