/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package net.kirauks.javafx.dialog;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

/**
 *
 * @author Karl
 */
public class Dialog extends Stage{
    public enum DialogType{
        ERROR("dialog_error.png"),
        WARNING("dialog_warning.png"),
        QUESTION("dialog_question.png"),
        INFORMATION("dialog_information.png");
        
        private final String imageURL;
        
        private DialogType(String imageURL){
            this.imageURL = imageURL;
        }
        
        private String getImageURL(){
            return this.imageURL;
        }
    }
    public enum DialogOptions{
        OK,
        OK_CANCEL,
        YES_NO,
        YES_NO_CANCEL;
    }
    public enum DialogResponse{
        OK, CANCEL, YES, NO;
    }
    public interface DialogListener{
        public void onResponse(DialogResponse response);
    }
    
    public Dialog(String message, final DialogListener responseListener, final DialogType type, final DialogOptions options){
        super();
        this.initStyle(StageStyle.UTILITY);
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Dialog.fxml"));
            Parent root = (Parent) fxmlLoader.load();
            DialogController controller = fxmlLoader.<DialogController>getController();
            
            controller.setDialog(this);
            controller.setMessage(message);
            controller.setImage(new Image(Dialog.class.getResource(type.imageURL).toString()));
            
            if(options == DialogOptions.OK || options == DialogOptions.OK_CANCEL){
                controller.addButton("Ok", new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent t) {
                        if(responseListener != null){
                            responseListener.onResponse(DialogResponse.OK);
                        }
                    }
                }, options == DialogOptions.OK, true);
            }
            if(options == DialogOptions.YES_NO || options == DialogOptions.YES_NO_CANCEL){
                controller.addButton("Oui", new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent t) {
                        if(responseListener != null){
                            responseListener.onResponse(DialogResponse.YES);
                        }
                    }
                }, false, true);
                controller.addButton("Non", new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent t) {
                        if(responseListener != null){
                            responseListener.onResponse(DialogResponse.NO);
                        }
                    }
                }, options == DialogOptions.YES_NO, false);
            }
            if(options == DialogOptions.OK_CANCEL || options == DialogOptions.YES_NO_CANCEL){
                controller.addButton("Annuler", new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent t) {
                        if(responseListener != null){
                            responseListener.onResponse(DialogResponse.CANCEL);
                        }
                    }
                }, true, false);
            }
            
            this.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent t) {
                    if(responseListener != null){
                        switch(options){
                            case OK:
                                responseListener.onResponse(DialogResponse.OK);
                                break;
                            case YES_NO:
                                responseListener.onResponse(DialogResponse.NO);
                                break;
                            case OK_CANCEL:
                            case YES_NO_CANCEL:
                                responseListener.onResponse(DialogResponse.CANCEL);
                                break;
                        }
                    }
                }
            });
            
            this.setScene(new Scene(root));
            this.centerOnScreen();
        } catch (IOException ex) {
            Logger.getLogger(Dialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

@@ -583,16 +583,44 @@
 eStyle;%0A
+import javafx.stage.Window;%0A
 import j
@@ -1419,24 +1419,327 @@
 %0A    %7D%0A    %0A
+    public Dialog(String message, final DialogListener responseListener, final DialogType type, final DialogOptions options, final Window ownerWindows)%7B%0A        this(message, responseListener, type, options);%0A        this.initOwner(ownerWindows);%0A        this.initModality(Modality.WINDOW_MODAL);%0A    %7D%0A
     public D
@@ -1913,16 +1913,50 @@
 ILITY);%0A
+        this.setResizable(false);%0A
         

Add parent window block if gived