TableViewBindings.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.tableview;

  30. import javax.annotation.Nonnull;
  31. import java.util.Optional;
  32. import java.util.concurrent.Executor;
  33. import it.tidalwave.role.ui.javafx.impl.common.ChangeListenerSelectableAdapter;
  34. import it.tidalwave.role.ui.PresentationModel;
  35. import it.tidalwave.role.ui.javafx.impl.common.CellBinder;
  36. import it.tidalwave.role.ui.javafx.impl.common.DelegateSupport;
  37. import it.tidalwave.role.ui.javafx.impl.common.JavaFXWorker;
  38. import it.tidalwave.role.ui.javafx.impl.common.PresentationModelObservable;
  39. import javafx.util.Callback;
  40. import javafx.collections.ObservableList;
  41. import javafx.beans.property.ReadOnlyObjectProperty;
  42. import javafx.beans.value.ChangeListener;
  43. import javafx.scene.control.TableCell;
  44. import javafx.scene.control.TableColumn;
  45. import javafx.scene.control.TableView;
  46. import lombok.extern.slf4j.Slf4j;
  47. import static it.tidalwave.role.ui.javafx.impl.common.JavaFXWorker.childrenPm;

  48. /***********************************************************************************************************************
  49.  *
  50.  * @author  Fabrizio Giudici
  51.  *
  52.  **********************************************************************************************************************/
  53. @Slf4j
  54. public class TableViewBindings extends DelegateSupport
  55.   {
  56.     private final Callback<TableColumn<PresentationModel, PresentationModel>,
  57.                      TableCell<PresentationModel, PresentationModel>> cellFactory;

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

  59.     /*******************************************************************************************************************
  60.      *
  61.      *
  62.      *
  63.      ******************************************************************************************************************/
  64.     public TableViewBindings (@Nonnull final Executor executor, @Nonnull final CellBinder cellBinder)
  65.       {
  66.         super(executor);
  67.         cellFactory = __ -> AsObjectTableCell.of(cellBinder);
  68.       }

  69.     /*******************************************************************************************************************
  70.      *
  71.      * {@inheritDoc}
  72.      *
  73.      ******************************************************************************************************************/
  74.     public void bind (@Nonnull final TableView<PresentationModel> tableView,
  75.                       @Nonnull final PresentationModel pm,
  76.                       @Nonnull final Optional<Runnable> callback)
  77.       {
  78.         assertIsFxApplicationThread();
  79.         log.debug("bind({}, {}, {})", tableView, pm, callback);

  80.         final ReadOnlyObjectProperty<PresentationModel> selectedProperty = tableView.getSelectionModel().selectedItemProperty();
  81.         selectedProperty.removeListener(changeListener);
  82.         JavaFXWorker.run(executor,
  83.                          () -> childrenPm(pm),
  84.                          items -> finalize(tableView, items, selectedProperty, callback));
  85.       }

  86.     /*******************************************************************************************************************
  87.      *
  88.      ******************************************************************************************************************/
  89.     private void finalize (@Nonnull final TableView<PresentationModel> tableView,
  90.                            @Nonnull final ObservableList<PresentationModel> items,
  91.                            @Nonnull final ReadOnlyObjectProperty<PresentationModel> selectedProperty,
  92.                            @Nonnull final Optional<Runnable> callback)
  93.       {
  94.         tableView.setItems(items);
  95.         selectedProperty.addListener(changeListener);

  96.         final ObservableList rawColumns = tableView.getColumns(); // FIXME cast

  97.         ((ObservableList<TableColumn<PresentationModel, PresentationModel>>)rawColumns).forEach(column ->
  98.           {
  99.             column.setCellValueFactory(PresentationModelObservable::of);
  100.             column.setCellFactory(cellFactory);
  101.           });

  102.         callback.ifPresent(Runnable::run);
  103.       }
  104.   }