Use deferred Command : DeferredCommand « GWT « Java






Use deferred Command


package com.java2s.gwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;

public class GWTClient implements EntryPoint,HistoryListener {
  private static final String LOGIN_STATE = "login";
  private static final String WELCOME_STATE = "welcome";

  public void onModuleLoad() {
    setupHistory();
  }
  private void setupHistory() {
    History.addHistoryListener(this);
    History.onHistoryChanged(LOGIN_STATE);
  }
  public void onHistoryChanged(String historyToken) {
    if (LOGIN_STATE.equals(historyToken)) {
      loadLoginView();
    }
    else
      if (WELCOME_STATE.equals(historyToken)) {
        loadWelcomeView();
      }
  }

  private void loadLoginView() {
    final Label loginPrompt = new Label("Login");
    final Grid grid = new Grid(3, 2);
    final Label namePrompt = new Label("name");
    final TextBox nameTextbox = new TextBox();
    final Label passwordPrompt = new Label("password");
    final PasswordTextBox passwordTextbox = new PasswordTextBox();
    final Button button = new Button("Login");

    button.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        History.newItem(WELCOME_STATE);
      }
    });

    loginPrompt.addStyleName("loginPrompt");
    nameTextbox.addStyleName("nameField");
    passwordTextbox.addStyleName("passwordField");

    DeferredCommand.add(new Command() {
      public void execute() {
        nameTextbox.setFocus(true);
      }
    });
    
    grid.setWidget(0, 0, namePrompt);
    grid.setWidget(0, 1, nameTextbox);

    grid.setWidget(1, 0, passwordPrompt);
    grid.setWidget(1, 1, passwordTextbox);

    grid.setWidget(2, 1, button);
    
    RootPanel.get().clear();
    RootPanel.get().add(loginPrompt);
    RootPanel.get().add(grid);
  }

  private void loadWelcomeView() {
    final Label welcomeMsg = new Label("welcome");

    welcomeMsg.addStyleName("welcomeMsg");

    RootPanel.get().clear();
    RootPanel.get().add(welcomeMsg);
    RootPanel.get().add(new Hyperlink("logoutLinkText",LOGIN_STATE));
  }
}



           
       








GWT-deferredCommand.zip( 3 k)

Related examples in the same category