tutorial.jsf.component.ajaxtree
Class AjaxTreeRenderer

java.lang.Object
  extended by javax.faces.render.Renderer
      extended by tutorial.jsf.component.ajaxtree.AjaxTreeRenderer
All Implemented Interfaces:
AjaxComponentRenderer

public class AjaxTreeRenderer
extends javax.faces.render.Renderer
implements AjaxComponentRenderer

Created: Nov 3, 2007
Title: AjaxComponentRenderer
Description:
Copyright: Copyright (c) 2008

Author:
Matt Tyson

Renderer for AjaxTree.

Two custom html attributes are used to send information. Each treeNode has a div that contains all its data and this div includes a nodeString="" and leaf="TRUE|FALSE" attributes. This could be done with hidden fields to stay within strict W3C. May add a flag on the tag allowing user to decide between the two: performance or compliance.

Originally, XML was used as the transport for the nodes, but now JSON is used to reduce size. The escapeJSON() method is taken from http://www.JSON.org/java/json_simple.zip.


Field Summary
static java.lang.String DEFAULT_FACET
          The default facet name that will be used if no facet is found for a node
protected static java.lang.String HTML_CHILD_WRAPPER_CLASS
           
protected static java.lang.String HTML_CLICK_AREA_CLASS
           
protected static java.lang.String HTML_NODE_CLOSED
           
protected static java.lang.String HTML_NODE_OPENED
           
protected static java.lang.String HTML_NODE_STRING_NAME
           
protected static java.lang.String HTML_ROOT_CLOSED
           
protected static java.lang.String HTML_ROOT_LEAF
           
protected static java.lang.String HTML_SERVER_URL_CLASS
           
protected static java.lang.String HTML_TREE_NODE_CLASS
           
protected static java.lang.String HTML_USER_DATA_CLASS
           
 
Constructor Summary
AjaxTreeRenderer()
          default empty constructor
 
Method Summary
 void encodeBegin(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component)
          Writes out the root nodes and required resources.
static java.lang.String escapeJSON(java.lang.String s)
          This method is taken from http://www.JSON.org/java/json_simple.zip, by Fang Yidong.
protected  javax.faces.component.UIComponent findFacet(AjaxTree tree, AjaxTreeNode node)
          Gets the facet for a given node.
protected  java.lang.String getErrorString(java.lang.String msg)
           
protected  java.lang.String getFacetOutput(javax.faces.context.FacesContext context, AjaxTree tree, AjaxTreeNode node)
          Takes a tree and a node and uses the facet configured for that node type to generate an HTML string.
 boolean getRendersChildren()
          AjaxTree does render children
 void handleAjaxRequest(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component)
          Handles the request from the button, which was setup above in the encodeBegin() method.
protected  void renderRootNode(AjaxTree tree, javax.faces.context.FacesContext context, AjaxTreeNode node, int rootNumber)
          Renders a root node.
 
Methods inherited from class javax.faces.render.Renderer
convertClientId, decode, encodeChildren, encodeEnd, getConvertedValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_FACET

public static final java.lang.String DEFAULT_FACET
The default facet name that will be used if no facet is found for a node

See Also:
Constant Field Values

HTML_TREE_NODE_CLASS

protected static final java.lang.String HTML_TREE_NODE_CLASS
See Also:
Constant Field Values

HTML_CHILD_WRAPPER_CLASS

protected static final java.lang.String HTML_CHILD_WRAPPER_CLASS
See Also:
Constant Field Values

HTML_CLICK_AREA_CLASS

protected static final java.lang.String HTML_CLICK_AREA_CLASS
See Also:
Constant Field Values

HTML_USER_DATA_CLASS

protected static final java.lang.String HTML_USER_DATA_CLASS
See Also:
Constant Field Values

HTML_NODE_STRING_NAME

protected static final java.lang.String HTML_NODE_STRING_NAME
See Also:
Constant Field Values

HTML_NODE_OPENED

protected static final java.lang.String HTML_NODE_OPENED
See Also:
Constant Field Values

HTML_NODE_CLOSED

protected static final java.lang.String HTML_NODE_CLOSED
See Also:
Constant Field Values

HTML_ROOT_LEAF

protected static final java.lang.String HTML_ROOT_LEAF
See Also:
Constant Field Values

HTML_ROOT_CLOSED

protected static final java.lang.String HTML_ROOT_CLOSED
See Also:
Constant Field Values

HTML_SERVER_URL_CLASS

protected static final java.lang.String HTML_SERVER_URL_CLASS
See Also:
Constant Field Values
Constructor Detail

AjaxTreeRenderer

public AjaxTreeRenderer()
default empty constructor

Method Detail

getRendersChildren

public boolean getRendersChildren()
AjaxTree does render children

Overrides:
getRendersChildren in class javax.faces.render.Renderer

encodeBegin

public void encodeBegin(javax.faces.context.FacesContext context,
                        javax.faces.component.UIComponent component)
                 throws java.io.IOException
Writes out the root nodes and required resources.

Overrides:
encodeBegin in class javax.faces.render.Renderer
Throws:
java.io.IOException

renderRootNode

protected void renderRootNode(AjaxTree tree,
                              javax.faces.context.FacesContext context,
                              AjaxTreeNode node,
                              int rootNumber)
                       throws java.io.IOException
Renders a root node. The structure of this mimicks how nodes are rendered by the client side JS.

.--NodeWrapper Div----------. | .-------------------. | | | LabelWrapper Div | | | '-------------------' | | .-------------------. | | | ChildWrapper Div | | | | | | | '-------------------' | '---------------------------'

Parameters:
component -
out -
node -
rootNumber - - A unique number for this root
Throws:
java.io.IOException

getFacetOutput

protected java.lang.String getFacetOutput(javax.faces.context.FacesContext context,
                                          AjaxTree tree,
                                          AjaxTreeNode node)
Takes a tree and a node and uses the facet configured for that node type to generate an HTML string.

Parameters:
context -
tree -
node -
Returns:

handleAjaxRequest

public void handleAjaxRequest(javax.faces.context.FacesContext context,
                              javax.faces.component.UIComponent component)
Handles the request from the button, which was setup above in the encodeBegin() method. Grabs the nodeString from the request, which identifies what node has been clicked. Uses that to delegate to the AjaxTreeDataProvider. Formulates response XML based on that data.

This is the XML format for the response. Its compact to go over the wire. OK|ERROR true|false [facet based output] [the nodeString]

Note that the leaf element is a boolean value determining if the node will be rendered as expandable. TODO: Use the simple JSON api to encode the json

Specified by:
handleAjaxRequest in interface AjaxComponentRenderer

findFacet

protected javax.faces.component.UIComponent findFacet(AjaxTree tree,
                                                      AjaxTreeNode node)
Gets the facet for a given node. If no facet is found explicitly for the node, uses default.

The precedence is this (falling to the next if no facet is found): 1) Get the facet based on node.getType-node.getLabel 2) Get the facet based on node.getType 3) Get the HtmlTreeRenderer.DEFAULT_FACET facet 4) Throw an NPE

Thanks to Larry Wood for the default facet idea.

Parameters:
tree -
node -
Returns:

getErrorString

protected java.lang.String getErrorString(java.lang.String msg)

escapeJSON

public static java.lang.String escapeJSON(java.lang.String s)
This method is taken from http://www.JSON.org/java/json_simple.zip, by Fang Yidong.

If more JSON is used in Tomahawk, we could incorporate the jsonSimple api in.

" => \" , \ => \\

Parameters:
s -
Returns:


Copyright © 2008. All Rights Reserved.