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