DialogBoxes

This commit is contained in:
Oystein 2021-04-26 11:36:28 +02:00
parent 075988f327
commit 08f9b2b881
4 changed files with 72 additions and 21 deletions

View File

@ -1,16 +1,51 @@
package app.service; package app.service;
import java.io.File;
import app.model.Model;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Alert.AlertType;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class DialogBoxes { public class DialogBoxes {
private DialogBoxes() {} private DialogBoxes() {}
private static FileChooser fc = new FileChooser();
private static DirectoryChooser dc = new DirectoryChooser();
private static Alert error = new Alert(AlertType.ERROR);
public static void showErrorMessage(String errorMessage) { public static void showErrorMessage(String errorMessage) {
Alert error = new Alert(AlertType.ERROR);
error.setContentText(errorMessage); error.setContentText(errorMessage);
error.showAndWait(); error.showAndWait();
} }
public static File showopenFileWithDialog(Stage stage) {
fc.setTitle("Open File");
File chosenFile = fc.showOpenDialog(stage);
return chosenFile;
}
public static File showOpenFolderWithDialog(Stage stage) {
dc.setTitle("Open Project");
File dir = dc.showDialog(stage);
return dir;
}
public static File showSaveFileWithDialog(Stage stage) {
FileChooser fc = new FileChooser();
fc.setTitle("Save as");
Model
.getProjectPath()
.ifPresent(path -> fc.setInitialDirectory(path.toFile()));
File chosenLocation = fc.showSaveDialog(stage);
return chosenLocation;
}
} }

View File

@ -8,8 +8,6 @@ import java.util.Optional;
import java.util.Scanner; import java.util.Scanner;
import app.model.Model; import app.model.Model;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage; import javafx.stage.Stage;
public class FileOperations { public class FileOperations {
@ -20,11 +18,7 @@ public class FileOperations {
// TODO: This class needs to be extensively error checked // TODO: This class needs to be extensively error checked
public static File openFileWithDialog(Stage stage) throws FileNotFoundException { public static File openFileWithDialog(Stage stage) throws FileNotFoundException {
File chosenFile = DialogBoxes.showopenFileWithDialog(stage);
FileChooser fc = new FileChooser();
fc.setTitle("Open File");
File chosenFile = fc.showOpenDialog(stage);
if (chosenFile == null) if (chosenFile == null)
throw new FileNotFoundException(); throw new FileNotFoundException();
@ -34,11 +28,7 @@ public class FileOperations {
} }
public static File openFolderWithDialog(Stage stage) throws FileNotFoundException { public static File openFolderWithDialog(Stage stage) throws FileNotFoundException {
File dir = DialogBoxes.showOpenFolderWithDialog(stage);
DirectoryChooser dc = new DirectoryChooser();
dc.setTitle("Open Project");
File dir = dc.showDialog(stage);
if (dir == null) if (dir == null)
throw new FileNotFoundException(); throw new FileNotFoundException();
@ -58,14 +48,7 @@ public class FileOperations {
} }
public static boolean saveFileWithDialog(Stage stage, String content) { public static boolean saveFileWithDialog(Stage stage, String content) {
FileChooser fc = new FileChooser(); File chosenLocation = DialogBoxes.showSaveFileWithDialog(stage);
fc.setTitle("Save as");
Model
.getProjectPath()
.ifPresent(path -> fc.setInitialDirectory(path.toFile()));
File chosenLocation = fc.showSaveDialog(stage);
if (chosenLocation == null) if (chosenLocation == null)
return false; return false;

View File

@ -0,0 +1,11 @@
package app.service;
public class DialogBoxesTest {
}

View File

@ -0,0 +1,22 @@
package app.settings;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class SettingsProviderTest {
@Test
@DisplayName("Test load settings")
public void testLoadSettings() {
}
@Test
@DisplayName("Test save settings")
public void testSaveSettings() {
}
}