Skip to contentMethod: lambda$bind$3(PresentationModel)
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.javafx.impl.list;
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.scene.control.ListCell;
36: import javafx.scene.control.ListView;
37: import javafx.util.Callback;
38: import it.tidalwave.ui.core.role.PresentationModel;
39: import it.tidalwave.ui.core.role.UserAction;
40: import it.tidalwave.ui.javafx.impl.common.AsObjectListCell;
41: import it.tidalwave.ui.javafx.impl.common.CellBinder;
42: import it.tidalwave.ui.javafx.impl.common.ChangeListenerSelectableAdapter;
43: import it.tidalwave.ui.javafx.impl.common.DelegateSupport;
44: import it.tidalwave.ui.javafx.impl.common.JavaFXWorker;
45: import it.tidalwave.ui.javafx.impl.common.RoleCollector;
46: import lombok.extern.slf4j.Slf4j;
47: import static javafx.collections.FXCollections.observableArrayList;
48: import static javafx.scene.input.KeyCode.*;
49: import static it.tidalwave.ui.core.role.UserActionProvider._UserActionProvider_;
50: import static it.tidalwave.ui.javafx.impl.DefaultJavaFXBinder.enforceFxApplicationThread;
51: import static it.tidalwave.ui.javafx.impl.common.JavaFXWorker.childrenPm;
52:
53: /***************************************************************************************************************************************************************
54: *
55: * This class takes care of bindings related to {@link ListView}.
56: *
57: * @author Fabrizio Giudici
58: *
59: **************************************************************************************************************************************************************/
60: @Slf4j
61: public class ListViewBindings extends DelegateSupport
62: {
63: @Nonnull
64: private final Callback<ListView<PresentationModel>, ListCell<PresentationModel>> cellFactory;
65:
66: private final ChangeListener<PresentationModel> changeListener = new ChangeListenerSelectableAdapter(executor);
67:
68: /***********************************************************************************************************************************************************
69: *
70: **********************************************************************************************************************************************************/
71: public ListViewBindings (@Nonnull final Executor executor, @Nonnull final CellBinder cellBinder)
72: {
73: super(executor);
74: cellFactory = listView -> new AsObjectListCell<>(cellBinder);
75: }
76:
77: /***********************************************************************************************************************************************************
78: * Binds a {@link ListView}.
79: * @param listView the {@code ListView}
80: * @param pm the {@link PresentationModel}
81: * @param callback an optional callback to invoke at the end of the binding
82: **********************************************************************************************************************************************************/
83: public void bind (@Nonnull final ListView<PresentationModel> listView, @Nonnull final PresentationModel pm, @Nonnull final Optional<Runnable> callback)
84: {
85: enforceFxApplicationThread();
86: listView.setCellFactory(cellFactory);
87: listView.setOnKeyPressed(event ->
88: {
89: if (List.of(SPACE, ENTER).contains(event.getCode()))
90: {
91: final var selectedPm = listView.getSelectionModel().getSelectedItem();
92: // TODO: must call the default action - but should we look up it again?
93: // Otherwise emulate mouse double click on the cell
94: log.debug("ListView onKeyPressed: {}", selectedPm);
95:
96: executor.execute(() ->
97: {
98: // TODO: it would be nicer to retrieve the cell and its associated RoleCollector?
99: final var roles = new RoleCollector(selectedPm, List.of(_UserActionProvider_));
100: roles.getDefaultUserAction().ifPresent(UserAction::actionPerformed);
101: });
102: }
103: });
104:
105: final var selectedProperty = listView.getSelectionModel().selectedItemProperty();
106: selectedProperty.removeListener(changeListener);
107: listView.setItems(observableArrayList()); // quick clear in case of long operations FIXME doesn't work
108: JavaFXWorker.run(executor,
109: () -> childrenPm(pm),
110: items -> finalizeBinding(listView, items, selectedProperty, callback));
111: }
112:
113: /***********************************************************************************************************************************************************
114: * Finalizes binding in the JavaFX thread and eventually invokes a callback.
115: * @param listView the {@link ListView}
116: * @param items the items of the table
117: * @param selectedProperty the 'selected' property
118: * @param callback an optional callback to invoke at the end
119: **********************************************************************************************************************************************************/
120: private void finalizeBinding (@Nonnull final ListView<PresentationModel> listView,
121: @Nonnull final ObservableList<PresentationModel> items,
122: @Nonnull final ReadOnlyObjectProperty<PresentationModel> selectedProperty,
123: @Nonnull final Optional<Runnable> callback)
124: {
125: listView.setItems(items);
126: selectedProperty.addListener(changeListener);
127: callback.ifPresent(Runnable::run);
128: }
129: }