aboutsummaryrefslogtreecommitdiffstats
path: root/ui-desktop/src/main/java/market/guess/ui/desktop/controllers/ResolveDialogController.java
blob: 4183f7f4cf064a736200d210dd3c974f333caff2 (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
package market.guess.ui.desktop.controllers;

import java.util.function.Consumer;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import market.guess.ui.desktop.util.Views;

public class ResolveDialogController {
  private Runnable onCancel;
  private Consumer<String> onConfirm;
  private String selectedOption = null;

  @FXML private Button resolveYesBtn;
  @FXML private Button resolveNoBtn;
  @FXML private Button confirmBtn;

  @FXML
  private void initialize() {
    updateSelection();
  }

  public void setOnCancel(Runnable onCancel) {
    this.onCancel = onCancel;
  }

  public void setOnConfirm(Consumer<String> onConfirm) {
    this.onConfirm = onConfirm;
  }

  public void reset() {
    selectedOption = null;
    updateSelection();
  }

  private void updateSelection() {
    Views.setSelected(resolveYesBtn, "YES".equals(selectedOption));
    Views.setSelected(resolveNoBtn, "NO".equals(selectedOption));
    boolean hasChoice = selectedOption != null;
    confirmBtn.setDisable(!hasChoice);
    confirmBtn.setText("Resolve as " + (hasChoice ? selectedOption : "…"));
  }

  @FXML
  private void handleSelectYes() {
    selectedOption = "YES";
    updateSelection();
  }

  @FXML
  private void handleSelectNo() {
    selectedOption = "NO";
    updateSelection();
  }

  @FXML
  private void handleCancel() {
    if (onCancel != null) {
      onCancel.run();
    }
  }

  @FXML
  private void handleConfirm() {
    if (onConfirm != null && selectedOption != null) {
      onConfirm.accept(selectedOption);
    }
  }
}