Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Elime1 committed Nov 26, 2016
1 parent 714a863 commit 043f5dd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
31 changes: 18 additions & 13 deletions src/main/java/elime/piceditor/application/App.java
Expand Up @@ -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;

Expand All @@ -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 -> {
Expand All @@ -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<String> 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();
}
Expand Down
41 changes: 27 additions & 14 deletions src/main/java/elime/piceditor/controllers/MainViewController.java
Expand Up @@ -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!");
}
}
}

Expand Down

0 comments on commit 043f5dd

Please sign in to comment.