开发者

Automatic Window.onClosing handling in GWT which checks user input

开发者 https://www.devze.com 2023-04-02 15:06 出处:网络
I am currently writing an instant messenger with the GWT. Now i want to implement a functionality to autologout the user when he closes the window and i found this small code snippet:

I am currently writing an instant messenger with the GWT. Now i want to implement a functionality to autologout the user when he closes the window and i found this small code snippet:

Window.addWindowClosingHandler(new Window.ClosingHandler() {
    @Override
    public void onWindowClosing(ClosingEvent event) {
        event.setM开发者_如何学Pythonessage("If you leave this page, your IM sessions will not be preserved!");
        // My async code to be executed
    }
});

This snippet is very nice as a modal dialog opens to ask whether to leave the page, however i can not check which options the user has chosen and therefore the user will be always logged out for now! Can anyone explain me how to catch the users input?

Here is an example:

  • User logs in to the IM
  • He chats
  • When the users closes the window (or tries) the modal dialog opens whether he wants to close the window or stay on the page (which works automatically due to GWT implementation)
    • if the user has chosen to leave the IM my logout code should be executed
    • otherwise nothing happens as he wants to stay on the page


I don't think you can get the chosen option (but I don't know it for sure).

An additional CloseHandler could workaround for your purpose:

    Window.addWindowClosingHandler(new Window.ClosingHandler() {
        @Override
        public void onWindowClosing(ClosingEvent event) {
            event.setMessage("If you leave this page, your IM sessions will not be preserved!");
        }
    });

    Window.addCloseHandler(new CloseHandler<Window>() {
        @Override
        public void onClose(CloseEvent<Window> event) {
            // My async code to be executed
        }
    });

This should implement the bahaviour you described.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号