Creating a modal dialog in GWT

I am new to GWT and Java trying to figure out how to create a synchronous (modal) dialog in GWT, but I'm having difficulties. The DialogBox class has a flag that states:

modal - true if keyboard and mouse events for widgets not contained in the dialog should be ignored

But that does not make synchronous dialogue.

I read one sentence that says I need to put code that processes the dialog data entered by the user inside the OK button handler. I don’t like this because it makes the dialogue responsible for displaying and processing the data. This leads to a poor “worry section” and violates the “single responsibility principle” of good design.

I put the code that I want to execute in the OK button handler, and now I want my GWT dialog to be used in two places in my code. In one case, user data from the dialog is added to the table when you click OK. Otherwise, the data in the table changes when you click OK. If I could create a truly synchronous dialog, I could handle the OK button outside the dialog when it returns from execution and it will be easy to reuse the dialog.

If you are forced to use my current asynchronous model, I will need to transfer additional data as described here . This seems like a lot of work for such a common task. Did I miss something? Are there any other options?

+4
source share
3 answers

The modal flag basically makes the dialogue in the focus of the user interface. Thus, nothing can be pressed or interact, except what is in the dialog box.

Unfortunately, as you will know, JavaScript (GWT) is essentially asynchronous - the browser does not execute JS code until the user does something to trigger the event. Therefore, we must respond to events.

As far as I know, GWT does not provide synchronous dialogue. If this were so, it would mean that a code block block is being introduced. Since JavaScript is single-threaded, this would block your application while waiting for busy - this meant that you could not do any meaningful processing in the dialog itself or anywhere else, for that matter.

Here's how I would handle it:

  • Create your own modal dialog class by wrapping the GWT DialogBox dialog box, where I would use the results of the dialog, for example: SAVE / OK / CANCEL.
  • Provide a notification event or callback - onDialogExit, for example, where the result of the dialogue / OK / CANCEL / SAVE / ANYTHING will be provided to the interested party. When the user performs an action (for example, pressing a button) to exit the dialog box, simply raise the correct event. An event can also return if some data should appear in a dialog box.
  • Provide a callback for the event in which you want to process the results of the dialog.

This is very similar to a pointer to a function link that you mentioned - you just write your own event.

+4
source

Open the DialogBox dialog box. You can use one of the constructors to make it modal.

0
source

There is a better way than an asynchronous approach.

boolean okPressed = Window.confirm("Your Message Here"); 

Your application runs until the user clicks "ok" or "cancel" and you can process the return boolean.

Unfortunately, you cannot change the text of the "ok" and "cancel" buttons.

0
source

All Articles