From 4333333750fc8290d879bcebce6133010a699f00 Mon Sep 17 00:00:00 2001 From: Donovan Smith Date: Sun, 2 Apr 2017 10:38:30 -0500 Subject: [PATCH] Fixed saving of plots Saving of plots was broken due to being overzealous with multithreading. --- dependency-reduced-pom.xml | 2 +- pom.xml | 2 +- .../skewtvsp/controllers/STVSPController.java | 50 +++++++------------ .../skewtvsp/controllers/SkewTPlot.java | 10 ++-- 4 files changed, 23 insertions(+), 41 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index aa3c30e..6b373aa 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ com.mccollinsmith.donovan SkewTVSP Skew-T Virtual Sounding Plotter - 0.2.0 + 0.2.1 http://github.com/donovan1983/SkewTVirtualSoundingPlotter/ 2016 diff --git a/pom.xml b/pom.xml index efdc714..cdd7e13 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.mccollinsmith.donovan SkewTVSP - 0.2.0 + 0.2.1 jar diff --git a/src/main/java/com/mccollinsmith/donovan/skewtvsp/controllers/STVSPController.java b/src/main/java/com/mccollinsmith/donovan/skewtvsp/controllers/STVSPController.java index 6e084f4..aebb6fd 100644 --- a/src/main/java/com/mccollinsmith/donovan/skewtvsp/controllers/STVSPController.java +++ b/src/main/java/com/mccollinsmith/donovan/skewtvsp/controllers/STVSPController.java @@ -491,32 +491,24 @@ protected void doSaveSkewT(ActionEvent event) { chooser.getExtensionFilters().addAll(fileExtsPNG); File file = chooser.showSaveDialog(anchorPane.getScene().getWindow()); - Task taskSavePlot = new Task() { - @Override - public Boolean call() { - updateMessage("Saving plot to " + file.getName() + "..."); - updateProgress(10, 100); - RenderedImage renderedImage = SkewTPlot.getHiResPlot(); - updateProgress(80, 100); - try { - ImageIO.write(renderedImage, "png", file); - } catch (IOException ex) { - LOG.error("{}\n{}", ex.getLocalizedMessage(), ex.toString()); - LOG.error("Unable to save PNG file!"); - return false; - } - return true; - } - }; - - taskSavePlot.setOnSucceeded(taskEvent -> { - lblStatus.textProperty().unbind(); - pbProgress.progressProperty().unbind(); - pbProgress.setVisible(false); - - if (taskSavePlot.getValue() == true) { + // Only try to save plot if a location and filename was chosen + if (file != null) { + pbProgress.setVisible(true); + + pbProgress.setProgress(0.1); + RenderedImage renderedImage = SkewTPlot.getHiResPlot(); + pbProgress.setProgress(0.8); + + try { + // Save the plot to the chosen PNG file + ImageIO.write(renderedImage, "png", file); doUpdateStatus("Plot saved to file " + file.getName()); - } else { + } catch (IOException ex) { + // Unable to save PNG so log the error... + LOG.error("{}\n{}", ex.getLocalizedMessage(), ex.toString()); + LOG.error("Unable to save PNG file!"); + + // ...and show an alert doUpdateStatus("Unable to save plot to file"); Alert alert = new Alert(AlertType.ERROR); alert.setTitle("File Save Error"); @@ -524,14 +516,8 @@ public Boolean call() { alert.setContentText("File name not valid or path not writeable."); alert.showAndWait(); } - }); - if (file != null) { - lblStatus.textProperty().bind(taskSavePlot.messageProperty()); - pbProgress.progressProperty().bind(taskSavePlot.progressProperty()); - pbProgress.setVisible(true); - - new Thread(taskSavePlot).start(); + pbProgress.setVisible(false); } } diff --git a/src/main/java/com/mccollinsmith/donovan/skewtvsp/controllers/SkewTPlot.java b/src/main/java/com/mccollinsmith/donovan/skewtvsp/controllers/SkewTPlot.java index 8a61a78..13fbb3c 100644 --- a/src/main/java/com/mccollinsmith/donovan/skewtvsp/controllers/SkewTPlot.java +++ b/src/main/java/com/mccollinsmith/donovan/skewtvsp/controllers/SkewTPlot.java @@ -242,13 +242,9 @@ public static RenderedImage getHiResPlot() { //Create raster image to hold a snapshot of the Canvas. WritableImage writableImage = new WritableImage((int) PLOT_PRINT_WIDTH, (int) PLOT_PRINT_HEIGHT); - /* - * Run Canvas.snapshot on the main JavaFX thread, otherwise it will lock - * up the application if ran in concurrent thread. - */ - Platform.runLater(() -> { - canvasHiResPlot.snapshot(null, writableImage); - }); + + // Take snapshot of plot and save to writableImage + canvasHiResPlot.snapshot(null, writableImage); // Restore original scale factor and GraphicsContext. scaleLineFactor = PLOT_VIEW_SCALE;