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