/
SplashScreenBuilder.java
120 lines (100 loc) · 3.5 KB
/
SplashScreenBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.splash;
import java.awt.EventQueue;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import javax.swing.SwingUtilities;
import org.terasology.splash.overlay.Overlay;
public final class SplashScreenBuilder {
private ConfigurableSplashScreen splashScreen;
private final URL resourceUrl;
private final int windowWidth;
private final int windowHeight;
private Runnable createInstanceTask = new Runnable() {
@Override
public void run() {
if (JvmSplashScreen.isAvailable()) {
splashScreen = new JvmSplashScreen();
return;
}
if (resourceUrl != null) {
try {
splashScreen = new SwingSplashScreen(resourceUrl);
} catch (IOException e) {
// eat silently - consider re-throwing or storing in a member field to throw later
}
} else if (windowWidth > 0 && windowHeight > 0) {
splashScreen = new SwingSplashScreen(windowWidth, windowHeight);
}
}
};
public SplashScreenBuilder() {
this(SwingSplashScreen.getDefaultSplashImageUrl(), 0, 0);
}
public SplashScreenBuilder(int width, int height) {
this(null, width, height);
}
public SplashScreenBuilder(URL imageUrl) {
this(imageUrl, 0, 0);
}
private SplashScreenBuilder(URL imageUrl, int width, int height) {
this.resourceUrl = imageUrl;
this.windowWidth = width;
this.windowHeight = height;
if (!java.awt.GraphicsEnvironment.isHeadless()) {
if (!EventQueue.isDispatchThread()) {
try {
SwingUtilities.invokeAndWait(createInstanceTask);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
createInstanceTask.run();
}
}
if (splashScreen == null) {
splashScreen = new SplashScreenStub();
}
}
/**
* @return a new stub instance that does nothing
*/
public static SplashScreen createStub() {
return new SplashScreenStub();
}
/**
* Adds an overlay. <code>null</code> values are silently ignored.
* @param overlay the overlay to add
* @return this
*/
public SplashScreenBuilder add(Overlay overlay) {
if (overlay != null) {
splashScreen.addOverlay(overlay);
}
return this;
}
/**
* Sets the maximum length of the message queue. As soon as the length is reached,
* the oldest messaged are dropped without being displayed. The default length is three.
* @param maxQueueLength the new maximum length
*/
public SplashScreenBuilder setMaxQueueLength(int maxQueueLength) {
splashScreen.setMaxQueueLength(maxQueueLength);
return this;
}
/**
* Set the minimum time a message is visible. The default length is 100 milliseconds.
* @param minVisTime the time in milli-seconds
*/
public SplashScreenBuilder setMinVisTime(double minVisTime) {
splashScreen.setMinVisTime(minVisTime);
return this;
}
public SplashScreen build() {
return splashScreen;
}
}