001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.bugreport; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.GridBagLayout; 007import java.io.IOException; 008import java.nio.charset.StandardCharsets; 009 010import javax.swing.JButton; 011import javax.swing.JPanel; 012import javax.swing.SwingUtilities; 013 014import org.openstreetmap.josm.Main; 015import org.openstreetmap.josm.data.Version; 016import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 017import org.openstreetmap.josm.gui.widgets.UrlLabel; 018import org.openstreetmap.josm.io.CachedFile; 019import org.openstreetmap.josm.tools.GBC; 020import org.openstreetmap.josm.tools.ImageProvider; 021import org.openstreetmap.josm.tools.Logging; 022 023/** 024 * This is a panel that displays the current JOSM version and the ability to update JOSM. 025 * @author Michael Zangl 026 * @since 10649 027 */ 028public class JosmUpdatePanel extends JPanel { 029 private final JMultilineLabel testedVersionField; 030 private final int josmVersion; 031 032 /** 033 * Create a new {@link JosmUpdatePanel} 034 */ 035 public JosmUpdatePanel() { 036 super(new GridBagLayout()); 037 josmVersion = Version.getInstance().getVersion(); 038 039 add(new JMultilineLabel(tr("Your current version of JOSM is {0}", Integer.toString(josmVersion))), GBC.eol().fill(GBC.HORIZONTAL)); 040 testedVersionField = new JMultilineLabel(tr("JOSM is searching for updates...")); 041 add(testedVersionField, GBC.eol().fill(GBC.HORIZONTAL)); 042 043 checkCurrentVersion(); 044 } 045 046 private void checkCurrentVersion() { 047 new Thread(this::readCurrentVersion, "JOSM version checker").start(); 048 } 049 050 private void readCurrentVersion() { 051 int testedVersion = getTestedVersion(); 052 053 if (testedVersion < 0) { 054 SwingUtilities.invokeLater(this::displayError); 055 } else if (josmVersion < testedVersion) { 056 SwingUtilities.invokeLater(() -> displayOutOfDate(testedVersion)); 057 } else { 058 SwingUtilities.invokeLater(this::displayUpToDate); 059 } 060 } 061 062 private static int getTestedVersion() { 063 try (CachedFile testedVersion = new CachedFile(Main.getJOSMWebsite() + "/tested")) { 064 testedVersion.setMaxAge(60L * 15); // 15 Minutes 065 String testedString = new String(testedVersion.getByteContent(), StandardCharsets.ISO_8859_1); 066 return Integer.parseInt(testedString.trim()); 067 } catch (NumberFormatException | IOException e) { 068 Logging.log(Logging.LEVEL_WARN, "Unable to detect current tested version of JOSM:", e); 069 return -1; 070 } 071 } 072 073 /** 074 * Display that there was an error while checking the current version. 075 */ 076 private void displayError() { 077 testedVersionField.setText(tr("An error occured while checking if your JOSM instance is up to date.")); 078 showUpdateButton(); 079 } 080 081 private void displayUpToDate() { 082 testedVersionField.setText(tr("JOSM is up to date.")); 083 } 084 085 private void displayOutOfDate(int testedVersion) { 086 testedVersionField 087 .setText(tr("JOSM is out of date. The current version is {0}. Try updating JOSM.", Integer.toString(testedVersion))); 088 showUpdateButton(); 089 } 090 091 private void showUpdateButton() { 092 add(new JMultilineLabel(tr("Before you file a bug report make sure you have updated to the latest version of JOSM here:")), GBC.eol()); 093 add(new UrlLabel(Main.getJOSMWebsite(), 2), GBC.eop().insets(8, 0, 0, 0)); 094 JButton updateButton = new JButton(tr("Update JOSM"), ImageProvider.getIfAvailable("download")); 095 updateButton.addActionListener(e -> openJosmUpdateSite()); 096 add(updateButton, GBC.eol().anchor(GBC.EAST)); 097 } 098 099 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}