Splash.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * SteelBlue
  6.  * http://steelblue.tidalwave.it - git clone git@bitbucket.org:tidalwave/steelblue-src.git
  7.  * %%
  8.  * Copyright (C) 2015 - 2015 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  *
  11.  * *********************************************************************************************************************
  12.  *
  13.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  14.  * the License. You may obtain a copy of the License at
  15.  *
  16.  *     http://www.apache.org/licenses/LICENSE-2.0
  17.  *
  18.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  19.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  20.  * specific language governing permissions and limitations under the License.
  21.  *
  22.  * *********************************************************************************************************************
  23.  *
  24.  *
  25.  *
  26.  * *********************************************************************************************************************
  27.  * #L%
  28.  */
  29. package it.tidalwave.ui.javafx;

  30. import javax.annotation.Nonnull;
  31. import java.io.IOException;
  32. import javafx.util.Duration;
  33. import javafx.animation.KeyFrame;
  34. import javafx.animation.KeyValue;
  35. import javafx.animation.Timeline;
  36. import javafx.fxml.FXMLLoader;
  37. import javafx.scene.Scene;
  38. import javafx.scene.layout.Pane;
  39. import javafx.stage.Stage;
  40. import lombok.RequiredArgsConstructor;
  41. import lombok.extern.slf4j.Slf4j;

  42. /***********************************************************************************************************************
  43.  *
  44.  * @author  Fabrizio Giudici
  45.  *
  46.  **********************************************************************************************************************/
  47. @RequiredArgsConstructor @Slf4j
  48. public class Splash
  49.   {
  50.     @Nonnull
  51.     private final Object application;

  52.     @Nonnull
  53.     private final String fxml;

  54.     private Pane splashPane;

  55.     private Stage splashStage;

  56.     /*******************************************************************************************************************
  57.      *
  58.      *
  59.      *
  60.      ******************************************************************************************************************/
  61.     public void init()
  62.       {
  63.         try
  64.           {
  65.             log.info("Loading Splash.fxml for application {}", application);
  66.             splashPane = FXMLLoader.load(application.getClass().getResource(fxml));
  67.           }
  68.         catch (IOException e)
  69.           {
  70.             log.warn("No Splash.fxml", e);
  71.           }
  72.       }

  73.     /*******************************************************************************************************************
  74.      *
  75.      *
  76.      *
  77.      ******************************************************************************************************************/
  78.     public void show (@Nonnull final Stage splashStage)
  79.       {
  80.         this.splashStage = splashStage;
  81.         final Scene splashScene = new Scene(splashPane);
  82.         splashStage.setScene(splashScene);
  83.         splashStage.show();
  84.       }

  85.     /*******************************************************************************************************************
  86.      *
  87.      *
  88.      *
  89.      ******************************************************************************************************************/
  90.     public void dismiss()
  91.       {
  92. //        loadProgress.progressProperty().unbind();
  93. //        loadProgress.setProgress(1);
  94. //        progressText.setText("Done.");

  95.         final KeyFrame start = new KeyFrame(Duration.ZERO, new KeyValue(splashStage.opacityProperty(), 1.0));
  96.         final KeyFrame end = new KeyFrame(Duration.millis(500), new KeyValue(splashStage.opacityProperty(), 0.0));
  97.         final Timeline slideAnimation = new Timeline(start, end);
  98.         slideAnimation.setOnFinished(event -> splashStage.close());
  99.         slideAnimation.play();

  100. //         FIXME: fade transition really doesn't work: it fades out the pane, but the stage is opaque.
  101. //        final FadeTransition fadeSplash = new FadeTransition(Duration.seconds(0.5), splashPane);
  102. //        fadeSplash.setFromValue(1.0);
  103. //        fadeSplash.setToValue(0.0);
  104. //        fadeSplash.setOnFinished(event -> splashStage.close());
  105. //        fadeSplash.play();
  106.       }
  107.   }