Search in sources :

Example 11 with Platform

use of javafx.application.Platform in project dwoss by gg-net.

the class UiCore method startJavaFx.

/**
 * Starts the Ui in JavaFx variant.
 * <p>
 * This also assumes two things:
 * <ul>
 * <li>The JavaFX Platform is already running (as a Stage already exists), most likely created through default lifecycle of javaFx</li>
 * <li>This Stage will always be open or the final to be closed, so implicitExit is ok</li>
 * </ul>
 *
 * @param <T>          type restriction.
 * @param primaryStage the primaryStage for the application, not yet visible.
 * @param builder      the build for the main ui.
 */
public static <T extends Parent> void startJavaFx(final Stage primaryStage, final Callable<T> builder) {
    if (isRunning())
        throw new IllegalStateException("UiCore is already initialised and running");
    mainStage = primaryStage;
    FxSaft.dispatch(() -> {
        T node = builder.call();
        primaryStage.setTitle(TitleUtil.title(node.getClass()));
        primaryStage.setScene(new Scene(node));
        primaryStage.centerOnScreen();
        primaryStage.sizeToScene();
        primaryStage.show();
        primaryStage.setOnCloseRequest((e) -> {
            L.debug("Closing with {}", e);
            // enure no loops.
            if (!shuttingDown.compareAndSet(false, true))
                return;
            ON_SHUTDOWN.forEach(Runnable::run);
            FxCore.ACTIVE_STAGES.values().forEach(w -> Optional.ofNullable(w.get()).ifPresent(s -> s.hide()));
            EXECUTOR_SERVICE.shutdownNow();
            new ArrayList<>(StageHelper.getStages()).forEach((Stage s) -> {
                // Close all free stages.
                if (s != primaryStage)
                    s.close();
            });
        });
        return null;
    });
}
Also used : Scene(javafx.scene.Scene) java.util(java.util) Logger(org.slf4j.Logger) java.util.concurrent(java.util.concurrent) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WindowAdapter(java.awt.event.WindowAdapter) SwingExceptionDialog(eu.ggnet.saft.core.exception.SwingExceptionDialog) WindowEvent(java.awt.event.WindowEvent) StageHelper(com.sun.javafx.stage.StageHelper) InvocationTargetException(java.lang.reflect.InvocationTargetException) java.awt(java.awt) Consumer(java.util.function.Consumer) Platform(javafx.application.Platform) Parent(javafx.scene.Parent) BooleanProperty(javafx.beans.property.BooleanProperty) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) Stage(javafx.stage.Stage) UiWorkflowBreak(eu.ggnet.saft.core.ui.builder.UiWorkflowBreak) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) eu.ggnet.saft.core.ui(eu.ggnet.saft.core.ui) ExceptionUtil(eu.ggnet.saft.core.exception.ExceptionUtil) WeakReference(java.lang.ref.WeakReference) JFrame(javax.swing.JFrame) Stage(javafx.stage.Stage) Scene(javafx.scene.Scene)

Aggregations

Platform (javafx.application.Platform)11 Scene (javafx.scene.Scene)7 IOException (java.io.IOException)6 Stage (javafx.stage.Stage)6 File (java.io.File)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Label (javafx.scene.control.Label)5 Parent (javafx.scene.Parent)4 Level (ch.qos.logback.classic.Level)3 Logger (ch.qos.logback.classic.Logger)3 Guice (com.google.inject.Guice)3 Injector (com.google.inject.Injector)3 Application (javafx.application.Application)3 StackPane (javafx.scene.layout.StackPane)3 LoggerFactory (org.slf4j.LoggerFactory)3 Key (com.google.inject.Key)2 Names (com.google.inject.name.Names)2 URL (java.net.URL)2 Paths (java.nio.file.Paths)2