Java-JSP Interface for HierMenus (3/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.
|
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.
[previous] [next] |
Created: November 13, 2002
Revised: November 13, 2002
URL: https://webreference.com/programming/java/jspmenus/3.html