O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The TreeRenderer Class

The com.mycompany.jsf.renderer.TreeRenderer class is similar to the renderers we’ve looked at earlier:

package com.mycompany.jsf.renderer;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.render.Renderer;

import com.mycompany.jsf.component.UITree;
import com.mycompany.jsf.model.TreeNode;

public class TreeRenderer extends Renderer {

    public boolean getRendersChildren( ) {
        return true;
    }

    public void encodeChildren(FacesContext context, UIComponent component)
        throws IOException {

        if (!component.isRendered( )) {
            return;
        }

        if (((UITree) component).getValue( ) == null) {
            return;
        }
        
        ResponseWriter out = context.getResponseWriter( );
        String clientId = null;
        if (component.getId( ) != null && 
            !component.getId( ).startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) {
            clientId = component.getClientId(context);
        }

        boolean isOuterSpanUsed = false;
        if (clientId != null) {
            isOuterSpanUsed = true;
            out.startElement("span", component);
            out.writeAttribute("id", clientId, "id");
        }
        encodeNodes(context, out, (UITree) component, null, 0);
        ((UITree) component).setNodeId(null);
        if (isOuterSpanUsed) {
            out.endElement("span");
        }
    }

It extends the Renderer class and returns true from the getRendersChildren( ) method because it controls the rendering ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required