ListViewBindings.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * SteelBlue: DCI User Interfaces
  5.  * http://tidalwave.it/projects/steelblue
  6.  *
  7.  * Copyright (C) 2015 - 2023 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
  12.  * the License. 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
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/steelblue-src
  23.  * git clone https://github.com/tidalwave-it/steelblue-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.role.ui.javafx.impl.list;

  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.scene.control.ListCell;
  36. import javafx.scene.control.ListView;
  37. import javafx.util.Callback;
  38. import it.tidalwave.role.ui.PresentationModel;
  39. import it.tidalwave.role.ui.UserAction;
  40. import it.tidalwave.role.ui.javafx.impl.common.CellBinder;
  41. import it.tidalwave.role.ui.javafx.impl.common.ChangeListenerSelectableAdapter;
  42. import it.tidalwave.role.ui.javafx.impl.common.DelegateSupport;
  43. import it.tidalwave.role.ui.javafx.impl.common.JavaFXWorker;
  44. import it.tidalwave.role.ui.javafx.impl.common.RoleBag;
  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.role.ui.UserActionProvider._UserActionProvider_;
  49. import static it.tidalwave.role.ui.javafx.impl.common.JavaFXWorker.childrenPm;

  50. /***********************************************************************************************************************
  51.  *
  52.  * @author  Fabrizio Giudici
  53.  *
  54.  **********************************************************************************************************************/
  55. @Slf4j
  56. public class ListViewBindings extends DelegateSupport
  57.   {
  58.     private final Callback<ListView<PresentationModel>, ListCell<PresentationModel>> cellFactory;

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

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

  70.     /*******************************************************************************************************************
  71.      *
  72.      * {@inheritDoc}
  73.      *
  74.      ******************************************************************************************************************/
  75.     public void bind (@Nonnull final ListView<PresentationModel> listView,
  76.                       @Nonnull final PresentationModel pm,
  77.                       @Nonnull final Optional<Runnable> callback)
  78.       {
  79.         listView.setCellFactory(cellFactory);

  80.         // FIXME: WEAK LISTENERS

  81.         // FIXME: this won't work with any external navigation system, such as CEC menus
  82.         // TODO: try by having CEC selection emulating RETURN and optionally accepting RETURN here
  83.         listView.setOnKeyPressed(event ->
  84.           {
  85.             if (List.of(SPACE, ENTER).contains(event.getCode()))
  86.               {
  87.                 final var selectedPm = listView.getSelectionModel().getSelectedItem();
  88.                 // TODO: must call the default action - but should we look up it again?
  89.                 // Otherwise emulate mouse double click on the cell
  90.                 ListViewBindings.log.debug("ListView onKeyPressed: {}", selectedPm);

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

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

  106.     /*******************************************************************************************************************
  107.      *
  108.      ******************************************************************************************************************/
  109.     private void finalize (@Nonnull final ListView<PresentationModel> listView,
  110.                            @Nonnull final ObservableList<PresentationModel> items,
  111.                            @Nonnull final ReadOnlyObjectProperty<PresentationModel> selectedProperty,
  112.                            @Nonnull final Optional<Runnable> callback)
  113.       {
  114.         listView.setItems(items);
  115.         selectedProperty.addListener(changeListener);
  116.         callback.ifPresent(Runnable::run);
  117.       }
  118.   }