Example #1
0
  public String interpretElement(XMLElement elem) throws UnableToCompleteException {
    if (!uiWriter.isWidgetElement(elem)) {
      return null;
    }

    FieldManager fieldManager = uiWriter.getFieldManager();
    FieldWriter fieldWriter = fieldManager.require(fieldName);

    // Allocate a local variable to hold the dom id for this widget. Note
    // that idHolder is a local variable reference, not a string id. We
    // have to generate the ids at runtime, not compile time, or else
    // we'll reuse ids for any template rendered more than once.
    String idHolder = uiWriter.declareDomIdHolder(null);
    uiWriter.ensureCurrentFieldAttached();

    FieldWriter childFieldWriter = uiWriter.parseElementToField(elem);
    String elementPointer = idHolder + "Element";
    uiWriter.addInitStatement(
        "Element %s = com.google.gwt.dom.client.Document.get().getElementById(%s);",
        elementPointer, idHolder);

    if (uiWriter.useLazyWidgetBuilders()) {

      // Register a DOM id field.
      String lazyDomElementPath = LazyDomElement.class.getCanonicalName();
      FieldWriter elementWriter = fieldManager.registerField(lazyDomElementPath, elementPointer);
      elementWriter.setInitializer(
          String.format(
              "new %s<Element>(%s)",
              lazyDomElementPath, fieldManager.convertFieldToGetter(idHolder)));

      // Add attach/detach sections for this element.
      fieldWriter.addAttachStatement(
          "%s.get();", fieldManager.convertFieldToGetter(elementPointer));
      fieldWriter.addDetachStatement(
          "%s.addAndReplaceElement(%s, %s.get());",
          fieldName,
          fieldManager.convertFieldToGetter(childFieldWriter.getName()),
          fieldManager.convertFieldToGetter(elementPointer));
    } else {

      // Delay replacing the placeholders with the widgets until after
      // detachment so as not to end up attaching the widget to the DOM
      // unnecessarily
      uiWriter.addDetachStatement(
          "%1$s.addAndReplaceElement(%2$s, %3$s);",
          fieldName, childFieldWriter.getName(), elementPointer);
    }

    // Create an element to hold the widget.
    String tag = getLegalPlaceholderTag(elem);
    idHolder = fieldManager.convertFieldToGetter(idHolder);
    if (uiWriter.useSafeHtmlTemplates()) {
      idHolder = uiWriter.tokenForStringExpression(elem, idHolder);
    } else {
      idHolder = "\" + " + idHolder + " + \"";
    }
    return "<" + tag + " id='" + idHolder + "'></" + tag + ">";
  }
  /**
   * Called by {@link XMLElement#consumeInnerHtml} after all elements have been handed to {@link
   * #interpretElement}.
   */
  @Override
  public String postProcess(String consumed) throws UnableToCompleteException {
    FieldWriter fieldWriter = fieldManager.require(fieldName);

    for (String idHolder : idToWidgetElement.keySet()) {
      XMLElement childElem = idToWidgetElement.get(idHolder);
      FieldWriter childFieldWriter = uiWriter.parseElementToField(childElem);

      genSetWidgetTextCall(idHolder, childFieldWriter.getName());

      if (uiWriter.useLazyWidgetBuilders()) {
        // Register a DOM id field.
        String lazyDomElementPath = LazyDomElement.class.getCanonicalName();
        String elementPointer = idHolder + "Element";
        FieldWriter elementWriter = fieldManager.registerField(lazyDomElementPath, elementPointer);
        elementWriter.setInitializer(
            String.format(
                "new %s<Element>(%s)",
                lazyDomElementPath, fieldManager.convertFieldToGetter(idHolder)));

        // Add attach/detach sections for this element.
        fieldWriter.addAttachStatement(
            "%s.get();", fieldManager.convertFieldToGetter(elementPointer));
        fieldWriter.addDetachStatement(
            "%s.addAndReplaceElement(%s, %s.get());",
            fieldName,
            fieldManager.convertFieldToGetter(childFieldWriter.getName()),
            fieldManager.convertFieldToGetter(elementPointer));
      } else {
        uiWriter.addInitStatement(
            "%1$s.addAndReplaceElement(%2$s, %3$s);",
            fieldName, childFieldWriter.getName(), idHolder);
      }
    }

    /*
     * We get used recursively, so this will be called again. Empty the map
     * or else we'll re-register things.
     */
    idToWidgetElement.clear();
    return super.postProcess(consumed);
  }