David
David

Reputation: 109

GWT DialogBox that returns values back to the "opener"

I want to create a popup (implemented as a DialogBox or other similar component) which i should be able to reuse in multiple pages or forms. I want the DialogBox to be able to return a value to the "opener".

I am thinking i.e. on a DialogBox that shows a table (obtained via RPC). That DialogBox can be used in several different pages. When the user selects a row, an object is "passed back to the page" (for example, calling a method on it), so it can write it to a form field, or do whatever with it. The called doesn't know anything about the logic inside de DialogBox, only knows how to deal with the returning type.

A good example of what i'm intending to do could be a DatePicker that returns a java.util.Date.

Have you done something similiar? I appreciate your help. Thanks! David

Upvotes: 2

Views: 3045

Answers (1)

Florent Bayle
Florent Bayle

Reputation: 11930

It's really easy. You should first create an interface that will be implemented by all the pages opening you DialogBox :

public interface DialogBoxOpener {
    void dialogBoxValidated (Date selectedDate);
    void dialogBoxCancelled ();
}

Then, create your DialogBox, and take a DialogBoxOpener as parameter to your showDialogBox method :

public class MyDialogBox extends DialogBox {
    private DialogBoxOpener opener = null;
    private final Button cancelButton = new Button("Cancel");
    private final Button validButton = new Button("Ok");
    private final DateBox myDateBox = new DateBox();

    public MyDialogBox () {
            cancelButton.addClickHandler(new ClickHandler () {
                    @Override
                    public void onClick(final ClickEvent event) {
                            hide();
                            if (opener!=null)
                                    opener.dialogBoxCancelled();
                    }
            });

            validButton.addClickHandler(new ClickHandler () {
                    @Override
                    public void onClick(final ClickEvent event) {
                            hide();
                            if (opener!=null)
                                    opener.dialogBoxValidated(myDateBox.getValue());
                    }
            });
            // TODO : create your DialogBox
    }

    public void showDialogBox (final DialogBoxOpener opener) {
            this.opener = opener;
            // Show the DialogBox
            center ();
    }
}

And now, you can show your DialogBox from your page :

public class MyPage implements DialogBoxOpener {
    private MyDialogBox myDialogBox = getMyDialogBox();

    private void openDialogBox () {
            myDialogBox.showDialogBox(this);
    }

    public void dialogBoxValidated (Date selectedDate) {
            // TODO : Do something with the date
    }

    public void  dialogBoxCancelled () {
            // TODO : Do something
    }
}

Upvotes: 8

Related Questions