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.combobox;
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.event.ActionEvent;
36  import javafx.event.EventHandler;
37  import javafx.scene.control.ComboBox;
38  import javafx.scene.control.ListCell;
39  import javafx.scene.control.ListView;
40  import javafx.util.Callback;
41  import it.tidalwave.ui.core.role.PresentationModel;
42  import it.tidalwave.ui.core.role.UserAction;
43  import it.tidalwave.ui.core.role.UserActionProvider;
44  import it.tidalwave.ui.javafx.impl.common.CellBinder;
45  import it.tidalwave.ui.javafx.impl.common.ChangeListenerSelectableAdapter;
46  import it.tidalwave.ui.javafx.impl.common.DelegateSupport;
47  import it.tidalwave.ui.javafx.impl.common.JavaFXWorker;
48  import it.tidalwave.ui.javafx.impl.list.AsObjectListCell;
49  import lombok.extern.slf4j.Slf4j;
50  import static javafx.scene.input.KeyCode.*;
51  import static it.tidalwave.ui.core.role.UserActionProvider._UserActionProvider_;
52  import static it.tidalwave.ui.javafx.impl.common.JavaFXWorker.childrenPm;
53  
54  /***************************************************************************************************************************************************************
55   *
56   * @author  Fabrizio Giudici
57   *
58   **************************************************************************************************************************************************************/
59  @Slf4j
60  public class ComboBoxBindings extends DelegateSupport
61    {
62      @Nonnull
63      private final CellBinder cellBinder;
64  
65      private final Callback<ListView<PresentationModel>, ListCell<PresentationModel>> cellFactory;
66  
67      private final ChangeListener<PresentationModel> changeListener = new ChangeListenerSelectableAdapter(executor);
68  
69      /***********************************************************************************************************************************************************
70       * Event handler that executes the default action bound to the given combobox item.
71       **********************************************************************************************************************************************************/
72      private final EventHandler<ActionEvent> eventHandler = event ->
73        {
74          final var comboBox = (ComboBox<PresentationModel>)event.getSource();
75          final var selectedPm = comboBox.getSelectionModel().getSelectedItem();
76          selectedPm.maybeAs(_UserActionProvider_)
77                    .flatMap(UserActionProvider::getOptionalDefaultAction)
78                    .ifPresent(UserAction::actionPerformed);
79        };
80  
81      /***********************************************************************************************************************************************************
82       *
83       **********************************************************************************************************************************************************/
84      public ComboBoxBindings (@Nonnull final Executor executor, @Nonnull final CellBinder cellBinder)
85        {
86          super(executor);
87          this.cellBinder = cellBinder;
88          cellFactory = comboBox -> new AsObjectListCell<>(cellBinder);
89        }
90  
91      /***********************************************************************************************************************************************************
92       * {@inheritDoc}
93       **********************************************************************************************************************************************************/
94      public void bind (@Nonnull final ComboBox<PresentationModel> comboBox,
95                        @Nonnull final PresentationModel pm,
96                        @Nonnull final Optional<Runnable> callback)
97        {
98          comboBox.setCellFactory(cellFactory);
99          comboBox.setButtonCell(new AsObjectListCell<>(cellBinder));
100         comboBox.setOnAction(eventHandler);
101 
102         // FIXME: WEAK LISTENERS
103 
104         // FIXME: this won't work with any external navigation system, such as CEC menus
105         // TODO: try by having CEC selection emulating RETURN and optionally accepting RETURN here
106         comboBox.setOnKeyPressed(event ->
107           {
108             if (List.of(SPACE, ENTER).contains(event.getCode()))
109               {
110                 comboBox.show();
111               }
112           });
113 
114         final var selectedProperty = comboBox.getSelectionModel().selectedItemProperty();
115         selectedProperty.removeListener(changeListener);
116         JavaFXWorker.run(executor,
117                          () -> childrenPm(pm),
118                          items -> finalize(comboBox, items, selectedProperty, callback));
119       }
120 
121     /***********************************************************************************************************************************************************
122      *
123      **********************************************************************************************************************************************************/
124     private void finalize (@Nonnull final ComboBox<PresentationModel> comboBox,
125                            @Nonnull final ObservableList<PresentationModel> items,
126                            @Nonnull final ReadOnlyObjectProperty<PresentationModel> selectedProperty,
127                            @Nonnull final Optional<Runnable> callback)
128       {
129         comboBox.setItems(items);
130 
131         if (!items.isEmpty())
132           {
133             comboBox.getSelectionModel().select(items.get(0));
134           }
135 
136         selectedProperty.addListener(changeListener);
137         callback.ifPresent(Runnable::run);
138       }
139   }