Content of file JavaFXApplicationWithSplash.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>JavaFXApplicationWithSplash.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">SteelBlue - Examples - Large</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-role-ui-javafx</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.ui.javafx</a> &gt; <span class="el_source">JavaFXApplicationWithSplash.java</span></div><h1>JavaFXApplicationWithSplash.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * SteelBlue: DCI User Interfaces
 * http://tidalwave.it/projects/steelblue
 *
 * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); 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 &quot;AS IS&quot; 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 javax.annotation.Nonnull;
import java.util.concurrent.Executors;
import java.util.concurrent.Executor;
import java.io.IOException;
import it.tidalwave.util.PreferencesHandler;
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyCombination;
import javafx.application.Application;
import javafx.application.Platform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.tidalwave.ui.javafx.JavaFXSafeProxyCreator.NodeAndDelegate;
import lombok.Getter;
import lombok.Setter;
import static it.tidalwave.util.PreferencesHandler.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L55">public abstract class JavaFXApplicationWithSplash extends Application</span>
  {
    private static final String DEFAULT_APPLICATION_FXML = &quot;Application.fxml&quot;;

    private static final String DEFAULT_SPLASH_FXML = &quot;Splash.fxml&quot;;

    // Don't use Slf4j and its static logger - give Main a chance to initialize things
<span class="nc" id="L62">    private final Logger log = LoggerFactory.getLogger(JavaFXApplicationWithSplash.class);</span>

    private Splash splash;

<span class="nc" id="L66">    @Getter @Setter</span>
    private boolean maximized;

<span class="nc" id="L69">    @Getter @Setter</span>
    private boolean fullScreen;

<span class="nc" id="L72">    @Getter @Setter</span>
    private boolean fullScreenLocked;

<span class="nc" id="L75">    @Getter @Setter</span>
    private boolean useAquaFxOnMacOsX;

<span class="nc" id="L78">    @Getter @Setter</span>
    protected String applicationFxml = DEFAULT_APPLICATION_FXML;

<span class="nc" id="L81">    @Getter @Setter</span>
    protected String splashFxml = DEFAULT_SPLASH_FXML;

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void init()
      {
<span class="nc" id="L92">        log.info(&quot;init()&quot;);</span>
<span class="nc" id="L93">        splash = new Splash(this, splashFxml);</span>
<span class="nc" id="L94">        splash.init();</span>
<span class="nc" id="L95">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void start (@Nonnull final Stage stage)
      {
<span class="nc" id="L105">        log.info(&quot;start({})&quot;, stage);</span>
<span class="nc" id="L106">        final Stage splashStage = new Stage(StageStyle.TRANSPARENT);</span>
<span class="nc" id="L107">        stage.setMaximized(maximized);</span>
//        splashStage.setMaximized(maximized); FIXME: doesn't work
<span class="nc" id="L109">        configureFullScreen(stage);</span>
//        configureFullScreen(splashStage); FIXME: deadlocks JDK 1.8.0_40

<span class="nc bnc" id="L112" title="All 4 branches missed.">        if (!maximized &amp;&amp; !fullScreen)</span>
          {
<span class="nc" id="L114">            splashStage.centerOnScreen();</span>
          }

<span class="nc" id="L117">        splash.show(splashStage);</span>

<span class="nc" id="L119">        getExecutor().execute(() -&gt; // FIXME: use JavaFX Worker?</span>
          {
<span class="nc" id="L121">            initializeInBackground();</span>
<span class="nc" id="L122">            Platform.runLater(() -&gt;</span>
              {
                try
                  {
<span class="nc" id="L126">                    final NodeAndDelegate applicationNad = createParent();</span>
<span class="nc" id="L127">                    final Scene scene = new Scene((Parent)applicationNad.getNode());</span>

<span class="nc bnc" id="L129" title="All 4 branches missed.">                    if (useAquaFxOnMacOsX &amp;&amp; isOSX())</span>
                      {
<span class="nc" id="L131">                        setMacOSXLookAndFeel(scene);</span>
                      }

<span class="nc" id="L134">                    stage.setOnCloseRequest(event -&gt; onClosing());</span>
<span class="nc" id="L135">                    stage.setScene(scene);</span>
<span class="nc" id="L136">                    onStageCreated(stage, applicationNad);</span>
<span class="nc" id="L137">                    final PreferencesHandler preferencesHandler = PreferencesHandler.getInstance();</span>

<span class="nc" id="L139">                    stage.setFullScreen(preferencesHandler.getProperty(KEY_FULL_SCREEN).orElse(false));</span>
<span class="nc" id="L140">                    final double scale = preferencesHandler.getProperty(KEY_INITIAL_SIZE).orElse(0.65);</span>
<span class="nc" id="L141">                    final Rectangle2D screenSize = Screen.getPrimary().getBounds();</span>
<span class="nc" id="L142">                    stage.setWidth(scale * screenSize.getWidth());</span>
<span class="nc" id="L143">                    stage.setHeight(scale * screenSize.getHeight());</span>
<span class="nc" id="L144">                    stage.show();</span>
<span class="nc" id="L145">                    splashStage.toFront();</span>
<span class="nc" id="L146">                    splash.dismiss();</span>
                  }
<span class="nc" id="L148">                catch (IOException e)</span>
                  {
<span class="nc" id="L150">                    log.error(&quot;&quot;, e);</span>
<span class="nc" id="L151">                  }</span>
<span class="nc" id="L152">              });</span>
<span class="nc" id="L153">          });</span>
<span class="nc" id="L154">      }</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    protected void onStageCreated (@Nonnull final Stage stage, @Nonnull final NodeAndDelegate applicationNad)
      {
<span class="nc" id="L163">      }</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected abstract NodeAndDelegate createParent()
      throws IOException;

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    protected abstract void initializeInBackground();

    /*******************************************************************************************************************
     *
     * Invoked when the main {@link Stage} is being closed.
     *
     ******************************************************************************************************************/
    protected void onClosing()
      {
<span class="nc" id="L188">      }</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected Executor getExecutor()
      {
<span class="nc" id="L198">        return Executors.newSingleThreadExecutor();</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    private void setMacOSXLookAndFeel (@Nonnull final Scene scene)
      {
<span class="nc" id="L208">      }</span>

    /*******************************************************************************************************************
     *
     * TODO: delegate to a provider
delegate to a provider
* ******************************************************************************************************************/ public static boolean isOSX() { <span class="nc" id="L217"> return System.getProperty(&quot;os.name&quot;).contains(&quot;OS X&quot;);</span> } /******************************************************************************************************************* * * ******************************************************************************************************************/ private void configureFullScreen (@Nonnull final Stage stage) { <span class="nc" id="L226"> stage.setFullScreen(fullScreen);</span> <span class="nc bnc" id="L228" title="All 4 branches missed."> if (fullScreen &amp;&amp; fullScreenLocked)</span> { <span class="nc" id="L230"> stage.setFullScreenExitHint(&quot;&quot;);</span> <span class="nc" id="L231"> stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);</span> } <span class="nc" id="L233"> }</span> } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>