1 /*
2 * *************************************************************************************************************************************************************
3 *
4 * SteelBlue: DCI User Interfaces
5 * http://tidalwave.it/projects/steelblue
6 *
7 * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8 *
9 * *************************************************************************************************************************************************************
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18 *
19 * *************************************************************************************************************************************************************
20 *
21 * git clone https://bitbucket.org/tidalwave/steelblue-src
22 * git clone https://github.com/tidalwave-it/steelblue-src
23 *
24 * *************************************************************************************************************************************************************
25 */
26 package it.tidalwave.ui.javafx.impl.common;
27
28 import jakarta.annotation.Nonnull;
29 import java.beans.PropertyChangeListener;
30 import java.util.concurrent.Executor;
31 import javafx.beans.property.ObjectProperty;
32 import javafx.beans.property.ReadOnlyObjectProperty;
33 import javafx.scene.control.TreeItem;
34 import javafx.application.Platform;
35 import it.tidalwave.util.annotation.VisibleForTesting;
36 import it.tidalwave.ui.core.role.PresentationModel;
37 import it.tidalwave.ui.core.role.Visibility;
38 import it.tidalwave.ui.javafx.impl.tree.ObsoletePresentationModelDisposer;
39 import lombok.Getter;
40 import lombok.extern.slf4j.Slf4j;
41 import static java.util.stream.Collectors.*;
42 import static it.tidalwave.ui.core.role.Visibility._Visible_;
43 import static it.tidalwave.ui.javafx.impl.DefaultJavaFXBinder.enforceFxApplicationThread;
44 import static it.tidalwave.ui.javafx.impl.common.JavaFXWorker.childrenPm;
45
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 = new ChangeListenerSelectableAdapter(executor);
58
59 private final ObsoletePresentationModelDisposer presentationModelDisposer = new ObsoletePresentationModelDisposer();
60
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 protected void setChildren (@Nonnull final TreeItem<PresentationModel> parentItem, final int depth)
76 {
77 JavaFXWorker.run(executor,
78 () -> childrenPm(parentItem.getValue(), depth),
79 items -> parentItem.getChildren().setAll(items.stream()
80 .map(childPm -> createTreeItem(childPm, depth))
81 .collect(toList())));
82 }
83
84 /***********************************************************************************************************************************************************
85 * Creates a single {@link TreeItem} for the given the {@link PresentationModel}. When the {@code PresentationModel}
86 * fires the {@link PresentationModel#PROPERTY_CHILDREN} property change event, children are recreated.
87 *
88 * @param pm the {@code PresentationModel}
89 * @param depth the depth level (used only for logging)
90 * @return the
91 **********************************************************************************************************************************************************/
92 @Nonnull
93 protected TreeItem<PresentationModel> createTreeItem (@Nonnull final PresentationModel pm, final int depth)
94 {
95 enforceFxApplicationThread();
96 final TreeItem<PresentationModel> item = new PresentationModelTreeItem(pm);
97
98 final PropertyChangeListener recreateChildrenOnUpdateListener = __ ->
99 Platform.runLater(() ->
100 {
101 log.debug("On recreateChildrenOnUpdateListener");
102 setChildren(item, depth + 1);
103 item.setExpanded(true);
104 });
105
106 pm.addPropertyChangeListener(PresentationModel.PROPERTY_CHILDREN, recreateChildrenOnUpdateListener);
107
108 item.expandedProperty().addListener((observable, oldValue, newValue) ->
109 {
110 if (newValue)
111 {
112 setChildren(item, depth + 1);
113 }
114 });
115
116 return item;
117 }
118
119 /***********************************************************************************************************************************************************
120 *
121 **********************************************************************************************************************************************************/
122 protected void setRootProperty (@Nonnull final PresentationModel pm,
123 @Nonnull final ObjectProperty<TreeItem<PresentationModel>> rootProperty)
124 {
125 rootProperty.removeListener(presentationModelDisposer);
126 rootProperty.addListener(presentationModelDisposer);
127 rootProperty.set(createTreeItem(pm, 0));
128 }
129
130 /***********************************************************************************************************************************************************
131 *
132 **********************************************************************************************************************************************************/
133 protected void bindSelectionListener (@Nonnull final ReadOnlyObjectProperty<TreeItem<PresentationModel>> selectedItemProperty)
134 {
135 selectedItemProperty.removeListener(selectionListener.asTreeItemChangeListener());
136 selectedItemProperty.addListener(selectionListener.asTreeItemChangeListener());
137 }
138
139 /***********************************************************************************************************************************************************
140 *
141 **********************************************************************************************************************************************************/
142 protected static Boolean shouldShowRoot (@Nonnull final PresentationModel pm)
143 {
144 return pm.maybeAs(_Visible_).map(Visibility::isVisible).orElse(true);
145 }
146 }