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.tools.GBC;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
21 | import 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 | */
|
---|
28 | public 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 (CachedFile testedVersion = new CachedFile(Main.getJOSMWebsite() + "/tested")) {
|
---|
64 | testedVersion.setMaxAge(60L * 15); // 15 Minutes
|
---|
65 | String testedString = new String(testedVersion.getByteContent(), StandardCharsets.ISO_8859_1);
|
---|
66 | return Integer.parseInt(testedString.trim());
|
---|
67 | } catch (NumberFormatException | IOException e) {
|
---|
68 | Logging.log(Logging.LEVEL_WARN, "Unable to detect current tested version of JOSM:", e);
|
---|
69 | return -1;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Display that there was an error while checking the current version.
|
---|
75 | */
|
---|
76 | private void displayError() {
|
---|
77 | testedVersionField.setText(tr("An error occured while checking if your JOSM instance is up to date."));
|
---|
78 | showUpdateButton();
|
---|
79 | }
|
---|
80 |
|
---|
81 | private void displayUpToDate() {
|
---|
82 | testedVersionField.setText(tr("JOSM is up to date."));
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void displayOutOfDate(int testedVersion) {
|
---|
86 | testedVersionField
|
---|
87 | .setText(tr("JOSM is out of date. The current version is {0}. Try updating JOSM.", Integer.toString(testedVersion)));
|
---|
88 | showUpdateButton();
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void showUpdateButton() {
|
---|
92 | add(new JMultilineLabel(tr("Before you file a bug report make sure you have updated to the latest version of JOSM here:")), GBC.eol());
|
---|
93 | add(new UrlLabel(Main.getJOSMWebsite(), 2), GBC.eop().insets(8, 0, 0, 0));
|
---|
94 | JButton updateButton = new JButton(tr("Update JOSM"), ImageProvider.getIfAvailable("download"));
|
---|
95 | updateButton.addActionListener(e -> openJosmUpdateSite());
|
---|
96 | add(updateButton, GBC.eol().anchor(GBC.EAST));
|
---|
97 | }
|
---|
98 |
|
---|
99 | private static void openJosmUpdateSite() {
|
---|
100 | try {
|
---|
101 | Main.platform.openUrl(Main.getJOSMWebsite());
|
---|
102 | } catch (IOException ex) {
|
---|
103 | Logging.log(Logging.LEVEL_WARN, "Unable to access JOSM website:", ex);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|