1 package fr.ifremer.quadrige3.ui.swing;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import fr.ifremer.quadrige3.core.config.QuadrigeCoreConfiguration;
25 import org.apache.commons.collections4.CollectionUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import javax.swing.ImageIcon;
30 import javax.swing.Timer;
31 import java.awt.*;
32 import java.net.URL;
33 import java.util.List;
34
35
36
37
38 public class ApplicationSplashScreen {
39
40 private static final Log LOG = LogFactory.getLog(ApplicationSplashScreen.class);
41
42
43 private static SplashScreen splash;
44
45 private static Graphics2D graphics;
46
47 private static boolean coolDown;
48
49 private static final int coolDownTime = 3000;
50
51
52
53
54
55
56 public static void init(QuadrigeCoreConfiguration config) {
57
58
59 if (create()) {
60
61
62 render(
63 config.getSplashScreenLeftLogos(),
64 config.getSplashScreenRightLogos()
65 );
66
67
68 coolDown = true;
69 Timer t = new Timer(10, e -> {
70 coolDown = false;
71 ((Timer) e.getSource()).stop();
72 });
73
74 t.setInitialDelay(coolDownTime);
75 t.start();
76 }
77 }
78
79
80
81
82
83
84 public static void progress(int progress) {
85 if (splash != null && splash.isVisible() && graphics != null) {
86 render(progress);
87 }
88 }
89
90
91
92
93 public static void waitFor() {
94 do {
95 try {
96 Thread.sleep(100);
97 } catch (InterruptedException ignored) {
98 }
99 } while (coolDown);
100 }
101
102
103
104
105 public static void hide() {
106 if (splash != null && splash.isVisible())
107 splash.close();
108 }
109
110
111
112
113
114
115 private static boolean create() {
116 splash = SplashScreen.getSplashScreen();
117 if (splash == null) {
118 LOG.error("splash screen cannot be shown");
119 return false;
120 }
121 graphics = splash.createGraphics();
122 if (graphics == null) {
123 LOG.error("splash screen graphics is null");
124 return false;
125 }
126
127 return true;
128 }
129
130
131
132
133
134
135
136
137 private static Image getImage(String name) {
138 String resourceName = "/images/" + name;
139 URL logoUrl = ApplicationSplashScreen.class.getResource(resourceName);
140 if (logoUrl != null) {
141 return new ImageIcon(logoUrl).getImage();
142 }
143 logoUrl = Application.getInstance().getClass().getResource(resourceName);
144 if (logoUrl != null) {
145 return new ImageIcon(logoUrl).getImage();
146 }
147 LOG.warn(resourceName + " not found in resources");
148 return null;
149 }
150
151
152
153
154
155
156
157 private static void render(List<String> leftLogos, List<String> rightLogos) {
158
159 int y = splash.getBounds().height
160 - 12
161 - 32
162 - 2;
163
164 if (CollectionUtils.isNotEmpty(leftLogos)) {
165 int x = 4;
166
167 for (String logo : leftLogos) {
168 Image image = getImage(logo);
169 if (image != null) {
170 if (LOG.isDebugEnabled()) {
171 LOG.debug(String.format("render logo %s (width=%s height=%s) at x=%s y=%s", logo, image.getWidth(null), image.getHeight(null), x, y));
172 }
173 graphics.drawImage(image, x, y, null);
174 x += image.getWidth(null) + 4;
175 }
176 }
177 }
178
179 if (CollectionUtils.isNotEmpty(rightLogos)) {
180 int x = splash.getBounds().width;
181
182 for (String logo : rightLogos) {
183 Image image = getImage(logo);
184 if (image != null) {
185 if (LOG.isDebugEnabled()) {
186 LOG.debug(String.format("render logo %s (width=%s height=%s) at x=%s y=%s", logo, image.getWidth(null), image.getHeight(null), x, y));
187 }
188 x -= image.getWidth(null) + 4;
189 graphics.drawImage(image, x, y, null);
190 }
191 }
192 }
193
194 splash.update();
195 }
196
197
198
199
200
201
202 private static void render(int progress) {
203 int x = progress * splash.getBounds().width / 100;
204 int y = splash.getBounds().height - 1;
205
206
207 graphics.setColor(new Color(0, 110, 171));
208 graphics.setStroke(new BasicStroke(2));
209 graphics.drawLine(0, y, x, y);
210
211 splash.update();
212 }
213
214 }