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

Last change on this file since 8513 was 8513, checked in by Don-vip, 11 years ago

checkstyle: blocks

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