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