aboutsummaryrefslogtreecommitdiffstats
path: root/ui-desktop/src/main/java/market/guess/ui/desktop/controllers/LoadDialogController.java
blob: e23cd9ecd7d5c65bc6a708c299221cf205efdea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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();
    }
  }
}