source: josm/trunk/src/org/openstreetmap/josm/gui/SplashScreen.java@ 8525

Last change on this file since 8525 was 8525, checked in by simon04, 9 years ago

see #11591, see #11355 - Splash screen: attempt to fix lock issue (2)

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