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

Last change on this file since 2482 was 2358, checked in by Gubaer, 15 years ago

fixed #3305: Version is UNKNOWN
fixed #3429: created_by=* includes the wrong language when uploading from a new layer

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