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.event.MouseAdapter;
|
---|
13 | import java.awt.event.MouseEvent;
|
---|
14 |
|
---|
15 | import java.util.Arrays;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import javax.swing.JFrame;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JPanel;
|
---|
20 | import javax.swing.JProgressBar;
|
---|
21 | import javax.swing.JSeparator;
|
---|
22 | import javax.swing.border.Border;
|
---|
23 | import javax.swing.border.EmptyBorder;
|
---|
24 | import javax.swing.border.EtchedBorder;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.data.Version;
|
---|
27 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
28 | import org.openstreetmap.josm.gui.progress.ProgressRenderer;
|
---|
29 | import org.openstreetmap.josm.gui.progress.SwingRenderingProgressMonitor;
|
---|
30 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
31 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
32 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Show a splash screen so the user knows what is happening during startup.
|
---|
36 | *
|
---|
37 | */
|
---|
38 | public class SplashScreen extends JFrame {
|
---|
39 |
|
---|
40 | private SplashScreenProgressRenderer progressRenderer;
|
---|
41 | private SwingRenderingProgressMonitor progressMonitor;
|
---|
42 |
|
---|
43 | public SplashScreen() {
|
---|
44 | super();
|
---|
45 | setUndecorated(true);
|
---|
46 |
|
---|
47 | // Add a nice border to the main splash screen
|
---|
48 | JPanel contentPane = (JPanel)this.getContentPane();
|
---|
49 | Border margin = new EtchedBorder(1, Color.white, Color.gray);
|
---|
50 | contentPane.setBorder(margin);
|
---|
51 |
|
---|
52 | // Add a margin from the border to the content
|
---|
53 | JPanel innerContentPane = new JPanel();
|
---|
54 | innerContentPane.setBorder(new EmptyBorder(10, 10, 2, 10));
|
---|
55 | contentPane.add(innerContentPane);
|
---|
56 | innerContentPane.setLayout(new GridBagLayout());
|
---|
57 |
|
---|
58 | // Add the logo
|
---|
59 | JLabel logo = new JLabel(ImageProvider.get("logo.png"));
|
---|
60 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
61 | gbc.gridheight = 2;
|
---|
62 | gbc.insets = new Insets(0, 0, 0, 70);
|
---|
63 | innerContentPane.add(logo, gbc);
|
---|
64 |
|
---|
65 | // Add the name of this application
|
---|
66 | JLabel caption = new JLabel("JOSM - " + tr("Java OpenStreetMap Editor"));
|
---|
67 | caption.setFont(GuiHelper.getTitleFont());
|
---|
68 | gbc.gridheight = 1;
|
---|
69 | gbc.gridx = 1;
|
---|
70 | gbc.insets = new Insets(30, 0, 0, 0);
|
---|
71 | innerContentPane.add(caption, gbc);
|
---|
72 |
|
---|
73 | // Add the version number
|
---|
74 | JLabel version = new JLabel(tr("Version {0}", Version.getInstance().getVersionString()));
|
---|
75 | gbc.gridy = 1;
|
---|
76 | gbc.insets = new Insets(0, 0, 0, 0);
|
---|
77 | innerContentPane.add(version, gbc);
|
---|
78 |
|
---|
79 | // Add a separator to the status text
|
---|
80 | JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);
|
---|
81 | gbc.gridx = 0;
|
---|
82 | gbc.gridy = 2;
|
---|
83 | gbc.gridwidth = 2;
|
---|
84 | gbc.fill = GridBagConstraints.HORIZONTAL;
|
---|
85 | gbc.insets = new Insets(15, 0, 5, 0);
|
---|
86 | innerContentPane.add(separator, gbc);
|
---|
87 |
|
---|
88 | // Add a status message
|
---|
89 | progressRenderer = new SplashScreenProgressRenderer();
|
---|
90 | gbc.gridy = 3;
|
---|
91 | gbc.insets = new Insets(0, 0, 10, 0);
|
---|
92 | innerContentPane.add(progressRenderer, gbc);
|
---|
93 | progressMonitor = new SwingRenderingProgressMonitor(progressRenderer);
|
---|
94 |
|
---|
95 | pack();
|
---|
96 |
|
---|
97 | WindowGeometry.centerOnScreen(this.getSize(), "gui.geometry").applySafe(this);
|
---|
98 |
|
---|
99 | // Add ability to hide splash screen by clicking it
|
---|
100 | addMouseListener(new MouseAdapter() {
|
---|
101 | @Override
|
---|
102 | public void mousePressed(MouseEvent event) {
|
---|
103 | setVisible(false);
|
---|
104 | }
|
---|
105 | });
|
---|
106 | }
|
---|
107 |
|
---|
108 | public ProgressMonitor getProgressMonitor() {
|
---|
109 | return progressMonitor;
|
---|
110 | }
|
---|
111 |
|
---|
112 | static private class SplashScreenProgressRenderer extends JPanel implements ProgressRenderer {
|
---|
113 | private JLabel lblTaskTitle;
|
---|
114 | private JLabel lblCustomText;
|
---|
115 | private JProgressBar progressBar;
|
---|
116 |
|
---|
117 | protected void build() {
|
---|
118 | setLayout(new GridBagLayout());
|
---|
119 | GridBagConstraints gc = new GridBagConstraints();
|
---|
120 | gc.gridx = 0;
|
---|
121 | gc.gridy = 0;
|
---|
122 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
123 | gc.weightx = 1.0;
|
---|
124 | gc.weighty = 0.0;
|
---|
125 | gc.insets = new Insets(5,0,0,0);
|
---|
126 | add(lblTaskTitle = new JLabel(" "), gc);
|
---|
127 |
|
---|
128 | gc.gridx = 0;
|
---|
129 | gc.gridy = 1;
|
---|
130 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
131 | gc.weightx = 1.0;
|
---|
132 | gc.weighty = 0.0;
|
---|
133 | gc.insets = new Insets(5,0,0,0);
|
---|
134 | add(lblCustomText = new JLabel(" ") {
|
---|
135 | @Override
|
---|
136 | public Dimension getPreferredSize() {
|
---|
137 | Dimension d = super.getPreferredSize();
|
---|
138 | if(d.width < 600) d.width = 600;
|
---|
139 | d.height *= MAX_NUMBER_OF_MESSAGES;
|
---|
140 | return d;
|
---|
141 | }
|
---|
142 | }, gc);
|
---|
143 |
|
---|
144 | gc.gridx = 0;
|
---|
145 | gc.gridy = 2;
|
---|
146 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
147 | gc.weightx = 1.0;
|
---|
148 | gc.weighty = 0.0;
|
---|
149 | gc.insets = new Insets(5,0,0,0);
|
---|
150 | add(progressBar = new JProgressBar(JProgressBar.HORIZONTAL), gc);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public SplashScreenProgressRenderer() {
|
---|
154 | build();
|
---|
155 | }
|
---|
156 |
|
---|
157 | public void setCustomText(String message) {
|
---|
158 | if(message.isEmpty())
|
---|
159 | message = " "; /* prevent killing of additional line */
|
---|
160 | lblCustomText.setText(message);
|
---|
161 | repaint();
|
---|
162 | }
|
---|
163 |
|
---|
164 | public void setIndeterminate(boolean indeterminate) {
|
---|
165 | progressBar.setIndeterminate(indeterminate);
|
---|
166 | repaint();
|
---|
167 | }
|
---|
168 |
|
---|
169 | public void setMaximum(int maximum) {
|
---|
170 | progressBar.setMaximum(maximum);
|
---|
171 | repaint();
|
---|
172 | }
|
---|
173 |
|
---|
174 | private static final int MAX_NUMBER_OF_MESSAGES = 3;
|
---|
175 | private LinkedList<String> messages = new LinkedList<String>(Arrays.asList("", "", "")); //update when changing MAX_NUMBER_OF_MESSAGES
|
---|
176 | private long time = System.currentTimeMillis();
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Stores and displays the {@code MAX_NUMBER_OF_MESSAGES} most recent
|
---|
180 | * task titles together with their execution time.
|
---|
181 | */
|
---|
182 | public void setTaskTitle(String taskTitle) {
|
---|
183 |
|
---|
184 | while (messages.size() >= MAX_NUMBER_OF_MESSAGES) {
|
---|
185 | messages.removeFirst();
|
---|
186 | }
|
---|
187 | long now = System.currentTimeMillis();
|
---|
188 | String prevMessageTitle = messages.getLast();
|
---|
189 | if (!prevMessageTitle.isEmpty()) {
|
---|
190 | messages.removeLast();
|
---|
191 | messages.add(tr("{0} ({1} ms)", prevMessageTitle, Long.toString(now - time)));
|
---|
192 | }
|
---|
193 | time = now;
|
---|
194 | if (!taskTitle.isEmpty()) {
|
---|
195 | messages.add(taskTitle);
|
---|
196 | }
|
---|
197 | String html = "";
|
---|
198 | int i = 0;
|
---|
199 | for (String m : messages) {
|
---|
200 | html += "<p class=\"entry" + (++i) + "\">" + m + "</p>";
|
---|
201 | }
|
---|
202 |
|
---|
203 | lblTaskTitle.setText("<html><style>"
|
---|
204 | + ".entry1{color:#CCCCCC;}"
|
---|
205 | + ".entry2{color:#999999;}"
|
---|
206 | + ".entry3{color:#000000;}</style>" + html + "</html>"); //update when changing MAX_NUMBER_OF_MESSAGES
|
---|
207 | repaint();
|
---|
208 | }
|
---|
209 |
|
---|
210 | public void setValue(int value) {
|
---|
211 | progressBar.setValue(value);
|
---|
212 | repaint();
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|