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

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

see #14602 - don't use thousands separator when displaying the JOSM version

  • Property svn:eol-style set to native
File size: 3.7 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;
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.Logging;
20import 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 */
27public 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}", Integer.toString(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.", Integer.toString(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}
Note: See TracBrowser for help on using the repository browser.