// file: SearchMenu.java
//

// import java libraries
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
 * Class for the BugMenu. Extension of JPanel
 */
public class BugMenu extends JPanel {

    // declare global variables
    //

    /**
     * JPanel object used as menu
     */
    JPanel menu = null;

    /**
     * Font variable used
     */
    Font newFont = null;
 
    /**
     * Default class constructor
     *
     * @param bugPanel BugPanel object to be passed to the menu_maker method
     */
    public BugMenu(BugPanel bugPanel) {
	
	newFont = bugPanel.newFont;
	
	menu_maker(this, bugPanel);

    }

    /**    
     * Creates the complete menu for this program
     * @param menu_panel panel to hold the menubar
     * @param transforms vector of names cooresponding transforms 
     *                   used in this program        
     */
    public void menu_maker(JPanel menu_panel, BugPanel target) {

	// create a border around the menu panel
        //
        menu_panel.setBorder(BorderFactory.createEtchedBorder());
	menu_panel.setFont(newFont);

	// make this menu panel use a gridbag layout and set up the 
	// contraints so that they menu will display properly
	//
	menu_panel.setLayout(new GridBagLayout());
	GridBagConstraints gbc = new GridBagConstraints();
	gbc.gridx = 0;
	gbc.gridy = 0;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gbc.gridheight = GridBagConstraints.REMAINDER;
	gbc.weightx = 1;
	gbc.weighty = 1;
	gbc.fill = GridBagConstraints.HORIZONTAL;

	JMenuBar menu_bar = new JMenuBar();
	menu_bar.setFont(newFont);
	menu_panel.add(menu_bar, gbc);

	// create the file menu
	//
	menu_bar.add(makeMenu("File", new Object[] {
	    "Close Bug Window"}, target));

	// add the space between the regular menu items and the help
	//
	menu_bar.add(Box.createHorizontalGlue());

	// create the help menu
	//
	menu_bar.add(makeMenu("Help", new Object[] {"Help"}, target));
    }


    /**
     * creates individual dropdown menus
     *
     * @param parent   name of or menu that will contain the items
     * @param items    names of the menu items that are to be added
     * @param target   tells where the event handlers are
     * @return JMenu object allow the user to take a returned menu 
     *         and do with it as they see fit
     */
    private JMenu makeMenu(Object parent, Object[] items, 
			   Object target) {
	JMenu m = null;
	
	
	//  at this point, the parent Object is examined.  if it is a string,
	//  a new menu item is created, but if it is a menu, then the menu
	//  is renamed to m
	//
	if (parent instanceof JMenu) {
	    m = (JMenu)parent;
	    m.setFont(newFont);
	}
	else if (parent instanceof String) {
	    m = new JMenu ( (String) parent);
	    m.setFont(newFont);
	    if (parent.equals("File")) {
		m.setMnemonic(KeyEvent.VK_F);
	    }
	}
	
	else 
	    return null;
	
	// here the individual menu items are added to the menu and an 
	// action listener is added with the name of the item being
	// the way to access it
	//
	for (int i = 0; i < items.length; i++) {
	    if (items[i] instanceof String) {
		JMenuItem mi = new JMenuItem((String)items[i]);
		mi.setFont(newFont);
		
		if(items[i].equals("Close Window"))
		    mi.setMnemonic(KeyEvent.VK_X);

		if (target instanceof ActionListener)
		    mi.addActionListener((ActionListener) target);
		m.add(mi);
	    }
	    else if (items[i] instanceof JMenuItem) {
		JMenuItem mi = (JMenuItem)items[i];
		mi.setFont(newFont);
		if (target instanceof ActionListener)
		    mi.addActionListener((ActionListener)target);
		m.add(mi);
	    }
	    else if (items[i] == null)
		m.addSeparator();
	}
	return m;
    }
}




