View Javadoc
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.list;
27  
28  import jakarta.annotation.Nonnull;
29  import java.util.List;
30  import java.util.Optional;
31  import java.util.concurrent.Executor;
32  import javafx.beans.property.ReadOnlyObjectProperty;
33  import javafx.beans.value.ChangeListener;
34  import javafx.collections.ObservableList;
35  import javafx.scene.control.ListCell;
36  import javafx.scene.control.ListView;
37  import javafx.util.Callback;
38  import it.tidalwave.ui.core.role.PresentationModel;
39  import it.tidalwave.ui.core.role.UserAction;
40  import it.tidalwave.ui.javafx.impl.common.CellBinder;
41  import it.tidalwave.ui.javafx.impl.common.ChangeListenerSelectableAdapter;
42  import it.tidalwave.ui.javafx.impl.common.DelegateSupport;
43  import it.tidalwave.ui.javafx.impl.common.JavaFXWorker;
44  import it.tidalwave.ui.javafx.impl.common.RoleBag;
45  import lombok.extern.slf4j.Slf4j;
46  import static javafx.collections.FXCollections.observableArrayList;
47  import static javafx.scene.input.KeyCode.*;
48  import static it.tidalwave.ui.core.role.UserActionProvider._UserActionProvider_;
49  import static it.tidalwave.ui.javafx.impl.common.JavaFXWorker.childrenPm;
50  
51  /***************************************************************************************************************************************************************
52   *
53   * @author  Fabrizio Giudici
54   *
55   **************************************************************************************************************************************************************/
56  @Slf4j
57  public class ListViewBindings extends DelegateSupport
58    {
59      private final Callback<ListView<PresentationModel>, ListCell<PresentationModel>> cellFactory;
60  
61      private final ChangeListener<PresentationModel> changeListener = new ChangeListenerSelectableAdapter(executor);
62  
63      /***********************************************************************************************************************************************************
64       *
65       **********************************************************************************************************************************************************/
66      public ListViewBindings (@Nonnull final Executor executor, @Nonnull final CellBinder cellBinder)
67        {
68          super(executor);
69          cellFactory = listView -> new AsObjectListCell<>(cellBinder);
70        }
71  
72      /***********************************************************************************************************************************************************
73       * {@inheritDoc}
74       **********************************************************************************************************************************************************/
75      public void bind (@Nonnull final ListView<PresentationModel> listView,
76                        @Nonnull final PresentationModel pm,
77                        @Nonnull final Optional<Runnable> callback)
78        {
79          listView.setCellFactory(cellFactory);
80  
81          // FIXME: WEAK LISTENERS
82  
83          // FIXME: this won't work with any external navigation system, such as CEC menus
84          // TODO: try by having CEC selection emulating RETURN and optionally accepting RETURN here
85          listView.setOnKeyPressed(event ->
86            {
87              if (List.of(SPACE, ENTER).contains(event.getCode()))
88                {
89                  final var selectedPm = listView.getSelectionModel().getSelectedItem();
90                  // TODO: must call the default action - but should we look up it again?
91                  // Otherwise emulate mouse double click on the cell
92                  ListViewBindings.log.debug("ListView onKeyPressed: {}", selectedPm);
93  
94                  executor.execute(() ->
95                    {
96                      // FIXME: it would be nicer to retrieve the cell and its associated RoleBag?
97                      final var roles = new RoleBag(selectedPm, List.of(_UserActionProvider_));
98                      roles.getDefaultUserAction().ifPresent(UserAction::actionPerformed);
99                    });
100               }
101           });
102 
103         final var selectedProperty = listView.getSelectionModel().selectedItemProperty();
104         selectedProperty.removeListener(changeListener);
105         listView.setItems(observableArrayList()); // quick clear in case of long operations FIXME doesn't work
106         JavaFXWorker.run(executor,
107                          () -> childrenPm(pm),
108                          items -> finalize(listView, items, selectedProperty, callback));
109       }
110 
111     /***********************************************************************************************************************************************************
112      *
113      **********************************************************************************************************************************************************/
114     private void finalize (@Nonnull final ListView<PresentationModel> listView,
115                            @Nonnull final ObservableList<PresentationModel> items,
116                            @Nonnull final ReadOnlyObjectProperty<PresentationModel> selectedProperty,
117                            @Nonnull final Optional<Runnable> callback)
118       {
119         listView.setItems(items);
120         selectedProperty.addListener(changeListener);
121         callback.ifPresent(Runnable::run);
122       }
123   }