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