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.     private static final String DEFAULT_APPLICATION_FXML = "Application.fxml";

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

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

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

  73.     private Splash splash;

  74.     private boolean maximized;

  75.     private boolean fullScreen;

  76.     private boolean fullScreenLocked;

  77.     @Getter @Setter
  78.     protected String applicationFxml = DEFAULT_APPLICATION_FXML;

  79.     @Getter @Setter
  80.     protected String splashFxml = DEFAULT_SPLASH_FXML;

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

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

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

  107.         if (!maximized && !fullScreen)
  108.           {
  109.             splashStage.centerOnScreen();
  110.           }

  111.         final var splashCreationTime = System.currentTimeMillis();
  112.         splash.show(splashStage);

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

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

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

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

  156.     /***********************************************************************************************************************************************************
  157.      *
  158.      **********************************************************************************************************************************************************/
  159.     protected abstract void initializeInBackground();

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

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

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

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

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