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.List;
31 import java.util.concurrent.Executor;
32 import java.util.function.Supplier;
33 import javafx.beans.property.ObjectProperty;
34 import javafx.beans.property.ReadOnlyObjectProperty;
35 import javafx.collections.ObservableList;
36 import javafx.scene.control.TreeItem;
37 import javafx.stage.Window;
38 import javafx.application.Platform;
39 import it.tidalwave.ui.core.role.PresentationModel;
40 import it.tidalwave.ui.core.role.Visibility;
41 import it.tidalwave.ui.javafx.impl.tree.ObsoletePresentationModelDisposer;
42 import lombok.Getter;
43 import lombok.extern.slf4j.Slf4j;
44 import static it.tidalwave.ui.core.role.Visibility._Visibility_;
45 import static it.tidalwave.ui.javafx.impl.DefaultJavaFXBinder.enforceFxApplicationThread;
46 import static it.tidalwave.ui.javafx.impl.common.JavaFXWorker.childrenPm;
47 import static java.util.stream.Collectors.*;
48
49 /***************************************************************************************************************************************************************
50 *
51 * Some common stuff for both {@code TreeView} and {@code TreeTableView}.
52 *
53 * @author Fabrizio Giudici
54 *
55 **************************************************************************************************************************************************************/
56 @Slf4j
57 public class TreeItemDelegateSupport extends DelegateSupport
58 {
59 @Getter // for testing
60 private final ChangeListenerSelectableAdapter selectionListener = new ChangeListenerSelectableAdapter(executor);
61
62 private final ObsoletePresentationModelDisposer presentationModelDisposer = new ObsoletePresentationModelDisposer();
63
64 /***********************************************************************************************************************************************************
65 *
66 **********************************************************************************************************************************************************/
67 public TreeItemDelegateSupport (@Nonnull final Executor executor, @Nonnull final Supplier<Window> mainWindow)
68 {
69 super(executor, mainWindow);
70 }
71
72 /***********************************************************************************************************************************************************
73 * Sets the children for a {@link TreeItem}.
74 * @param parentItem the {@code TreeItem}
75 * @param depth the depth level (used only for logging)
76 **********************************************************************************************************************************************************/
77 private void setChildren (@Nonnull final TreeItem<PresentationModel> parentItem, final int depth)
78 {
79 JavaFXWorker.run(executor,
80 () -> childrenPm(parentItem.getValue(), depth),
81 items -> parentItem.getChildren().setAll(createTreeItems(items, depth)));
82 }
83
84 /***********************************************************************************************************************************************************
85 * {@return a list of {@link TreeItem}s populated with the given items}.
86 * @param depth the depth level (used only for logging)
87 **********************************************************************************************************************************************************/
88 @Nonnull
89 private List<TreeItem<PresentationModel>> createTreeItems (@Nonnull final ObservableList<PresentationModel> items, final int depth)
90 {
91 enforceFxApplicationThread();
92 return items.stream().map(childPm -> createTreeItem(childPm, depth)).collect(toList());
93 }
94
95 /***********************************************************************************************************************************************************
96 * Creates a single {@link TreeItem} for the given the {@link PresentationModel}. When the {@code PresentationModel} fires the
97 * {@link PresentationModel#PROPERTY_CHILDREN} property change event, children are recreated.
98 * @param pm the {@code PresentationModel}
99 * @param depth the depth level (used only for logging)
100 * @return the
101 **********************************************************************************************************************************************************/
102 @Nonnull
103 private TreeItem<PresentationModel> createTreeItem (@Nonnull final PresentationModel pm, final int depth)
104 {
105 enforceFxApplicationThread();
106 final TreeItem<PresentationModel> item = new PresentationModelTreeItem(pm);
107
108 final PropertyChangeListener recreateChildrenOnUpdateListener = ignored -> Platform.runLater(() ->
109 {
110 log.debug("On recreateChildrenOnUpdateListener");
111 setChildren(item, depth + 1);
112 item.setExpanded(true);
113 });
114
115 pm.addPropertyChangeListener(PresentationModel.PROPERTY_CHILDREN, recreateChildrenOnUpdateListener);
116
117 item.expandedProperty().addListener((observable, oldValue, newValue) ->
118 {
119 if (newValue)
120 {
121 setChildren(item, depth + 1);
122 }
123 });
124
125 return item;
126 }
127
128 /***********************************************************************************************************************************************************
129 *
130 **********************************************************************************************************************************************************/
131 protected void setRootProperty (@Nonnull final PresentationModel pm, @Nonnull final ObjectProperty<TreeItem<PresentationModel>> rootProperty)
132 {
133 rootProperty.removeListener(presentationModelDisposer);
134 rootProperty.addListener(presentationModelDisposer);
135 rootProperty.set(createTreeItem(pm, 0));
136 }
137
138 /***********************************************************************************************************************************************************
139 *
140 **********************************************************************************************************************************************************/
141 protected void bindSelectionListener (@Nonnull final ReadOnlyObjectProperty<TreeItem<PresentationModel>> selectedItemProperty)
142 {
143 selectedItemProperty.removeListener(selectionListener.asTreeItemChangeListener());
144 selectedItemProperty.addListener(selectionListener.asTreeItemChangeListener());
145 }
146
147 /***********************************************************************************************************************************************************
148 *
149 **********************************************************************************************************************************************************/
150 protected static Boolean shouldShowRoot (@Nonnull final PresentationModel pm)
151 {
152 return pm.maybeAs(_Visibility_).map(Visibility::isVisible).orElse(true);
153 }
154 }