View Javadoc
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 java.util.function.Supplier;
33  import javafx.beans.property.ReadOnlyObjectProperty;
34  import javafx.beans.value.ChangeListener;
35  import javafx.collections.ObservableList;
36  import javafx.scene.control.ListCell;
37  import javafx.scene.control.ListView;
38  import javafx.stage.Window;
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.javafx.impl.common.AsObjectListCell;
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.RoleCollector;
48  import lombok.extern.slf4j.Slf4j;
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  import static javafx.collections.FXCollections.observableArrayList;
53  import static javafx.scene.input.KeyCode.*;
54  
55  /***************************************************************************************************************************************************************
56   *
57   * This class takes care of bindings related to {@link ListView}.
58   *
59   * @author  Fabrizio Giudici
60   *
61   **************************************************************************************************************************************************************/
62  @Slf4j
63  public class ListViewBindings extends DelegateSupport
64    {
65      @Nonnull
66      private final Callback<ListView<PresentationModel>, ListCell<PresentationModel>> cellFactory;
67  
68      private final ChangeListener<PresentationModel> changeListener = new ChangeListenerSelectableAdapter(executor);
69  
70      /***********************************************************************************************************************************************************
71       *
72       **********************************************************************************************************************************************************/
73      public ListViewBindings (@Nonnull final Executor executor, @Nonnull final CellBinder cellBinder, @Nonnull final Supplier<Window> mainWindow)
74        {
75          super(executor, mainWindow);
76          cellFactory = listView -> new AsObjectListCell<>(cellBinder);
77        }
78  
79      /***********************************************************************************************************************************************************
80       * Binds a {@link ListView}.
81       * @param   listView            the {@code ListView}
82       * @param   pm                  the {@link PresentationModel}
83       * @param   callback            an optional callback to invoke at the end of the binding
84       **********************************************************************************************************************************************************/
85      public void bind (@Nonnull final ListView<PresentationModel> listView, @Nonnull final PresentationModel pm, @Nonnull final Optional<Runnable> callback)
86        {
87          enforceFxApplicationThread();
88          listView.setCellFactory(cellFactory);
89          listView.setOnKeyPressed(event ->
90            {
91              if (List.of(SPACE, ENTER).contains(event.getCode()))
92                {
93                  final var selectedPm = listView.getSelectionModel().getSelectedItem();
94                  // TODO: must call the default action - but should we look up it again?
95                  // Otherwise emulate mouse double click on the cell
96                  log.debug("ListView onKeyPressed: {}", selectedPm);
97  
98                  executor.execute(() ->
99                    {
100                     // TODO: it would be nicer to retrieve the cell and its associated RoleCollector?
101                     final var roles = new RoleCollector(selectedPm, List.of(_UserActionProvider_));
102                     roles.getDefaultUserAction().ifPresent(UserAction::actionPerformed);
103                   });
104               }
105           });
106 
107         final var selectedProperty = listView.getSelectionModel().selectedItemProperty();
108         selectedProperty.removeListener(changeListener);
109         listView.setItems(observableArrayList()); // quick clear in case of long operations FIXME doesn't work
110         JavaFXWorker.run(executor,
111                          () -> childrenPm(pm),
112                          items -> finalizeBinding(listView, items, selectedProperty, callback));
113       }
114 
115     /***********************************************************************************************************************************************************
116      * Finalizes binding in the JavaFX thread and eventually invokes a callback.
117      * @param   listView            the {@link ListView}
118      * @param   items               the items of the table
119      * @param   selectedProperty    the 'selected' property
120      * @param   callback            an optional callback to invoke at the end
121      **********************************************************************************************************************************************************/
122     private void finalizeBinding (@Nonnull final ListView<PresentationModel> listView,
123                                   @Nonnull final ObservableList<PresentationModel> items,
124                                   @Nonnull final ReadOnlyObjectProperty<PresentationModel> selectedProperty,
125                                   @Nonnull final Optional<Runnable> callback)
126       {
127         listView.setItems(items);
128         selectedProperty.addListener(changeListener);
129         callback.ifPresent(Runnable::run);
130       }
131   }