DialogUtils.java 7.25 KB
package cn.csbr.app.gui.util;

import cn.csbr.app.gui.GUIContext;
import cn.csbr.app.gui.component.common.ActionCallBack;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.controlsfx.dialog.CommandLinksDialog;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/***
 * 对话框工具类
 */
public class DialogUtils {

    public static void showHeaderTextInformationAlert(String message ,Stage stage) {
        Alert dlg = new Alert(Alert.AlertType.INFORMATION, message);
        dlg.initOwner(stage);
        dlg.initModality(Modality.APPLICATION_MODAL);
        dlg.setHeaderText("");
        dlg.setTitle("提示信息");
        dlg.setOnCloseRequest(e -> {
            dlg.close();
        });
        dlg.initModality(Modality.APPLICATION_MODAL);
        //dlg.initOwner(owner);
        dlg.show();
        return;
    }
    public static void showHeaderTextInformationAlert2(String message ,Stage stage,boolean falg) {
        Alert dlg = new Alert(Alert.AlertType.INFORMATION, message);
        dlg.initOwner(stage);
        dlg.initModality(Modality.APPLICATION_MODAL);
        dlg.setHeaderText("");
        dlg.setTitle("提示信息");
        dlg.setOnCloseRequest(e -> {
            dlg.close();
            if(falg){
                stage.close();
            }

        });
        dlg.initModality(Modality.APPLICATION_MODAL);
        //dlg.initOwner(owner);
        dlg.show();
        return;
    }
    public static void showAlert(String message, ActionCallBack callBack) {
        Alert dlg = new Alert(Alert.AlertType.INFORMATION, message);
        dlg.initModality(Modality.APPLICATION_MODAL);
        dlg.setHeaderText("");
        dlg.setTitle("提示信息");
        //dlg.initOwner(owner);
        dlg.setOnCloseRequest(e -> {
            dlg.close();
        });
        dlg.initModality(Modality.APPLICATION_MODAL);
        dlg.showAndWait().ifPresent(new Consumer<ButtonType>() {
            @Override
            public void accept(ButtonType buttonType) {
                try {
                    callBack.callback(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return;
    }

    public static void showAlert(String message, ActionCallBack callBack, Stage stage) {
        Alert dlg = new Alert(Alert.AlertType.INFORMATION, message);
        dlg.initOwner(stage);
        dlg.initModality(Modality.APPLICATION_MODAL);
        dlg.setHeaderText("");
        dlg.setTitle("提示信息");
        //dlg.initOwner(owner);
        dlg.setOnCloseRequest(e -> {
            dlg.close();
        });
        dlg.initModality(Modality.APPLICATION_MODAL);
        dlg.showAndWait().ifPresent(new Consumer<ButtonType>() {
            @Override
            public void accept(ButtonType buttonType) {
                try {
                    callBack.callback(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return;
    }


    public static void showConfirm(String message, ActionCallBack callBack, Stage stage) {
        Alert dlg = new Alert(Alert.AlertType.CONFIRMATION, message);
        dlg.initOwner(stage);
        dlg.initModality(Modality.APPLICATION_MODAL);
        dlg.setHeaderText("");
        dlg.setTitle("提示信息");
        dlg.initModality(Modality.APPLICATION_MODAL);
        //dlg.initOwner(owner);
        dlg.setOnCloseRequest(e -> {
            dlg.close();
        });
        dlg.showAndWait().ifPresent(new Consumer<ButtonType>() {
            @Override
            public void accept(ButtonType buttonType) {
                if (buttonType.equals(ButtonType.OK)) {
                    try {
                        callBack.callback(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("----------------------buttonType:" + buttonType);
//
            }
        });
        return;
    }

    public static void showCommand(String message, ActionCallBack callBack, List<String> titles, List values, Stage stage) {

        List<CommandLinksDialog.CommandLinksButtonType> links = new ArrayList();
//        List<CommandLinksDialog.CommandLinksButtonType> links = Arrays
//                .asList(new CommandLinksDialog.CommandLinksButtonType(
//                                "Add a network that is in the range of this computer",
//                                "This shows you a list of networks that are currently available and lets you connect to one.", false),
//                        new CommandLinksDialog.CommandLinksButtonType(
//                                "Manually create a network profile",
//                                "This creates a new network profile or locates an existing one and saves it on your computer",
//                                true /*default*/),
//                        new CommandLinksDialog.CommandLinksButtonType("Create an ad hoc network",
//                                "This creates a temporary network for sharing files or and Internet connection", false));
        int i = 0;
        for (String title : titles) {
            boolean isDefault = false;
            if (i == 0) {
                isDefault = true;
            }
            MyCommandLinksButtonType button = new MyCommandLinksButtonType(
                    title, isDefault);
            button.setData(values.get(i));
            links.add(button);
            i++;
        }

        CommandLinksDialog dlg = new CommandLinksDialog(links);
        dlg.initOwner(stage);
        dlg.setOnCloseRequest(e -> {
            dlg.close();
        });
        dlg.setTitle(message);
        dlg.initModality(Modality.APPLICATION_MODAL);
        // String optionalMasthead = "Manually connect to wireless network";
        //   dlg.getDialogPane().setContentText("How do you want to add a network?");
        dlg.showAndWait().ifPresent(new Consumer<ButtonType>() {
            @Override
            public void accept(ButtonType buttonType) {
                System.out.println("-------------------------buttonType:" + buttonType);
                if (buttonType.getButtonData().equals(ButtonType.CLOSE.getButtonData())) {
                    return;
                }
                try {
                    callBack.callback(buttonType.getText());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
}

class MyCommandLinksButtonType<T> extends CommandLinksDialog.CommandLinksButtonType {
    private T data;

    public MyCommandLinksButtonType(String text, boolean isDefault) {
        super(text, isDefault);
    }

    public MyCommandLinksButtonType(String text, String longText, boolean isDefault) {
        super(text, longText, isDefault);
    }

    public MyCommandLinksButtonType(String text, String longText, Node graphic, boolean isDefault) {
        super(text, longText, graphic, isDefault);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}