Application Techniques



Building Menus and Generating Menu Code

How to build and display menus for forms.

About this technique

Details

Category

Java Client Techniques> Menus

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on advanced form topics in the Programmer's Guide

Creating a tree menu   Top of page

The following code creates a default menu by adding menu items as nodes to a tree menu. It uses add() to add one node to another, specifying how the added node should be added, that is, whether it should be added as a child node (of the specified node) or as the last node on the same level as the specified node. After each of the top level menus is complete, the code calls expandNode(node), which graphically displays the node and its child nodes.

  private void handle_btnDefineMenu_actionPerformed(ActionEvent evt) 
  { 
   
     AgoTreeControlNode node; 
     node = treeMenu.add (null, treeMenu.LAST, "File"); 
     treeMenu.add (node, treeMenu.CHILD, "Save"); 
     treeMenu.add (node, treeMenu.CHILD, "SaveAs"); 
     treeMenu.add (node, treeMenu.CHILD, "Exit"); 
     treeMenu.expandNode(node); 
   
     node = treeMenu.add (null, treeMenu.LAST, "Edit"); 
     treeMenu.add (node, treeMenu.CHILD, "Cut"); 
     treeMenu.add (node, treeMenu.CHILD, "Copy"); 
     treeMenu.add (node, treeMenu.CHILD, "Paste"); 
     treeMenu.expandNode(node); 
   
     node = treeMenu.add (null, treeMenu.LAST, "Help"); 
     treeMenu.add (node, treeMenu.CHILD, "About"); 
  } 

Notes about the code

Creating a popup menu for editing the tree menu   Top of page

The following code creates a popup menu that is used to add and delete nodes in the menu tree. The addActionListener()adds an ActionListenerobject to the menu item that calls it. This ActionListener receives action events from this menu item.

  public void createPopup() 
      { 
             
        MenuItem menuItem; 
        m_popupMenu = new PopupMenu("Title Goes Here"); 
         
        m_popupMenu.add(menuItem = new MenuItem ("Insert Before")); 
        menuItem.addActionListener (this); 
        m_popupMenu.add(menuItem = new MenuItem ("Insert After")); 
        menuItem.addActionListener (this); 
        m_popupMenu.add(menuItem = new MenuItem ("Add Child")); 
        menuItem.addActionListener (this); 
        m_popupMenu.addSeparator(); 
         
        m_popupMenu.add(menuItem = new MenuItem("Edit")); 
        menuItem.addActionListener (this); 
        m_popupMenu.add(menuItem = new MenuItem("Cut")); 
        menuItem.addActionListener (this); 
        m_popupMenu.add(menuItem = new MenuItem ("Paste Before")); 
        menuItem.addActionListener (this); 
        m_popupMenu.add(menuItem = new MenuItem ("Paste After")); 
        menuItem.addActionListener (this); 
        m_popupMenu.add(menuItem = new MenuItem("Delete")); 
        menuItem.addActionListener (this); 
        add (m_popupMenu);   
           
      } 

Notes about the code

Implementing the popup menu's operations   Top of page

The following code shows how to insert, delete, rename nodes in a tree menu. In order to perform the correct operation on the correct node, you must get the selected operation and the selected node. This code is from the menuItemSelected event:

  String sAction = actionEvent.getActionCommand(); 
   
     AgoTreeControlNode node = treeMenu.getSelectedNode(); 
     if (node == null) 
        node = treeMenu.getRootNode(); 
   
     if (sAction.equals("Insert Before")) { 
        String sMenu = (String) agDialog.showFormDialog ( 
           "Add menu item", "dlgMenuItem"); 
        if (sMenu==null) return; 
        treeMenu.add (node, treeMenu.PREVIOUS, sMenu); 
        return;    
     } 
     if (sAction.equals("Edit")) { 
        if (node == null) return; 
        Hashtable htParm = new Hashtable(); 
        htParm.put ("ITEM", node.getText()); 
        String sMenu = (String) agDialog.showFormDialog ( 
           "Edit menu item", "dlgMenuItem", htParm); 
        if (sMenu==null) return; 
        node.setText (sMenu); 
        return;    
     } 
     if (sAction.equals("Delete")) { 
        if (node == null) return;          
        boolean bResults; 
        if (node.getChildNode() == null){ 
           bResults = agDialog.showMessageYesNo("Removing Node",  
              "Do you want to remove the node?"); 
           if (bResults) treeMenu.remove(node); 
        } 
        else { 
         
           bResults = agDialog.showMessageYesNo("Node has children",  
            "This will delete all child nodes. Do you want to continue?"); 
           if (bResults) treeMenu.remove(node); 
        } 
        return;    

Notes about the code

Obtaining generated code for the formActivate() and menuItemSelected() methods   Top of page

This Menu Builder Example provides a quick way for you to get code for the formActivate() and menuItemSelected() methods. To ensure that you get the menu building code for the desired menu tree:

  1. Generate the default menu tree by clicking the Generate Default Menu button.

  2. Edit the default tree by using the pop-up menu (right-click to invoke the pop-up menu). Add the menus, submenus, and menu selections that you want.

  3. Click the Generate Menu Code button to generate the code.

  4. Copy the generated code from the text area.






Copyright © 2000, SilverStream Software, Inc. All rights reserved.