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.ui.javafx;
27
28 import javax.annotation.Nonnull;
29 import javafx.scene.control.Button;
30 import javafx.scene.control.ToolBar;
31 import it.tidalwave.role.ui.spi.ToolBarModelSupport;
32 import it.tidalwave.role.ui.Displayable;
33 import it.tidalwave.role.ui.UserAction;
34 import it.tidalwave.role.ui.javafx.JavaFXBinder;
35 import static it.tidalwave.role.ui.Displayable._Displayable_;
36
37 /***************************************************************************************************************************************************************
38 *
39 * The JavaFX implementation for {@link it.tidalwave.role.ui.ToolBarModel}.
40 *
41 * @author Fabrizio Giudici
42 * @since 1.1-ALPHA-4
43 *
44 **************************************************************************************************************************************************************/
45 public class JavaFXToolBarModel extends ToolBarModelSupport
46 {
47 /***********************************************************************************************************************************************************
48 * {@inheritDoc}
49 **********************************************************************************************************************************************************/
50 @Override
51 public void populate (@Nonnull final Object binder, @Nonnull final Object toolBar)
52 {
53 final var buttons = findToolBarUserActions()
54 .map((action) -> createButton((JavaFXBinder)binder, action))
55 .toArray(Button[]::new);
56 ((ToolBar)toolBar).getItems().addAll(buttons);
57 }
58
59 /***********************************************************************************************************************************************************
60 * Creates a {@link Button} bound to the given {@link UserAction}.
61 *
62 * @param binder the binder
63 * @param action the user action
64 * @return the button
65 **********************************************************************************************************************************************************/
66 @Nonnull
67 private static Button createButton (@Nonnull final JavaFXBinder binder, @Nonnull final UserAction action)
68 {
69 final var button = new Button();
70 // FIXME: move to JavaFXBinder
71 button.setText(action.maybeAs(_Displayable_).map(Displayable::getDisplayName).orElse("???"));
72 binder.bind(button, action);
73 return button;
74 }
75 }