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 package it.tidalwave.ui.javafx.impl.combobox;
27
28 import jakarta.annotation.Nonnull;
29 import java.util.List;
30 import java.util.Optional;
31 import java.util.concurrent.Executor;
32 import javafx.beans.property.ReadOnlyObjectProperty;
33 import javafx.beans.value.ChangeListener;
34 import javafx.collections.ObservableList;
35 import javafx.event.ActionEvent;
36 import javafx.scene.control.ComboBox;
37 import javafx.scene.control.ListCell;
38 import javafx.scene.control.ListView;
39 import javafx.util.Callback;
40 import it.tidalwave.ui.core.role.PresentationModel;
41 import it.tidalwave.ui.core.role.UserAction;
42 import it.tidalwave.ui.core.role.UserActionProvider;
43 import it.tidalwave.ui.javafx.impl.common.CellBinder;
44 import it.tidalwave.ui.javafx.impl.common.ChangeListenerSelectableAdapter;
45 import it.tidalwave.ui.javafx.impl.common.DelegateSupport;
46 import it.tidalwave.ui.javafx.impl.common.JavaFXWorker;
47 import it.tidalwave.ui.javafx.impl.common.AsObjectListCell;
48 import lombok.extern.slf4j.Slf4j;
49 import static javafx.scene.input.KeyCode.*;
50 import static it.tidalwave.ui.core.role.UserActionProvider._UserActionProvider_;
51 import static it.tidalwave.ui.javafx.impl.DefaultJavaFXBinder.enforceFxApplicationThread;
52 import static it.tidalwave.ui.javafx.impl.common.JavaFXWorker.childrenPm;
53
54
55
56
57
58
59
60
61 @Slf4j
62 public class ComboBoxBindings extends DelegateSupport
63 {
64 @Nonnull
65 private final CellBinder cellBinder;
66
67 private final Callback<ListView<PresentationModel>, ListCell<PresentationModel>> cellFactory;
68
69 private final ChangeListener<PresentationModel> changeListener = new ChangeListenerSelectableAdapter(executor);
70
71
72
73
74 public ComboBoxBindings (@Nonnull final Executor executor, @Nonnull final CellBinder cellBinder)
75 {
76 super(executor);
77 this.cellBinder = cellBinder;
78 cellFactory = comboBox -> new AsObjectListCell<>(cellBinder);
79 }
80
81
82
83
84
85
86
87 public void bind (@Nonnull final ComboBox<PresentationModel> comboBox, @Nonnull final PresentationModel pm, @Nonnull final Optional<Runnable> callback)
88 {
89 enforceFxApplicationThread();
90 log.debug("bind({}, {}, {})", comboBox, pm, callback);
91 comboBox.setCellFactory(cellFactory);
92 comboBox.setButtonCell(new AsObjectListCell<>(cellBinder));
93 comboBox.setOnAction(this::onActionEvent);
94 comboBox.setOnKeyPressed(event ->
95 {
96 if (List.of(SPACE, ENTER).contains(event.getCode()))
97 {
98 comboBox.show();
99 }
100 });
101
102 final var selectedProperty = comboBox.getSelectionModel().selectedItemProperty();
103 selectedProperty.removeListener(changeListener);
104 JavaFXWorker.run(executor,
105 () -> childrenPm(pm),
106 items -> finalizeBinding(comboBox, items, selectedProperty, callback));
107 }
108
109
110
111
112 @SuppressWarnings("unchecked")
113 private void onActionEvent (@Nonnull final ActionEvent event)
114 {
115 final var comboBox = (ComboBox<PresentationModel>)event.getSource();
116 final var selectedPm = comboBox.getSelectionModel().getSelectedItem();
117 selectedPm.maybeAs(_UserActionProvider_)
118 .flatMap(UserActionProvider::getOptionalDefaultAction)
119 .ifPresent(UserAction::actionPerformed);
120 }
121
122
123
124
125
126
127
128
129 private void finalizeBinding (@Nonnull final ComboBox<PresentationModel> comboBox,
130 @Nonnull final ObservableList<PresentationModel> items,
131 @Nonnull final ReadOnlyObjectProperty<PresentationModel> selectedProperty,
132 @Nonnull final Optional<Runnable> callback)
133 {
134 comboBox.setItems(items);
135
136 if (!items.isEmpty())
137 {
138 comboBox.getSelectionModel().select(items.get(0));
139 }
140
141 selectedProperty.addListener(changeListener);
142 callback.ifPresent(Runnable::run);
143 }
144 }