From 0793287044d0f9a5590e23403650c41acc5bf1e0 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 26 Apr 2021 21:46:54 +0200 Subject: [PATCH] Change Folder to Directory once again --- .../app/controllers/MenubarController.java | 2 +- src/main/java/app/service/DialogBoxes.java | 10 ++++++++- src/main/java/app/service/FileOperations.java | 21 ++++++++++++++----- .../java/app/service/FiletreeOperations.java | 4 ++-- .../java/app/service/FileOperationsTest.java | 12 +++++------ 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/main/java/app/controllers/MenubarController.java b/src/main/java/app/controllers/MenubarController.java index 34bafd9..4952a72 100644 --- a/src/main/java/app/controllers/MenubarController.java +++ b/src/main/java/app/controllers/MenubarController.java @@ -100,7 +100,7 @@ public class MenubarController implements Initializable, Controller { Stage stage = (Stage) menubar.getScene().getWindow(); try { - File dir = FileOperations.openFolderWithDialog(stage); + File dir = FileOperations.openDirectoryWithDialog(stage); this.eventBus.post(new OpenProjectEvent(Optional.of(dir.toPath()))); } catch (FileNotFoundException e) {} diff --git a/src/main/java/app/service/DialogBoxes.java b/src/main/java/app/service/DialogBoxes.java index b57b7ba..741da20 100644 --- a/src/main/java/app/service/DialogBoxes.java +++ b/src/main/java/app/service/DialogBoxes.java @@ -53,13 +53,21 @@ public class DialogBoxes { * * @return The file chosen through the dialog window */ - public static File showOpenFolderWithDialog(Stage stage) { + public static File showOpenDirectoryWithDialog(Stage stage) { dc.setTitle("Open Project"); File dir = dc.showDialog(stage); return dir; } + /** + * Shows an OS specific file chooser to specifyy a new path on the disk + * + * @param stage The JavaFX stage to connect to the dialog box. This is needed + * for the window to be able to run on the JavaFX thread. + * + * @return The filepath chosen through the dialog window + */ public static File showSaveFileWithDialog(Stage stage) { FileChooser fc = new FileChooser(); fc.setTitle("Save as"); diff --git a/src/main/java/app/service/FileOperations.java b/src/main/java/app/service/FileOperations.java index 6215aca..64b6200 100644 --- a/src/main/java/app/service/FileOperations.java +++ b/src/main/java/app/service/FileOperations.java @@ -15,9 +15,13 @@ public class FileOperations { private FileOperations() {} - // TODO: Needs documentation and cleanup - // TODO: This class needs to be extensively error checked - + /** + * A function to get a file through a dialog + * + * @param stage A JavaFX stage is required to show the dialog + * @return The chosen file + * @throws FileNotFoundException + */ public static File openFileWithDialog(Stage stage) throws FileNotFoundException { File chosenFile = DialogBoxes.showopenFileWithDialog(stage); @@ -28,8 +32,15 @@ public class FileOperations { } - public static File openFolderWithDialog(Stage stage) throws FileNotFoundException { - File dir = DialogBoxes.showOpenFolderWithDialog(stage); + /** + * A function to get a file through a dialog + * + * @param stage A JavaFX stage is required to show the dialog + * @return The chosen file + * @throws FileNotFoundException + */ + public static File openDirectoryWithDialog(Stage stage) throws FileNotFoundException { + File dir = DialogBoxes.showOpenDirectoryWithDialog(stage); if (dir == null) throw new FileNotFoundException(); diff --git a/src/main/java/app/service/FiletreeOperations.java b/src/main/java/app/service/FiletreeOperations.java index 75b68ef..5d64efe 100644 --- a/src/main/java/app/service/FiletreeOperations.java +++ b/src/main/java/app/service/FiletreeOperations.java @@ -119,13 +119,13 @@ public class FiletreeOperations { .getProjectPath() .orElseThrow(() -> new IllegalStateException()); - final String rootFolderName = + final String rootDirName = projectPath .getFileName() .toString(); String path = ""; - while (!rootFolderName.equals(item.getValue())) { + while (!rootDirName.equals(item.getValue())) { path = File.separator + item.getValue() + path; item = item.getParent(); if (item == null) diff --git a/src/test/java/app/service/FileOperationsTest.java b/src/test/java/app/service/FileOperationsTest.java index 2ccedaa..d69d0ab 100644 --- a/src/test/java/app/service/FileOperationsTest.java +++ b/src/test/java/app/service/FileOperationsTest.java @@ -78,20 +78,20 @@ public class FileOperationsTest { // @Test - // @DisplayName("Test openFolderWithDialog") - // public void testOpenFolderWithDialog() { + // @DisplayName("Test openDirectoryWithDialog") + // public void testOpenDirectoryWithDialog() { // try (MockedStatic mocked = mockStatic(DialogBoxes.class)) { // Stage stage = mock(Stage.class); - // mocked.when(() -> DialogBoxes.showOpenFolderWithDialog(any())) + // mocked.when(() -> DialogBoxes.showOpenDirectoryWithDialog(any())) // .thenReturn(null); - // assertThrows(FileNotFoundException.class, () -> FileOperations.openFolderWithDialog(stage)); + // assertThrows(FileNotFoundException.class, () -> FileOperations.openDirectoryWithDialog(stage)); // File file = mock(File.class); - // mocked.when(() -> DialogBoxes.showOpenFolderWithDialog(any())) + // mocked.when(() -> DialogBoxes.showOpenDirectoryWithDialog(any())) // .thenReturn(file); // try { - // assertEquals(file, FileOperations.openFolderWithDialog(stage)); + // assertEquals(file, FileOperations.openDirectoryWithDialog(stage)); // } catch (FileNotFoundException e) { // fail("Chosen file was null when it was expected to be mock file"); // }