ObsoletePresentationModelDisposer.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 javafx.beans.value.ChangeListener;
  32. import javafx.beans.value.ObservableValue;
  33. import javafx.scene.control.TreeItem;
  34. import javafx.scene.control.TreeView;
  35. import it.tidalwave.role.ui.PresentationModel;
  36. import lombok.extern.slf4j.Slf4j;

  37. /***********************************************************************************************************************
  38.  *
  39.  * This listener calls {@link PresentationModel#dispose()} on instances that have been detached from a {@link TreeView}.
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **********************************************************************************************************************/
  44. @Slf4j
  45. public class ObsoletePresentationModelDisposer implements ChangeListener<TreeItem<PresentationModel>>
  46.   {
  47.     @Override
  48.     public void changed (@Nonnull final ObservableValue<? extends TreeItem<PresentationModel>> ov,
  49.                          final TreeItem<PresentationModel> oldTreeItem,
  50.                          final TreeItem<PresentationModel> newTreeItem)
  51.       {
  52.         if (oldTreeItem != null)
  53.           {
  54.             disposeRecursively(oldTreeItem);
  55.           }
  56.       }

  57.     private void disposeRecursively (@Nonnull final TreeItem<PresentationModel> treeItem)
  58.       {
  59.         treeItem.getValue().dispose();
  60.         treeItem.setValue(null);

  61.         for (final TreeItem<PresentationModel> childTreeItem : treeItem.getChildren())
  62.           {
  63.             disposeRecursively(childTreeItem);
  64.           }
  65.       }
  66.   }