Matthias Wessendorf’s Weblog

Extending Trinidad’s default renderers

February 20, 2008 · 3 Comments

Once in a while there is a question on the MyFaces user/dev list about how to extend the default rendering of the components, like decorating <tr:inputText /> (for instance).

It’s not hard, but it requires some steps, like creating renderer class(es) and registering these new renderers with the Trinidad renderkit. This demonstrates a very simple example on extending the <tr:inputText />. This tag has a attribute for short descriptions (shortDesc=”blah”), now imagine you need to decorate that inside the renderer, so that the page developer can’t change the extra text (for what ever reason…).

For that you need two Java classes (b/c of extending a <tr:inputXyz/> or <tr:selectXzy />). One is for extending the InputTextRenderer class, which is registered with the Trinidad internal RenderKit. The other one is kind of an internal renderer, that is used to render the component’s content.

The extension of the InputTextRenderer looks like this:

package net.wessendorf.trinidad;

import org.apache.myfaces.trinidad.bean.FacesBean;
import org.apache.myfaces.trinidad.component.core.input.CoreInputText;
import org.apache.myfaces.trinidadinternal.renderkit.core.xhtml.FormInputRenderer;
import org.apache.myfaces.trinidadinternal.renderkit.core.xhtml.InputTextRenderer;

public class CustomInputTextRenderer extends InputTextRenderer
{
  public CustomInputTextRenderer()
  {
    super(CoreInputText.TYPE);
  } 

  protected CustomInputTextRenderer(FacesBean.Type type)
  {
    super(type);
  }

  @Override
  protected FormInputRenderer getFormInputRenderer()
  {
    return new CustomSimpleInputTextRenderer();
  }
}

In Trinidad the *layout* of the <tr:inputXyz/> is done by these renderers (those, that extend the InputLabelAndMessageRenderer class). The custom code isn’t really hard. It is only interesting to see how to create the internal renderer (CustomSimpleInputTextRenderer), which actually renders the content (which we want to decorate):

package net.wessendorf.trinidad;

import javax.faces.context.FacesContext;

import org.apache.myfaces.trinidad.bean.FacesBean;
import org.apache.myfaces.trinidad.component.core.input.CoreInputText;
import org.apache.myfaces.trinidadinternal.renderkit.core.xhtml.SimpleInputTextRenderer;

public class CustomSimpleInputTextRenderer extends SimpleInputTextRenderer
{
  public CustomSimpleInputTextRenderer()
  {
    this(CoreInputText.TYPE);
  }

  public CustomSimpleInputTextRenderer(FacesBean.Type type)
  {
    super(type);
  }
  @Override
  protected String getShortDesc(FacesBean bean)
  {
    return super.getShortDesc (bean) + " what ever you want to decorate";
  }
}

As you see this adds some (bogus) text to the short description of the component. That’s it from the java side. If you really want to change the entire rendering of the <tr:inputText /> your custom “content” renderer (CustomSimpleInputTextRenderer) could also override the protected encodeAllAsElement() method.The last step is to register the new renderer in your project’s faces-config.xml file:

...
<render-kit>
  <render-kit-id>
    org.apache.myfaces.trinidadinternal.core
  </render-kit-id>
  <renderer>
    <component-family>
      org.apache.myfaces.trinidad.Input
    </component-family>
    <renderer-type>
      org.apache.myfaces.trinidad.Text
    </renderer-type>
    <renderer-class>
      net.wessendorf.trinidad.CustomInputTextRenderer
    </renderer-class>
  </renderer>
</render-kit>
...

As you see, you register the CustomInputTextRenderer and not the renderer that renders the content (CustomSimpleInputTextRenderer).Now using the <tr:inputText> in your XHTML page will decorate the short description:

<tr:inputText label="Blah" shortDesc="foo bar" ... />

It is important to note, that these classes extend internal features of Trinidad. None of these classes is part of the Trinidad API. This means that anyone subclassing the renderers has to know their code can very well break any time they move to a newer version. Another example of extending a Trinidad renderer (for <tr:message/>), I posted here.

Categories: apache · java · jsf · myfaces · trinidad