TreeItemDelegateSupport.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.common;

  30. import javax.annotation.Nonnull;
  31. import javafx.application.Platform;
  32. import java.beans.PropertyChangeListener;
  33. import java.util.concurrent.Executor;
  34. import javafx.beans.property.ObjectProperty;
  35. import javafx.beans.property.ReadOnlyObjectProperty;
  36. import javafx.scene.control.TreeItem;
  37. import it.tidalwave.util.annotation.VisibleForTesting;
  38. import it.tidalwave.role.ui.PresentationModel;
  39. import it.tidalwave.role.ui.Visible;
  40. import it.tidalwave.role.ui.javafx.impl.tree.ObsoletePresentationModelDisposer;
  41. import lombok.Getter;
  42. import lombok.extern.slf4j.Slf4j;
  43. import static it.tidalwave.role.ui.Visible._Visible_;
  44. import static java.util.stream.Collectors.toList;
  45. import static it.tidalwave.role.ui.javafx.impl.common.JavaFXWorker.childrenPm;

  46. /***********************************************************************************************************************
  47.  *
  48.  * Some common stuff for both {@code TreeView} and {@code TreeTableView}.
  49.  *
  50.  * @author  Fabrizio Giudici
  51.  *
  52.  **********************************************************************************************************************/
  53. @Slf4j
  54. public class TreeItemDelegateSupport extends DelegateSupport
  55.   {
  56.     @VisibleForTesting @Getter()
  57.     private final ChangeListenerSelectableAdapter selectionListener =
  58.           new ChangeListenerSelectableAdapter(executor);

  59.     private final ObsoletePresentationModelDisposer presentationModelDisposer =
  60.           new ObsoletePresentationModelDisposer();

  61.     /*******************************************************************************************************************
  62.      *
  63.      ******************************************************************************************************************/
  64.     public TreeItemDelegateSupport (@Nonnull final Executor executor)
  65.       {
  66.         super(executor);
  67.       }

  68.     /*******************************************************************************************************************
  69.      *
  70.      * Sets the children for a {@link TreeItem}.
  71.      *
  72.      * @param   parentItem  the {@code TreeItem}
  73.      * @param   depth       the depth level (used only for logging)
  74.      *
  75.      ******************************************************************************************************************/
  76.     protected void setChildren (@Nonnull final TreeItem<PresentationModel> parentItem, final int depth)
  77.       {
  78.         JavaFXWorker.run(executor,
  79.                          () -> childrenPm(parentItem.getValue(), depth),
  80.                          items -> parentItem.getChildren().setAll(items.stream()
  81.                                                                        .map(childPm -> createTreeItem(childPm, depth))
  82.                                                                        .collect(toList())));
  83.       }

  84.     /*******************************************************************************************************************
  85.      *
  86.      * Creates a single {@link TreeItem} for the given the {@link PresentationModel}. When the {@code PresentationModel}
  87.      * fires the {@link PresentationModel#PROPERTY_CHILDREN} property change event, children are recreated.
  88.      *
  89.      * @param   pm        the {@code PresentationModel}
  90.      * @param   depth     the depth level (used only for logging)
  91.      * @return            the
  92.      *
  93.      ******************************************************************************************************************/
  94.     @Nonnull
  95.     protected TreeItem<PresentationModel> createTreeItem (@Nonnull final PresentationModel pm, final int depth)
  96.       {
  97.         assertIsFxApplicationThread();
  98.         final TreeItem<PresentationModel> item = new PresentationModelTreeItem(pm);

  99.         final PropertyChangeListener recreateChildrenOnUpdateListener = __ ->
  100.                 Platform.runLater(() ->
  101.           {
  102.             log.debug("On recreateChildrenOnUpdateListener");
  103.             setChildren(item, depth + 1);
  104.             item.setExpanded(true);
  105.           });

  106.         pm.addPropertyChangeListener(PresentationModel.PROPERTY_CHILDREN, recreateChildrenOnUpdateListener);

  107.         item.expandedProperty().addListener(((observable, oldValue, newValue) ->
  108.           {
  109.             if (newValue)
  110.               {
  111.                 setChildren(item, depth + 1);
  112.               }
  113.           }));

  114.         return item;
  115.       }

  116.     /*******************************************************************************************************************
  117.      *
  118.      *
  119.      ******************************************************************************************************************/
  120.     protected void setRootProperty (@Nonnull final PresentationModel pm,
  121.                                     @Nonnull final ObjectProperty<TreeItem<PresentationModel>> rootProperty)
  122.       {
  123.         rootProperty.removeListener(presentationModelDisposer);
  124.         rootProperty.addListener(presentationModelDisposer);
  125.         rootProperty.set(createTreeItem(pm, 0));
  126.       }

  127.     /*******************************************************************************************************************
  128.      *
  129.      *
  130.      ******************************************************************************************************************/
  131.     protected void bindSelectionListener (@Nonnull final ReadOnlyObjectProperty<TreeItem<PresentationModel>> selectedItemProperty)
  132.       {
  133.         selectedItemProperty.removeListener(selectionListener.asTreeItemChangeListener());
  134.         selectedItemProperty.addListener(selectionListener.asTreeItemChangeListener());
  135.       }

  136.     /*******************************************************************************************************************
  137.      *
  138.      *
  139.      ******************************************************************************************************************/
  140.     protected static Boolean shouldShowRoot (@Nonnull final PresentationModel pm)
  141.       {
  142.         return pm.maybeAs(_Visible_).map(Visible::isVisible).orElse(true);
  143.       }
  144.   }