NodeAndDelegate.java

  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. import javax.annotation.Nonnull;
  28. import java.io.IOException;
  29. import javafx.fxml.FXMLLoader;
  30. import javafx.scene.Node;
  31. import javafx.application.Platform;
  32. import it.tidalwave.util.ReflectionUtils;
  33. import lombok.Getter;
  34. import lombok.RequiredArgsConstructor;
  35. import lombok.extern.slf4j.Slf4j;
  36. import static lombok.AccessLevel.PRIVATE;

  37. /***************************************************************************************************************************************************************
  38.  *
  39.  * @author  Fabrizio Giudici
  40.  *
  41.  **************************************************************************************************************************************************************/
  42. @RequiredArgsConstructor(access = PRIVATE) @Getter @Slf4j
  43. public final class NodeAndDelegate<T>
  44.   {
  45.     @Nonnull
  46.     private final Node node;

  47.     @Nonnull
  48.     private final T delegate;

  49.     @Nonnull
  50.     public static <T> NodeAndDelegate<T> load (@Nonnull final Class<T> clazz, @Nonnull final String resource)
  51.             throws IOException
  52.       {
  53.         log.debug("NodeAndDelegate({}, {})", clazz, resource);
  54.         assert Platform.isFxApplicationThread() : "Not in JavaFX UI Thread";
  55.         final var loader = new FXMLLoader(clazz.getResource(resource), null, null,
  56.                                           type -> ReflectionUtils.instantiateWithDependencies(type, JavaFXSafeProxyCreator.BEANS));
  57.         final Node node = loader.load();
  58.         final T jfxController = loader.getController();
  59.         ReflectionUtils.injectDependencies(jfxController, JavaFXSafeProxyCreator.BEANS);
  60.         final var interfaces = jfxController.getClass().getInterfaces();

  61.         if (interfaces.length == 0)
  62.           {
  63.             log.warn("{} has no interface: not creating safe proxy", jfxController.getClass());
  64.             log.debug(">>>> load({}, {}) completed", clazz, resource);
  65.             return new NodeAndDelegate<>(node, jfxController);
  66.           }
  67.         else
  68.           {
  69.             final var safeDelegate = JavaFXSafeProxyCreator.createSafeProxy(jfxController, interfaces);
  70.             log.debug(">>>> load({}, {}) completed", clazz, resource);
  71.             return new NodeAndDelegate<>(node, safeDelegate);
  72.           }
  73.       }
  74.   }