ComboBoxBindings.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * SteelBlue
  6.  * http://steelblue.tidalwave.it - git clone git@bitbucket.org:tidalwave/steelblue-src.git
  7.  * %%
  8.  * Copyright (C) 2015 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  *
  11.  * *********************************************************************************************************************
  12.  *
  13.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  14.  * the License. You may obtain a copy of the License at
  15.  *
  16.  *     http://www.apache.org/licenses/LICENSE-2.0
  17.  *
  18.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  19.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  20.  * specific language governing permissions and limitations under the License.
  21.  *
  22.  * *********************************************************************************************************************
  23.  *
  24.  *
  25.  *
  26.  * *********************************************************************************************************************
  27.  * #L%
  28.  */
  29. package it.tidalwave.role.ui.javafx.impl.combobox;

  30. import javax.annotation.Nonnull;
  31. import java.util.List;
  32. import java.util.Optional;
  33. import java.util.concurrent.Executor;
  34. import javafx.collections.ObservableList;
  35. import javafx.util.Callback;
  36. import javafx.event.ActionEvent;
  37. import javafx.event.EventHandler;
  38. import javafx.beans.value.ChangeListener;
  39. import javafx.beans.property.ReadOnlyObjectProperty;
  40. import javafx.scene.control.ListCell;
  41. import javafx.scene.control.ListView;
  42. import javafx.scene.control.ComboBox;
  43. import it.tidalwave.role.ui.PresentationModel;
  44. import it.tidalwave.role.ui.UserAction;
  45. import it.tidalwave.role.ui.UserActionProvider;
  46. import it.tidalwave.role.ui.javafx.impl.list.AsObjectListCell;
  47. import it.tidalwave.role.ui.javafx.impl.common.CellBinder;
  48. import it.tidalwave.role.ui.javafx.impl.common.ChangeListenerSelectableAdapter;
  49. import it.tidalwave.role.ui.javafx.impl.common.DelegateSupport;
  50. import it.tidalwave.role.ui.javafx.impl.common.JavaFXWorker;
  51. import lombok.extern.slf4j.Slf4j;
  52. import static it.tidalwave.role.ui.javafx.impl.common.JavaFXWorker.childrenPm;
  53. import static javafx.scene.input.KeyCode.*;
  54. import static it.tidalwave.role.ui.UserActionProvider._UserActionProvider_;

  55. /***********************************************************************************************************************
  56.  *
  57.  * @author  Fabrizio Giudici
  58.  *
  59.  **********************************************************************************************************************/
  60. @Slf4j
  61. public class ComboBoxBindings extends DelegateSupport
  62.   {
  63.     @Nonnull
  64.     private final CellBinder cellBinder;

  65.     private final Callback<ListView<PresentationModel>, ListCell<PresentationModel>> cellFactory;

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

  67.     /*******************************************************************************************************************
  68.      *
  69.      * Event handler that executes the default action bound to the given combobox item.
  70.      *
  71.      ******************************************************************************************************************/
  72.     private final EventHandler<ActionEvent> eventHandler = event ->
  73.       {
  74.         final ComboBox<PresentationModel> comboBox = (ComboBox<PresentationModel>)event.getSource();
  75.         final PresentationModel selectedPm = comboBox.getSelectionModel().getSelectedItem();
  76.         selectedPm.maybeAs(_UserActionProvider_)
  77.                   .flatMap(UserActionProvider::getOptionalDefaultAction)
  78.                   .ifPresent(UserAction::actionPerformed);
  79.       };

  80.     /*******************************************************************************************************************
  81.      *
  82.      *
  83.      *
  84.      ******************************************************************************************************************/
  85.     public ComboBoxBindings (@Nonnull final Executor executor, @Nonnull final CellBinder cellBinder)
  86.       {
  87.         super(executor);
  88.         this.cellBinder = cellBinder;
  89.         cellFactory = comboBox -> new AsObjectListCell<>(cellBinder);
  90.       }

  91.     /*******************************************************************************************************************
  92.      *
  93.      * {@inheritDoc}
  94.      *
  95.      ******************************************************************************************************************/
  96.     public void bind (@Nonnull final ComboBox<PresentationModel> comboBox,
  97.                       @Nonnull final PresentationModel pm,
  98.                       @Nonnull final Optional<Runnable> callback)
  99.       {
  100.         comboBox.setCellFactory(cellFactory);
  101.         comboBox.setButtonCell(new AsObjectListCell<>(cellBinder));
  102.         comboBox.setOnAction(eventHandler);

  103.         // FIXME: WEAK LISTENERS

  104.         // FIXME: this won't work with any external navigation system, such as CEC menus
  105.         // TODO: try by having CEC selection emulating RETURN and optionally accepting RETURN here
  106.         comboBox.setOnKeyPressed(event ->
  107.           {
  108.             if (List.of(SPACE, ENTER).contains(event.getCode()))
  109.               {
  110.                 comboBox.show();
  111.               }
  112.           });

  113.         final ReadOnlyObjectProperty<PresentationModel> selectedProperty = comboBox.getSelectionModel().selectedItemProperty();
  114.         selectedProperty.removeListener(changeListener);
  115.         JavaFXWorker.run(executor,
  116.                          () -> childrenPm(pm),
  117.                          items -> finalize(comboBox, items, selectedProperty, callback));
  118.       }

  119.     /*******************************************************************************************************************
  120.      *
  121.      ******************************************************************************************************************/
  122.     private void finalize (@Nonnull final ComboBox<PresentationModel> comboBox,
  123.                            @Nonnull final ObservableList<PresentationModel> items,
  124.                            @Nonnull final ReadOnlyObjectProperty<PresentationModel> selectedProperty,
  125.                            @Nonnull final Optional<Runnable> callback)
  126.       {
  127.         comboBox.setItems(items);

  128.         if (!items.isEmpty())
  129.           {
  130.             comboBox.getSelectionModel().select(items.get(0));
  131.           }

  132.         selectedProperty.addListener(changeListener);
  133.         callback.ifPresent(Runnable::run);
  134.       }
  135.   }