Skip to content

Commit

Permalink
Fixed saving of plots
Browse files Browse the repository at this point in the history
Saving of plots was broken due to being overzealous with multithreading.
  • Loading branch information
averydonovan committed Apr 2, 2017
1 parent 74874b1 commit 4333333
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 41 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.mccollinsmith.donovan</groupId>
<artifactId>SkewTVSP</artifactId>
<name>Skew-T Virtual Sounding Plotter</name>
<version>0.2.0</version>
<version>0.2.1</version>
<url>http://github.com/donovan1983/SkewTVirtualSoundingPlotter/</url>
<inceptionYear>2016</inceptionYear>
<developers>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.mccollinsmith.donovan</groupId>
<artifactId>SkewTVSP</artifactId>
<version>0.2.0</version>
<version>0.2.1</version>
<packaging>jar</packaging>
<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,47 +491,33 @@ protected void doSaveSkewT(ActionEvent event) {
chooser.getExtensionFilters().addAll(fileExtsPNG);
File file = chooser.showSaveDialog(anchorPane.getScene().getWindow());

Task<Boolean> taskSavePlot = new Task<Boolean>() {
@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");
alert.setHeaderText("Unable to save PNG file");
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4333333

Please sign in to comment.