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