All Downloads are FREE. Search and download functionalities are using the official Maven repository.

view.updateAvailableDialog.UpdateAvailableDialog Maven / Gradle / Ivy

There is a newer version: 0.1.5
Show newest version
/**
 * Sample Skeleton for "AlertDialog.fxml" Controller Class
 * You can copy and paste this code into your favorite IDE
 **/

package view.updateAvailableDialog;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import common.Common;
import common.UpdateChecker;
import common.UpdateInfo;
import common.UpdateProgressDialog;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.stage.Stage;

public class UpdateAvailableDialog implements UpdateProgressDialog {

	private static Stage stage;
	private ResourceBundle bundle = ResourceBundle.getBundle("view.updateAvailableDialog.AlertDialog");
	private static String messageText;
	private static UpdateInfo updateInfo;
	private static Thread downloadThread = new Thread();

	/**
	 * Constructor stub necessary for FXML, please use
	 * {@link #UpdateAvailableDialog(UpdateInfo update)}.
	 */
	@Deprecated
	public UpdateAvailableDialog() {

	}

	/**
	 * Constructs a new UpdateAvailableDialog and presents the specified update
	 * info to the user.
	 * 
	 * @param update
	 *            The info about the update do install.
	 */
	public UpdateAvailableDialog(UpdateInfo update) {
		show(update);
	}

	@FXML // ResourceBundle that was given to the FXMLLoader
	private ResourceBundle resources;

	@FXML // URL location of the FXML file that was given to the FXMLLoader
	private URL location;
	
    @FXML // fx:id="updateProgressBar"
    private ProgressBar updateProgressBar; // Value injected by FXMLLoader

	@FXML // fx:id="cancelButton"
	private Button cancelButton; // Value injected by FXMLLoader

	@FXML // fx:id="detailsLabel"
	private Label detailsLabel; // Value injected by FXMLLoader

	@FXML // fx:id="messageLabel"
	private Label messageLabel; // Value injected by FXMLLoader

	@FXML // fx:id="okButton"
	private Button okButton; // Value injected by FXMLLoader

	@FXML // fx:id="updateProgressAnimation"
	private ProgressIndicator updateProgressAnimation; // Value injected by
														// FXMLLoader

	@FXML // fx:id="updateProgressText"
	private Label updateProgressText; // Value injected by FXMLLoader

	// Handler for Button[fx:id="cancelButton"] onAction
	@FXML
	void ignoreButtonOnAction(ActionEvent event) {
		if (!downloadThread.isAlive()) {
			// ignore this update
			this.hide();
			UpdateChecker.ignoreUpdate(updateInfo.toVersion);
		} else {
			// Cancel download
			UpdateChecker.cancelDownloadAndLaunch(this);
		}
	}

	// Handler for Button[fx:id="okButton"] onAction
	@FXML
	void okButtonOnAction(ActionEvent event) {
		UpdateAvailableDialog t = this;

		okButton.setDisable(true);
		cancelButton.setText(bundle.getString("button.cancel.cancelDownload"));

		downloadThread = new Thread() {
			@Override
			public void run() {
				try {
					boolean res = UpdateChecker.downloadAndInstallUpdate(updateInfo, t);
					if (res) {
						t.hide();
					}
				} catch (IllegalStateException | IOException e) {
					showErrorMessage(e.getLocalizedMessage());
					e.printStackTrace();
				}
			}
		};
		downloadThread.start();
	}

	@FXML // This method is called by the FXMLLoader when initialization is
			// complete
	void initialize() {
		 assert cancelButton != null : "fx:id=\"cancelButton\" was not injected: check your FXML file 'AlertDialog.fxml'.";
	        assert detailsLabel != null : "fx:id=\"detailsLabel\" was not injected: check your FXML file 'AlertDialog.fxml'.";
	        assert messageLabel != null : "fx:id=\"messageLabel\" was not injected: check your FXML file 'AlertDialog.fxml'.";
	        assert okButton != null : "fx:id=\"okButton\" was not injected: check your FXML file 'AlertDialog.fxml'.";
	        assert updateProgressAnimation != null : "fx:id=\"updateProgressAnimation\" was not injected: check your FXML file 'AlertDialog.fxml'.";
	        assert updateProgressBar != null : "fx:id=\"updateProgressBar\" was not injected: check your FXML file 'AlertDialog.fxml'.";
	        assert updateProgressText != null : "fx:id=\"updateProgressText\" was not injected: check your FXML file 'AlertDialog.fxml'.";
		// Initialize your logic here: all @FXML variables will have been
		// injected
		if (updateInfo.showAlert) {
			// an update is available, show its info
			detailsLabel.setText(messageText);
		} else {
			// No update is available, show a corresponding message
			detailsLabel.setText("");
			messageLabel.setText(bundle.getString("label.noUpdate"));
			okButton.setDisable(true);
			cancelButton.setDisable(true);

		}
		updateProgressAnimation.setVisible(false);
		updateProgressText.setVisible(false);
	}

	private void show(UpdateInfo update) {
		stage = new Stage();
		Parent root;
		try {

			if (update != null) {
				if (update.fileSizeInMB != -1) {
					messageText = "Filesize: " + (Math.round(update.fileSizeInMB * 100)) / 100.0
							+ " MB, Version to download: " + update.toVersion.toString();
				} else {
					// File sie unknown, see Javadoc of UpdateInfo
					messageText = "Filesize: unknown, Version to download: " + update.toVersion.toString();
				}
			}
			updateInfo = update;

			root = FXMLLoader.load(UpdateAvailableDialog.class.getResource("AlertDialog.fxml"), bundle);
			Scene scene = new Scene(root);
			// scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

			// Set the window title and put the app name in it
			stage.setTitle(bundle.getString("window.Title").replace("{appName}", Common.getAppName()));

			stage.setMinWidth(scene.getRoot().minWidth(0) + 70);
			stage.setMinHeight(scene.getRoot().minHeight(0) + 70);

			stage.setScene(scene);
			stage.show();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void hide() {
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				stage.hide();
			}

		});
	}

	@Override
	public void preparePhaseStarted() {
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				updateProgressAnimation.setVisible(true);
				updateProgressText.setVisible(true);
				updateProgressText.setText(bundle.getString("progress.preparing"));
			}

		});
	}

	@Override
	public void downloadStarted() {
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				updateProgressText.setText(bundle.getString("progress.downloading"));
				updateProgressBar.setVisible(true);
				updateProgressBar.setProgress(-1);
			}

		});
	}

	@Override
	public void installStarted() {
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				updateProgressText.setText(bundle.getString("progress.installing"));
			}

		});
	}

	@Override
	public void launchStarted() {
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				updateProgressText.setText(bundle.getString("progress.launching"));
			}

		});
	}

	public void showErrorMessage(String message) {
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				detailsLabel.setText("An error occurred:\n" + message);
				updateProgressAnimation.setVisible(false);
				updateProgressText.setVisible(false);
				okButton.setDisable(false);
				okButton.setText(bundle.getString("button.ok.retry"));
				updateProgressBar.setVisible(false);
			}

		});
	}

	@Override
	public void operationCanceled() {
		Platform.runLater(new Runnable(){
			@Override
			public void run() {
				updateProgressAnimation.setVisible(false);
				updateProgressText.setVisible(false);
				okButton.setText(bundle.getString("button.ok"));
				okButton.setDisable(false);
				cancelButton.setText(bundle.getString("button.cancel"));
				updateProgressBar.setVisible(false);
			}
			
		});
	}

	@Override
	public void cancelRequested() {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				updateProgressText.setText(bundle.getString("progress.cancelRequested"));
			}
		});
	}
	
	@Override
	public void downloadProgressChanged(double kilobytesDownloaded, double totalFileSizeInKB) {
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				updateProgressBar.setProgress(kilobytesDownloaded/totalFileSizeInKB);
			}

		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy