From f984bf7b3438d080a3a258ac02abfd2edeace116 Mon Sep 17 00:00:00 2001 From: Thomas Battermann Date: Sat, 20 Jun 2015 22:36:39 +0200 Subject: [PATCH] Ask on close --- .../dhbw/todolist/Controller.java | 55 +++++++++++++++++-- src/de/t_battermann/dhbw/todolist/close.fxml | 30 ++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/de/t_battermann/dhbw/todolist/close.fxml diff --git a/src/de/t_battermann/dhbw/todolist/Controller.java b/src/de/t_battermann/dhbw/todolist/Controller.java index c55751c..e8a90c3 100644 --- a/src/de/t_battermann/dhbw/todolist/Controller.java +++ b/src/de/t_battermann/dhbw/todolist/Controller.java @@ -287,11 +287,11 @@ public class Controller { } n = primaryStage.getScene().lookup("#menuClose"); if ( n != null && n instanceof Button ) { - ((Button) n).setOnAction(event -> Platform.exit()); + ((Button) n).setOnAction(event -> showCloseDialog()); }else{ ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#menuClose'"); } - this.primaryStage.setOnCloseRequest(event -> Platform.exit()); + this.primaryStage.setOnCloseRequest(event -> showCloseDialog()); n = primaryStage.getScene().lookup("#todoDetailSave"); if ( n != null && n instanceof Button) { ((Button) n).setOnAction(event -> this.saveTodoEntry()); @@ -497,6 +497,10 @@ public class Controller { } private void showSaveAs() { + showSaveAs(false); + } + + private void showSaveAs(boolean exitAfterSave) { Stage save = new Stage(); try { save.setScene(new Scene(FXMLLoader.load(getClass().getResource("saveAs.fxml")),500,150)); @@ -521,8 +525,13 @@ public class Controller { if (!file.isDirectory()) { if (this.filename == null) this.filename = f.getText(); - if (this.export(f.getText())) - save.close(); + if (this.export(f.getText())) { + if (exitAfterSave) { + Platform.exit(); + } else { + save.close(); + } + } } else { this.updateStatusLine("Can’t write to file!"); } @@ -928,6 +937,44 @@ public class Controller { }); } + private void showCloseDialog() { + Stage close = new Stage(); + try { + close.setScene(new Scene(FXMLLoader.load(getClass().getResource("close.fxml")))); + } catch (IOException e) { + ErrorPrinter.printError("showCloseDialog > Failed to open window 'close'! Goodbye!"); + e.printStackTrace(); + Platform.exit(); + } + close.setTitle("Close program?"); + close.show(); + Node n = close.getScene().lookup("#abort"); + if ( n == null || !(n instanceof Button)) { + ErrorPrinter.printWarning("showCloseDialog > Didn’t find element #abort"); + return; + } + ((Button) n).setOnAction(event -> close.close()); + n = close.getScene().lookup("#save"); + if ( n == null || !(n instanceof Button)) { + ErrorPrinter.printWarning("showCloseDialog > Didn’t find element #save"); + return; + } + ((Button) n).setOnAction(event -> { + if (this.filename != null) { + this.export(this.filename); + Platform.exit(); + } else { + this.showSaveAs(true); + } + }); + n = close.getScene().lookup("#close"); + if ( n == null || !(n instanceof Button)) { + ErrorPrinter.printWarning("showCloseDialog > Didn’t find element #close"); + return; + } + ((Button) n).setOnAction(event -> Platform.exit()); + } + /** * Notify a list about a changed item * Taken from: http://stackoverflow.com/a/21435063 diff --git a/src/de/t_battermann/dhbw/todolist/close.fxml b/src/de/t_battermann/dhbw/todolist/close.fxml new file mode 100644 index 0000000..1c23d49 --- /dev/null +++ b/src/de/t_battermann/dhbw/todolist/close.fxml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + +