Exemplo n.º 1
0
  public static FormPanel getLoginPanel() {
    // Create a FormPanel and point it at a service.
    final FormPanel form = new FormPanel();

    form.setAction("/loginFormHandler");
    form.setMethod(FormPanel.METHOD_POST);
    // Create a panel to hold all of the form widgets.
    VerticalPanel panel = new VerticalPanel();
    form.setWidget(panel);
    panel.setSpacing(20);
    final Label emailLabel = new Label("UnityID:");
    final TextBox emailBox = new TextBox();
    final Label passwordLabel = new Label("Password:"******"Log in",
            new ClickHandler() {
              public void onClick(ClickEvent event) {
                form.submit();
              }
            });
    panel.add(loginButton);

    Anchor signUpLabel = new Anchor("-- New User: Sign up --");

    signUpLabel.addStyleName("gwt-RegisterAnchor");
    panel.addStyleName("gwt-LoginPanel");
    loginButton.addStyleName("gwt-LoginButton");

    signUpLabel.addClickHandler(
        new ClickHandler() {
          public void onClick(ClickEvent event) {

            RootPanel.get("leftnav").clear();
            RootPanel.get("header").clear();
            RootPanel.get("content").clear();

            RootPanel.get("content").add(CustomWidgets.getRegisterForm());
            final Label titleLabel = new Label("Register");
            titleLabel.addStyleName("gwt-Heading");
            RootPanel.get("header").add(titleLabel);
          }
        });
    panel.add(signUpLabel);

    // Add an event handler to the form.
    form.addSubmitHandler(
        new FormPanel.SubmitHandler() {
          public void onSubmit(FormPanel.SubmitEvent event) {
            if (emailBox.getText().length() == 0 || passwordBox.getText().length() == 0) {
              Window.alert("Username and Password must not be empty");
              rootLogger.log(Level.SEVERE, "LOGIN ATTEMPT FAIL");
              event.cancel();
            }
          }
        });
    form.addSubmitCompleteHandler(
        new FormPanel.SubmitCompleteHandler() {
          public void onSubmitComplete(FormPanel.SubmitCompleteEvent event) {
            final String userid = emailBox.getText();
            String password = passwordBox.getText();

            loginService.login(
                userid,
                password,
                new AsyncCallback<String>() {
                  public void onFailure(Throwable caught) {
                    rootLogger.log(Level.SEVERE, "LOGIN ATTEMPT FAIL");
                  }

                  public void onSuccess(String result) {
                    if (result.equals("success")) {
                      RootPanel.get("leftnav").clear();
                      RootPanel.get("header").clear();
                      RootPanel.get("content").clear();

                      dbService.readUserData(
                          "ssharm20",
                          new AsyncCallback<HashMap<String, String>>() {
                            public void onFailure(Throwable caught) {
                              rootLogger.log(Level.SEVERE, "Failed reading data from server");
                            }

                            public void onSuccess(HashMap<String, String> result) {
                              RootPanel.get("content").add(CustomWidgets.getHomeDataForm(userid));
                            }
                          });

                      RootPanel.get("leftnav").add(CustomWidgets.getNavBar(userid));
                      final Label titleLabel = new Label("Home");
                      titleLabel.addStyleName("gwt-Heading");
                      RootPanel.get("header").add(titleLabel);
                    } else {
                      Window.alert(result);
                      rootLogger.log(Level.SEVERE, "LOGIN ATTEMPT FAIL");
                    }
                  }
                });
          }
        });
    return form;
  }