Application Techniques



Broadcasting Events between Subpages

How to broadcast events from one subpage to another.

About this technique

Details

Category

HTML Client Techniques> Miscellaneous

Description

You'll learn about:

Related reading

See the chapter on programming pages in the Programmer's Guide

Suppose you have a set of pages that allows the user to browse a list of products. The parent page contains two subpages that allow the user to navigate through the data. The user can view a list of products in one subpage and click on a particular product to see product details in another subpage. This architecture has several components:

The code for each of these components is shown below.

Adding code to the parent page   Top of page

The pageLoaded event for the parent page registers the ProductDetails subpage as a listener by calling a method on the ProductList subpage:

  ProductList.addProductChangeListener(ProductDetails); 

Adding code to the list page   Top of page

The ProductList page has a member variable that keeps track of listeners in a Vector. The Vector provides support for a potentially unlimited number of listeners. In this example, there is only one listener (the ProductDetails subpage):

  Vector listeners = new Vector(); 

The ProductList page has these import statements:

  import ProductChangeListener; 
  import ProductEvent; 

The addProductChangeListener() method adds listeners to the Vector:

  public void addProductChangeListener(ProductChangeListener l)  
  { 
     listeners.addElement(l); 
  } 

The notifyProductChange() method sends an event message to each listener:

  public void notifyProductChange(String prodid) 
  { 
     for (int i=0; i<listeners.size(); i++) 
        { 
           ProductChangeListener l = (ProductChangeListener) 
           listeners.elementAt(i);  
           l.productChange(new ProductEvent(this, prodid)); 
        } 
  } 

The ProductList page uses an HTML data view to retrieve a list of products. The data view has a label called Label1 that is an event link. Label1 gets data from the products.productname column in the products table. The eventLinkPerformed event for Label1 calls the notifyProductChange() method, passing in a parameter called prodid that maps to another label in the data view called Label2. Label2 gets data from the products.productid column:

  notifyProductChange(getCurrentRequest().getParameter("prodid")); 

Adding code to the details page   Top of page

The ProductDetails page implements the ProductChangeListener. It has a member variable to keep track of the currently selected product:

  private String iProdID = null; 

The ProductDetails page has these import statements:

  import ProductChangeListener; 
  import ProductEvent; 

The pageGenerateBegin event on the ProductDetails page queries the database, using the product that the user selected in the ProductList page to narrow the selection. The query gets the product ID from the iProdID member variable, whose value is set by the productChange() method:

  protected void pageGenerateBegin() throws Exception 
  { 
     if (iProdID != null) 
     { 
        try  
        { 
           agData.query("products.productid='" + iProdID"`"); 
        } 
        catch (Exception ex)  
        { 
        //Error handling here 
        } 
     } 
  } 

The ProductDetails page provides an implementation for the productChange() method declared on the ProductChangeListener interface:

  public void productChange(ProductEvent e)  
  { 
     String prodid = e.getProduct(); 
     iProdID = prodid; 
  } 

Defining the listener interface   Top of page

The ProductChangeListener interface declares the following method. Each listener must provide an implementation for this method:

  public abstract void productChange(ProductEvent e); 

Defining the event utility class   Top of page

The ProductEvent class provides information about the event that occurred. It extends java.util.EventObject.

The ProductEvent class has a member variable to keep track of the currently selected product:

  private String iProdID = null; 

The constructor for the ProductEvent class calls the constructor on the superclass and sets the member variable:

  public ProductEvent(Object src, String prodid) 
  { 
     super(src); 
     iProdID = prodid; 
  } 

The ProductEvent class has a method that returns the currently selected product:

  public String getProduct()  
  { 
     return iProdID; 
  } 
   





Copyright © 2000, SilverStream Software, Inc. All rights reserved.