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
28 import jakarta.annotation.Nonnull;
29 import java.util.function.Function;
30 import java.io.IOException;
31 import javafx.animation.KeyFrame;
32 import javafx.animation.KeyValue;
33 import javafx.animation.Timeline;
34 import javafx.fxml.FXMLLoader;
35 import javafx.scene.Parent;
36 import javafx.scene.Scene;
37 import javafx.scene.layout.Pane;
38 import javafx.stage.Stage;
39 import javafx.util.Duration;
40 import lombok.RequiredArgsConstructor;
41 import lombok.extern.slf4j.Slf4j;
42
43 /***************************************************************************************************************************************************************
44 *
45 * @author Fabrizio Giudici
46 *
47 **************************************************************************************************************************************************************/
48 @RequiredArgsConstructor @Slf4j
49 public class Splash
50 {
51 @Nonnull
52 private final Object application;
53
54 @Nonnull
55 private final String fxml;
56
57 @Nonnull
58 private final Function<Parent, Scene> sceneFactory;
59
60 private Pane splashPane;
61
62 private Stage splashStage;
63
64 /***********************************************************************************************************************************************************
65 *
66 **********************************************************************************************************************************************************/
67 public void init()
68 {
69 try
70 {
71 log.info("Loading Splash.fxml for application {}", application);
72 splashPane = FXMLLoader.load(application.getClass().getResource(fxml));
73 }
74 catch (IOException e)
75 {
76 log.warn("No Splash.fxml", e);
77 }
78 }
79
80 /***********************************************************************************************************************************************************
81 *
82 **********************************************************************************************************************************************************/
83 public void show (@Nonnull final Stage splashStage)
84 {
85 this.splashStage = splashStage;
86 final var splashScene = sceneFactory.apply(splashPane);
87 splashStage.setScene(splashScene);
88 splashStage.show();
89 }
90
91 /***********************************************************************************************************************************************************
92 *
93 **********************************************************************************************************************************************************/
94 public void dismiss()
95 {
96 // loadProgress.progressProperty().unbind();
97 // loadProgress.setProgress(1);
98 // progressText.setText("Done.");
99
100 final var start = new KeyFrame(Duration.ZERO, new KeyValue(splashStage.opacityProperty(), 1.0));
101 final var end = new KeyFrame(Duration.millis(500), new KeyValue(splashStage.opacityProperty(), 0.0));
102 final var slideAnimation = new Timeline(start, end);
103 slideAnimation.setOnFinished(event -> splashStage.close());
104 slideAnimation.play();
105
106 // FIXME: fade transition really doesn't work: it fades out the pane, but the stage is opaque.
107 // final FadeTransition fadeSplash = new FadeTransition(Duration.seconds(0.5), splashPane);
108 // fadeSplash.setFromValue(1.0);
109 // fadeSplash.setToValue(0.0);
110 // fadeSplash.setOnFinished(event -> splashStage.close());
111 // fadeSplash.play();
112 }
113 }