GWT确认对话框

时间:2010-07-01 23:33:04

标签: gwt

我正在尝试创建模态确认对话框。我希望它像Window.confirm("")一样工作,我可以在其中调用它,并得到一个布尔响应。

我的麻烦是我不知道怎么做。我正在尝试在我的应用程序中使用MVP。这是我到目前为止的代码:

public class DialogBoxPresenter implements Presenter {

    public interface Display {

        Label getDialogText();

        Button getAffirmativeButton();

        Button getCancelButton();

        Widget asWidget();

        public void center();

        public void hide();

        public void setHeader(String text);
    }
    private Display display;
    private String header;
    private String dialogText;
    private String cancelButtonText;
    private String affirmativeButtonText;

    protected DialogBoxPresenter() {
    }

    public DialogBoxPresenter(Display display, String header, String dialogText, String cancelButtonText, String affirmativeButtonText) {
        this.display = display;
        this.header = header;
        this.dialogText = dialogText;
        this.cancelButtonText = cancelButtonText;
        this.affirmativeButtonText = affirmativeButtonText;

        bind();
    }

    public DialogBoxPresenter(Display display, String header, String dialogText) {
        this.display = display;
        this.header = header;
        this.dialogText = dialogText;
        this.cancelButtonText = "Cancel";
        this.affirmativeButtonText = "OK";

        bind();
    }

    private void bind() {

        this.display.getDialogText().setText(dialogText);
        this.display.getAffirmativeButton().setText(affirmativeButtonText);
        this.display.getCancelButton().setText(cancelButtonText);
        this.display.setHeader(header);

        addClickHandlers();

    }

    private void addClickHandlers() {
        this.display.getAffirmativeButton().addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                doAffirmative();
            }
        });

        this.display.getCancelButton().addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                doCancel();
            }
        });
    }

    private void doAffirmative() {
        //do something
        display.hide();
    }

    private void doCancel() {
        //do something
        display.hide();
    }

    public void init() {
        display.center();
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }
}

和我的观点:

public class DialogBoxView extends DialogBox implements DialogBoxPresenter.Display {

    private Label dialogText;
    private Button affirmativeButton;
    private Button cancelButton;
    private VerticalPanel container;

    public DialogBoxView() {
        //init items
        dialogText = new Label();

        affirmativeButton = new Button();
        cancelButton = new Button();

        container = new VerticalPanel();

        setGlassEnabled(true);
        setAnimationEnabled(true);
        setModal(false);

        init();
    }

    private void init() {
        //add items
        container.add(dialogText);

        HorizontalPanel hp = new HorizontalPanel();
        hp.add(affirmativeButton);
        hp.add(cancelButton);

        container.add(hp);
        this.add(container);
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public Label getDialogText() {
       return dialogText;
    }

    @Override
    public Button getAffirmativeButton() {
        return affirmativeButton;
    }

    @Override
    public Button getCancelButton() {
       return cancelButton;
    }

    @Override
    public void setHeader(String text) {
       this.setText(text);
    }

}

2 个答案:

答案 0 :(得分:19)

您无法以与Window.confirm()完全相同的方式使其工作。问题是网页中的所有javascript都在一个线程中运行。您会注意到,只要标准确认对话框打开,页面的其余部分就会消失。那是因为一个javascript线程被阻止,等待confirm()返回。如果要为对话框创建类似的方法,只要它等待该方法返回,就不会处理用户生成的事件,因此您的对话框将无法工作。我希望这是有道理的。

您可以做的最好的事情类似于GWT库为RPC调用所做的事情 - AsyncCallback接口。您甚至可以自己重用该界面,或者您可能更喜欢自己动手:

public interface DialogCallback {
    void onOk();
    void onCancel();
}

而不是Window.confirm(String),您的方法签名将更像Dialog.confirm(String,DialogCallback)。然后,您的对话框会保留对传入的回调的引用,并且您在代码中// do something的位置调用onOkonCancel

答案 1 :(得分:8)

如果有人好奇的话,这是我工作的代码。

    public class DialogBoxPresenter implements Presenter {

        public interface Display {

            Label getDialogText();

            Button getAffirmativeButton();

            Button getCancelButton();

            Widget asWidget();

            public void center();

            public void hide();

            public void setHeader(String text);
        }
        private Display display;
        private String header;
        private String dialogText;
        private String cancelButtonText;
        private String affirmativeButtonText;
        private ConfirmDialogCallback confirmCallback;
        private AlertDialogCallback alertCallback;

        protected DialogBoxPresenter() {
        }

        public DialogBoxPresenter(Display display, String header, String dialogText, String cancelButtonText, String affirmativeButtonText, ConfirmDialogCallback callback) {
            this.display = display;
            this.header = header;
            this.dialogText = dialogText;
            this.cancelButtonText = cancelButtonText;
            this.affirmativeButtonText = affirmativeButtonText;
            this.confirmCallback = callback;

            bind();
        }

        public DialogBoxPresenter(Display display, String header, String dialogText, String affirmativeButtonText, AlertDialogCallback callback) {
            this.display = display;
            this.header = header;
            this.dialogText = dialogText;
            this.affirmativeButtonText = affirmativeButtonText;
            this.alertCallback = callback;

            this.display.getCancelButton().setVisible(false);

            bind();
        }

        private void bind() {

            this.display.getDialogText().setText(dialogText);
            this.display.getAffirmativeButton().setText(affirmativeButtonText);
            this.display.getCancelButton().setText(cancelButtonText);
            this.display.setHeader(header);

            addClickHandlers();

        }

        private void addClickHandlers() {
            this.display.getAffirmativeButton().addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    doAffirmative();
                }
            });

            this.display.getCancelButton().addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    doCancel();
                }
            });
        }

        private void doAffirmative() {
            if (confirmCallback != null) {
                confirmCallback.onAffirmative();
            } else {
                alertCallback.onAffirmative();
            }
            display.hide();
        }

        private void doCancel() {
            confirmCallback.onCancel();
            display.hide();
        }

        public void init() {
            display.center();
        }

        @Override
        public void go(HasWidgets container) {
            container.clear();
            container.add(display.asWidget());
        }
    }




    public class DialogBoxView extends DialogBox implements DialogBoxPresenter.Display {

        private Label dialogText;
        private Button affirmativeButton;
        private Button cancelButton;
        private VerticalPanel container;

        public DialogBoxView() {
            //init items
            dialogText = new Label();

            affirmativeButton = new Button();
            cancelButton = new Button();

            container = new VerticalPanel();

            setGlassEnabled(true);
            setAnimationEnabled(true);
            setModal(false);

            init();
        }

        private void init() {
            //add items
            container.add(dialogText);

            HorizontalPanel hp = new HorizontalPanel();
            hp.add(affirmativeButton);
            hp.add(cancelButton);

            container.add(hp);
            this.add(container);
        }

        @Override
        public Widget asWidget() {
            return this;
        }

        @Override
        public Label getDialogText() {
           return dialogText;
        }

        @Override
        public Button getAffirmativeButton() {
            return affirmativeButton;
        }

        @Override
        public Button getCancelButton() {
           return cancelButton;
        }

        @Override
        public void setHeader(String text) {
           this.setText(text);
        }

    }


    public class DialogBoxWidget implements LensooConstant {

        private static DialogBoxView view = null;
        private static DialogBoxPresenter presenter = null;

        public static DialogBoxPresenter confirm(String header, String dialogText, String cancelButtonText, String affirmativeButtonText, ConfirmDialogCallback callback) {
            view = new DialogBoxView();
            presenter = new DialogBoxPresenter(view, header, dialogText, cancelButtonText, affirmativeButtonText, callback);

            presenter.init();

            return presenter;
        }

        public static DialogBoxPresenter confirm(String header, String dialogText, ConfirmDialogCallback callback) {
            return DialogBoxWidget.confirm(header, dialogText, constants.cancelButton(), constants.okButton(), callback);
        }

        public static DialogBoxPresenter alert(String header, String dialogText, String affirmativeButtonText, AlertDialogCallback callback) {
            view = new DialogBoxView();
            presenter = new DialogBoxPresenter(view, header, dialogText, affirmativeButtonText, callback);

            presenter.init();

            return presenter;
        }

        public static DialogBoxPresenter alert(String header, String dialogText, AlertDialogCallback callback) {
            return DialogBoxWidget.alert(header, dialogText, constants.okButton(), callback);
        }

        protected DialogBoxWidget() {
        }
    }

public interface AlertDialogCallback {

    void onAffirmative();

}

public interface ConfirmDialogCallback {

    void onAffirmative();

    void onCancel();
}