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

  27. import jakarta.annotation.Nonnull;
  28. import java.util.concurrent.Executor;
  29. import java.util.function.Function;
  30. import javafx.beans.property.Property;
  31. import javafx.scene.control.TextField;
  32. import javafx.stage.Window;
  33. import javafx.application.Platform;
  34. import it.tidalwave.ui.core.BoundProperty;
  35. import it.tidalwave.ui.javafx.JavaFXBinder;
  36. import it.tidalwave.ui.javafx.impl.button.ButtonBindings;
  37. import it.tidalwave.ui.javafx.impl.combobox.ComboBoxBindings;
  38. import it.tidalwave.ui.javafx.impl.common.DefaultCellBinder;
  39. import it.tidalwave.ui.javafx.impl.common.PropertyAdapter;
  40. import it.tidalwave.ui.javafx.impl.dialog.DialogBindings;
  41. import it.tidalwave.ui.javafx.impl.filechooser.FileChooserBindings;
  42. import it.tidalwave.ui.javafx.impl.list.ListViewBindings;
  43. import it.tidalwave.ui.javafx.impl.tableview.TableViewBindings;
  44. import it.tidalwave.ui.javafx.impl.tree.TreeViewBindings;
  45. import it.tidalwave.ui.javafx.impl.treetable.TreeTableViewBindings;
  46. import lombok.experimental.Delegate;
  47. import lombok.extern.slf4j.Slf4j;
  48. import static java.util.Objects.requireNonNull;

  49. /***************************************************************************************************************************************************************
  50.  *
  51.  * @author  Fabrizio Giudici
  52.  *
  53.  **************************************************************************************************************************************************************/
  54. @Slf4j
  55. public class DefaultJavaFXBinder implements JavaFXBinder
  56.   {
  57.     private final Executor executor;

  58.     private static final String FX_BACKGROUND_COLOR_PINK = "-fx-background-color: pink";

  59.     @Delegate
  60.     private final ButtonBindings buttonBindings;

  61.     @Delegate
  62.     private final TreeViewBindings treeItemBindings;

  63.     @Delegate
  64.     private final TableViewBindings tableViewBindings;

  65.     @Delegate
  66.     private final TreeTableViewBindings treeTableViewBindings;

  67.     @Delegate
  68.     private final ListViewBindings listViewBindings;

  69.     @Delegate
  70.     private final ComboBoxBindings comboBoxBindings;

  71.     @Delegate
  72.     private final DialogBindings dialogBindings;

  73.     @Delegate
  74.     private final FileChooserBindings fileChooserBindings;

  75.     /***********************************************************************************************************************************************************
  76.      *
  77.      **********************************************************************************************************************************************************/
  78.     public DefaultJavaFXBinder (@Nonnull final Executor executor)
  79.       {
  80.         this.executor = executor;
  81.         final var cellBinder = new DefaultCellBinder(executor);
  82.         buttonBindings = new ButtonBindings(executor);
  83.         comboBoxBindings = new ComboBoxBindings(executor, cellBinder);
  84.         treeItemBindings = new TreeViewBindings(executor, cellBinder);
  85.         tableViewBindings = new TableViewBindings(executor, cellBinder);
  86.         treeTableViewBindings = new TreeTableViewBindings(executor, cellBinder);
  87.         listViewBindings = new ListViewBindings(executor, cellBinder);
  88.         dialogBindings = new DialogBindings(executor);
  89.         fileChooserBindings = new FileChooserBindings(executor);
  90.       }

  91.     /***********************************************************************************************************************************************************
  92.      * {@inheritDoc}
  93.      **********************************************************************************************************************************************************/
  94.     @Override
  95.     public void setMainWindow (@Nonnull final Window mainWindow)
  96.       {
  97.         enforceFxApplicationThread();
  98.         treeItemBindings.setMainWindow(mainWindow);
  99.         tableViewBindings.setMainWindow(mainWindow);
  100.         dialogBindings.setMainWindow(mainWindow);
  101.         fileChooserBindings.setMainWindow(mainWindow);
  102.       }

  103.     /***********************************************************************************************************************************************************
  104.      * {@inheritDoc}
  105.      **********************************************************************************************************************************************************/
  106.     @Override
  107.     public <T, S> void bind (@Nonnull final BoundProperty<? super T> target, @Nonnull final Property<? extends S> source, @Nonnull final Function<S, T> adapter)
  108.       {
  109.         enforceFxApplicationThread();
  110.         source.addListener((_1, _2, newValue) -> executor.execute(() -> target.set(adapter.apply(newValue))));
  111.       }

  112.     /***********************************************************************************************************************************************************
  113.      * {@inheritDoc}
  114.      **********************************************************************************************************************************************************/
  115.     @Override @SuppressWarnings("unchecked")
  116.     public <T, S> void bindBidirectionally (@Nonnull final BoundProperty<? super T> property1,
  117.                                             @Nonnull final Property<S> property2,
  118.                                             @Nonnull final Function<? super S, T> adapter,
  119.                                             @Nonnull final Function<? super T, ? extends S> reverseAdapter)
  120.       {
  121.         enforceFxApplicationThread();
  122.         property2.addListener((_1, _2, newValue) -> executor.execute(() -> property1.set(adapter.apply(newValue))));
  123.         property1.addPropertyChangeListener(evt -> Platform.runLater(() -> property2.setValue(reverseAdapter.apply((T)evt.getNewValue()))));
  124.       }

  125.     /***********************************************************************************************************************************************************
  126.      * {@inheritDoc}
  127.      **********************************************************************************************************************************************************/
  128.     @Override
  129.     public void bindBidirectionally (@Nonnull final TextField textField,
  130.                                      @Nonnull final BoundProperty<String> textProperty,
  131.                                      @Nonnull final BoundProperty<Boolean> validProperty)
  132.       {
  133.         enforceFxApplicationThread();
  134.         requireNonNull(textField, "textField");
  135.         requireNonNull(textProperty, "textProperty");
  136.         requireNonNull(validProperty, "validProperty");

  137.         textField.textProperty().bindBidirectional(new PropertyAdapter<>(executor, textProperty));

  138.         // FIXME: weak listener
  139.         validProperty.addPropertyChangeListener(ignored -> textField.setStyle(validProperty.get() ? "" : FX_BACKGROUND_COLOR_PINK));
  140.       }

  141.     /***********************************************************************************************************************************************************
  142.      *
  143.      **********************************************************************************************************************************************************/
  144.     public static void enforceFxApplicationThread()
  145.       {
  146.         if (!Platform.isFxApplicationThread())
  147.           {
  148.             throw new IllegalStateException("Must run in the JavaFX Application Thread");
  149.           }
  150.       }
  151.   }