spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java / jspmenus To page 1current pageTo page 3To page 4
[previous] [next]

A Java-JSP Menu Builder for HierMenus

Technical Lead
Thomson Reuters (Markets) LLC
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


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.


home / programming / java / jspmenus To page 1current pageTo page 3To page 4
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

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

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