Example: populate() function

The following is an example of a populate() method. Note the technique used to preinstall each child in the looper() method, which loads each child in a specified search path (for example, Item 1.1/Item 1.1.5/Item 1.1.5.3).


public void populate() throws Exception {
// if searching for a specific node from URL param gotoNode, method looper() will take
// care of it for us.  Otherwise, just populate the tree with the node clicked on.
if (!gotoNode.equals("")) {
   StringTokenizer st = new StringTokenizer(gotoNode, "/");
   if (st.hasMoreTokens()) {
      for (Enumeration e = looper(st, "Item 1").elements(); e.hasMoreElements();) {
         nodeInfo.addElement(e.nextElement());
         }
      }
   } else {
      // create 5 nodes in this sample
      for (int i = 1; i <= 5; i++) {
      // construct the children URL parameters,
      // the following is just an example (every other node has children)
      String param = "";
      if (i % 2 == 1)
         param = "uid=" + uid + "." + String.valueOf(i) + "&para2=somevalue";
         // set sample value of all node to 13
         String testValue = new String("13");
         // create a new node.  "Catalog" is the menu type.
         DynamicTreeNode node = new DynamicTreeNode(uid + "." + String.valueOf(i), testValue,
               param, "", "Catalog");
         // make every other node different
         if (i % 2 == 0 ) {
            node.setIconType(new String[] {"Arrow"});
            node.setMenuType("Speed");
         }
         // nodeInfo is a class field of type Vector  
         nodeInfo.addElement(node);
      }
   }
 
   // create some menu groups 
   String[][] groupMenu = {{"Test","http://www.ibm.com"},
   {"",""},
   {"Erase","http://www.ibm.com"},
   {"Shutdown","http://www.ibm.com"}};
   DynamicTreeMenuType mnu = new DynamicTreeMenuType(groupMenu, "Catalog");
 
   menuInfo.addElement(mnu);
 
   String[][] groupMenu2 = {{"Fast","http://www.ibm.com"},
   {"Slow","http://www.ibm.com"},
   {"Stop","http://www.ibm.com"}};
   DynamicTreeMenuType mnu2 = new DynamicTreeMenuType(groupMenu2, "Speed");
 
   // menuInfo is a class field of type Vector  
   menuInfo.addElement(mnu2);
 
   // create some icon types 
   DynamicTreeIconType iconType = new DynamicTreeIconType("Calendar", "calendar/calendar.gif");
 
   // iconInfo is a class field of type Vector  
   iconInfo.addElement(iconType);
 
   DynamicTreeIconType iconType2 = new DynamicTreeIconType("Arrow", "list/arrow.gif");
 
   iconInfo.addElement(iconType2);
   }
 
   // looper method used when trying to locate a specific node
   // this method rerecursively calls itself until the search path is exhausted
   public java.util.Vector looper(StringTokenizer st, String sName) {
   // create a default menu
   String[][] menu = {{"Open","http://www.ibm.com"},
      {"",""},
      {"Copy","http://www.ibm.com"},
      {"Delete","http://www.ibm.com"},
      {"Modify","http://www.ibm.com"}};
      Vector newVec = new Vector();
      // get the first part of the search path
      String match = (String)st.nextElement();
      // create 5 nodes on each level
      for (int i = 1; i <= 5; i++) {
         String param = "";
         String testValue = new String("13");
         String cMenuParam = new String("myValue=Test&otherValue=3");
         String objType = new String("Speed");
         String name = sName + "." + String.valueOf(i);
       DynamicTreeNode node;
       // every other node will be a leaf by setting the childrenURLParam="" (param)
       if (i % 2 == 1) {
          param = "uid=" + name + "&para2=somevalue";
          node = new DynamicTreeNode(name, testValue, param, menu);
       } else {
          node = new DynamicTreeNode(name, testValue, param, cMenuParam, objType);
         node.setIconType(new String[] {"Calendar", "Arrow"});
       }
   // if the newly created node/leaf matches the search string and there are still
   // more parts of the search path, call looper() again setting this nodes children
   // to contain the next level of the search path
   if (match.equals(name) && i % 2 == 1) {
      if (st.hasMoreElements()) {
         node.setChildren(looper(st, name));
      }
   }
   // finally add the DynamicTreeNode to the newVec
   newVec.addElement(node);
}
return newVec;
}