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

  27. import jakarta.annotation.Nonnull;
  28. import java.io.IOException;
  29. import java.net.URL;
  30. import javafx.fxml.FXMLLoader;
  31. import javafx.scene.Node;
  32. import javafx.application.Platform;
  33. import it.tidalwave.util.ReflectionUtils;
  34. import lombok.Getter;
  35. import lombok.RequiredArgsConstructor;
  36. import lombok.extern.slf4j.Slf4j;
  37. import static lombok.AccessLevel.PRIVATE;

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

  48.     @Nonnull
  49.     private final T delegate;

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

  64.             if (interfaces.length == 0)
  65.               {
  66.                 log.warn("{} has no interface: not creating safe proxy", jfxController.getClass());
  67.                 log.debug(">>>> load({}, {}) completed", clazz, resource);
  68.                 return new NodeAndDelegate<>(node, jfxController);
  69.               }
  70.             else
  71.               {
  72.                 final var safeDelegate = JavaFXSafeProxyCreator.createSafeProxy(jfxController, interfaces);
  73.                 log.debug(">>>> load({}, {}) completed", clazz, resource);
  74.                 return new NodeAndDelegate<>(node, safeDelegate);
  75.               }
  76.           }
  77.         catch (IllegalStateException e)
  78.           {
  79.             final var message = String.format("ERROR: Cannot find resource: %s/%s", clazz.getPackageName().replace('.','/'), resource);
  80.             log.error("ERROR: Cannot find resource: {}", message);
  81.             throw new IllegalStateException(message);
  82.           }
  83.       }
  84.   }