Skip to content

Instantly share code, notes, and snippets.

@andrewmd5
Created May 28, 2015 09:23
Show Gist options
  • Save andrewmd5/bb205ab3d9bfa06c8ba5 to your computer and use it in GitHub Desktop.
Save andrewmd5/bb205ab3d9bfa06c8ba5 to your computer and use it in GitHub Desktop.
package me.aurous.ui.handlers;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
/**
* @author Andrew
*
*/
public class AurousAlert {
/**
* The displayed message
*/
String message;
/**
* Title of our alert window
*/
String title;
/**
* The flag for the alert type
*/
AlertType type;
/**
* Construct an alert
*
* @param message
* to be dispalyed at time of alert
* @param title
* of our targeted alert
* @param the
* type of our alert
*/
public AurousAlert(final String message, final String title) {
this.message = message;
this.title = title;
this.type = AlertType.INFORMATION;
}
/**
* Get the display message
*
* @return display message
*/
public String getMessage() {
return message;
}
/**
* Get the title of the alert
*
* @return display title
*/
public String getTitle() {
return title;
}
/**
* Set the message externally
*
* @param message
* to be displayed
*/
public void setMessage(final String message) {
this.message = message;
}
/**
* Set the alert title externally
*
* @param title
* to be displayed
*/
public void setTitle(final String title) {
this.title = title;
}
/**
* Display an alert and wait for user to close disabling main UI
*/
public void showAlert() {
Platform.runLater(() -> {
final Alert alert = new Alert(type);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
});
}
/**
* Display an alert with basic callback of results
*
* @return status of button
*/
public boolean showAlertWithCallback() {
final FutureTask<?> conformation = new FutureTask<Object>(() -> {
final Alert alert = new Alert(type);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
final String buttonText = alert.getResult().getText();
switch (buttonText) {
case "Yes":
return true;
case "Cancel":
return false;
default:
return false;
}
});
Platform.runLater(conformation);
try {
return Boolean.valueOf(conformation.get().toString());
} catch (InterruptedException | ExecutionException e) {
// global error handler to do
}
return false;
}
/**
* Display an alert with conformation and callback
*
* @return status of the button
*/
public boolean showConformationAlert() {
type = AlertType.CONFIRMATION;
final FutureTask<?> conformation = new FutureTask<Object>(() -> {
final Alert alert = new Alert(type, message, ButtonType.YES,
ButtonType.NO, ButtonType.CANCEL);
alert.setTitle(title);
alert.setHeaderText(null);
alert.showAndWait();
final String buttonText = alert.getResult().getText();
switch (buttonText) {
case "Yes":
return true;
case "No":
return false;
case "Cancel":
return false;
default:
return false;
}
});
Platform.runLater(conformation);
try {
return Boolean.valueOf(conformation.get().toString());
} catch (InterruptedException | ExecutionException e) {
// global error handler to do
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment