From 043f5dd1f822d42e5a0b7b3e05e695589e1de982 Mon Sep 17 00:00:00 2001 From: Elime1 Date: Sat, 26 Nov 2016 23:08:30 +0100 Subject: [PATCH] Improve error handling --- .../java/elime/piceditor/application/App.java | 31 ++++++++------ .../controllers/MainViewController.java | 41 ++++++++++++------- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/main/java/elime/piceditor/application/App.java b/src/main/java/elime/piceditor/application/App.java index 763583f..3b17e37 100644 --- a/src/main/java/elime/piceditor/application/App.java +++ b/src/main/java/elime/piceditor/application/App.java @@ -9,6 +9,8 @@ import javafx.stage.Stage; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.ThreadContext; + import java.io.IOException; import java.util.List; @@ -35,7 +37,7 @@ public void start(Stage window) throws Exception { this.window.getIcons().add(new Image(App.class.getResourceAsStream("/img/icon.png"))); log.debug("Setup uncaught exception handler"); - Thread.currentThread().setUncaughtExceptionHandler(this::showErrorAlert); + Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> showErrorAlert(throwable)); //onCloseRequest is not triggered when calling window.close() so we use onHiding instead window.setOnHiding(event -> { @@ -56,29 +58,32 @@ public void start(Stage window) throws Exception { log.debug("Scene created"); + window.centerOnScreen(); + + log.debug("Showing application window"); + window.show(); + //Check for command-line argument List parameters = getParameters().getUnnamed(); if (!parameters.isEmpty()) { viewController.openPic(parameters.get(0)); } + } - window.centerOnScreen(); - - log.debug("Showing application window"); - window.show(); + private void showErrorAlert(Throwable throwable) { + String message = throwable.getMessage(); + if (message == null) { + message = throwable.toString(); + } + log.error(message, throwable); + showErrorAlert(message); } - private void showErrorAlert(Thread thread, Throwable throwable) { - log.error(throwable.getMessage(), throwable); + private void showErrorAlert(String message) { Alert alert = new Alert(Alert.AlertType.WARNING); alert.setTitle("Error"); alert.setHeaderText(null); - String message = throwable.getMessage(); - if (message == null) { - alert.setContentText(throwable.toString()); - } else { - alert.setContentText(message); - } + alert.setContentText(message); alert.initOwner(this.window); alert.show(); } diff --git a/src/main/java/elime/piceditor/controllers/MainViewController.java b/src/main/java/elime/piceditor/controllers/MainViewController.java index 53a9a3f..714a402 100644 --- a/src/main/java/elime/piceditor/controllers/MainViewController.java +++ b/src/main/java/elime/piceditor/controllers/MainViewController.java @@ -162,26 +162,39 @@ else if (event.isControlDown()) { } //////////////////////////////////////////////////////////////////////// - //Public methods - - public boolean openPic(String fileName) { - File file = new File(fileName); - if (file.exists()) { - log.debug("Opening pic: " + file.getPath()); - loadPic(file); - return true; + //Public + + public void openPic(String fileName) { + log.debug("Opening file: " + fileName); + String extension = fileName.substring(fileName.length() - 4); + extension = extension.toLowerCase(); + if (extension.equals(".pic")) { + openPic(new File(fileName)); + } else { + log.debug("The file is not a pic file: " + fileName); + showAlert("Failed to open pic", "The file '" + fileName + "' does not seem to be a pic file."); } - return false; } //////////////////////////////////////////////////////////////////////// - //Private methods + //Private private void openPic() { - File file = fileChooserService.showOpenPicDialog(window); - if (file != null) { - log.debug("Opening pic: " + file.getPath()); - loadPic(file); + openPic(fileChooserService.showOpenPicDialog(window)); + } + + private void openPic(File picFile) { + if (picFile != null) { + if (picFile.exists()) { + try { + loadPic(picFile); + } catch (Exception e) { + log.error("Failed to open pic", e); + showAlert("Failed to open pic", "The file '" + picFile.getPath() + "' could not be opened: " + e.toString()); + } + } else { + log.warn("The file '" + picFile.getPath() + " does not exist!"); + } } }