1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.bugreport;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.nio.charset.StandardCharsets;
|
---|
9 |
|
---|
10 | import javax.swing.JButton;
|
---|
11 | import javax.swing.JPanel;
|
---|
12 | import javax.swing.SwingUtilities;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.Version;
|
---|
16 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
17 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
18 | import org.openstreetmap.josm.io.CachedFile;
|
---|
19 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
20 | import org.openstreetmap.josm.tools.GBC;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 | import org.openstreetmap.josm.tools.Logging;
|
---|
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 | */
|
---|
29 | public 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 | int testedVersion = getTestedVersion();
|
---|
53 |
|
---|
54 | if (testedVersion < 0) {
|
---|
55 | SwingUtilities.invokeLater(this::displayError);
|
---|
56 | } else if (josmVersion < testedVersion) {
|
---|
57 | SwingUtilities.invokeLater(() -> displayOutOfDate(testedVersion));
|
---|
58 | } else {
|
---|
59 | SwingUtilities.invokeLater(this::displayUpToDate);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | private static int getTestedVersion() {
|
---|
64 | try (CachedFile testedVersion = new CachedFile(Config.getUrls().getJOSMWebsite() + "/tested")) {
|
---|
65 | testedVersion.setMaxAge(60L * 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(Config.getUrls().getJOSMWebsite(), 2), GBC.eop().insets(8, 0, 0, 0));
|
---|
95 | JButton updateButton = new JButton(tr("Update JOSM"), ImageProvider.getIfAvailable("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(Config.getUrls().getJOSMWebsite());
|
---|
103 | } catch (IOException ex) {
|
---|
104 | Logging.log(Logging.LEVEL_WARN, "Unable to access JOSM website:", ex);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|