View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * SteelBlue: DCI User Interfaces
5    * http://tidalwave.it/projects/steelblue
6    *
7    * Copyright (C) 2015 - 2024 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.role.ui.javafx.impl;
27  
28  import javax.annotation.Nonnull;
29  import java.util.Collection;
30  import java.util.function.Supplier;
31  import javafx.scene.control.Button;
32  import javafx.scene.control.ToolBar;
33  import it.tidalwave.util.annotation.VisibleForTesting;
34  import it.tidalwave.role.ui.spi.ToolBarModelSupport;
35  import it.tidalwave.role.ui.UserAction;
36  import it.tidalwave.role.ui.javafx.JavaFXBinder;
37  import lombok.NoArgsConstructor;
38  import lombok.extern.slf4j.Slf4j;
39  
40  /***************************************************************************************************************************************************************
41   *
42   * The JavaFX implementation for {@link it.tidalwave.role.ui.ToolBarModel}.
43   *
44   * @author  Fabrizio Giudici
45   * @since   1.1-ALPHA-4
46   *
47   **************************************************************************************************************************************************************/
48  @NoArgsConstructor @Slf4j
49  public class JavaFXToolBarModel extends ToolBarModelSupport<JavaFXBinder, ToolBar>
50    {
51      /***********************************************************************************************************************************************************
52       * @param   userActionsSupplier   the supplier of actions
53       **********************************************************************************************************************************************************/
54      @VisibleForTesting JavaFXToolBarModel (@Nonnull final Supplier<Collection<? extends UserAction>> userActionsSupplier)
55        {
56          super(userActionsSupplier);
57        }
58  
59      /***********************************************************************************************************************************************************
60       * {@inheritDoc}
61       **********************************************************************************************************************************************************/
62      @Override
63      public void populateImpl (@Nonnull final JavaFXBinder binder, @Nonnull final ToolBar toolBar)
64        {
65          final var actions = userActionsSupplier.get();
66          log.info("Toolbar user actions: {}", actions);
67          final var buttons = actions.stream()
68                                     .map((action) -> createButton(binder, action))
69                                     .toArray(Button[]::new);
70          toolBar.getItems().addAll(buttons);
71        }
72  
73      /***********************************************************************************************************************************************************
74       * Creates a {@link Button} bound to the given {@link UserAction}.
75       *
76       * @param   binder    the binder
77       * @param   action    the user action
78       * @return            the button
79       **********************************************************************************************************************************************************/
80      @Nonnull
81      private static Button createButton (@Nonnull final JavaFXBinder binder, @Nonnull final UserAction action)
82        {
83          final var button = new Button();
84          binder.bind(button, action);
85          return button;
86        }
87    }