JavaFXApplicationWithSplash.java

  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. import jakarta.annotation.Nonnull;
  28. import java.util.concurrent.Executor;
  29. import java.util.concurrent.Executors;
  30. import java.io.IOException;
  31. import javafx.animation.KeyFrame;
  32. import javafx.animation.Timeline;
  33. import javafx.scene.Parent;
  34. import javafx.scene.Scene;
  35. import javafx.scene.input.KeyCombination;
  36. import javafx.stage.Screen;
  37. import javafx.stage.Stage;
  38. import javafx.stage.StageStyle;
  39. import javafx.util.Duration;
  40. import javafx.application.Application;
  41. import javafx.application.Platform;
  42. import jfxtras.styles.jmetro.JMetro;
  43. import jfxtras.styles.jmetro.Style;
  44. import org.slf4j.Logger;
  45. import org.slf4j.LoggerFactory;
  46. import it.tidalwave.util.Key;
  47. import it.tidalwave.util.PreferencesHandler;
  48. import lombok.Getter;
  49. import lombok.Setter;

  50. /***************************************************************************************************************************************************************
  51.  *
  52.  * @author  Fabrizio Giudici
  53.  *
  54.  **************************************************************************************************************************************************************/
  55. public abstract class JavaFXApplicationWithSplash extends Application
  56.   {
  57.     private static final String K_BASE_NAME = "it.tidalwave.javafx";

  58.     /** A property representing the initial main window size as a percentual of the screen size. */
  59.     public static final Key<Double> K_INITIAL_SIZE = Key.of(K_BASE_NAME + ".initialSize", Double.class);

  60.     /** Whether the application should start maximized. */
  61.     public static final Key<Boolean> K_MAXIMIZED = Key.of(K_BASE_NAME + ".maximized", Boolean.class);

  62.     /** Whether the application should start at full screen. */
  63.     public static final Key<Boolean> K_FULL_SCREEN = Key.of(K_BASE_NAME + ".fullScreen", Boolean.class);

  64.     /** Whether the application should always stay at full screen. */
  65.     public static final Key<Boolean> K_FULL_SCREEN_LOCKED = Key.of(K_BASE_NAME + ".fullScreenLocked", Boolean.class);

  66.     /** The minimum duration of the splash screen. */
  67.     public static final Key<Duration> K_MIN_SPLASH_DURATION = Key.of(K_BASE_NAME + ".minSplashDuration", Duration.class);

  68.     /** Whether invocations to presentation delegate methods should be logged at debug level. */
  69.     public static final Key<Boolean> K_LOG_DELEGATE_INVOCATIONS = Key.of(K_BASE_NAME + ".logDelegateInvocations", Boolean.class);

  70.     private static final String DEFAULT_APPLICATION_FXML = "Application.fxml";

  71.     private static final String DEFAULT_SPLASH_FXML = "Splash.fxml";

  72.     private static final Duration DEFAULT_MIN_SPLASH_DURATION = Duration.seconds(2);

  73.     // Don't use Slf4j and its static logger - give Main a chance to initialize things
  74.     private final Logger log = LoggerFactory.getLogger(JavaFXApplicationWithSplash.class);

  75.     private Splash splash;

  76.     private boolean maximized;

  77.     private boolean fullScreen;

  78.     private boolean fullScreenLocked;

  79.     @Getter @Setter
  80.     protected String applicationFxml = DEFAULT_APPLICATION_FXML;

  81.     @Getter @Setter
  82.     protected String splashFxml = DEFAULT_SPLASH_FXML;

  83.     /***********************************************************************************************************************************************************
  84.      * {@inheritDoc}
  85.      **********************************************************************************************************************************************************/
  86.     @Override
  87.     public void init()
  88.       {
  89.         log.info("init()");
  90.         splash = new Splash(this, splashFxml, this::createScene);
  91.         splash.init();
  92.       }

  93.     /***********************************************************************************************************************************************************
  94.      * {@inheritDoc}
  95.      **********************************************************************************************************************************************************/
  96.     @Override
  97.     public void start (@Nonnull final Stage stage)
  98.       {
  99.         log.info("start({})", stage);
  100.         final var preferencesHandler = PreferencesHandler.getInstance();
  101.         fullScreen = preferencesHandler.getProperty(K_FULL_SCREEN).orElse(false);
  102.         fullScreenLocked = preferencesHandler.getProperty(K_FULL_SCREEN_LOCKED).orElse(false);
  103.         maximized = preferencesHandler.getProperty(K_MAXIMIZED).orElse(false);

  104.         final var splashStage = new Stage(StageStyle.TRANSPARENT);
  105.         stage.setMaximized(maximized);
  106. //        splashStage.setMaximized(maximized); FIXME: doesn't work
  107.         configureFullScreen(stage);
  108. //        configureFullScreen(splashStage); FIXME: deadlocks JDK 1.8.0_40

  109.         if (!maximized && !fullScreen)
  110.           {
  111.             splashStage.centerOnScreen();
  112.           }

  113.         final var splashCreationTime = System.currentTimeMillis();
  114.         splash.show(splashStage);

  115.         getExecutor().execute(() -> // FIXME: use JavaFX Worker?
  116.           {
  117.             initializeInBackground();
  118.             Platform.runLater(() ->
  119.               {
  120.                 try
  121.                   {
  122.                     final var applicationNad = createParent();
  123.                     final var scene = createScene((Parent)applicationNad.getNode());
  124.                     stage.setOnCloseRequest(event -> onClosing());
  125.                     stage.setScene(scene);
  126.                     onStageCreated(stage, applicationNad);
  127.                     stage.setFullScreen(fullScreen);
  128.                     final double scale = preferencesHandler.getProperty(K_INITIAL_SIZE).orElse(0.65);
  129.                     final var screenSize = Screen.getPrimary().getBounds();
  130.                     stage.setWidth(scale * screenSize.getWidth());
  131.                     stage.setHeight(scale * screenSize.getHeight());
  132.                     stage.show();
  133.                     splashStage.toFront();

  134.                     final var duration = preferencesHandler.getProperty(K_MIN_SPLASH_DURATION).orElse(DEFAULT_MIN_SPLASH_DURATION);
  135.                     final var delay = Math.max(0, splashCreationTime + duration.toMillis() - System.currentTimeMillis());
  136.                     final var dismissSplash = new Timeline(new KeyFrame(Duration.millis(delay), e -> splash.dismiss()));
  137.                     Platform.runLater(dismissSplash::play);
  138.                   }
  139.                 catch (IOException e)
  140.                   {
  141.                     log.error("", e);
  142.                   }
  143.               });
  144.           });
  145.       }

  146.     /***********************************************************************************************************************************************************
  147.      *
  148.      **********************************************************************************************************************************************************/
  149.     protected void onStageCreated (@Nonnull final Stage stage, @Nonnull final NodeAndDelegate<?> applicationNad)
  150.       {
  151.       }

  152.     /***********************************************************************************************************************************************************
  153.      *
  154.      **********************************************************************************************************************************************************/
  155.     @Nonnull
  156.     protected abstract NodeAndDelegate<?> createParent()
  157.       throws IOException;

  158.     /***********************************************************************************************************************************************************
  159.      *
  160.      **********************************************************************************************************************************************************/
  161.     protected abstract void initializeInBackground();

  162.     /***********************************************************************************************************************************************************
  163.      * Invoked when the main {@link Stage} is being closed.
  164.      **********************************************************************************************************************************************************/
  165.     protected void onClosing()
  166.       {
  167.       }

  168.     /***********************************************************************************************************************************************************
  169.      *
  170.      **********************************************************************************************************************************************************/
  171.     @Nonnull
  172.     protected Executor getExecutor()
  173.       {
  174.         return Executors.newSingleThreadExecutor();
  175.       }

  176.     /***********************************************************************************************************************************************************
  177.      *
  178.      **********************************************************************************************************************************************************/
  179.     protected Scene createScene (@Nonnull final Parent parent)
  180.       {
  181.         final var scene = new Scene(parent);
  182.         final var jMetro = new JMetro(Style.DARK);
  183.         jMetro.setScene(scene);
  184.         return scene;
  185.       }

  186.     /***********************************************************************************************************************************************************
  187.      *
  188.      **********************************************************************************************************************************************************/
  189.     private void configureFullScreen (@Nonnull final Stage stage)
  190.       {
  191.         stage.setFullScreen(fullScreen);

  192.         if (fullScreen && fullScreenLocked)
  193.           {
  194.             stage.setFullScreenExitHint("");
  195.             stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
  196.           }
  197.       }
  198.   }