Skip to contentMethod: of(Class)
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:
28: import jakarta.annotation.Nonnull;
29: import java.io.IOException;
30: import javafx.fxml.FXMLLoader;
31: import javafx.scene.Node;
32: import it.tidalwave.ui.javafx.impl.DefaultNodeAndDelegate;
33:
34: /***************************************************************************************************************************************************************
35: *
36: * This facility class create a thread-safe proxy for the JavaFX delegate (controller). Thread-safe means that it can
37: * be called by any thread and the JavaFX UI related stuff will be safely invoked in the JavaFX UI Thread.
38: * It is usually used in this way:
39: *
40: * <pre>
41: * // This is a Spring bean
42: * public class JavaFxFooBarPresentation implements FooBarPresentation
43: * {
44: * private static final String FXML_URL = "/my/package/javafx/FooBar.fxml";
45: *
46: * {@literal @}Inject
47: * private FlowController flowController;
48: *
49: * private final NodeAndDelegate nad = createNodeAndDelegate(getClass(), FXML_URL);
50: *
51: * private final FooBarPresentation delegate = nad.getDelegate();
52: *
53: * public void showUp()
54: * {
55: * flowController.doSomething(nad.getNode());
56: * }
57: *
58: * public void showData (final String data)
59: * {
60: * delegate.showData(data);
61: * }
62: * }
63: * </pre>
64: *
65: * The method {@link #of(java.lang.Class, java.lang.String)} safely invokes the {@link FXMLLoader}
66: * and returns a {@link NodeAndDelegate} that contains both the visual {@link Node} and its delegate (controller).
67: *
68: * The latter is wrapped by a safe proxy that makes sure that any method invocation (such as {@code showData()} in the
69: * example is again executed in the JavaFX UI Thread. This means that the Presentation object methods can be invoked
70: * in any thread.
71: *
72: * For method returning {@code void}, the method invocation is asynchronous; that is, the caller is not blocked waiting
73: * for the method execution completion. If a return value is provided, the invocation is synchronous, and the caller
74: * will correctly wait the completion of the execution in order to get the result value.
75: *
76: * A typical JavaFX delegate (controller) looks like:
77: *
78: * <pre>
79: * // This is not a Spring bean - created by the FXMLLoader
80: * public class JavaFxFooBarPresentationDelegate implements FooBarPresentation
81: * {
82: * {@literal @}FXML
83: * private Label label;
84: *
85: * {@literal @}FXML
86: * private Button button;
87: *
88: * {@literal @}Inject // the only thing that can be injected, by means of JavaFXSafeProxyCreator
89: * private JavaFxBinder binder;
90: *
91: * {@literal @}Override
92: * public void bind (final UserAction action)
93: * {
94: * binder.bind(button, action);
95: * }
96: *
97: * {@literal @}Override
98: * public void showData (final String data)
99: * {
100: * label.setText(data);
101: * }
102: * }
103: * </pre>
104: *
105: * Not only all the methods invoked on the delegate are guaranteed to run in the JavaFX UI thread, but also its
106: * constructor, as per JavaFX requirements.
107: *
108: * A Presentation Delegate must not try to have dependency injection from Spring (for instance, by means of AOP),
109: * otherwise a deadlock could be triggered. Injection in constructors is safe.
110: *
111: * @author Fabrizio Giudici
112: *
113: **************************************************************************************************************************************************************/
114: public interface NodeAndDelegate<T>
115: {
116: /***********************************************************************************************************************************************************
117: * Creates a {@link NodeAndDelegate} for the given presentation class. The FXML resource name is inferred by
118: * default, For instance, is the class is named {@code JavaFXFooBarPresentation}, the resource name is
119: * {@code FooBar.fxml} and searched in the same packages as the class.
120: * @param presentationClass the class of the presentation for which the resources must be created.
121: * @since 1.0-ALPHA-13
122: * @see #of(java.lang.Class, java.lang.String)
123: **********************************************************************************************************************************************************/
124: @Nonnull
125: public static <T> NodeAndDelegate<T> of (@Nonnull final Class<T> presentationClass)
126: {
127: return DefaultNodeAndDelegate.of(presentationClass);
128: }
129:
130: /***********************************************************************************************************************************************************
131: * Creates a {@link NodeAndDelegate} for the given presentation class.
132: * @param presentationClass the class of the presentation for which the resources must be created.
133: * @param fxmlResourcePath the path of the FXML resource
134: **********************************************************************************************************************************************************/
135: @Nonnull
136: public static <T> NodeAndDelegate<T> of (@Nonnull final Class<T> presentationClass, @Nonnull final String fxmlResourcePath)
137: {
138: return DefaultNodeAndDelegate.of(presentationClass, fxmlResourcePath);
139: }
140:
141: @Nonnull
142: public static <T> NodeAndDelegate<T> load (@Nonnull final Class<T> clazz, @Nonnull final String resource)
143: throws IOException
144: {
145: return DefaultNodeAndDelegate.load(clazz, resource);
146: }
147:
148: @Nonnull
149: public Node getNode();
150:
151: @Nonnull
152: public T getDelegate();
153: }