Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT confirmation dialog box

Tags:

gwt

I'm trying to create a modal confirmation dialog box. I'd like it to work like Window.confirm(""), where I can just call it, and get a boolean response.

My trouble is I'm not sure how to do it. I'm trying to use MVP in my application. Here is the code I have so far:

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());
    }
}

and my view:

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);
    }

}
like image 871
KevMo Avatar asked Jul 01 '10 23:07

KevMo


2 Answers

You're not going to be able to have it work in exactly the same way as Window.confirm(). The problem is that all of the javascript in a web page runs in a single thread. You'll notice that as long as a standard confirm dialog is open, the rest of the page goes dead. That's because the one javascript thread is blocked, waiting for confirm() to return. If you were to create a similar method for your dialog, as long as it was waiting for that method to return no user generated events would be processed and so your dialog wouldn't work. I hope that makes sense.

The best you will be able to do is similar to what the GWT library does for RPC calls -- the AsyncCallback interface. You could even reuse that interface yourself, or you might prefer to roll your own:

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

Instead of Window.confirm(String), your method signature will be more like Dialog.confirm(String,DialogCallback). Then your dialog just keeps a reference to the callback that's passed in, and where you have // do something in your code you make calls to onOk and onCancel.

like image 192
dslh Avatar answered Oct 21 '22 19:10

dslh


Here is the code I have working if anyone is curious.

    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();
}
like image 8
KevMo Avatar answered Oct 21 '22 21:10

KevMo