io.github.medinam.jcheesum.util.VersionChecker.java Source code

Java tutorial

Introduction

Here is the source code for io.github.medinam.jcheesum.util.VersionChecker.java

Source

/*
 * Copyright (C) 2017 Luis Medina
 *
 * JCheesum is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JCheesum is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package io.github.medinam.jcheesum.util;

import io.github.medinam.jcheesum.App;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import org.json.JSONObject;
import org.json.JSONTokener;

public class VersionChecker {

    public static void showDownloadLatestAlert() {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);

        ResourceBundle resources = ResourceBundle.getBundle("i18n/Bundle", Locale.getDefault());

        alert.setTitle(resources.getString("newVersion"));
        alert.setHeaderText(resources.getString("newVersionLooksLike"));
        alert.setContentText(resources.getString("youHave") + App.class.getPackage().getImplementationVersion()
                + " " + resources.getString("latestVersionIs") + getLatestStable() + "\n\n"
                + resources.getString("wannaDownload"));

        ButtonType yesBtn = new ButtonType(resources.getString("yes"));
        ButtonType noBtn = new ButtonType(resources.getString("no"), ButtonBar.ButtonData.CANCEL_CLOSE);

        alert.getButtonTypes().setAll(yesBtn, noBtn);

        Optional<ButtonType> o = alert.showAndWait();

        if (o.get() == yesBtn) {
            General.openLink(getLatestStableDownloadURL());
        } else if (o.get() == noBtn) {
            alert.close();
        }
    }

    public static boolean newVersionAvailable() {
        Version implementedVersion = new Version(App.class.getPackage().getImplementationVersion());
        Version latestStableVersion = new Version(getLatestStable());

        boolean newVersion = implementedVersion.compareTo(latestStableVersion) < 0;

        return newVersion;
    }

    public static String getLatestStable() {
        String version = new String();
        try {
            JSONTokener tokener = getVersionTokener();
            JSONObject json = new JSONObject(tokener);
            version = json.getString("version");
        } catch (URISyntaxException ex) {
            Logger.getLogger(VersionChecker.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(VersionChecker.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(VersionChecker.class.getName()).log(Level.SEVERE, null, ex);
        }
        return version;
    }

    public static String getLatestStableDownloadURL() {
        String url = new String();
        try {
            JSONTokener tokener = getVersionTokener();
            JSONObject json = new JSONObject(tokener);
            url = json.getString("url");
        } catch (URISyntaxException ex) {
            Logger.getLogger(VersionChecker.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(VersionChecker.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(VersionChecker.class.getName()).log(Level.SEVERE, null, ex);
        }
        return url;
    }

    private static JSONTokener getVersionTokener() throws URISyntaxException, MalformedURLException, IOException {
        URI uri = new URI("https://medinam.github.io/jcheesum/version.json");
        return new JSONTokener(uri.toURL().openConnection().getInputStream());
    }

}