Search code examples
gwtuibinder

Generating Cells with UiBinder


GWT provides the following as an example of building cells for a CellList:

  /**
   * A simple data type that represents a contact.
   */
  private static class Contact {
    private static int nextId = 0;

    private final int id;
    private String name;

    public Contact(String name) {
      nextId++;
      this.id = nextId;
      this.name = name;
    }
  }

  /**
   * A custom {@link Cell} used to render a {@link Contact}.
   */
  private static class ContactCell extends AbstractCell<Contact> {
    @Override
    public void render(Context context, Contact value, SafeHtmlBuilder sb) {
      if (value != null) {
        sb.appendEscaped(value.name);
      }
    }
  }

If I have a complex cell, simply returning a safe HTML string from render() becomes tedious. Is there a way to use UiBinder for this, or anything better than building the HTML string by hand?


Solution

  • GWT 2.5 will add UiRenderer exactly for that purpose: leveraging an *.ui.xml template to build a renderer, with template variables, ability to get a handle on a subelement when renderered, etc.

    Pending GWT 2.5, you can use SafeHtmlTemplates, splitting your template into separate methods and then compositing them to build your cell's content.