Search code examples
eventsgwtevent-handlinguibinder

GWT - Handling events from underlying widgets in EntryPoint


I have searched the web for the correct answer, but I've been failing to achieve this :

In EntryPoint class, I need to manage widgets according to events that occur in nested widgets. I've cleaned of the code to focus only on what is important here.

I have built a few UiBinder widgets, for example, a Login pane where the user can enter his credentials. In my EntryPoint class, I add the widgets in the correct position.

// This is from EntryPoint class

public void onModuleLoad() {            
        LoginPane lp = new LoginPane();
        RootPanel.get("headerRightPane").add(lp);
        lp.setFocus();

        // Other widgets added in same manner after this point...
}

I would like a successful login to remove the LoginPane and replace it by another widget (AccountPane) that would show the account information for the user that is logged in. I have an onClick event, in LoginPane, that sends a request to a fully functional Servlet that checks the credentials. At this exact point, if the Servlet determines that the login is indeed successful, I would like to fire a "successfulLogin" event (from LoginPane) that could notify the EntryPoint class that the LoginPane can now be replaced by the AccountPane.

    // This is from LoginPane class

    @UiHandler("loginButton")
    void onClick(ClickEvent e) {
        checkCredentials(usernameField.getText(), passwordField.getText());
    }

    public void checkCredentials(String username, String password) {
    String usernameToServer = username;
    String passwordToServer = password;

    credentialsService.credentialsServer(usernameToServer, passwordToServer,
            new AsyncCallback<CredentialsPaneContent>() {
                public void onFailure(Throwable caught) {
                    answerLabel.setText(Utilities.SERVER_ERROR);
                }

                public void onSuccess(CredentialsPaneContent result) {
                    if ( result == null ) {
                        answerLabel.setText("Login Failed.");
                    } else {
                        // Fire event here (to be caught by EntryPoint class)
                        answerLabel.setText("Login Successful.");
                    }
                }
            });
}

So, the question : How should I proceed to create, fire and listen to the event from my nested widget?


Solution

  • Use an EventBus. Additionally, consider adopting the Model-View-Presenter pattern to keep your application maintainable as it grows: