gwtXPDownloadsBlog

GWT Map Generator

Posted in GWT on January 5th, 2010 Be the first to comment

When working with GWT History, we usually need a Map for view transitions.

Traditional approach

For example, our EntryPoint class

public class MyEntryPoint implements EntryPoint, ValueChangeHandler{
  ...

  public void onModuleLoad() {
    History.addValueChangeHandler(this);
  }

  public void onValueChange(ValueChangeEvent event) {
    String token = event.getValue();

    if (token != null) {
      Panel panel = null;

      if (token.equals("list")) {
        panel = new ContactsListPanel();
      } else if (token.equals("add")) {
        panel = new EditContactPanel();
      }else if (token.equals("edit")) {
        panel = new EditContactpanel();
      }

      if (panel != null) {
        container.setWidget(panel);
      }
    }
  }
}

Generator

We can archive this by a simple generator

public class MapGenerator extends Generator{
  public String generate(TreeLogger logger, GeneratorContext context,
                         String requestedClass) throws UnableToCompleteException{
    TypeOracle typeOracle = context.getTypeOracle();
    assert(typeOracle != null);

    JClassType baseType = typeOracle.findType(requestedClass);
    if (baseType == null){
      logger.log(TreeLogger.ERROR, "Unable find type " + requestedClass, null);
      throw new UnableToCompleteException();
    }
    String packageName = baseType.getPackage().getName();
    String baseName = baseType.getSimpleSourceName();
    String simpleName = baseName + "Map";
    JClassType[] types = baseType.getSubtypes();

    // Generate the source file
    PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
    ClassSourceFileComposerFactory composer =
      new ClassSourceFileComposerFactory(packageName, simpleName);
    composer.setSuperclass("HashMap");
    composer.addImport("java.util.*");
    SourceWriter out = composer.createSourceWriter(context, printWriter);
    out.println("public " + simpleName + "(){");
    out.indent();
    for (JClassType type : types) {
      String key = type.getSimpleSourceName();
      if (key.endsWith(baseName)){
        key = key.substring(0, key.length() - baseName.length());
      }
      out.println("put(\"" + key + "\", new " + type.getQualifiedSourceName() + "());");
    }
    out.outdent();;
    out.println("}");
    out.commit(logger);

    return packageName + "." + simpleName;
  }
}

The Generator will look for all sub-class of requestedClass, and put an instance of each these classes to a HashMap.

Setting up your module

<module>
  ...
  <generate-with class="com.example.rebind.MapGenerator">
    <when-type-assignable class="com.example.client.MyBasePanel" />
  </generate-with>
</module>

Using the generated Map

public class MyEntryPoint implements EntryPoint, ValueChangeHandler{
  private static final Map<String, MyBasePanel> TOKEN_MAP = GWT.create(MyBasePanel.class);
  ...

  public void onModuleLoad() {
    History.addValueChangeHandler(this);
  }

  public void onValueChange(ValueChangeEvent event) {
    String token = event.getValue();

    if (token != null) {
      Panel panel = TOKEN_MAP.get(token);

      if (panel != null) {
        container.setWidget(panel);
      }
    }
  }
}
Share and Enjoy:

  • Google Bookmarks
  • Twitter
  • Facebook
  • Digg
  • Technorati
  • Live
  • DZone
  • Reddit
  • del.icio.us
  • Mixx
  • Netvibes
  • StumbleUpon
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • email
Leave a Comment »

Leave a Reply



Recent Comments:

  • Trung: There are no Benchmarks now. However, GWT RPC is NOT a generic, language-independent...
  • Jeff Schnitzer: This is very cool! Any metrics on how it compares, performancewise, to Hessian,...
  • Trung: Hi Ed, Thanks for your questions. It suggests me a new feature of SyncProxy. I just...
  • Ed: One more thing. Sorry to keep bothering. Will this work with http sessions?
  • Ed: Awesome! Thanks. I’ll try this today!