/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.component;

import java.io.IOException;

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.Window;
import javafx.util.Callback;

public class DialogComponent extends Stage {

    public DialogComponent(IDialogComponent controller, Window owner, String title) {
        super();
        initOwner(owner);
        //initModality(Modality.WINDOW_MODAL);
        initModality(Modality.NONE);
        setResizable(false);
        setTitle(title);
        // set icon
        getIcons().add(new Image(getClass().getResource("/fr/tlse/irap/pt/client/images/favicon.png").toString()));
        //getIcons().add(new Image("images/logopt.png"));
        loadComponent(controller);
    }

    public DialogComponent(IDialogComponent controller, Window owner, Modality modality, StageStyle style) {
        super(style);
        initOwner(owner);
        initModality(modality);
        loadComponent(controller);
    }

    private void loadComponent(final IDialogComponent controller) {
        String fxmlComponent = controller.getClass().getSimpleName() + ".fxml";
        FXMLLoader fxmlLoader = new FXMLLoader(controller.getClass().getResource(fxmlComponent));
        fxmlLoader.setRoot(controller);
        fxmlLoader.setController(controller);
        try {
            fxmlLoader.setControllerFactory(new Callback<Class<?>, Object>() {
                @Override
                public Object call(Class<?> aClass) {
                    return controller;
                }
            });
            // link the dialogBox to the controller
            controller.setDialog(this);
            setScene(new Scene((Parent) fxmlLoader.load()));
            getScene().getStylesheets().add(controller.getClass().getResource("/fr/tlse/irap/pt/client/styles/Calendar.css").toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
