private void setImageTo(URL result) {
try {
log.info("Setting image to {}", result);
if (result.getProtocol().startsWith("http")) { // Also catch https
final String oldLabel = coverImageLabel.getText();
final DownloadProgress task = new DownloadProgress(result);
task.setOnSucceeded(ev -> {
log.info("Image downloaded succeeded");
ByteString bytes = task.getValue();
coverImageLabel.setGraphic(null);
coverImageLabel.setText(oldLabel);
setImageTo(bytes);
});
task.setOnFailed(ev -> {
informationalAlert("Image load failed", "Could not download the image from the remote server: %s", task.getException().getLocalizedMessage());
coverImageLabel.setGraphic(null);
coverImageLabel.setText(oldLabel);
});
ProgressIndicator indicator = new ProgressIndicator();
indicator.progressProperty().bind(task.progressProperty());
indicator.setPrefHeight(50);
indicator.setPrefWidth(50);
coverImageLabel.setGraphic(indicator);
coverImageLabel.setText("");
Thread download = new Thread(task);