com.taursys.xml
Class Template

java.lang.Object
  extended bycom.taursys.xml.Component
      extended bycom.taursys.xml.Container
          extended bycom.taursys.xml.DispatchingContainer
              extended bycom.taursys.xml.DocumentElement
                  extended bycom.taursys.xml.Template
All Implemented Interfaces:
DocumentComponent, Element, MapperComponent

public class Template
extends DocumentElement

A Template is a component used to display and/or input multiple values. It acts as a Container for other components which provide the actual display or input. A typical application for the Template is to display or edit a table of data. The Template is bound to a single tag in the XML document. This tag in turn contains other tags that make up the "template" row. The Template is also bound to a CollectionValueHolder which contains the values. When rendered, the Template will replicate itself (and its child tags) in the XML document for each value in the holder.

The following is an example illustrates a Template used to display a list of names (from a collection of Person objects). A portion of the HTML is listed below along with a portion of the java code. The initForm and openForm methods have been ommitted. The HTMLComponentFactory was not used in this example so that the actual components could be illustrated.

  <h2>List presented in a table</h2>
  <table border="1">
    <tr>
      <td >First Name</td>
      <td >Last Name</td>
    </tr>
    <tr id="People__TEMPLATE_NODE">
      <td id="People__firstName">John</td>
      <td id="People__lastName">Smith</td>
    </tr>
  </table>

 -----------------------------------------------------------------------

 public class TemplatePage extends ServletForm {
   VOCollectionValueHolder people = new VOCollectionValueHolder();
   // ===================================================================
   // The following code can be ommitted if the component factory is used
   // ===================================================================
   TextField firstName = new TextField();
   TextField lastName = new TextField();
   Template report = new Template();
   // ===================================================================

   public TemplatePage() {
     try {
       jbInit();
     } catch(Exception e) {
       e.printStackTrace();
     }
   }

   private void jbInit() throws Exception {
     people.setAlias("People");
     people.setValueObjectClass(Person.class);
     // ===================================================================
     // The following code can be ommitted if the component factory is used
     // ===================================================================
     firstName.setPropertyName(Person.FIRST_NAME);
     firstName.setValueHolder(people);
     firstName.setId("People__firstName");
     lastName.setPropertyName(Person.LAST_NAME);
     lastName.setValueHolder(people);
     lastName.setId("People__lastName");
     report.setId("report");
     report.setCollectionValueHolder(people);
     report.add(firstName);
     report.add(lastName);
     this.add(report);
     // ===================================================================
   }
 

A Template can also be used to input multiple values. The code is essentially the same, except that you will use input type components in the HTML and Java code. The order and count of the values in the request must correspond to the order and count in the holder. The BusinessDelegate that supplies the values should ensure this.

The following example illustrates a form to edit inventory items. A portion of the HTML is listed below along with a portion of the java code. The openForm methods has been ommitted. This example uses the HTMLComponent factory to create the actual components at runtime.


 <h2>List of Inventory Items to Edit</h2>
   <form method="post" action="InventoryEditPage.sf">
     <table border="1" width="100%">
       <tr bgcolor="orange">
         <td>Quantity</td>
         <td>Product ID</td>
         <td>Unit Price</td>
         <td>Extension</td>
       </tr>
       <tr id="Inventory__TEMPLATE_NODE">
         <td>
           <input type="text" id="Inventory__quantity" name="quantity" value="1"/>
         </td>
         <td id="Inventory__productID">XXXX</td>
         <td>
           <input type="text" id="Inventory__unitPrice" name="unitPrice" value="0.00"/>
         </td>
         <td id="Inventory__extension">0.00</td>
       </tr>
     </table>
     <br/>
     <input type="submit" name="action" value="Save"/>
   </form>

 -----------------------------------------------------------------------

 public class InventoryEditPage extends ServletForm {
   private VOListValueHolder inventoryHolder = new VOListValueHolder();

   public InventoryEditPage() {
     try {
       jbInit();
     } catch(Exception e) {
       e.printStackTrace();
     }
   }

   private void jbInit() throws Exception {
     inventoryHolder.setAlias("Inventory");
     inventoryHolder.setValueObjectClass(InvoiceItemVO.class);
     this.add(warehouseField);
   }

   protected void initForm() throws java.lang.Exception {
     super.initForm();
     DOMParser parser = new DOMParser();
     InputSource is = new InputSource(
         getClass().getResourceAsStream("InventoryEditPage.html"));
     parser.parse(is);
     this.setDocument(parser.getDocument());
     // Use HTMLComponentFactory to create components
     HTMLComponentFactory.getInstance().createComponents(this,
       new ValueHolder[] {inventoryHolder});
   }
 

Technical Information:

The Template uses a TemplateRenderer to do the actual rendering. Typically, this involves dispatching a RenderEvent to its children, then cloning itself for each item in its collectionValueHolder.

The Template creates specialized dispatchers for Input and Trigger events. These dispatchers iterate over each item in the collection (in the order of the iterator), increment the parameter index and dispatch input to child components.


Constructor Summary
Template()
          Constructs a new template
 
Method Summary
protected  DocumentElementRenderer createDefaultRenderer()
          Creates the default Renderer for this component.
protected  InputDispatcher createInputDispatcher()
          Create the InputDispatcher for this Container.
protected  TriggerDispatcher createTriggerDispatcher()
          Create the TriggerDispatcher for this Container.
 CollectionValueHolder getCollectionValueHolder()
          Get the CollectionValueHolder that this template will iterate for rendering.
 void setCollectionValueHolder(CollectionValueHolder holder)
          Set the CollectionValueHolder that this template will iterate for rendering.
 java.lang.String toString()
          Returns a string representation of this object.
 
Methods inherited from class com.taursys.xml.DocumentElement
addAttribute, addNotify, addTextNode, createAttribute, createAttribute, createAttribute, createBoundAttribute, createBoundTextNode, createTextNode, createTextNode, createTextNode, getAttribute, getAttributeText, getAttributeValue, getElementDelegate, getId, getRenderer, getTextNode, getTextNodeText, getTextNodeValue, processRecycleEvent, processRenderEvent, removeAttribute, removeAttribute, removeNotify, removeTextNode, removeTextNode, setAttributeText, setAttributeValue, setId, setTextNodeText, setTextNodeValue
 
Methods inherited from class com.taursys.xml.DispatchingContainer
add, createParameterDispatcher, createRecycleDispatcher, createRenderDispatcher, dispatchActions, dispatchInput, dispatchParameters, dispatchRecycle, dispatchRender, getParameterMap, processInputEvent, processParameterEvent, processTriggerEvent, setParameterMap
 
Methods inherited from class com.taursys.xml.Container
addDispatcher, contains, get, getComponents, getDispatcher, getDispatchers, getDocumentAdapter, getInputDispatcher, getParameterDispatcher, getRecycleDispatcher, getRenderDispatcher, getTriggerDispatcher, remove, removeDispatcher, setComponents
 
Methods inherited from class com.taursys.xml.Component
addEventType, addInputListener, addParameterListener, addRecycleListener, addRenderListener, addTriggerListener, dispatchEvent, fireActionPerformed, fireInputReceived, fireParameterReceived, fireRecycle, fireRender, getEventTypeList, getParent, isNotifySet, isVisible, lazyAddNotify, lazyRemoveNotify, processEvent, removeEventType, removeInputListener, removeParameterListener, removeRecycleListener, removeRenderListener, removeTriggerListener, setVisible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Template

public Template()
Constructs a new template

Method Detail

createInputDispatcher

protected InputDispatcher createInputDispatcher()
Create the InputDispatcher for this Container.

Overrides:
createInputDispatcher in class DispatchingContainer
Returns:
the InputDispatcher for this Container.

createTriggerDispatcher

protected TriggerDispatcher createTriggerDispatcher()
Create the TriggerDispatcher for this Container.

Overrides:
createTriggerDispatcher in class DispatchingContainer
Returns:
the TriggerDispatcher for this Container.

createDefaultRenderer

protected DocumentElementRenderer createDefaultRenderer()
Creates the default Renderer for this component. By Default this methos returns a new TemplateRenderer. Override this method to define your own TemplateRenderer.

Overrides:
createDefaultRenderer in class DocumentElement

setCollectionValueHolder

public void setCollectionValueHolder(CollectionValueHolder holder)
Set the CollectionValueHolder that this template will iterate for rendering.

Parameters:
holder - the CollectionValueHolder that this template will iterate for rendering.

getCollectionValueHolder

public CollectionValueHolder getCollectionValueHolder()
Get the CollectionValueHolder that this template will iterate for rendering.

Returns:
the CollectionValueHolder that this template will iterate for rendering.

toString

public java.lang.String toString()
Returns a string representation of this object. This contains the object identity and state information.

Overrides:
toString in class DocumentElement
Returns:
a string representation of this object


Copyright © 2007 Martin T Phelan. All Rights Reserved.