Sunday, February 14, 2010

GWT and UIBinder Password Asteriser/Fader

This tutorial shows how to fade the password into asterisks when the user mouses out of the password field. Instead of using a password input type, we would use a text input type with the asterisation/deasterisation performed algorithmically on Mouseout and Mouseover events.

Some user authentication techniques has a [Show Password] checkbox, which when checked would show the password you are keying in rather than hiding it. After you have keyed in your password and visually verified its correctness, if you are really diligent, you might uncheck it to asterise it (turn it back into a string of asterisks/stars).

However, isn't it better if the password field asterises itself when the user moves focus away from the password field. Here is how you could do it, using GWT.

If you don't want to use GWT, you could translate the asteriser routine documented here into javascript.

The tutorial is found at Google code svn
under the namespace

The UIBinder pwfade.ui.xml
<!DOCTYPE ui:UiBinder SYSTEM "">
  <g:VerticalPanel horizontalAlignment="ALIGN_RIGHT" ui:field="allFields">
    <g:Label>Enter Name and Password, then click [Log in]</g:Label>
     columnCount='2' rowCount='3'>
      <g:TextBox ui:field="name" width="15em"/>
      <g:TextBox ui:field="password" width="15em"/>
      <g:Button ui:field="logIn" text="Log in"/>

The UIBinder bean as well as the module entry point
public class PasswordAsteriser
  implements EntryPoint

  private static PasswordAsteriserUiBinder uiBinder =

  interface PasswordAsteriserUiBinder
    extends UiBinder<Widget, PasswordAsteriser>

  VerticalPanel allFields;
  TextBox name, password;

  private String passwordString;

Here is where the asterising/deasterising action is
//on Mouseout asterise the field and deasterise on Mouseover
  @UiHandler ("password")
  public void asteriseOnMouseOut(MouseOutEvent event){
    this.passwordString = password.getText();
    String masked = "*****";
    for(int i=5; i<this.passwordString.length(); i++)
      masked += '*';
  @UiHandler ("password")
  public void deasteriseOnMouseOver(MouseOverEvent event){

  void onClick(
    ClickEvent e){
    Window.alert("Name:"+name.getText() + "\nPassword:" + this.passwordString);

  public void onModuleLoad(){

Unfortunately, UIBinder is not too mature currently to have a well provisioned layout policy. You have to extend Grid class to provide add(Widget) method for the benefit of the ui.xml.

Here, UIGrid extends Grid. Note that the constructor for UIGrid is annotated with @UiConstructor so that the arguments rowCount and columnCount are associated with UIGrid's ui xml attributes of the same name rowCount and columnCount.

  static public class UIGrid
    extends Grid
    public @UiConstructor UIGrid(
      int rowCount,
      int columnCount)
      this.resize(rowCount, columnCount);
    public void setRowCount(int n){
      this.numRows = n;
    public void setColumnCount(int n){
      this.numColumns = n;
    public void add(Widget w){
      int row = this.count/this.numColumns;
      int col = this.count - row*this.numColumns ;
      if (this.numRows<row)
      this.setWidget(row, col, w);

    public void add(String t){
      int row = this.count/this.numColumns;
      int col = this.count - row*this.numColumns;
      this.setText(row, col, t);
    protected int count=0;

The layout classes in UIBinder (like Grid and Flextable) should also be further enhanced to work directly with the ui.xml and not require us having to create extension classes. The very idea of using UIBinder is to reduce coding not increase it.

Here is the GWT launching html.
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link type="text/css" rel="stylesheet" href="pwfade.css">
    <title>Password Asteriser Example</title>
    <script type="text/javascript" language="javascript" src="pwfade.nocache.js"></script>
    <h1>Password Asteriser Example</h1>
    <div id="allFields"/>