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

Last change on this file since 13019 was 13019, checked in by bastiK, 7 years ago

fixed #15449 - JOSM version checker is broken

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