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        try {
053            int testedVersion = getTestedVersion();
054
055            if (testedVersion < 0) {
056                SwingUtilities.invokeLater(this::displayError);
057            } else if (josmVersion < testedVersion) {
058                SwingUtilities.invokeLater(() -> displayOutOfDate(testedVersion));
059            } else {
060                SwingUtilities.invokeLater(this::displayUpToDate);
061            }
062        } catch (RuntimeException e) {
063            Logging.error(e);
064        }
065    }
066
067    private static int getTestedVersion() {
068        try (CachedFile testedVersion = new CachedFile(Config.getUrls().getJOSMWebsite() + "/tested")) {
069            testedVersion.setMaxAge(60L * 15); // 15 Minutes
070            String testedString = new String(testedVersion.getByteContent(), StandardCharsets.ISO_8859_1);
071            return Integer.parseInt(testedString.trim());
072        } catch (NumberFormatException | IOException e) {
073            Logging.log(Logging.LEVEL_WARN, "Unable to detect current tested version of JOSM:", e);
074            return -1;
075        }
076    }
077
078    /**
079     * Display that there was an error while checking the current version.
080     */
081    private void displayError() {
082        testedVersionField.setText(tr("An error occurred while checking if your JOSM instance is up to date."));
083        showUpdateButton();
084    }
085
086    private void displayUpToDate() {
087        testedVersionField.setText(tr("JOSM is up to date."));
088    }
089
090    private void displayOutOfDate(int testedVersion) {
091        testedVersionField
092                .setText(tr("JOSM is out of date. The current version is {0}. Try updating JOSM.", Integer.toString(testedVersion)));
093        showUpdateButton();
094    }
095
096    private void showUpdateButton() {
097        add(new JMultilineLabel(tr("Before you file a bug report make sure you have updated to the latest version of JOSM here:")), GBC.eol());
098        add(new UrlLabel(Config.getUrls().getJOSMWebsite(), 2), GBC.eop().insets(8, 0, 0, 0));
099        JButton updateButton = new JButton(tr("Update JOSM"), ImageProvider.getIfAvailable("download"));
100        updateButton.addActionListener(e -> openJosmUpdateSite());
101        add(updateButton, GBC.eol().anchor(GBC.EAST));
102    }
103
104    private static void openJosmUpdateSite() {
105        try {
106            PlatformManager.getPlatform().openUrl(Config.getUrls().getJOSMWebsite());
107        } catch (IOException ex) {
108            Logging.log(Logging.LEVEL_WARN, "Unable to access JOSM website:", ex);
109        }
110    }
111}