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

Java-JSP Interface for HierMenus (2/4)

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

A Java-JSP Menu Builder for HierMenus

Represent Menu Contents as a tree data structure

The figure below shows how a menu structure can be represented as a tree structure.

Diagramming of an HM menu in a tree format
Figure 1: Diagramming an HM menu as a tree structure

The tree root is the starting point of a menu, even though it's not visible. The siblings of the tree root represent the menu first level. Each menu item having a sub-menu is represented by a tree node with its siblings.

Build a Java implementation of the menu content tree data structure

The classes HMContentTree, HMContentTreeRoot and HMContentTreeItem provide a Java data structure implementation for the menu content tree representation. These classes represent respectively, the tree, the tree root and the tree item:

// HMContentTree class
public class HMContentTree implements java.io.Serializable{
    private HMContentTreeRoot root = new HMContentTreeRoot();
    public HMContentTree() {}
    public void addItem(HMContentTreeItem node, String parent)    {
      root.addItem(node, parent);
    }
    public String toContentRepresentation() {
      StringBuffer ret = new StringBuffer();
      ret.append(root.toContentRepresentation("HM_Array1"));
      return ret.toString();
    }
}
// HMContentTreeRoot class
class HMContentTreeRoot extends HMContentTreeItem {
    private final static String ROOT_MARKER = "#ROOT_MARKER#";
    public HMContentTreeRoot() {}
    protected String getLabel() {
      return ROOT_MARKER;
    }
}
// HMContentTreeItem class
import java.util.ArrayList;
import java.util.ListIterator;
public class HMContentTreeItem implements java.io.Serializable{
    private ArrayList siblings = new ArrayList();
    private String label;
    private String link;
    public HMContentTreeItem() {}
    public HMContentTreeItem(String _label, String _link){
      label = _label;
      link = _link;
    }
    protected String getLabel(){
      return label;
    }
    protected String getLink() {
      return link;
    }
    public void addItem(HMContentTreeItem item, String parent) {
      // this is my sibling
      if (parent.compareToIgnoreCase(this.getLabel())==0) {
        // adding in my siblings list
        siblings.add(item);
      // checking if it is a sibling of some of my siblings
      }else{
        ListIterator siblingsIterator =  siblings.listIterator();
        HMContentTreeItem sibling;
        while (siblingsIterator.hasNext()) {
          sibling = (HMContentTreeItem) siblingsIterator.next();
          sibling.addItem( item, parent);
        }
      }
    }
    public int hasChild() {
      if (siblings.isEmpty()) {
        return 0;
      }else {
        return 1;
      }
    }
    public String toContentRepresentation(String hsArrayName){
      StringBuffer ret = new StringBuffer();
      ret.append(hsArrayName);
      ret.append(" = [");
      ret.append("\n");
      ret.append("[]");
      ListIterator siblingsIterator =  siblings.listIterator();
      HMContentTreeItem sibling;
      while (siblingsIterator.hasNext()) {
        ret.append(",\n[\"");
        sibling = (HMContentTreeItem) siblingsIterator.next();
        ret.append(sibling.getLabel());
        ret.append("\",\"");
        ret.append(sibling.getLink());
        ret.append("\",1,0,");
        ret.append(sibling.hasChild());
        ret.append("]");
      }
      ret.append("\n]\n\n");
      siblingsIterator = siblings.listIterator();
      int childNumber = 0;
      while (siblingsIterator.hasNext()) {
        childNumber++;
        sibling = (HMContentTreeItem) siblingsIterator.next();
        if(sibling.hasChild()==1) {
          ret.append(sibling.toContentRepresentation(hsArrayName + "_" + childNumber));
        }
      }
      return ret.toString();
    }
}

Note that HMContentTreeRoot extends HMContentTreeItem and HMContentTree has an addItem() and a toHMContent() method.


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

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

URL: http://webreference.com/programming/java/jspmenus/2.html