Search code examples
javajavascriptsmartgwt

Prevent User From leaving unsaved forms with smartgwt


Right now I'm working in a big smartgwt project. I need to implement a nice feature but I have no idea how to do it.

Our application has a lots of forms located in different tabs and grids and so on. Each form has his own save button and when it gets pressed it performs some few actions before the save action. For the application, it is really important to only save the form when the user presses the button. What I need is some kind of handler to prevent a user leaving unsaved forms. My intention is to show some kind of warning popup but I have no idea how to bind the action.

I don't know even if it's possible to do something like that with smartgwt but it seems to me a very typical feature to have in a big web application.

Some ideas? Someone had this problem before?


Solution

  • You can intercept almost every user action in GWT. If you want for example to prevent user from leaving page you can use something like this:

    Window.addWindowClosingHandler(new Window.ClosingHandler() {
        @Override
        public void onWindowClosing(ClosingEvent event) {
            if (unsavedData) {
                event.setMessage("There is unsaved data. Do you really want to leave?");
            }
        }
    });
    

    If you want to stop user on different action like closing tab you can also do that by adding proper event listener.

    Determining whether there is some unsaved data is also fairly simple but depends on UI component. For example in listGrid you can use listGrid.getAllEditRows().lenght In form you can compare form.getOldValues() with form.getValues() (Guava library will be perfect for this task: Maps.difference(form.getOldValues(), form.getValues()).areEqual()) Maybe there is better way to check unsaved data in form but I don't know it.

    And last problem - linking it all together. I created custom save controller with multiple save participants. With this approach you can ask controller.isThereAnyUnsavedData() and controller should ask each participant.