source: josm/trunk/src/org/openstreetmap/josm/tools/bugreport/JosmUpdatePanel.java@ 10830

Last change on this file since 10830 was 10830, checked in by Don-vip, 8 years ago

fix #13281 - Small typo in phrase

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