it.tidalwave.ui.javafx.JavaFXSafeProxyCreator Maven / Gradle / Ivy
/*
* *************************************************************************************************************************************************************
*
* SteelBlue: DCI User Interfaces
* http://tidalwave.it/projects/steelblue
*
* Copyright (C) 2015 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *************************************************************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*
* *************************************************************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/steelblue-src
* git clone https://github.com/tidalwave-it/steelblue-src
*
* *************************************************************************************************************************************************************
*/
package it.tidalwave.ui.javafx;
import java.lang.reflect.Proxy;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.application.Platform;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import it.tidalwave.role.ui.MenuBarModel;
import it.tidalwave.role.ui.ToolBarModel;
import it.tidalwave.role.ui.javafx.JavaFXBinder;
import it.tidalwave.role.ui.javafx.impl.DefaultJavaFXBinder;
import it.tidalwave.role.ui.javafx.impl.JavaFXMenuBarModel;
import it.tidalwave.role.ui.javafx.impl.JavaFXToolBarModel;
import it.tidalwave.role.ui.javafx.impl.util.JavaFXSafeProxy;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/***************************************************************************************************************************************************************
*
* This facility class create a thread-safe proxy for the JavaFX delegate (controller). Thread-safe means that it can
* be called by any thread and the JavaFX UI related stuff will be safely invoked in the JavaFX UI Thread.
* It is usually used in this way:
*
*
* // This is a Spring bean
* public class JavaFxFooBarPresentation implements FooBarPresentation
* {
* private static final String FXML_URL = "/my/package/javafx/FooBar.fxml";
*
* {@literal @}Inject
* private FlowController flowController;
*
* private final NodeAndDelegate nad = createNodeAndDelegate(getClass(), FXML_URL);
*
* private final FooBarPresentation delegate = nad.getDelegate();
*
* public void showUp()
* {
* flowController.doSomething(nad.getNode());
* }
*
* public void showData (final String data)
* {
* delegate.showData(data);
* }
* }
*
*
* The method {@link #createNodeAndDelegate(java.lang.Class, java.lang.String)} safely invokes the {@link FXMLLoader}
* and returns a {@link NodeAndDelegate} that contains both the visual {@link Node} and its delegate (controller).
*
* The latter is wrapped by a safe proxy that makes sure that any method invocation (such as {@code showData()} in the
* example is again executed in the JavaFX UI Thread. This means that the Presentation object methods can be invoked
* in any thread.
*
* For method returning {@code void}, the method invocation is asynchronous; that is, the caller is not blocked waiting
* for the method execution completion. If a return value is provided, the invocation is synchronous, and the caller
* will correctly wait the completion of the execution in order to get the result value.
*
* A typical JavaFX delegate (controller) looks like:
*
*
* // This is not a Spring bean - created by the FXMLLoader
* public class JavaFxFooBarPresentationDelegate implements FooBarPresentation
* {
* {@literal @}FXML
* private Label label;
*
* {@literal @}FXML
* private Button button;
*
* {@literal @}Inject // the only thing that can be injected, by means of JavaFXSafeProxyCreator
* private JavaFxBinder binder;
*
* {@literal @}Override
* public void bind (final UserAction action)
* {
* binder.bind(button, action);
* }
*
* {@literal @}Override
* public void showData (final String data)
* {
* label.setText(data);
* }
* }
*
*
* Not only all the methods invoked on the delegate are guaranteed to run in the JavaFX UI thread, but also its
* constructor, as per JavaFX requirements.
*
* A Presentation Delegate must not try to have dependency injection from Spring (for instance, by means of AOP),
* otherwise a deadlock could be triggered. Injection in constructors is safe.
*
* @author Fabrizio Giudici
*
**************************************************************************************************************************************************************/
@Slf4j
public class JavaFXSafeProxyCreator
{
private static final String P_TIMEOUT = JavaFXSafeProxyCreator.class.getName() + ".initTimeout";
private static final int initializerTimeout = Integer.getInteger(P_TIMEOUT, 10);
public static final Map, Object> BEANS = new HashMap<>();
@Getter
private static final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
@Getter
private static final JavaFXBinder javaFxBinder = new DefaultJavaFXBinder(executor);
@Getter
private static final ToolBarModel toolBarModel = new JavaFXToolBarModel();
@Getter
private static final MenuBarModel menuBarModel = new JavaFXMenuBarModel();
static
{
executor.setWaitForTasksToCompleteOnShutdown(false);
executor.setThreadNamePrefix("javafxBinder-");
// Fix for STB-26
executor.setCorePoolSize(1);
executor.setMaxPoolSize(1);
executor.setQueueCapacity(10000);
BEANS.put(JavaFXBinder.class, javaFxBinder);
BEANS.put(Executor.class, executor);
BEANS.put(ToolBarModel.class, toolBarModel);
BEANS.put(MenuBarModel.class, menuBarModel);
}
private JavaFXSafeProxyCreator () {}
/***********************************************************************************************************************************************************
* Creates a {@link NodeAndDelegate} for the given presentation class. The FXML resource name is inferred by
* default, For instance, is the class is named {@code JavaFXFooBarPresentation}, the resource name is
* {@code FooBar.fxml} and searched in the same packages as the class.
*
* @see #createNodeAndDelegate(java.lang.Class, java.lang.String)
*
* @since 1.0-ALPHA-13
*
* @param presentationClass the class of the presentation for which the resources must be created.
**********************************************************************************************************************************************************/
@Nonnull
public static NodeAndDelegate createNodeAndDelegate (@Nonnull final Class presentationClass)
{
final var resource = presentationClass.getSimpleName().replaceAll("^JavaFX", "")
.replaceAll("^JavaFx", "")
.replaceAll("Presentation$", "")
+ ".fxml";
return createNodeAndDelegate(presentationClass, resource);
}
/***********************************************************************************************************************************************************
* Creates a {@link NodeAndDelegate} for the given presentation class.
*
* @param presentationClass the class of the presentation for which the resources must be created.
* @param fxmlResourcePath the path of the FXML resource
**********************************************************************************************************************************************************/
@Nonnull
public static NodeAndDelegate createNodeAndDelegate (@Nonnull final Class presentationClass, @Nonnull final String fxmlResourcePath)
{
log.debug("createNodeAndDelegate({}, {})", presentationClass, fxmlResourcePath);
final var latch = new CountDownLatch(1);
final var nad = new AtomicReference>();
final var exception = new AtomicReference();
if (Platform.isFxApplicationThread())
{
try
{
return NodeAndDelegate.load(presentationClass, fxmlResourcePath);
}
catch (IOException e)
{
exception.set(new RuntimeException(e));
}
}
Platform.runLater(() ->
{
try
{
nad.set(NodeAndDelegate.load(presentationClass, fxmlResourcePath));
}
catch (RuntimeException e)
{
exception.set(e);
}
catch (Exception e)
{
exception.set(new RuntimeException(e));
}
latch.countDown();
});
try
{
log.debug("Waiting for NodeAndDelegate initialisation in JavaFX thread...");
log.debug("If deadlocks and you need longer time with the debugger, set {} (current value: {})", P_TIMEOUT, initializerTimeout);
latch.await(initializerTimeout, TimeUnit.SECONDS); // FIXME
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
if (exception.get() != null)
{
throw exception.get();
}
if (nad.get() == null)
{
final var message = String.format("Likely deadlock in the JavaFX Thread: couldn't create NodeAndDelegate: %s, %s",
presentationClass, fxmlResourcePath);
throw new RuntimeException(message);
}
return nad.get();
}
/***********************************************************************************************************************************************************
*
**********************************************************************************************************************************************************/
@Nonnull
public static T createSafeProxy (@Nonnull final T target, final Class>[] interfaces)
{
return (T)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, new JavaFXSafeProxy<>(target));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy