package sample; 

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.DatePicker; 
import javafx.scene.effect.BoxBlur; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.stage.Modality; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 
import javafx.stage.Window; 

public class Main extends Application { 

    public static void main(String[] args) { 
        launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
        VBox vbox = new VBox(); 
        vbox.setSpacing(10); 
        vbox.setAlignment(Pos.CENTER); 

        Button buttonFails = new Button("Fails"); 
        buttonFails.setOnAction(event -> onFails(primaryStage)); 

        Button buttonWorks = new Button("Works"); 
        buttonWorks.setOnAction(event -> onWorks(primaryStage)); 

        vbox.getChildren().addAll(buttonFails, buttonWorks); 

        primaryStage.setTitle("Sample"); 
        primaryStage.setScene(new Scene(vbox, 300, 300)); 
        primaryStage.show(); 
    } 


    private void onFails(Stage primaryStage) { 
        Stage stage = buildModal(primaryStage); 
        stage.showingProperty().addListener((observable, oldValue, newValue) -> { 
            System.out.println("(testing fails listener) oldValue=<" + oldValue + "> newValue=<" + newValue + ">"); 
        }); 
        showModal(primaryStage, stage); 
    } 

    private void onWorks(Stage primaryStage) { 
        Stage stage = buildModal(primaryStage); 
        stage.showingProperty().addListener((observable, oldValue, newValue) -> { 
            System.out.println("(testing works listener1) oldValue=<" + oldValue + "> newValue=<" + newValue + ">"); 
        }); 
        stage.showingProperty().addListener((observable, oldValue, newValue) -> { 
            System.out.println("(testing works listener2) oldValue=<" + oldValue + "> newValue=<" + newValue + ">"); 
        }); 
        showModal(primaryStage, stage); 
    } 

    private Stage buildModal(Window parent) { 
        Stage stage = new Stage(); 
        stage.initStyle(StageStyle.TRANSPARENT); 
        stage.initOwner(parent); 
        stage.initModality(Modality.WINDOW_MODAL); 

        DatePicker picker = new DatePicker(); 

        StackPane contentPane = new StackPane(); 
        contentPane.getChildren().add(picker); 
        contentPane.setStyle(" -fx-padding: 20;\n" + 
                " -fx-alignment: center;\n" + 
                " -fx-background-color: linear-gradient(to bottom, derive(cadetblue, 20%), cadetblue);\n" + 
                " -fx-border-color: derive(cadetblue, -20%);\n" + 
                " -fx-border-width: 5;\n" + 
                " -fx-border-radius: 6;\n" + 
                " -fx-background-radius: 6;\n"); 
        Scene scene = new Scene(contentPane, Color.TRANSPARENT); 
        stage.setScene(scene); 

        picker.setOnAction(event -> stage.close()); 

        return stage; 
    } 

    private void showModal(Stage primaryStage, Stage stage) { 
        primaryStage.getScene().getRoot().setEffect(new BoxBlur()); 
        stage.showAndWait(); 
        primaryStage.getScene().getRoot().setEffect(null); 
    } 

} 
