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

Last change on this file since 2284 was 1212, checked in by stoecker, 15 years ago

language fixes, hopefully the last set

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