Class ElementBuilderFactory

java.lang.Object
com.google.gwt.dom.builder.shared.ElementBuilderFactory
Direct Known Subclasses:
DomBuilderFactory, HtmlBuilderFactory

public abstract class ElementBuilderFactory extends Object
Factory for creating element builders.

Use get() to fetch the builder factory optimized for the browser platform.

If you are using the builder on a server, use HtmlBuilderFactory.get() instead. HtmlBuilderFactory can construct a SafeHtml string and will work on the server. Other implementations may only work on a browser client.

Element builder methods can be chained together as with a traditional builder:

 DivBuilder divBuilder = ElementBuilderFactory.get().createDivBuilder();
 divBuilder.id("myId").text("Hello World!").endDiv();
 

See this example:

public class ElementBuilderFactoryChainingExample implements EntryPoint {

  @Override
  public void onModuleLoad() {
    /*
     * Create a builder for the outermost element. The initial state of the
     * builder is a started element ready for attributes (eg. "<div").
     */
    DivBuilder divBuilder = ElementBuilderFactory.get().createDivBuilder();

    /*
     * Build the element.
     * 
     * First, we set the element's id to "myId", then set its title to
     * "This is a div". Next, we set the background-color style property to
     * "red". Finally, we set some inner text to "Hello World!". When we are
     * finished, we end the div.
     * 
     * When building elements, the order of methods matters. Attributes and
     * style properties must be added before setting inner html/text or
     * appending children. This is because the string implementation cannot
     * concatenate an attribute after child content has been added.
     * 
     * Note that endStyle() takes the builder type that we want to return, which
     * must be the "parent" builder. endDiv() does not need the optional
     * argument because we are finished building the element.
     */
    divBuilder.id("myId").title("This is a div");
    divBuilder.style().trustedBackgroundColor("red").endStyle();
    divBuilder.text("Hello World!").endDiv();

    // Get the element out of the builder.
    Element div = divBuilder.finish();

    // Attach the element to the page.
    Document.get().getBody().appendChild(div);
  }
}
.

Alternatively, builders can be used as separate objects and operated on individually. This may be the preferred method if you are creating a complex or dynamic element. The code below produces the same output as the code above.

 DivBuilder divBuilder = ElementBuilderFactory.get().createDivBuilder();
 divBuilder.id("myId");
 divBuilder.text("Hello World!");
 divBuilder.endDiv();
 

See an example:

public class ElementBuilderFactoryNonChainingExample implements EntryPoint {

  @Override
  public void onModuleLoad() {
    /*
     * Create a builder for the outermost element. The initial state of the
     * builder is a started element ready for attributes (eg. "<div").
     */
    DivBuilder divBuilder = ElementBuilderFactory.get().createDivBuilder();

    // Add attributes to the div.
    divBuilder.id("myId");
    divBuilder.title("This is a div");

    // Add style properties to the div.
    StylesBuilder divStyle = divBuilder.style();
    divStyle.trustedBackgroundColor("red");
    divStyle.endStyle();

    // Append a child select element to the div.
    SelectBuilder selectBuilder = divBuilder.startSelect();

    // Append three options to the select element.
    for (int i = 0; i < 3; i++) {
      OptionBuilder optionBuilder = selectBuilder.startOption();
      optionBuilder.value("value" + i);
      optionBuilder.text("Option " + i);
      optionBuilder.endOption();
    }

    /*
     * End the select and div elements. Note that ending the remaining elements
     * before calling asElement() below is optional, but a good practice. If we
     * did not call endOption() above, we would append each option element to
     * the preceding option element, which is not what we want.
     * 
     * In general, you must pay close attention to ensure that you close
     * elements correctly.
     */
    selectBuilder.endSelect();
    divBuilder.endDiv();

    // Get the element out of the builder.
    Element div = divBuilder.finish();

    // Attach the element to the page.
    Document.get().getBody().appendChild(div);
  }
}
.

You can also mix chaining and non-chaining methods when appropriate. For example, you can add attributes to an element by chaining methods, but use a separate builder object for each separate element.

NOTE: Builders always operate on the current element. For example, in the code below, we create two divBuilders, one a child of the other. However, they are actually the same builder instance! Implementations of ElementBuilderFactory use a single instance of each builder type to improve performance. The implication is that all element builders operate on the current element, so the call to divBuilder0.id("div1") will set the "id" of the child div, and is functionally equivalent to divBuilder1.id("div1"). Its important to always call end() before resuming work on the previous element builder.

 DivBuilder divBuilder0 = ElementBuilderFactory.get().createDivBuilder();
 DivBuilder divBuilder1 = divBuilder0.startDiv();
 divBuilder0.id("div1"); // Operates on the first element!
 
  • Constructor Details

    • ElementBuilderFactory

      protected ElementBuilderFactory()
      Created from static factory method.
  • Method Details

    • get

      public static ElementBuilderFactory get()
      Get the instance of the ElementBuilderFactory.
      Returns:
      the ElementBuilderFactory
    • createAnchorBuilder

      public abstract AnchorBuilder createAnchorBuilder()
    • createAreaBuilder

      public abstract AreaBuilder createAreaBuilder()
    • createAudioBuilder

      public abstract AudioBuilder createAudioBuilder()
    • createBaseBuilder

      public abstract BaseBuilder createBaseBuilder()
    • createBlockQuoteBuilder

      public abstract QuoteBuilder createBlockQuoteBuilder()
    • createBodyBuilder

      public abstract BodyBuilder createBodyBuilder()
    • createBRBuilder

      public abstract BRBuilder createBRBuilder()
    • createButtonInputBuilder

      public abstract InputBuilder createButtonInputBuilder()
    • createCanvasBuilder

      public abstract CanvasBuilder createCanvasBuilder()
    • createCheckboxInputBuilder

      public abstract InputBuilder createCheckboxInputBuilder()
    • createColBuilder

      public abstract TableColBuilder createColBuilder()
    • createColGroupBuilder

      public abstract TableColBuilder createColGroupBuilder()
    • createDivBuilder

      public abstract DivBuilder createDivBuilder()
    • createDListBuilder

      public abstract DListBuilder createDListBuilder()
    • createFieldSetBuilder

      public abstract FieldSetBuilder createFieldSetBuilder()
    • createFileInputBuilder

      public abstract InputBuilder createFileInputBuilder()
    • createFormBuilder

      public abstract FormBuilder createFormBuilder()
    • createFrameBuilder

      public abstract FrameBuilder createFrameBuilder()
    • createFrameSetBuilder

      public abstract FrameSetBuilder createFrameSetBuilder()
    • createH1Builder

      public abstract HeadingBuilder createH1Builder()
    • createH2Builder

      public abstract HeadingBuilder createH2Builder()
    • createH3Builder

      public abstract HeadingBuilder createH3Builder()
    • createH4Builder

      public abstract HeadingBuilder createH4Builder()
    • createH5Builder

      public abstract HeadingBuilder createH5Builder()
    • createH6Builder

      public abstract HeadingBuilder createH6Builder()
    • createHeadBuilder

      public abstract HeadBuilder createHeadBuilder()
    • createHiddenInputBuilder

      public abstract InputBuilder createHiddenInputBuilder()
    • createHRBuilder

      public abstract HRBuilder createHRBuilder()
    • createIFrameBuilder

      public abstract IFrameBuilder createIFrameBuilder()
    • createImageBuilder

      public abstract ImageBuilder createImageBuilder()
    • createImageInputBuilder

      public abstract InputBuilder createImageInputBuilder()
    • createLabelBuilder

      public abstract LabelBuilder createLabelBuilder()
    • createLegendBuilder

      public abstract LegendBuilder createLegendBuilder()
    • createLIBuilder

      public abstract LIBuilder createLIBuilder()
    • createLinkBuilder

      public abstract LinkBuilder createLinkBuilder()
    • createMapBuilder

      public abstract MapBuilder createMapBuilder()
    • createMetaBuilder

      public abstract MetaBuilder createMetaBuilder()
    • createOListBuilder

      public abstract OListBuilder createOListBuilder()
    • createOptGroupBuilder

      public abstract OptGroupBuilder createOptGroupBuilder()
    • createOptionBuilder

      public abstract OptionBuilder createOptionBuilder()
    • createParagraphBuilder

      public abstract ParagraphBuilder createParagraphBuilder()
    • createParamBuilder

      public abstract ParamBuilder createParamBuilder()
    • createPasswordInputBuilder

      public abstract InputBuilder createPasswordInputBuilder()
    • createPreBuilder

      public abstract PreBuilder createPreBuilder()
    • createPushButtonBuilder

      public abstract ButtonBuilder createPushButtonBuilder()
    • createQuoteBuilder

      public abstract QuoteBuilder createQuoteBuilder()
    • createRadioInputBuilder

      public abstract InputBuilder createRadioInputBuilder(String name)
      Create a builder for an <input type='radio'> element.
      Parameters:
      name - name the name of the radio input (used for grouping)
      Returns:
      the builder for the new element
    • createResetButtonBuilder

      public abstract ButtonBuilder createResetButtonBuilder()
    • createResetInputBuilder

      public abstract InputBuilder createResetInputBuilder()
    • createScriptBuilder

      public abstract ScriptBuilder createScriptBuilder()
    • createSelectBuilder

      public abstract SelectBuilder createSelectBuilder()
    • createSourceBuilder

      public abstract SourceBuilder createSourceBuilder()
    • createSpanBuilder

      public abstract SpanBuilder createSpanBuilder()
    • createStyleBuilder

      public abstract StyleBuilder createStyleBuilder()
    • createSubmitButtonBuilder

      public abstract ButtonBuilder createSubmitButtonBuilder()
    • createSubmitInputBuilder

      public abstract InputBuilder createSubmitInputBuilder()
    • createTableBuilder

      public abstract TableBuilder createTableBuilder()
    • createTableCaptionBuilder

      public abstract TableCaptionBuilder createTableCaptionBuilder()
    • createTBodyBuilder

      public abstract TableSectionBuilder createTBodyBuilder()
    • createTDBuilder

      public abstract TableCellBuilder createTDBuilder()
    • createTextAreaBuilder

      public abstract TextAreaBuilder createTextAreaBuilder()
    • createTextInputBuilder

      public abstract InputBuilder createTextInputBuilder()
    • createTFootBuilder

      public abstract TableSectionBuilder createTFootBuilder()
    • createTHBuilder

      public abstract TableCellBuilder createTHBuilder()
    • createTHeadBuilder

      public abstract TableSectionBuilder createTHeadBuilder()
    • createTRBuilder

      public abstract TableRowBuilder createTRBuilder()
    • createUListBuilder

      public abstract UListBuilder createUListBuilder()
    • createVideoBuilder

      public abstract VideoBuilder createVideoBuilder()
    • trustedCreate

      public abstract ElementBuilder trustedCreate(String tagName)
      Create an ElementBuilder for an arbitrary tag name. The tag name will will not be checked or escaped. The calling code should be carefully reviewed to ensure that the provided tag name will not cause a security issue if including in an HTML document. In general, this means limiting the code to HTML tagName constants supported by the HTML specification.
      Parameters:
      tagName - the tag name of the new element
      Returns:
      an ElementBuilder used to build the element