Generating the HTML Content

As before, the DOM Core API is used to create the HTML content and the content to be rendered is appended to the DocumentFragment passed to the render method.

Figure 1. Marking Up the Input Control
Element input = fragment.getOwnerDocument()
        .createElement("input");
    fragment.appendChild(input);

    input.setAttribute("type", "text");
    input.setAttribute("autocomplete", "no");
    input.setAttribute("id", targetID);
    input.setAttribute("name", targetID);

    if (title != null && title.length() > 0) {
      input.setAttribute("title", title);
    }

    if (value != null && value.length() > 0) {
      input.setAttribute("value", value);
    }

The first statement creates the HTML input element. The input element is then added to the document fragment. The required attributes are then set on the element. Note that both the id and the name attributes are defined and assigned the same target ID value; this ensures compatibility with most web browsers. The title and value attributes are only set if they are not null and not empty strings.

There are several other features of fields in UIM that the renderer must support. The code required to implement the basic features is shown below.

Figure 2. Supporting Other UIM Features
if ("true".equals(field.getParameters()
                        .get(FieldParameters.INITIAL_FOCUS))) {
      input.setAttribute("tabindex", "1");
    }

    String width
        = field.getParameters().get(FieldParameters.WIDTH);
    if (width != null && width.length() > 0
        && !"0".equals(width)) {
      String units;
      if ("CHARS".equals(field.getParameters()
                           .get(FieldParameters.WIDTH_UNITS))) {
        units = "em";
      } else {
        units = "%";
      }
      input.setAttribute("style", "width:" + width + units + ";");
    }

    setScriptAttributes(input, field);

When a form page is first shown, the input focus is normally given to the first input control on that page. However, if the INITIAL_FOCUS attribute is set to true on a UIM FIELD element other than the first one, the input focus should be given to that field instead. If not specified, the INITIAL_FOCUS attribute is assumed to be set to false.

Support for this feature can be achieved by setting the tabindex attribute of the HTML input element to 1 if the field object's INITIAL_FOCUS parameter is set to "true" (as it reflects the value defined for the corresponding attribute in UIM). The parameter value may be null, but calling the equals method on the literal string value is still safe in that case and yields the desired result.

The width of an input control is set by combining the WIDTH parameter value with the WIDTH_UNITS parameter value. Both values are optional and may be null. If the WIDTH parameter is null, is empty, or is explicitly set to zero, then the width is not set on the input control. If the WIDTH_UNITS parameter is null or not recognized, then "PERCENT" is assumed. The width is set using the style attribute of the input element.

UIM FIELD elements support child SCRIPT elements that define JavaScript handlers to be associated with the rendered HTML content. The SCRIPT elements are transposed into further parameter values on the Field object passed to the renderer. For example, this UIM SCRIPT element will be represented as a parameter named ONCLICK_ACTION with a value set to the value of the ACTION attribute in the UIM:

<SCRIPT EVENT="ONCLICK" ACTION="doSomething();"/>

There can be many different scripts for different events. A helper method provided by the abstract base class can set all of the appropriate event attributes on a HTML element for these scripts. Simply call setScriptAttributes passing the HTML element to which to add any required event attributes and the Field object on which the parameters record the necessary information.