ButtonBindings.java

  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.button;

  27. import jakarta.annotation.Nonnull;
  28. import java.util.Collection;
  29. import java.util.List;
  30. import java.util.concurrent.Executor;
  31. import java.util.concurrent.atomic.AtomicInteger;
  32. import javafx.beans.property.BooleanProperty;
  33. import javafx.geometry.HPos;
  34. import javafx.geometry.VPos;
  35. import javafx.scene.control.Button;
  36. import javafx.scene.control.ButtonBase;
  37. import javafx.scene.control.MenuItem;
  38. import javafx.scene.control.ToggleButton;
  39. import javafx.scene.control.ToggleGroup;
  40. import javafx.scene.layout.ColumnConstraints;
  41. import javafx.scene.layout.GridPane;
  42. import javafx.scene.layout.Pane;
  43. import javafx.scene.layout.Priority;
  44. import javafx.application.Platform;
  45. import it.tidalwave.ui.core.BoundProperty;
  46. import it.tidalwave.ui.core.role.Displayable;
  47. import it.tidalwave.ui.core.role.PresentationModel;
  48. import it.tidalwave.ui.core.role.Styleable;
  49. import it.tidalwave.ui.core.role.UserAction;
  50. import it.tidalwave.ui.core.role.UserActionProvider;
  51. import it.tidalwave.ui.javafx.impl.common.DelegateSupport;
  52. import it.tidalwave.util.As;
  53. import it.tidalwave.role.SimpleComposite;
  54. import static it.tidalwave.ui.core.role.Displayable._Displayable_;
  55. import static it.tidalwave.ui.core.role.Styleable._Styleable_;
  56. import static it.tidalwave.ui.core.role.UserActionProvider._UserActionProvider_;
  57. import static it.tidalwave.ui.javafx.impl.DefaultJavaFXBinder.enforceFxApplicationThread;
  58. import static java.util.Collections.emptyList;
  59. import static java.util.stream.Collectors.*;

  60. /***************************************************************************************************************************************************************
  61.  *
  62.  * @author  Fabrizio Giudici
  63.  *
  64.  **************************************************************************************************************************************************************/
  65. public class ButtonBindings extends DelegateSupport
  66.   {
  67.     private static final As.Type<SimpleComposite<PresentationModel>>  _SimpleCompositePresentationModel_ = new As.Type<>(SimpleComposite.class);

  68.     /***********************************************************************************************************************************************************
  69.      *
  70.      **********************************************************************************************************************************************************/
  71.     public ButtonBindings (@Nonnull final Executor executor)
  72.       {
  73.         super(executor);
  74.       }

  75.     /***********************************************************************************************************************************************************
  76.      *
  77.      **********************************************************************************************************************************************************/
  78.     public void bind (@Nonnull final ButtonBase button, @Nonnull final UserAction action)
  79.       {
  80.         enforceFxApplicationThread();
  81.         action.maybeAs(_Displayable_).ifPresent(d -> button.setText(d.getDisplayName()));
  82.         button.setOnAction(ignored -> executor.execute(action::actionPerformed));
  83.         bindEnableProperty(button.disableProperty(), action.enabled());
  84.       }

  85.     /***********************************************************************************************************************************************************
  86.      *
  87.      **********************************************************************************************************************************************************/
  88.     public void bind (@Nonnull final MenuItem menuItem, @Nonnull final UserAction action)
  89.       {
  90.         enforceFxApplicationThread();
  91.         menuItem.setText(action.maybeAs(_Displayable_).map(Displayable::getDisplayName).orElse(""));
  92.         menuItem.setOnAction(ignored -> executor.execute(action::actionPerformed));
  93.         bindEnableProperty(menuItem.disableProperty(), action.enabled());
  94.       }

  95.     /***********************************************************************************************************************************************************
  96.      * {@inheritDoc}
  97.      **********************************************************************************************************************************************************/
  98.     public void bindToggleButtons (@Nonnull final Pane pane, @Nonnull final PresentationModel pm)
  99.       {
  100.         enforceFxApplicationThread();
  101.         final var group = new ToggleGroup();
  102.         final var children = pane.getChildren();
  103.         final var prototypeStyleClass = children.get(0).getStyleClass();
  104.         final var pmc = pm.as(_SimpleCompositePresentationModel_);
  105.         children.setAll(pmc.findChildren().stream().map(cpm -> createToggleButton(cpm, prototypeStyleClass, group)).collect(toList()));
  106.       }

  107.     /***********************************************************************************************************************************************************
  108.      *
  109.      **********************************************************************************************************************************************************/
  110.     public void bindButtonsInPane (@Nonnull final GridPane gridPane, @Nonnull final Collection<UserAction> actions)
  111.       {
  112.         enforceFxApplicationThread();
  113.         final var columnConstraints = gridPane.getColumnConstraints();
  114.         final var children = gridPane.getChildren();

  115.         columnConstraints.clear();
  116.         children.clear();
  117.         final var columnIndex = new AtomicInteger(0);

  118.         actions.forEach(menuAction ->
  119.                           {
  120.                           final var column = new ColumnConstraints();
  121.                           column.setPercentWidth(100.0 / actions.size());
  122.                           columnConstraints.add(column);
  123.                           final var button = createButton();
  124.                           GridPane.setConstraints(button, columnIndex.getAndIncrement(), 0);
  125.                           bind(button, menuAction);
  126.                           children.add(button);
  127.                           });
  128.       }

  129.     /***********************************************************************************************************************************************************
  130.      * {@return a new {@code Button}} for the menu bar.
  131.      **********************************************************************************************************************************************************/
  132.     @Nonnull
  133.     private Button createButton()
  134.       {
  135.         final var button = new Button();
  136.         GridPane.setHgrow(button, Priority.ALWAYS);
  137.         GridPane.setVgrow(button, Priority.ALWAYS);
  138.         GridPane.setHalignment(button, HPos.CENTER);
  139.         GridPane.setValignment(button, VPos.CENTER);
  140.         button.setPrefSize(999, 999); // fill
  141.         button.getStyleClass().add("mainMenuButton");

  142.         return button;
  143.       }

  144.     /***********************************************************************************************************************************************************
  145.      *
  146.      **********************************************************************************************************************************************************/
  147.     @Nonnull
  148.     private ToggleButton createToggleButton (@Nonnull final PresentationModel pm, @Nonnull final List<String> baseStyleClass, @Nonnull final ToggleGroup group)
  149.       {
  150.         final var button = new ToggleButton();
  151.         button.setToggleGroup(group);
  152.         button.setText(pm.maybeAs(_Displayable_).map(Displayable::getDisplayName).orElse(""));
  153.         button.getStyleClass().addAll(baseStyleClass);
  154.         button.getStyleClass().addAll(pm.maybeAs(_Styleable_).map(Styleable::getStyles).orElse(emptyList()));
  155.         pm.maybeAs(_UserActionProvider_).flatMap(UserActionProvider::getOptionalDefaultAction)
  156.           .ifPresent(action -> bind(button, action));

  157.         if (group.getSelectedToggle() == null)
  158.           {
  159.             group.selectToggle(button);
  160.           }

  161.         return button;
  162.       }

  163.     /***********************************************************************************************************************************************************
  164.      *
  165.      **********************************************************************************************************************************************************/
  166.     private void bindEnableProperty (@Nonnull final BooleanProperty property1, @Nonnull final BoundProperty<Boolean> property2)
  167.       {
  168.         property1.setValue(!property2.get());
  169.         property1.addListener((_1, _2, newValue) -> executor.execute(() -> property2.set(!newValue)));
  170.         property2.addPropertyChangeListener(evt -> Platform.runLater(() -> property1.setValue(!(boolean)evt.getNewValue())));
  171.       }
  172.   }