//==================================================================
// Future value application - with validation 
//==================================================================
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.control.*;
import javafx.geometry.Insets;

public class Dialogs extends Application 
{    
    public static void main(String[] args) 
    {
        Application.launch(args);                                       //launch the application
    }    

    @Override
    public void start(Stage primaryStage) 
    {
        primaryStage.setTitle("Example of 3 Types of Dialogs");

        GridPane grid  = new GridPane();                            //create a gridPane layout
        Insets padding = new Insets(25, 25, 25, 25);                //create padding top,left,bottom,right 
        grid.setPadding(padding);                            
        grid.setHgap(10);                                           //horizontal gap between rows
        grid.setVgap(10);                                           //vertical gap between columns
 
        Button alert = new Button("Alert");
        alert.setOnAction(event -> displayAlert());                 //on action call displayAlert()
        grid.add(alert, 0, 0);

        Button prompt = new Button("TextInputDialog");
        prompt.setOnAction(event -> displayPrompt());               //on action call displayPrompt()
        grid.add(prompt, 1, 0);

        Button choice = new Button("ChoiceDialog");
        choice.setOnAction(event -> displayChoice());               //on action call displayChoice() 
        grid.add(choice, 2, 0);

        Scene scene = new Scene(grid, 400, 300);                    //create a scene, add the grid layout, width, height

        primaryStage.setScene(scene);                               //add the scene to the stage
        primaryStage.show();                                        //display the window
    }

    private void displayAlert() 
    {
        Alert alert = new Alert(Alert.AlertType.ERROR);             //icon type ERROR,INFORMATION,CONFIRMATION,WARNING,NONE 
        alert.setTitle("This is an alert dialog");
        alert.setHeaderText("Invalid Data");
        alert.setContentText("The detail error messages");
        alert.showAndWait();                                        //display and wait 
    }                

    private void displayPrompt() 
    {
        TextInputDialog prompt = new TextInputDialog("Sam");        //with default value in the textbox 
        prompt.setTitle("This is a prompt dialog");
        prompt.setHeaderText("Name prompt");
        prompt.setContentText("Please enter your full name");
        prompt.showAndWait();                                            
        String entry = prompt.getEditor().getText();                //get the entered text
        Alert alert = new Alert(Alert.AlertType.INFORMATION);          
        alert.setHeaderText(null);
        alert.setContentText(entry);
        alert.show();
     }                

    private void displayChoice() 
    {
        String[] days = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
        ChoiceDialog<String> choice = new ChoiceDialog<>(days[0], days);    //the default choice, and all choices
        choice.setTitle("This is a choice dialog");
        choice.setHeaderText("Choose from the list below");
        choice.setContentText("Choice");
        choice.showAndWait();                                               
        String selected = choice.getSelectedItem();                         //get the selected choice                                
        Alert alert = new Alert(Alert.AlertType.INFORMATION);          
        alert.setHeaderText(null);
        alert.setContentText(selected);
        alert.show();
    }
}