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