Java-JSP Interface for HierMenus (3/4) | WebReference

Java-JSP Interface for HierMenus (3/4)

To page 1To page 2current pageTo page 4
[previous] [next]

A Java-JSP Menu Builder for HierMenus

Create and populate the HMContentTree

From your JSP code, a Java class, lets call it MenuContentBuilder, is responsible for creation of the menu items and their addition to the tree. Below is sample code for a MenuContentBuilder class. Basically, this sample class offers a getTree(String user) method, which returns an HMContentTree object. For populating HMContentTree with the proper information, the user specific contents are retrieved from a data source based on the user name. The HMContentTree object is filled with the user-specific content and then returned.

// MenuContentBuilder class
public class MenuContentBuilder {
    public HMContentTree getTree(String user) {
      // step 1 - create HMContentTree object
      HMContentTree menuTree = new HMContentTree();
      // build Paulo's menu
      // The following code simulate how to build the Tree.
      // Real code would be getting the Tree contents
      // from some data source
      if (user.compareToIgnoreCase("Paulo")==0){
        // step 2 - create HMContentTreeItem object with label and link
        HMContentTreeItem item = new HMContentTreeItem("A", "link !");
        // step 3 - HMContentTreeItem object is added to the Tree
        menuTree.addItem(item, "#ROOT_MARKER#");
        // step 4 - repeat steps 2 and 3 for each menu item
        menuTree.addItem(new HMContentTreeItem("B", "link B"), "#ROOT_MARKER#");
        menuTree.addItem(new HMContentTreeItem("C", "link C"), "B");
        menuTree.addItem(new HMContentTreeItem("D", "link D"), "B");
        menuTree.addItem(new HMContentTreeItem("E", "link E"), "C");
        menuTree.addItem(new HMContentTreeItem("F", "link F"), "C");
        menuTree.addItem(new HMContentTreeItem("G", "link G"), "F");
        // step 5 - returns HMContentTree object
        return menuTree;
      }
      // build Srimant's menu
      if (user.compareToIgnoreCase("Srimant ")==0){
        menuTree.addItem(new HMContentTreeItem("A", "link A"), "#ROOT_MARKER#");
        menuTree.addItem(new HMContentTreeItem("B", "link B"), "#ROOT_MARKER#");
        menuTree.addItem(new HMContentTreeItem("C", "link C"), "#ROOT_MARKER#");
        menuTree.addItem(new HMContentTreeItem("D", "link D"), "#ROOT_MARKER#");
        menuTree.addItem(new HMContentTreeItem("E", "link E"), "A");
        menuTree.addItem(new HMContentTreeItem("F", "link F"), "A");
        menuTree.addItem(new HMContentTreeItem("G", "link G"), "B");
        menuTree.addItem(new HMContentTreeItem("H", "link H"), "B");
        menuTree.addItem(new HMContentTreeItem("I", "link I"), "F");
        return menuTree;
      }
        // build defaults menu
        menuTree.addItem(new HMContentTreeItem("A", "link A"), "#ROOT_MARKER#");
        menuTree.addItem(new HMContentTreeItem("B", "link B"), "#ROOT_MARKER#");
        menuTree.addItem(new HMContentTreeItem("C", "link C"), "#ROOT_MARKER#");
        menuTree.addItem(new HMContentTreeItem("D", "link D"), "A");
        menuTree.addItem(new HMContentTreeItem("E", "link E"), "A");
        menuTree.addItem(new HMContentTreeItem("F", "link F"), "B");
        menuTree.addItem(new HMContentTreeItem("G", "link G"), "C");
        menuTree.addItem(new HMContentTreeItem("H", "link H"), "C");
        menuTree.addItem(new HMContentTreeItem("I", "link I"), "G");
        return menuTree;
    }
}

These are the steps followed by the MenuContentBuilder class to build the HMContentTree object. These steps are placed as comments in the MenuContentBuilder sample code.

  1. HMContentTree object is created.
  2. An HMContentTreeItem object is created with a label and a link.
  3. The HMContentTreeItem is added to HMContentTree, by invoking the HMContentTree.addItem(item, parentLabel) method. Whenever adding an item, the item's parent label has to be provided. Very important:
    • First level menu items must have #ROOT_MARKER# as the parent label
    • In order to add an item, its parent already has to be in the tree.
  4. Repeat actions 2 and 3 for each menu item.
  5. HMContentTree object is returned.

The menu content might come from a relational database, a remote procedure call, an EJB or a Web service call. The key point is, MenuContentBuilder is able to read the proper data from a data source and populate the HMContentTree object.


To page 1To page 2current pageTo page 4
[previous] [next]

Created: November 13, 2002
Revised: November 13, 2002

URL: https://webreference.com/programming/java/jspmenus/3.html