From d63a5d90bfb57253098e9c55d08e72b7ae23452f Mon Sep 17 00:00:00 2001 From: h7x4 Date: Tue, 20 Apr 2021 23:42:33 +0200 Subject: [PATCH] Make nullable paths explicitly nullable --- .../app/controllers/EditorController.java | 10 ++++-- .../app/controllers/FiletreeController.java | 5 ++- .../app/controllers/MenubarController.java | 11 +++---- .../java/app/events/FileSelectedEvent.java | 11 +++++-- src/main/java/app/events/OpenFileEvent.java | 32 +++++++++++-------- .../java/app/events/OpenProjectEvent.java | 4 +++ src/main/java/app/model/Model.java | 13 ++++---- src/main/java/app/service/FileOperations.java | 6 ++-- .../java/app/service/FiletreeOperations.java | 2 +- .../app/controllers/EditorControllerTest.java | 9 ++---- 10 files changed, 57 insertions(+), 46 deletions(-) diff --git a/src/main/java/app/controllers/EditorController.java b/src/main/java/app/controllers/EditorController.java index 4e93761..1b15b62 100644 --- a/src/main/java/app/controllers/EditorController.java +++ b/src/main/java/app/controllers/EditorController.java @@ -146,8 +146,7 @@ public class EditorController implements Initializable, Controller { * * @param filePath The path of the file */ - public void setEditorContent(Path filePath) { - String newContent = FileOperations.readFile(filePath); + public void setEditorContent(String newContent) { editor.clear(); editor.appendText(newContent); } @@ -182,7 +181,12 @@ public class EditorController implements Initializable, Controller { */ @Subscribe public void handle(FileSelectedEvent event) { - this.setEditorContent(event.getPath()); + String newContent = + event + .getPath() + .map(path -> FileOperations.readFile(path)) + .orElse(""); + this.setEditorContent(newContent); } /** diff --git a/src/main/java/app/controllers/FiletreeController.java b/src/main/java/app/controllers/FiletreeController.java index bec339b..45a0bab 100644 --- a/src/main/java/app/controllers/FiletreeController.java +++ b/src/main/java/app/controllers/FiletreeController.java @@ -10,6 +10,7 @@ import java.io.File; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Optional; import java.util.ResourceBundle; import com.google.common.eventbus.EventBus; @@ -75,9 +76,7 @@ public class FiletreeController implements Initializable, Controller { Path path = FiletreeOperations.getPathOfTreeItem(item); if (!Files.isDirectory(path)) { - // TODO: Add setActiveFilePath Model modification into FileSelectedEvent - Model.setActiveFilePath(path); - this.eventBus.post(new FileSelectedEvent(path)); + this.eventBus.post(new FileSelectedEvent(Optional.ofNullable(path))); } } } diff --git a/src/main/java/app/controllers/MenubarController.java b/src/main/java/app/controllers/MenubarController.java index 28ee796..fbd8e3e 100644 --- a/src/main/java/app/controllers/MenubarController.java +++ b/src/main/java/app/controllers/MenubarController.java @@ -3,6 +3,7 @@ package app.controllers; import java.io.File; import java.io.FileNotFoundException; import java.net.URL; +import java.util.Optional; import java.util.ResourceBundle; import com.google.common.eventbus.EventBus; @@ -33,6 +34,7 @@ import javafx.scene.control.MenuBar; import javafx.scene.control.RadioMenuItem; import javafx.scene.control.ToggleGroup; import javafx.stage.Stage; +import java.util.Optional; /** * A FXML controller that controls the menubar component of the UI @@ -65,9 +67,7 @@ public class MenubarController implements Initializable, Controller { /* ------------------------------------------------------------------------ */ @FXML private void handleNewFile() { - // TODO: Move Model modification inside event - Model.setActiveFilePath(null); - this.eventBus.post(new FileSelectedEvent(null)); + this.eventBus.post(new FileSelectedEvent(Optional.empty())); } @FXML @@ -89,9 +89,7 @@ public class MenubarController implements Initializable, Controller { try { File file = FileOperations.openFileWithDialog(stage); - // TODO: Move Model modification inside event - Model.setActiveFilePath(file.toPath()); - this.eventBus.post(new FileSelectedEvent(file.toPath())); + this.eventBus.post(new FileSelectedEvent(Optional.ofNullable(file.toPath()))); } catch (FileNotFoundException e) { DialogBoxes.showErrorMessage("File not found!"); } @@ -108,7 +106,6 @@ public class MenubarController implements Initializable, Controller { File dir = FileOperations.openFolderWithDialog(stage); // TODO: Move Model modification inside event - Model.setProjectPath(dir.toPath()); this.eventBus.post(new OpenProjectEvent(dir.toPath())); } catch (FileNotFoundException e) { DialogBoxes.showErrorMessage("Folder not found!"); diff --git a/src/main/java/app/events/FileSelectedEvent.java b/src/main/java/app/events/FileSelectedEvent.java index 21647c7..a4d7edb 100644 --- a/src/main/java/app/events/FileSelectedEvent.java +++ b/src/main/java/app/events/FileSelectedEvent.java @@ -2,6 +2,10 @@ package app.events; import java.nio.file.Path; +import java.util.Optional; + +import app.model.Model; + /** * Event signalizing that a file was selected in the filetree. * @@ -9,7 +13,7 @@ import java.nio.file.Path; */ public class FileSelectedEvent extends Event { - private Path path; + private Optional path; /** * Event signalizing that a file was selected in the filetree. @@ -17,14 +21,15 @@ public class FileSelectedEvent extends Event { * Not to be confused with {@link OpenFileEvent} * @param path The path of the selected file */ - public FileSelectedEvent(Path path) { + public FileSelectedEvent(Optional path) { this.path = path; + Model.setActiveFilePath(path); } /** * @return The path of the selected file */ - public Path getPath() { + public Optional getPath() { return this.path; } diff --git a/src/main/java/app/events/OpenFileEvent.java b/src/main/java/app/events/OpenFileEvent.java index 0c7b9ec..c27239b 100644 --- a/src/main/java/app/events/OpenFileEvent.java +++ b/src/main/java/app/events/OpenFileEvent.java @@ -1,6 +1,9 @@ package app.events; import java.nio.file.Path; +import java.util.Optional; + +import app.model.Model; /** * Event signalizing that a file outside the current project is supposed to be opened in the editor. @@ -9,21 +12,22 @@ import java.nio.file.Path; */ public class OpenFileEvent extends Event { - private Path path; + private Optional path; - /** - * Event signalizing that a file outside the current project is supposed to be opened in the editor. - * @param path The path of the file to be opened - */ - public OpenFileEvent(Path path) { - this.path = path; - } + /** + * Event signalizing that a file outside the current project is supposed to be opened in the editor. + * @param path The path of the file to be opened + */ + public OpenFileEvent(Optional path) { + this.path = path; + Model.setActiveFilePath(path); + } - /** - * @return The path of the file to be opened - */ - public Path getPath() { - return this.path; - } + /** + * @return The path of the file to be opened + */ + public Optional getPath() { + return this.path; + } } diff --git a/src/main/java/app/events/OpenProjectEvent.java b/src/main/java/app/events/OpenProjectEvent.java index 41e058a..3e45d6a 100644 --- a/src/main/java/app/events/OpenProjectEvent.java +++ b/src/main/java/app/events/OpenProjectEvent.java @@ -1,6 +1,9 @@ package app.events; import java.nio.file.Path; +import java.util.Optional; + +import app.model.Model; /** * Event signalizing that a folder is supposed to be opened in the filetree. @@ -15,6 +18,7 @@ public class OpenProjectEvent extends Event { */ public OpenProjectEvent(Path path) { this.path = path; + Model.setProjectPath(Optional.of(path)); } /** diff --git a/src/main/java/app/model/Model.java b/src/main/java/app/model/Model.java index 00102b4..850ce95 100644 --- a/src/main/java/app/model/Model.java +++ b/src/main/java/app/model/Model.java @@ -1,6 +1,7 @@ package app.model; import java.nio.file.Path; +import java.util.Optional; import app.settings.SettingsProvider; import javafx.scene.Scene; @@ -13,26 +14,26 @@ import javafx.scene.Scene; */ public class Model { private static boolean fileIsSaved; - private static Path activeFilePath; - private static Path currentProjectPath; + private static Optional activeFilePath; + private static Optional currentProjectPath; private static ProgrammingLanguage currentProgrammingLanguage; private static String theme; private static Scene scene; private static SettingsProvider settings; - public static Path getActiveFilePath() { + public static Optional getActiveFilePath() { return activeFilePath; } - public static void setActiveFilePath(Path path) { + public static void setActiveFilePath(Optional path) { Model.activeFilePath = path; } - public static Path getProjectPath() { + public static Optional getProjectPath() { return currentProjectPath; } - public static void setProjectPath(Path path) { + public static void setProjectPath(Optional path) { Model.currentProjectPath = path; } diff --git a/src/main/java/app/service/FileOperations.java b/src/main/java/app/service/FileOperations.java index 7ffc89b..f710e1b 100644 --- a/src/main/java/app/service/FileOperations.java +++ b/src/main/java/app/service/FileOperations.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.nio.file.Path; +import java.util.Optional; import java.util.Scanner; import app.model.Model; @@ -64,8 +65,7 @@ public class FileOperations { FileChooser fc = new FileChooser(); fc.setTitle("Save as"); - if (Model.getProjectPath() != null) - fc.setInitialDirectory(Model.getProjectPath().toFile()); + Model.getProjectPath().ifPresent(path -> fc.setInitialDirectory(path.toFile())); FileChooser.ExtensionFilter extJava = new FileChooser.ExtensionFilter("Java", "*.java"); FileChooser.ExtensionFilter extMd = new FileChooser.ExtensionFilter("Markdown", "*.md"); @@ -77,7 +77,7 @@ public class FileOperations { return false; if (saveFile(chosenLocation.getAbsolutePath(), content)) { - Model.setActiveFilePath(chosenLocation.toPath()); + Model.setActiveFilePath(Optional.of(chosenLocation.toPath())); return true; } diff --git a/src/main/java/app/service/FiletreeOperations.java b/src/main/java/app/service/FiletreeOperations.java index 13e9344..71e8b62 100644 --- a/src/main/java/app/service/FiletreeOperations.java +++ b/src/main/java/app/service/FiletreeOperations.java @@ -130,7 +130,7 @@ public class FiletreeOperations { } public static Path getPathOfTreeItem(TreeItem item) { - String root = Model.getProjectPath().getFileName().toString(); + String root = Model.getProjectPath().orElseThrow().getFileName().toString(); String path = ""; while (!root.equals(item.getValue())) { path = File.separator + item.getValue() + path; diff --git a/src/test/java/app/controllers/EditorControllerTest.java b/src/test/java/app/controllers/EditorControllerTest.java index 48caa2e..71e7de9 100644 --- a/src/test/java/app/controllers/EditorControllerTest.java +++ b/src/test/java/app/controllers/EditorControllerTest.java @@ -2,9 +2,7 @@ package app.controllers; import java.io.File; import java.io.IOException; -import java.nio.file.Files; -import java.util.List; -import java.util.stream.Collectors; +import java.util.Optional; import org.fxmisc.richtext.CodeArea; import org.fxmisc.richtext.model.StyleSpans; @@ -16,7 +14,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; @@ -94,7 +91,7 @@ public class EditorControllerTest extends FxTestTemplate { mocked.when(() -> FileOperations.readFile(any())) .thenReturn(""); - eventBus.post(new FileSelectedEvent(file.toPath())); + eventBus.post(new FileSelectedEvent(Optional.of(file.toPath()))); mocked.verify(() -> FileOperations.readFile(any())); } @@ -105,7 +102,7 @@ public class EditorControllerTest extends FxTestTemplate { public void testFileSelectedEventWithUnrealFile() throws IOException { String brokenFilePath = "/doesNotExist.txt"; - eventBus.post(new FileSelectedEvent(new File(brokenFilePath).toPath())); + eventBus.post(new FileSelectedEvent(Optional.ofNullable(new File(brokenFilePath).toPath()))); verify(editor, never()).clear(); }