diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java index 6b5741fab46419d3bb8b62d04425da3c156f7f22..f9e49bc5dcc03c2eb2fedb6d120878ef1df87a5e 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java @@ -3,6 +3,7 @@ package edu.ntnu.idatt2001.group_30.paths.controller; import static edu.ntnu.idatt2001.group_30.paths.PathsSingleton.INSTANCE; import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileReader; +import edu.ntnu.idatt2001.group_30.paths.view.views.NewStoryView; import edu.ntnu.idatt2001.group_30.paths.view.views.PlaythroughView; import java.io.File; import java.io.IOException; diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java index 1d1a7eee8568f7c1b78c77479f2caacaf6187308..80537a9b8aec8cdb999af750fbb295120aaa15aa 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java @@ -2,8 +2,7 @@ package edu.ntnu.idatt2001.group_30.paths.controller; import edu.ntnu.idatt2001.group_30.paths.model.Passage; import edu.ntnu.idatt2001.group_30.paths.model.Story; -import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileHandler; -import edu.ntnu.idatt2001.group_30.paths.view.components.pop_up.AlertDialog; +import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileWriter; import edu.ntnu.idatt2001.group_30.paths.view.views.NewStoryView; import javafx.stage.FileChooser; @@ -36,8 +35,8 @@ public class NewStoryController extends Controller{ File selectedFile = fileChooser.showSaveDialog(null); if (selectedFile != null) { - StoryFileHandler fileHandler = new StoryFileHandler(); - fileHandler.createStoryFile(story, selectedFile); + StoryFileWriter storyFileWriter = new StoryFileWriter(); + storyFileWriter.create(story, selectedFile); } } } diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriter.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriter.java index 40256fd7981c102e3eb8f9d8a2a9b6a1f83ce154..673d0d5ec0bba7cfcd70dc0680b352594a768942 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriter.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriter.java @@ -20,7 +20,24 @@ import java.util.*; public class StoryFileWriter { /** - * Creates a new story file with the given name and writes the story to it. + * This method takes a story and writes its contents to a .paths file. The story information is transcribed + * in the given format: + * <pre> + * Story title + * + * ::Opening Passage Title + * Opening Passage Content + * [Link Text](Link Reference) + * + * ::Another Passage Title + * Passage Content + * [Link Text](Link Reference) + * {@code <Action Type>}\Action Value/ + * [Link Text](Link Reference) + * + * ... + * </pre> + * * @param story The story to be written to the file. * @param fileName The name of the file to be created. * @throws IOException if an I/O error occurs with the writer, or if the file already exists. @@ -30,14 +47,48 @@ public class StoryFileWriter { Objects.requireNonNull(fileName, "File name cannot be null"); File file = FileHandler.createFile(fileName); + + create(story, file); + } + + /** + * This method takes a story and writes its contents to a .paths file. The story information is transcribed + * in the given format: + * <pre> + * Story title + * + * ::Opening Passage Title + * Opening Passage Content + * [Link Text](Link Reference) + * + * ::Another Passage Title + * Passage Content + * [Link Text](Link Reference) + * {@code <Action Type>}\Action Value/ + * [Link Text](Link Reference) + * + * ... + * </pre> + * + * @param story The story to be written to the file. + * @param file The file the story is going to be written to. + * @throws IOException if an I/O error occurs with the writer, or if the file already exists. + */ + public void create(Story story, File file) throws IOException { + Objects.requireNonNull(story, "Story cannot be null"); + Objects.requireNonNull(file, "File cannot be null"); + if (FileHandler.fileExists(file)) throw new FileAlreadyExistsException( - "You cannot overwrite a pre-existing story file" + "You cannot overwrite a pre-existing story file" ); /* propagate any errors while writing */ writeStory(story, file); } + + //TODO: add test for story files... + /** * Writes the story to the given file. * @param story The story to be written to the file. diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java index f84736ff170ad7c4a8677174e0d876416a79e35e..4332df530ebe4ef099860267cd85b0bd4225113f 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java @@ -8,7 +8,6 @@ import edu.ntnu.idatt2001.group_30.paths.model.Story; import static edu.ntnu.idatt2001.group_30.paths.PathsSingleton.INSTANCE; -import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileHandler; import edu.ntnu.idatt2001.group_30.paths.view.components.pop_up.AlertDialog; import edu.ntnu.idatt2001.group_30.paths.view.components.pop_up.PassagePopUp; import edu.ntnu.idatt2001.group_30.paths.view.components.table.PassageTable; @@ -25,10 +24,7 @@ import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.text.Text; -import javafx.stage.FileChooser; -import java.io.File; -import java.io.IOException; import java.net.URL; import java.util.stream.Collectors; diff --git a/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriterImplTest.java b/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriterImplTest.java index d6815495ee00802ed3a0bc64e5d0f9c02c2c5161..0adf5335ce2c7717b5f744bd193f3708e668d228 100644 --- a/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriterImplTest.java +++ b/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriterImplTest.java @@ -220,7 +220,7 @@ public class StoryFileWriterImplTest { Assertions.assertThrows( NullPointerException.class, () -> { - storyFileWriter.create(story, null); + storyFileWriter.create(story, (String) null); } ); }