source: josm/trunk/src/org/openstreetmap/josm/gui/bugreport/JosmUpdatePanel.java

Last change on this file was 17076, checked in by Don-vip, 6 years ago

fix #19860 - prevent illegal state to raise an NPE at JOSM startup on macOS

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.bugreport;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.io.IOException;
8import java.nio.charset.StandardCharsets;
9
10import javax.swing.JButton;
11import javax.swing.JPanel;
12import javax.swing.SwingUtilities;
13
14import org.openstreetmap.josm.data.Version;
15import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
16import org.openstreetmap.josm.gui.widgets.UrlLabel;
17import org.openstreetmap.josm.io.CachedFile;
18import org.openstreetmap.josm.spi.preferences.Config;
19import org.openstreetmap.josm.tools.GBC;
20import org.openstreetmap.josm.tools.ImageProvider;
21import org.openstreetmap.josm.tools.Logging;
22import org.openstreetmap.josm.tools.PlatformManager;
23
24/**
25 * This is a panel that displays the current JOSM version and the ability to update JOSM.
26 * @author Michael Zangl
27 * @since 10649
28 */
29public class JosmUpdatePanel extends JPanel {
30 private final JMultilineLabel testedVersionField;
31 private final int josmVersion;
32
33 /**
34 * Create a new {@link JosmUpdatePanel}
35 */
36 public JosmUpdatePanel() {
37 super(new GridBagLayout());
38 josmVersion = Version.getInstance().getVersion();
39
40 add(new JMultilineLabel(tr("Your current version of JOSM is {0}", Integer.toString(josmVersion))), GBC.eol().fill(GBC.HORIZONTAL));
41 testedVersionField = new JMultilineLabel(tr("JOSM is searching for updates..."));
42 add(testedVersionField, GBC.eol().fill(GBC.HORIZONTAL));
43
44 checkCurrentVersion();
45 }
46
47 private void checkCurrentVersion() {
48 new Thread(this::readCurrentVersion, "JOSM version checker").start();
49 }
50
51 private void readCurrentVersion() {
52 try {
53 int testedVersion = getTestedVersion();
54
55 if (testedVersion < 0) {
56 SwingUtilities.invokeLater(this::displayError);
57 } else if (josmVersion < testedVersion) {
58 SwingUtilities.invokeLater(() -> displayOutOfDate(testedVersion));
59 } else {
60 SwingUtilities.invokeLater(this::displayUpToDate);
61 }
62 } catch (RuntimeException e) {
63 Logging.error(e);
64 }
65 }
66
67 private static int getTestedVersion() {
68 try (CachedFile testedVersion = new CachedFile(Config.getUrls().getJOSMWebsite() + "/tested")) {
69 testedVersion.setMaxAge(60L * 15); // 15 Minutes
70 String testedString = new String(testedVersion.getByteContent(), StandardCharsets.ISO_8859_1);
71 return Integer.parseInt(testedString.trim());
72 } catch (NumberFormatException | IOException e) {
73 Logging.log(Logging.LEVEL_WARN, "Unable to detect current tested version of JOSM:", e);
74 return -1;
75 }
76 }
77
78 /**
79 * Display that there was an error while checking the current version.
80 */
81 private void displayError() {
82 testedVersionField.setText(tr("An error occurred while checking if your JOSM instance is up to date."));
83 showUpdateButton();
84 }
85
86 private void displayUpToDate() {
87 testedVersionField.setText(tr("JOSM is up to date."));
88 }
89
90 private void displayOutOfDate(int testedVersion) {
91 testedVersionField
92 .setText(tr("JOSM is out of date. The current version is {0}. Try updating JOSM.", Integer.toString(testedVersion)));
93 showUpdateButton();
94 }
95
96 private void showUpdateButton() {
97 add(new JMultilineLabel(tr("Before you file a bug report make sure you have updated to the latest version of JOSM here:")), GBC.eol());
98 add(new UrlLabel(Config.getUrls().getJOSMWebsite(), 2), GBC.eop().insets(8, 0, 0, 0));
99 JButton updateButton = new JButton(tr("Update JOSM"), ImageProvider.getIfAvailable("download"));
100 updateButton.addActionListener(e -> openJosmUpdateSite());
101 add(updateButton, GBC.eol().anchor(GBC.EAST));
102 }
103
104 private static void openJosmUpdateSite() {
105 try {
106 PlatformManager.getPlatform().openUrl(Config.getUrls().getJOSMWebsite());
107 } catch (IOException ex) {
108 Logging.log(Logging.LEVEL_WARN, "Unable to access JOSM website:", ex);
109 }
110 }
111}
Note: See TracBrowser for help on using the repository browser.