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;
});
}
Aggregations