ListViewBindings.java

  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. import jakarta.annotation.Nonnull;
  28. import java.util.List;
  29. import java.util.Optional;
  30. import java.util.concurrent.Executor;
  31. import javafx.beans.property.ReadOnlyObjectProperty;
  32. import javafx.beans.value.ChangeListener;
  33. import javafx.collections.ObservableList;
  34. import javafx.scene.control.ListCell;
  35. import javafx.scene.control.ListView;
  36. import javafx.util.Callback;
  37. import it.tidalwave.ui.core.role.PresentationModel;
  38. import it.tidalwave.ui.core.role.UserAction;
  39. import it.tidalwave.ui.javafx.impl.common.AsObjectListCell;
  40. import it.tidalwave.ui.javafx.impl.common.CellBinder;
  41. import it.tidalwave.ui.javafx.impl.common.ChangeListenerSelectableAdapter;
  42. import it.tidalwave.ui.javafx.impl.common.DelegateSupport;
  43. import it.tidalwave.ui.javafx.impl.common.JavaFXWorker;
  44. import it.tidalwave.ui.javafx.impl.common.RoleCollector;
  45. import lombok.extern.slf4j.Slf4j;
  46. import static javafx.collections.FXCollections.observableArrayList;
  47. import static javafx.scene.input.KeyCode.*;
  48. import static it.tidalwave.ui.core.role.UserActionProvider._UserActionProvider_;
  49. import static it.tidalwave.ui.javafx.impl.DefaultJavaFXBinder.enforceFxApplicationThread;
  50. import static it.tidalwave.ui.javafx.impl.common.JavaFXWorker.childrenPm;

  51. /***************************************************************************************************************************************************************
  52.  *
  53.  * This class takes care of bindings related to {@link ListView}.
  54.  *
  55.  * @author  Fabrizio Giudici
  56.  *
  57.  **************************************************************************************************************************************************************/
  58. @Slf4j
  59. public class ListViewBindings extends DelegateSupport
  60.   {
  61.     @Nonnull
  62.     private final Callback<ListView<PresentationModel>, ListCell<PresentationModel>> cellFactory;

  63.     private final ChangeListener<PresentationModel> changeListener = new ChangeListenerSelectableAdapter(executor);

  64.     /***********************************************************************************************************************************************************
  65.      *
  66.      **********************************************************************************************************************************************************/
  67.     public ListViewBindings (@Nonnull final Executor executor, @Nonnull final CellBinder cellBinder)
  68.       {
  69.         super(executor);
  70.         cellFactory = listView -> new AsObjectListCell<>(cellBinder);
  71.       }

  72.     /***********************************************************************************************************************************************************
  73.      * Binds a {@link ListView}.
  74.      * @param   listView            the {@code ListView}
  75.      * @param   pm                  the {@link PresentationModel}
  76.      * @param   callback            an optional callback to invoke at the end of the binding
  77.      **********************************************************************************************************************************************************/
  78.     public void bind (@Nonnull final ListView<PresentationModel> listView, @Nonnull final PresentationModel pm, @Nonnull final Optional<Runnable> callback)
  79.       {
  80.         enforceFxApplicationThread();
  81.         listView.setCellFactory(cellFactory);
  82.         listView.setOnKeyPressed(event ->
  83.           {
  84.             if (List.of(SPACE, ENTER).contains(event.getCode()))
  85.               {
  86.                 final var selectedPm = listView.getSelectionModel().getSelectedItem();
  87.                 // TODO: must call the default action - but should we look up it again?
  88.                 // Otherwise emulate mouse double click on the cell
  89.                 log.debug("ListView onKeyPressed: {}", selectedPm);

  90.                 executor.execute(() ->
  91.                   {
  92.                     // TODO: it would be nicer to retrieve the cell and its associated RoleCollector?
  93.                     final var roles = new RoleCollector(selectedPm, List.of(_UserActionProvider_));
  94.                     roles.getDefaultUserAction().ifPresent(UserAction::actionPerformed);
  95.                   });
  96.               }
  97.           });

  98.         final var selectedProperty = listView.getSelectionModel().selectedItemProperty();
  99.         selectedProperty.removeListener(changeListener);
  100.         listView.setItems(observableArrayList()); // quick clear in case of long operations FIXME doesn't work
  101.         JavaFXWorker.run(executor,
  102.                          () -> childrenPm(pm),
  103.                          items -> finalizeBinding(listView, items, selectedProperty, callback));
  104.       }

  105.     /***********************************************************************************************************************************************************
  106.      * Finalizes binding in the JavaFX thread and eventually invokes a callback.
  107.      * @param   listView            the {@link ListView}
  108.      * @param   items               the items of the table
  109.      * @param   selectedProperty    the 'selected' property
  110.      * @param   callback            an optional callback to invoke at the end
  111.      **********************************************************************************************************************************************************/
  112.     private void finalizeBinding (@Nonnull final ListView<PresentationModel> listView,
  113.                                   @Nonnull final ObservableList<PresentationModel> items,
  114.                                   @Nonnull final ReadOnlyObjectProperty<PresentationModel> selectedProperty,
  115.                                   @Nonnull final Optional<Runnable> callback)
  116.       {
  117.         listView.setItems(items);
  118.         selectedProperty.addListener(changeListener);
  119.         callback.ifPresent(Runnable::run);
  120.       }
  121.   }