TreeViewBindings.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.tree;

  30. import javax.annotation.Nonnull;
  31. import java.util.Optional;
  32. import java.util.concurrent.Executor;
  33. import javafx.util.Callback;
  34. import javafx.scene.control.TreeCell;
  35. import javafx.scene.control.TreeView;
  36. import it.tidalwave.util.annotation.VisibleForTesting;
  37. import it.tidalwave.role.ui.PresentationModel;
  38. import it.tidalwave.role.ui.javafx.impl.common.CellBinder;
  39. import it.tidalwave.role.ui.javafx.impl.common.TreeItemDelegateSupport;
  40. import lombok.extern.slf4j.Slf4j;

  41. /***********************************************************************************************************************
  42.  *
  43.  * @author  Fabrizio Giudici
  44.  *
  45.  **********************************************************************************************************************/
  46. @Slf4j
  47. public class TreeViewBindings extends TreeItemDelegateSupport
  48.   {
  49.     @VisibleForTesting final Callback<TreeView<PresentationModel>, TreeCell<PresentationModel>> cellFactory;

  50.     /*******************************************************************************************************************
  51.      *
  52.      *
  53.      *
  54.      ******************************************************************************************************************/
  55.     public TreeViewBindings (@Nonnull final Executor executor, @Nonnull final CellBinder cellBinder)
  56.       {
  57.         super(executor);
  58.         cellFactory = __ -> new AsObjectTreeCell<>(cellBinder);
  59.       }

  60.     /*******************************************************************************************************************
  61.      *
  62.      * {@inheritDoc}
  63.      *
  64.      ******************************************************************************************************************/
  65.     public void bind (@Nonnull final TreeView<PresentationModel> treeView,
  66.                       @Nonnull final PresentationModel pm,
  67.                       @Nonnull final Optional<Runnable> callback)
  68.       {
  69.         assertIsFxApplicationThread();
  70.         log.debug("bind({}, {}, {})", treeView, pm, callback);

  71.         setRootProperty(pm, treeView.rootProperty());
  72.         treeView.setCellFactory(cellFactory);
  73.         treeView.setShowRoot(shouldShowRoot(pm));
  74.         bindSelectionListener(treeView.getSelectionModel().selectedItemProperty());
  75.         callback.ifPresent(Runnable::run); // FIXME: thread?
  76.       }
  77.   }