tags:

views:

92

answers:

1

I don't have much Tapestry experience so I don't really know where to start.

I need to extend the Insert component with a new component, say NewInsert, that applies a given CSS class to what is being inserted. How should I do this?

I basically want to end up with something that generates something like <span class="myClass">The value</span>.

Why do it by extending Insert? Because the application is pretty much done but we realized that everywhere we use Insert we need this CSS class. We'll just do a global replace on 'type="Insert">' with 'type="NewInsert">' in all files.

A: 

To achieve what I wanted I had to override Insert's renderComponent method. This is only because Tapestry 4.0.2 does not have a setStyleClass method. It looked basically like

    if (!cycle.isRewinding()) {
      Object value = getValue();

      if (value != null) {
        String styleClass;
        String insert = null;
        Format format = getFormat();

        if (format == null) {
          insert = value.toString();
        }
        else {
          insert = format.format(value);
        }

        styleClass = getStyleClass();

        if (styleClass == null) {
          /* No classes specified */
          styleClass = MY_CLASS;
        }
        else {
          /* Append the preserveWhiteSpace class to the string listing the style classes. */
          styleClass += " " + MY_CLASS;
        }

        if (styleClass != null) {
          writer.begin("span");
          writer.attribute("class", styleClass);

          renderInformalParameters(writer, cycle);
        }

        writer.print(insert, getRaw());

        if (styleClass != null) {
          /* </span> */
          writer.end();
        }
      }
    }
  }

If we have a setStyleClass method we could have just done

setStyleClass(MY_CLASS);
super.renderComponent;
ntownsend