package market.guess.ui.desktop.controllers; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.control.ProgressBar; import javafx.util.Duration; import market.guess.ui.desktop.task.InitialLoadTask; public final class LoadDialogController { private Runnable onCancel; private Timeline currentFillAnimation; @FXML private Label pathLabel; @FXML private ProgressBar progressBar; public void setOnCancel(Runnable onCancel) { this.onCancel = onCancel; } public void reset() { if (currentFillAnimation != null) { currentFillAnimation.stop(); currentFillAnimation = null; } if (progressBar != null) { progressBar.setProgress(0.0); } } public void show(InitialLoadTask task, String pendingFile) { reset(); if (pathLabel != null) { pathLabel.setText(pendingFile); } task.progressProperty() .addListener( (observable, oldValue, newValue) -> { if (newValue == null) return; double val = newValue.doubleValue(); if (val < 0) { if (progressBar != null) { progressBar.setProgress(0.0); } return; } if (progressBar != null) { if (currentFillAnimation != null) { currentFillAnimation.stop(); } currentFillAnimation = new Timeline( new KeyFrame( Duration.millis(100), new KeyValue(progressBar.progressProperty(), val))); currentFillAnimation.play(); } }); } @FXML private void handleCancel() { reset(); if (onCancel != null) { onCancel.run(); } } }