Skip to content

Method: init()

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.concurrent.Executor;
30: import java.util.concurrent.Executors;
31: import java.io.IOException;
32: import javafx.animation.KeyFrame;
33: import javafx.animation.Timeline;
34: import javafx.scene.Parent;
35: import javafx.scene.Scene;
36: import javafx.scene.input.KeyCombination;
37: import javafx.stage.Screen;
38: import javafx.stage.Stage;
39: import javafx.stage.StageStyle;
40: import javafx.util.Duration;
41: import javafx.application.Application;
42: import javafx.application.Platform;
43: import jfxtras.styles.jmetro.JMetro;
44: import jfxtras.styles.jmetro.Style;
45: import org.slf4j.Logger;
46: import org.slf4j.LoggerFactory;
47: import it.tidalwave.util.Key;
48: import it.tidalwave.util.PreferencesHandler;
49: import lombok.Getter;
50: import lombok.Setter;
51:
52: /***************************************************************************************************************************************************************
53: *
54: * @author Fabrizio Giudici
55: *
56: **************************************************************************************************************************************************************/
57: public abstract class JavaFXApplicationWithSplash extends Application
58: {
59: private static final String K_BASE_NAME = "it.tidalwave.javafx";
60:
61: /** A property representing the initial main window size as a percentual of the screen size. */
62: public static final Key<Double> K_INITIAL_SIZE = Key.of(K_BASE_NAME + ".initialSize", Double.class);
63:
64: /** Whether the application should start maximized. */
65: public static final Key<Boolean> K_MAXIMIZED = Key.of(K_BASE_NAME + ".maximized", Boolean.class);
66:
67: /** Whether the application should start at full screen. */
68: public static final Key<Boolean> K_FULL_SCREEN = Key.of(K_BASE_NAME + ".fullScreen", Boolean.class);
69:
70: /** Whether the application should always stay at full screen. */
71: public static final Key<Boolean> K_FULL_SCREEN_LOCKED = Key.of(K_BASE_NAME + ".fullScreenLocked", Boolean.class);
72:
73: /** The minimum duration of the splash screen. */
74: public static final Key<Duration> K_MIN_SPLASH_DURATION = Key.of(K_BASE_NAME + ".minSplashDuration", Duration.class);
75:
76: /** Whether invocations to presentation delegate methods should be logged at debug level. */
77: public static final Key<Boolean> K_LOG_DELEGATE_INVOCATIONS = Key.of(K_BASE_NAME + ".logDelegateInvocations", Boolean.class);
78:
79: private static final String DEFAULT_APPLICATION_FXML = "Application.fxml";
80:
81: private static final String DEFAULT_SPLASH_FXML = "Splash.fxml";
82:
83: private static final Duration DEFAULT_MIN_SPLASH_DURATION = Duration.seconds(2);
84:
85: // Don't use Slf4j and its static logger - give Main a chance to initialize things
86: private final Logger log = LoggerFactory.getLogger(JavaFXApplicationWithSplash.class);
87:
88: private Splash splash;
89:
90: private boolean maximized;
91:
92: private boolean fullScreen;
93:
94: private boolean fullScreenLocked;
95:
96: @Getter @Setter
97: protected String applicationFxml = DEFAULT_APPLICATION_FXML;
98:
99: @Getter @Setter
100: protected String splashFxml = DEFAULT_SPLASH_FXML;
101:
102: /***********************************************************************************************************************************************************
103: * {@inheritDoc}
104: **********************************************************************************************************************************************************/
105: @Override
106: public void init()
107: {
108: log.info("init()");
109: splash = new Splash(this, splashFxml, this::createScene);
110: splash.init();
111: }
112:
113: /***********************************************************************************************************************************************************
114: * {@inheritDoc}
115: **********************************************************************************************************************************************************/
116: @Override
117: public void start (@Nonnull final Stage stage)
118: {
119: log.info("start({})", stage);
120: final var preferencesHandler = PreferencesHandler.getInstance();
121: fullScreen = preferencesHandler.getProperty(K_FULL_SCREEN).orElse(false);
122: fullScreenLocked = preferencesHandler.getProperty(K_FULL_SCREEN_LOCKED).orElse(false);
123: maximized = preferencesHandler.getProperty(K_MAXIMIZED).orElse(false);
124:
125: final var splashStage = new Stage(StageStyle.TRANSPARENT);
126: stage.setMaximized(maximized);
127: // splashStage.setMaximized(maximized); FIXME: doesn't work
128: configureFullScreen(stage);
129: // configureFullScreen(splashStage); FIXME: deadlocks JDK 1.8.0_40
130:
131: if (!maximized && !fullScreen)
132: {
133: splashStage.centerOnScreen();
134: }
135:
136: final var splashCreationTime = System.currentTimeMillis();
137: splash.show(splashStage);
138:
139: getExecutor().execute(() -> // FIXME: use JavaFX Worker?
140: {
141: initializeInBackground();
142: Platform.runLater(() ->
143: {
144: try
145: {
146: final var applicationNad = createParent();
147: final var scene = createScene((Parent)applicationNad.getNode());
148: stage.setOnCloseRequest(event -> onClosing());
149: stage.setScene(scene);
150: onStageCreated(stage, applicationNad);
151: stage.setFullScreen(fullScreen);
152: final double scale = preferencesHandler.getProperty(K_INITIAL_SIZE).orElse(0.65);
153: final var screenSize = Screen.getPrimary().getBounds();
154: stage.setWidth(scale * screenSize.getWidth());
155: stage.setHeight(scale * screenSize.getHeight());
156: stage.show();
157: splashStage.toFront();
158:
159: final var duration = preferencesHandler.getProperty(K_MIN_SPLASH_DURATION).orElse(DEFAULT_MIN_SPLASH_DURATION);
160: final var delay = Math.max(0, splashCreationTime + duration.toMillis() - System.currentTimeMillis());
161: final var dismissSplash = new Timeline(new KeyFrame(Duration.millis(delay), e -> splash.dismiss()));
162: Platform.runLater(dismissSplash::play);
163: }
164: catch (IOException e)
165: {
166: log.error("", e);
167: }
168: });
169: });
170: }
171:
172: /***********************************************************************************************************************************************************
173: *
174: **********************************************************************************************************************************************************/
175: protected void onStageCreated (@Nonnull final Stage stage, @Nonnull final NodeAndDelegate<?> applicationNad)
176: {
177: }
178:
179: /***********************************************************************************************************************************************************
180: *
181: **********************************************************************************************************************************************************/
182: @Nonnull
183: protected abstract NodeAndDelegate<?> createParent()
184: throws IOException;
185:
186: /***********************************************************************************************************************************************************
187: *
188: **********************************************************************************************************************************************************/
189: protected abstract void initializeInBackground();
190:
191: /***********************************************************************************************************************************************************
192: * Invoked when the main {@link Stage} is being closed.
193: **********************************************************************************************************************************************************/
194: protected void onClosing()
195: {
196: }
197:
198: /***********************************************************************************************************************************************************
199: *
200: **********************************************************************************************************************************************************/
201: @Nonnull
202: protected Executor getExecutor()
203: {
204: return Executors.newSingleThreadExecutor();
205: }
206:
207: /***********************************************************************************************************************************************************
208: *
209: **********************************************************************************************************************************************************/
210: protected Scene createScene (@Nonnull final Parent parent)
211: {
212: final var scene = new Scene(parent);
213: final var jMetro = new JMetro(Style.DARK);
214: jMetro.setScene(scene);
215: return scene;
216: }
217:
218: /***********************************************************************************************************************************************************
219: *
220: **********************************************************************************************************************************************************/
221: private void configureFullScreen (@Nonnull final Stage stage)
222: {
223: stage.setFullScreen(fullScreen);
224:
225: if (fullScreen && fullScreenLocked)
226: {
227: stage.setFullScreenExitHint("");
228: stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
229: }
230: }
231: }