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.Dimension;
|
---|
8 | import java.awt.Font;
|
---|
9 | import java.awt.GridBagConstraints;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.Insets;
|
---|
12 | import java.awt.Toolkit;
|
---|
13 | import java.awt.event.MouseAdapter;
|
---|
14 | import java.awt.event.MouseEvent;
|
---|
15 |
|
---|
16 | import javax.swing.ImageIcon;
|
---|
17 | import javax.swing.JLabel;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JSeparator;
|
---|
20 | import javax.swing.JWindow;
|
---|
21 | import javax.swing.SwingUtilities;
|
---|
22 | import javax.swing.border.Border;
|
---|
23 | import javax.swing.border.EmptyBorder;
|
---|
24 | import javax.swing.border.EtchedBorder;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.actions.AboutAction;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Show a splash screen so the user knows what is happening during startup.
|
---|
30 | *
|
---|
31 | * @author cbrill
|
---|
32 | */
|
---|
33 | public class SplashScreen extends JWindow {
|
---|
34 |
|
---|
35 | private JLabel status;
|
---|
36 |
|
---|
37 | private Runnable closerRunner;
|
---|
38 |
|
---|
39 | public SplashScreen() {
|
---|
40 | super();
|
---|
41 |
|
---|
42 | // Add a nice border to the main splash screen
|
---|
43 | JPanel contentPane = (JPanel)this.getContentPane();
|
---|
44 | Border margin = new EtchedBorder(1, Color.white, Color.gray);
|
---|
45 | contentPane.setBorder(margin);
|
---|
46 |
|
---|
47 | // Add a margin from the border to the content
|
---|
48 | JPanel innerContentPane = new JPanel();
|
---|
49 | innerContentPane.setBorder(new EmptyBorder(10, 10, 2, 10));
|
---|
50 | contentPane.add(innerContentPane);
|
---|
51 | innerContentPane.setLayout(new GridBagLayout());
|
---|
52 |
|
---|
53 | // Add the logo
|
---|
54 | JLabel logo = new JLabel(new ImageIcon("images/logo.png"));
|
---|
55 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
56 | gbc.gridheight = 2;
|
---|
57 | innerContentPane.add(logo, gbc);
|
---|
58 |
|
---|
59 | // Add the name of this application
|
---|
60 | JLabel caption = new JLabel(tr("JOSM - Java OpenStreetMap Editor"));
|
---|
61 | caption.setFont(new Font("Helvetica", Font.BOLD, 20));
|
---|
62 | gbc.gridheight = 1;
|
---|
63 | gbc.gridx = 1;
|
---|
64 | gbc.insets = new Insets(30, 0, 0, 0);
|
---|
65 | innerContentPane.add(caption, gbc);
|
---|
66 |
|
---|
67 | // Add the version number
|
---|
68 | JLabel version = new JLabel(tr("Version {0}", AboutAction.version));
|
---|
69 | gbc.gridy = 1;
|
---|
70 | gbc.insets = new Insets(0, 0, 0, 0);
|
---|
71 | innerContentPane.add(version, gbc);
|
---|
72 |
|
---|
73 | // Add a separator to the status text
|
---|
74 | JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);
|
---|
75 | gbc.gridx = 0;
|
---|
76 | gbc.gridy = 2;
|
---|
77 | gbc.gridwidth = 2;
|
---|
78 | gbc.fill = GridBagConstraints.HORIZONTAL;
|
---|
79 | gbc.insets = new Insets(15, 0, 5, 0);
|
---|
80 | innerContentPane.add(separator, gbc);
|
---|
81 |
|
---|
82 | // Add a status message
|
---|
83 | status = new JLabel();
|
---|
84 | gbc.gridy = 3;
|
---|
85 | gbc.insets = new Insets(0, 0, 0, 0);
|
---|
86 | innerContentPane.add(status, gbc);
|
---|
87 | setStatus(tr("Initializing"));
|
---|
88 |
|
---|
89 | pack();
|
---|
90 |
|
---|
91 | // Center the splash screen
|
---|
92 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
93 | Dimension labelSize = contentPane.getPreferredSize();
|
---|
94 | setLocation(screenSize.width / 2 - (labelSize.width / 2),
|
---|
95 | screenSize.height / 2 - (labelSize.height / 2));
|
---|
96 |
|
---|
97 | // Method to close the splash screen when being clicked or when closeSplash is called
|
---|
98 | closerRunner = new Runnable() {
|
---|
99 | public void run() {
|
---|
100 | setVisible(false);
|
---|
101 | dispose();
|
---|
102 | }
|
---|
103 | };
|
---|
104 |
|
---|
105 | // Add ability to hide splash screen by clicking it
|
---|
106 | addMouseListener(new MouseAdapter() {
|
---|
107 | public void mousePressed(MouseEvent event) {
|
---|
108 | try {
|
---|
109 | closerRunner.run();
|
---|
110 | } catch (Exception e) {
|
---|
111 | e.printStackTrace();
|
---|
112 | // can catch InvocationTargetException
|
---|
113 | // can catch InterruptedException
|
---|
114 | }
|
---|
115 | }
|
---|
116 | });
|
---|
117 | setVisible(true);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * This method sets the status message. It should be called prior to
|
---|
122 | * actually doing the action.
|
---|
123 | *
|
---|
124 | * @param message
|
---|
125 | * the message to be displayed
|
---|
126 | */
|
---|
127 | public void setStatus(String message) {
|
---|
128 | status.setText(message + " ...");
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Closes the splashscreen. Call once you are done starting.
|
---|
133 | */
|
---|
134 | public void closeSplash() {
|
---|
135 | try {
|
---|
136 | SwingUtilities.invokeAndWait(closerRunner);
|
---|
137 | } catch (Exception e) {
|
---|
138 | e.printStackTrace();
|
---|
139 | // can catch InvocationTargetException
|
---|
140 | // can catch InterruptedException
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|