Question

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.

Was it helpful?

Solution

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;

OTHER TIPS

Why override Insert? Why not create your own InsertSpan component? Just look at the source for Insert and you'll see how simple it is ... feel free to cut-and-paste, it's open source.

Better yet, look into upgrading to Tapestry 5; the Tapestry 4 stuff has not been actively developed in about four years.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top