This commit is contained in:
Oystein 2021-04-26 21:47:02 +02:00
commit 9b2d70c876
8 changed files with 69 additions and 25 deletions

View File

@ -62,9 +62,9 @@ public class FiletreeController implements Initializable, Controller {
} catch (Exception e) { } catch (Exception e) {
Model.setProjectPath(Optional.empty()); Model.setProjectPath(Optional.empty());
DialogBoxes.showErrorMessage( DialogBoxes.showErrorMessage(
"Could not open folder.\n\n" "Could not open directory.\n\n"
+ "Do you have the right permissions for this folder?\n" + "Do you have the right permissions for this directory?\n"
+ "Or does the folder contain any shortcut to somewhere within itself?" + "Or does the directory contain any shortcut to somewhere within itself?"
); );
} }
} }

View File

@ -100,7 +100,7 @@ public class MenubarController implements Initializable, Controller {
Stage stage = (Stage) menubar.getScene().getWindow(); Stage stage = (Stage) menubar.getScene().getWindow();
try { try {
File dir = FileOperations.openFolderWithDialog(stage); File dir = FileOperations.openDirectoryWithDialog(stage);
this.eventBus.post(new OpenProjectEvent(Optional.of(dir.toPath()))); this.eventBus.post(new OpenProjectEvent(Optional.of(dir.toPath())));
} catch (FileNotFoundException e) {} } catch (FileNotFoundException e) {}

View File

@ -6,15 +6,15 @@ import java.util.Optional;
import app.model.Model; import app.model.Model;
/** /**
* Event signalizing that a folder is supposed to be opened in the filetree. * Event signalizing that a directory is supposed to be opened in the filetree.
*/ */
public class OpenProjectEvent extends Event { public class OpenProjectEvent extends Event {
private Optional<Path> path; private Optional<Path> path;
/** /**
* Event signalizing that a folder is supposed to be opened in the filetree. * Event signalizing that a directory is supposed to be opened in the filetree.
* @param path The path of the folder to be opened * @param path The path of the directory to be opened
*/ */
public OpenProjectEvent(Optional<Path> path) { public OpenProjectEvent(Optional<Path> path) {
this.path = path; this.path = path;
@ -22,7 +22,7 @@ public class OpenProjectEvent extends Event {
} }
/** /**
* @return The path of the folder to be opened * @return The path of the directory to be opened
*/ */
public Optional<Path> getPath() { public Optional<Path> getPath() {
return this.path; return this.path;

View File

@ -9,6 +9,10 @@ import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import javafx.stage.Stage; import javafx.stage.Stage;
/**
* Class containing static methods for different kinds of popup window interactions
* with the user.
*/
public class DialogBoxes { public class DialogBoxes {
private DialogBoxes() {} private DialogBoxes() {}
@ -16,11 +20,24 @@ public class DialogBoxes {
private static DirectoryChooser dc = new DirectoryChooser(); private static DirectoryChooser dc = new DirectoryChooser();
private static Alert error = new Alert(AlertType.ERROR); private static Alert error = new Alert(AlertType.ERROR);
/**
* Shows a specified message to the user with an error icon.
*
* @param errorMessage The message to show the user
*/
public static void showErrorMessage(String errorMessage) { public static void showErrorMessage(String errorMessage) {
error.setContentText(errorMessage); error.setContentText(errorMessage);
error.showAndWait(); error.showAndWait();
} }
/**
* Shows an OS specific file chooser to choose a file 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 file chosen through the dialog window
*/
public static File showopenFileWithDialog(Stage stage) { public static File showopenFileWithDialog(Stage stage) {
fc.setTitle("Open File"); fc.setTitle("Open File");
File chosenFile = fc.showOpenDialog(stage); File chosenFile = fc.showOpenDialog(stage);
@ -28,13 +45,29 @@ public class DialogBoxes {
return chosenFile; return chosenFile;
} }
public static File showOpenFolderWithDialog(Stage stage) { /**
* Shows an OS specific directory chooser to choose a directory 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 file chosen through the dialog window
*/
public static File showOpenDirectoryWithDialog(Stage stage) {
dc.setTitle("Open Project"); dc.setTitle("Open Project");
File dir = dc.showDialog(stage); File dir = dc.showDialog(stage);
return dir; 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) { public static File showSaveFileWithDialog(Stage stage) {
FileChooser fc = new FileChooser(); FileChooser fc = new FileChooser();
fc.setTitle("Save as"); fc.setTitle("Save as");

View File

@ -15,9 +15,13 @@ public class FileOperations {
private 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 { public static File openFileWithDialog(Stage stage) throws FileNotFoundException {
File chosenFile = DialogBoxes.showopenFileWithDialog(stage); 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) if (dir == null)
throw new FileNotFoundException(); throw new FileNotFoundException();

View File

@ -71,7 +71,7 @@ public class FiletreeOperations {
} }
/** /**
* A helping function to sort the files/folders in the fileTree so that it shows * A helping function to sort the files/directories in the fileTree so that it shows
* in the correct order. * in the correct order.
*/ */
private static void sortFiles(List<File> dirList, List<File> fileList, File file) { private static void sortFiles(List<File> dirList, List<File> fileList, File file) {
@ -119,13 +119,13 @@ public class FiletreeOperations {
.getProjectPath() .getProjectPath()
.orElseThrow(() -> new IllegalStateException()); .orElseThrow(() -> new IllegalStateException());
final String rootFolderName = final String rootDirName =
projectPath projectPath
.getFileName() .getFileName()
.toString(); .toString();
String path = ""; String path = "";
while (!rootFolderName.equals(item.getValue())) { while (!rootDirName.equals(item.getValue())) {
path = File.separator + item.getValue() + path; path = File.separator + item.getValue() + path;
item = item.getParent(); item = item.getParent();
if (item == null) if (item == null)

View File

@ -78,20 +78,20 @@ public class FileOperationsTest {
// @Test // @Test
// @DisplayName("Test openFolderWithDialog") // @DisplayName("Test openDirectoryWithDialog")
// public void testOpenFolderWithDialog() { // public void testOpenDirectoryWithDialog() {
// try (MockedStatic<DialogBoxes> mocked = mockStatic(DialogBoxes.class)) { // try (MockedStatic<DialogBoxes> mocked = mockStatic(DialogBoxes.class)) {
// Stage stage = mock(Stage.class); // Stage stage = mock(Stage.class);
// mocked.when(() -> DialogBoxes.showOpenFolderWithDialog(any())) // mocked.when(() -> DialogBoxes.showOpenDirectoryWithDialog(any()))
// .thenReturn(null); // .thenReturn(null);
// assertThrows(FileNotFoundException.class, () -> FileOperations.openFolderWithDialog(stage)); // assertThrows(FileNotFoundException.class, () -> FileOperations.openDirectoryWithDialog(stage));
// File file = mock(File.class); // File file = mock(File.class);
// mocked.when(() -> DialogBoxes.showOpenFolderWithDialog(any())) // mocked.when(() -> DialogBoxes.showOpenDirectoryWithDialog(any()))
// .thenReturn(file); // .thenReturn(file);
// try { // try {
// assertEquals(file, FileOperations.openFolderWithDialog(stage)); // assertEquals(file, FileOperations.openDirectoryWithDialog(stage));
// } catch (FileNotFoundException e) { // } catch (FileNotFoundException e) {
// fail("Chosen file was null when it was expected to be mock file"); // fail("Chosen file was null when it was expected to be mock file");
// } // }

View File

@ -41,8 +41,8 @@ public class FileTreeOperationsTest {
} }
private void createRecursiveSymlink() throws IOException { private void createRecursiveSymlink() throws IOException {
File folders = new File(tmp, "test/innerFolder/"); File dirs = new File(tmp, "test/innerFolder/");
folders.mkdirs(); dirs.mkdirs();
Path target = Paths.get(tmp.toPath().toString(), "test"); Path target = Paths.get(tmp.toPath().toString(), "test");
Path link = Paths.get(tmp.toPath().toString(), "test/innerFolder/test"); Path link = Paths.get(tmp.toPath().toString(), "test/innerFolder/test");