Skip to contentPackage: NodeAndDelegate
NodeAndDelegate
Coverage
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 java.io.IOException;
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: *
41: * @author Fabrizio Giudici
42: *
43: **************************************************************************************************************************************************************/
44: @RequiredArgsConstructor(access = PRIVATE) @Getter @Slf4j
45: public final class NodeAndDelegate<T>
46: {
47: @Nonnull
48: private final Node node;
49:
50: @Nonnull
51: private final T delegate;
52:
53: @Nonnull
54: public static <T> NodeAndDelegate<T> load (@Nonnull final Class<T> clazz, @Nonnull final String resource)
55: throws IOException
56: {
57: log.debug("NodeAndDelegate({}, {})", clazz, resource);
58:• assert Platform.isFxApplicationThread() : "Not in JavaFX UI Thread";
59: final var loader = new FXMLLoader(clazz.getResource(resource), null, null,
60: type -> ReflectionUtils.instantiateWithDependencies(type, JavaFXSafeProxyCreator.BEANS));
61: final Node node = loader.load();
62: final T jfxController = loader.getController();
63: ReflectionUtils.injectDependencies(jfxController, JavaFXSafeProxyCreator.BEANS);
64: final var interfaces = jfxController.getClass().getInterfaces();
65:
66:• if (interfaces.length == 0)
67: {
68: log.warn("{} has no interface: not creating safe proxy", jfxController.getClass());
69: log.debug(">>>> load({}, {}) completed", clazz, resource);
70: return new NodeAndDelegate<>(node, jfxController);
71: }
72: else
73: {
74: final var safeDelegate = JavaFXSafeProxyCreator.createSafeProxy(jfxController, interfaces);
75: log.debug(">>>> load({}, {}) completed", clazz, resource);
76: return new NodeAndDelegate<>(node, safeDelegate);
77: }
78: }
79: }