1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Graphics;
|
---|
9 | import java.awt.GridBagConstraints;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.Image;
|
---|
12 | import java.awt.Insets;
|
---|
13 | import java.awt.event.MouseAdapter;
|
---|
14 | import java.awt.event.MouseEvent;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.Collections;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Objects;
|
---|
19 |
|
---|
20 | import javax.swing.ImageIcon;
|
---|
21 | import javax.swing.JFrame;
|
---|
22 | import javax.swing.JLabel;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 | import javax.swing.JProgressBar;
|
---|
25 | import javax.swing.JSeparator;
|
---|
26 | import javax.swing.border.Border;
|
---|
27 | import javax.swing.border.EmptyBorder;
|
---|
28 | import javax.swing.border.EtchedBorder;
|
---|
29 | import javax.swing.event.ChangeEvent;
|
---|
30 | import javax.swing.event.ChangeListener;
|
---|
31 |
|
---|
32 | import org.openstreetmap.josm.Main;
|
---|
33 | import org.openstreetmap.josm.data.Version;
|
---|
34 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
35 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
---|
36 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
37 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
38 | import org.openstreetmap.josm.tools.Predicates;
|
---|
39 | import org.openstreetmap.josm.tools.Utils;
|
---|
40 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Show a splash screen so the user knows what is happening during startup.
|
---|
44 | * @since 976
|
---|
45 | */
|
---|
46 | public class SplashScreen extends JFrame implements ChangeListener {
|
---|
47 |
|
---|
48 | private final SplashProgressMonitor progressMonitor;
|
---|
49 | private final SplashScreenProgressRenderer progressRenderer;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Constructs a new {@code SplashScreen}.
|
---|
53 | */
|
---|
54 | public SplashScreen() {
|
---|
55 | setUndecorated(true);
|
---|
56 |
|
---|
57 | // Add a nice border to the main splash screen
|
---|
58 | JPanel contentPane = (JPanel) this.getContentPane();
|
---|
59 | Border margin = new EtchedBorder(1, Color.white, Color.gray);
|
---|
60 | contentPane.setBorder(margin);
|
---|
61 |
|
---|
62 | // Add a margin from the border to the content
|
---|
63 | JPanel innerContentPane = new JPanel();
|
---|
64 | innerContentPane.setBorder(new EmptyBorder(10, 10, 2, 10));
|
---|
65 | contentPane.add(innerContentPane);
|
---|
66 | innerContentPane.setLayout(new GridBagLayout());
|
---|
67 |
|
---|
68 | // Add the logo
|
---|
69 | JLabel logo = new JLabel(new ImageIcon(ImageProvider.get("logo.svg").getImage().getScaledInstance(128, 129, Image.SCALE_SMOOTH)));
|
---|
70 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
71 | gbc.gridheight = 2;
|
---|
72 | gbc.insets = new Insets(0, 0, 0, 70);
|
---|
73 | innerContentPane.add(logo, gbc);
|
---|
74 |
|
---|
75 | // Add the name of this application
|
---|
76 | JLabel caption = new JLabel("JOSM – " + tr("Java OpenStreetMap Editor"));
|
---|
77 | caption.setFont(GuiHelper.getTitleFont());
|
---|
78 | gbc.gridheight = 1;
|
---|
79 | gbc.gridx = 1;
|
---|
80 | gbc.insets = new Insets(30, 0, 0, 0);
|
---|
81 | innerContentPane.add(caption, gbc);
|
---|
82 |
|
---|
83 | // Add the version number
|
---|
84 | JLabel version = new JLabel(tr("Version {0}", Version.getInstance().getVersionString()));
|
---|
85 | gbc.gridy = 1;
|
---|
86 | gbc.insets = new Insets(0, 0, 0, 0);
|
---|
87 | innerContentPane.add(version, gbc);
|
---|
88 |
|
---|
89 | // Add a separator to the status text
|
---|
90 | JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);
|
---|
91 | gbc.gridx = 0;
|
---|
92 | gbc.gridy = 2;
|
---|
93 | gbc.gridwidth = 2;
|
---|
94 | gbc.fill = GridBagConstraints.HORIZONTAL;
|
---|
95 | gbc.insets = new Insets(15, 0, 5, 0);
|
---|
96 | innerContentPane.add(separator, gbc);
|
---|
97 |
|
---|
98 | // Add a status message
|
---|
99 | progressRenderer = new SplashScreenProgressRenderer();
|
---|
100 | gbc.gridy = 3;
|
---|
101 | gbc.insets = new Insets(0, 0, 10, 0);
|
---|
102 | innerContentPane.add(progressRenderer, gbc);
|
---|
103 | progressMonitor = new SplashProgressMonitor(null, this);
|
---|
104 |
|
---|
105 | pack();
|
---|
106 |
|
---|
107 | WindowGeometry.centerOnScreen(this.getSize(), "gui.geometry").applySafe(this);
|
---|
108 |
|
---|
109 | // Add ability to hide splash screen by clicking it
|
---|
110 | addMouseListener(new MouseAdapter() {
|
---|
111 | @Override
|
---|
112 | public void mousePressed(MouseEvent event) {
|
---|
113 | setVisible(false);
|
---|
114 | }
|
---|
115 | });
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public void stateChanged(ChangeEvent ignore) {
|
---|
120 | progressRenderer.setTasks(progressMonitor.toString());
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * A task (of a {@link ProgressMonitor}).
|
---|
125 | */
|
---|
126 | private abstract static class Task {
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Returns a HTML representation for this task.
|
---|
130 | */
|
---|
131 | public abstract String toHtml();
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public final String toString() {
|
---|
135 | return toHtml();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * A single task (of a {@link ProgressMonitor}) which keeps track of its execution duration
|
---|
141 | * (requires a call to {@link #finish()}).
|
---|
142 | */
|
---|
143 | private static class MeasurableTask extends Task {
|
---|
144 | private final String name;
|
---|
145 | private final long start;
|
---|
146 | private String duration = "";
|
---|
147 |
|
---|
148 | public MeasurableTask(String name) {
|
---|
149 | this.name = name;
|
---|
150 | this.start = System.currentTimeMillis();
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void finish() {
|
---|
154 | if (!"".equals(duration)) {
|
---|
155 | throw new IllegalStateException("This tasks has already been finished");
|
---|
156 | }
|
---|
157 | duration = tr(" ({0})", Utils.getDurationString(System.currentTimeMillis() - start));
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | public String toHtml() {
|
---|
162 | return name + "<i style='color: #666666;'>" + duration + "</i>";
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public boolean equals(Object o) {
|
---|
167 | if (this == o) return true;
|
---|
168 | if (o == null || getClass() != o.getClass()) return false;
|
---|
169 | MeasurableTask that = (MeasurableTask) o;
|
---|
170 | return Objects.equals(name, that.name);
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | public int hashCode() {
|
---|
175 | return Objects.hashCode(name);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * A {@link ProgressMonitor} which stores the (sub)tasks in a tree.
|
---|
181 | */
|
---|
182 | public static class SplashProgressMonitor extends Task implements ProgressMonitor {
|
---|
183 |
|
---|
184 | private final String name;
|
---|
185 | private final ChangeListener listener;
|
---|
186 | private final List<Task> tasks = Collections.synchronizedList(new ArrayList<Task>());
|
---|
187 | private SplashProgressMonitor latestSubtask;
|
---|
188 |
|
---|
189 | public SplashProgressMonitor(String name, ChangeListener listener) {
|
---|
190 | this.name = name;
|
---|
191 | this.listener = listener;
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Override
|
---|
195 | public String toHtml() {
|
---|
196 | synchronized (tasks) {
|
---|
197 | return Utils.firstNonNull(name, "") + (tasks.isEmpty() ? "" : Utils.joinAsHtmlUnorderedList(tasks));
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | @Override
|
---|
202 | public void beginTask(String title) {
|
---|
203 | if (title != null) {
|
---|
204 | final MeasurableTask task = new MeasurableTask(title);
|
---|
205 | tasks.add(task);
|
---|
206 | listener.stateChanged(null);
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | @Override
|
---|
211 | public void beginTask(String title, int ticks) {
|
---|
212 | this.beginTask(title);
|
---|
213 | }
|
---|
214 |
|
---|
215 | @Override
|
---|
216 | public void setCustomText(String text) {
|
---|
217 | this.beginTask(text);
|
---|
218 | }
|
---|
219 |
|
---|
220 | @Override
|
---|
221 | public void setExtraText(String text) {
|
---|
222 | this.beginTask(text);
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public void indeterminateSubTask(String title) {
|
---|
227 | this.subTask(title);
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Override
|
---|
231 | public void subTask(String title) {
|
---|
232 | latestSubtask = new SplashProgressMonitor(title, listener);
|
---|
233 | tasks.add(latestSubtask);
|
---|
234 | listener.stateChanged(null);
|
---|
235 | }
|
---|
236 |
|
---|
237 | @Override
|
---|
238 | public ProgressMonitor createSubTaskMonitor(int ticks, boolean internal) {
|
---|
239 | return latestSubtask;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * @deprecated Use {@link #finishTask(String)} instead.
|
---|
244 | */
|
---|
245 | @Override
|
---|
246 | @Deprecated
|
---|
247 | public void finishTask() {
|
---|
248 | }
|
---|
249 |
|
---|
250 | public void finishTask(String title) {
|
---|
251 | final Task task = Utils.find(tasks, Predicates.<Task>equalTo(new MeasurableTask(title)));
|
---|
252 | if (task != null && task instanceof MeasurableTask) {
|
---|
253 | ((MeasurableTask) task).finish();
|
---|
254 | Main.debug(tr("{0} completed in {1}", title, ((MeasurableTask) task).duration));
|
---|
255 | listener.stateChanged(null);
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | @Override
|
---|
260 | public void invalidate() {
|
---|
261 | }
|
---|
262 |
|
---|
263 | @Override
|
---|
264 | public void setTicksCount(int ticks) {
|
---|
265 | }
|
---|
266 |
|
---|
267 | @Override
|
---|
268 | public int getTicksCount() {
|
---|
269 | return 0;
|
---|
270 | }
|
---|
271 |
|
---|
272 | @Override
|
---|
273 | public void setTicks(int ticks) {
|
---|
274 | }
|
---|
275 |
|
---|
276 | @Override
|
---|
277 | public int getTicks() {
|
---|
278 | return 0;
|
---|
279 | }
|
---|
280 |
|
---|
281 | @Override
|
---|
282 | public void worked(int ticks) {
|
---|
283 | }
|
---|
284 |
|
---|
285 | @Override
|
---|
286 | public boolean isCanceled() {
|
---|
287 | return false;
|
---|
288 | }
|
---|
289 |
|
---|
290 | @Override
|
---|
291 | public void cancel() {
|
---|
292 | }
|
---|
293 |
|
---|
294 | @Override
|
---|
295 | public void addCancelListener(CancelListener listener) {
|
---|
296 | }
|
---|
297 |
|
---|
298 | @Override
|
---|
299 | public void removeCancelListener(CancelListener listener) {
|
---|
300 | }
|
---|
301 |
|
---|
302 | @Override
|
---|
303 | public void appendLogMessage(String message) {
|
---|
304 | }
|
---|
305 |
|
---|
306 | @Override
|
---|
307 | public void setProgressTaskId(ProgressTaskId taskId) {
|
---|
308 | }
|
---|
309 |
|
---|
310 | @Override
|
---|
311 | public ProgressTaskId getProgressTaskId() {
|
---|
312 | return null;
|
---|
313 | }
|
---|
314 |
|
---|
315 | @Override
|
---|
316 | public Component getWindowParent() {
|
---|
317 | return Main.parent;
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Returns the progress monitor.
|
---|
323 | * @return The progress monitor
|
---|
324 | */
|
---|
325 | public SplashProgressMonitor getProgressMonitor() {
|
---|
326 | return progressMonitor;
|
---|
327 | }
|
---|
328 |
|
---|
329 | private static class SplashScreenProgressRenderer extends JPanel {
|
---|
330 | private JLabel lblTaskTitle;
|
---|
331 | private JProgressBar progressBar;
|
---|
332 | private static final String labelHtml = "<html>"
|
---|
333 | + "<style>ul {margin-top: 0; margin-bottom: 0; padding: 0;} li {margin: 0; padding: 0;}</style>"
|
---|
334 | + "<body height='320'>";
|
---|
335 |
|
---|
336 | protected void build() {
|
---|
337 | setLayout(new GridBagLayout());
|
---|
338 | GridBagConstraints gc = new GridBagConstraints();
|
---|
339 | gc.gridx = 0;
|
---|
340 | gc.gridy = 0;
|
---|
341 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
342 | gc.weightx = 1.0;
|
---|
343 | gc.weighty = 0.0;
|
---|
344 | gc.insets = new Insets(5, 0, 0, 0);
|
---|
345 | add(lblTaskTitle = new JLabel(labelHtml), gc);
|
---|
346 |
|
---|
347 | gc.gridy = 1;
|
---|
348 | gc.insets = new Insets(15, 0, 0, 0);
|
---|
349 | add(progressBar = new JProgressBar(JProgressBar.HORIZONTAL), gc);
|
---|
350 | progressBar.setIndeterminate(true);
|
---|
351 | }
|
---|
352 |
|
---|
353 | public SplashScreenProgressRenderer() {
|
---|
354 | build();
|
---|
355 | }
|
---|
356 |
|
---|
357 | @Override
|
---|
358 | public void paint(Graphics g) {
|
---|
359 | try {
|
---|
360 | super.paint(g);
|
---|
361 | } catch (NullPointerException ignore) {
|
---|
362 | // NullPointerException at javax.swing.text.html.StyleSheet$ListPainter.paint
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | public void setTasks(String tasks) {
|
---|
367 | lblTaskTitle.setText(labelHtml + tasks);
|
---|
368 | repaint();
|
---|
369 | }
|
---|
370 | }
|
---|
371 | }
|
---|