// file: SubMainMenu.java
//

// import necessary java libraries
//
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.applet.*;

/**
 * class that implements the menu driven system that drives the applet
 */
class SubMainMenu extends JPanel implements ActionListener, Constants {

    // ---------------------------------------------------    
    //
    // declare global variables
    //
    // ---------------------------------------------------    

    // creates a fram to manipulate the configuration options
    //
    public JFrame config = null;

    // textfields and labels for the connector properties
    //
    public JLabel nameLabel = null;
    public JTextField nameField = null;

    // declare a reference to the SubDiGraph
    //
    public SubDiGraph subdigraph = null;

    // menu that holds all functionality
    //
    public JMenu menu = null;
    public JMenu subMenu = null;
    public JMenu subSubMenu = null;
    public JMenuBar menuBar = null;
    public JMenuItem menuItem = null;
    
    // declare the layout manager
    //
    public GridBagLayout gridbag = new GridBagLayout();
    
    // ---------------------------------------------------        
    //
    // declare class constructors
    //
    // ---------------------------------------------------    
   
    /**
     * constructor initializes objects and containers
     *
     * @param sdg SubDiGraph Object for menu
     */
    SubMainMenu(SubDiGraph sdg) {
	
	super();

	// set the default layout
	//
	setLayout(gridbag);

	// set the reference to vertex box
	//
	subdigraph = sdg;

	// initialize the menu bar
	//
	menuBar = new JMenuBar();

	// create the file menu
	//	
	menu = new JMenu(fileStr);
	menu.setFont(newFont);
	menuBar.add(menu);

	// add the menu items
	//
	menuItem = new JMenuItem(closeStr);
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 
						       ActionEvent.ALT_MASK));
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(closeStr);
	menu.add(menuItem);

	// create the edit menu
	//
	menu = new JMenu(editStr);
	menu.setFont(newFont);
	menuBar.add(menu);

	menuItem = new JMenuItem(cutStr);
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, 
						       ActionEvent.CTRL_MASK));
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(cutStr);
	menu.add(menuItem);

	menuItem = new JMenuItem(copyStr);
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 
						       ActionEvent.CTRL_MASK));
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(copyStr);
	menu.add(menuItem);

	menuItem = new JMenuItem(pasteStr);
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 
						       ActionEvent.CTRL_MASK));
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(pasteStr);
	menu.add(menuItem);

	menu.addSeparator();

	menuItem = new JMenuItem(insArcStr);
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 
						       ActionEvent.ALT_MASK));
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(insArcStr);
	menu.add(menuItem);

	menu.addSeparator();

	menuItem = new JMenuItem(delArcStr);
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 
						       ActionEvent.ALT_MASK));
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(delArcStr);
	menu.add(menuItem);

	menuItem = new JMenuItem(delBlkStr);
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, 
						       ActionEvent.ALT_MASK));
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(delBlkStr);
	menu.add(menuItem);

	menuItem = new JMenuItem(clearStr);
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 
						       ActionEvent.ALT_MASK));
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(clearStr);
	menu.add(menuItem);

	// create the algorithms menu
	//
	menu = new JMenu(algoStr);
	menu.setFont(newFont);
	menuBar.add(menu);

	int index = subdigraph.data.association.indexOf(TRANSFORM_TAG);
	Vector algorithms = (Vector)subdigraph.data.values.elementAt(index);
	for (int i=0; i < algorithms.size(); i++) {
	    String algo = (String)algorithms.elementAt(i);	
	    if(algo.equals("AudioFrontEnd")) {
		continue;
	    }   
	    menuItem = new JMenuItem(algo);
	    menuItem.setFont(newFont);
	    menuItem.addActionListener(this);
	    menuItem.setActionCommand(algo);
	    menu.add(menuItem);   
	}	    

	// set the dimensions of the menu
	//
	this.setSize(new Dimension(subdigraph.MAX_WIDTH, 30));
	this.setMinimumSize(new Dimension(subdigraph.MAX_WIDTH, 30));
	this.setMaximumSize(new Dimension(subdigraph.MAX_WIDTH, 30));
	this.setPreferredSize(new Dimension(subdigraph.MAX_WIDTH, 30));
    }
    
    // ---------------------------------------------------
    //
    // declare class methods
    //
    // ---------------------------------------------------
    
  
    /**
     * adds components to the control panel
     */
    public void add_components () {
	
	constrain(this, menuBar, 0, 0, 
		  GridBagConstraints.REMAINDER, GridBagConstraints.REMAINDER,
		  GridBagConstraints.BOTH, GridBagConstraints.CENTER,
		  1, 1, 0, 0, 0, 0);
    }

    /**
     * method listens for actions taking place on text only menu items   
     * @param e action command that is fired
     */
    public void actionPerformed(ActionEvent e) {

	//System.out.println("SubMainMenu action");

	// get the source of the action that just occured
	//
	String source = e.getActionCommand();

	if(source.equals(closeStr)) {

	    // close the application
	    //
	    subdigraph.closeContainer();

	} else if(source.equals(clearStr)) {

	    // clear out all existing memory
	    //
	    subdigraph.clearAllMemory();

	    // refresh the work area
	    //
	    subdigraph.subWorkArea.repaint();

	} else if (source.equals(cancelStr)) {

	    // remove the frame from the screen
	    //
	    config.setVisible(false);

	} else if(source.equals(delBlkStr)) {

	    // remove all children and parent edges
	    //
	    if (subdigraph.currVertexFocus != null) {
		if (subdigraph.currVertexFocus.getClass().getName().equals("Vertex")) {
		    subdigraph.removeVertex(subdigraph.currVertexFocus);
		}
	    }

	} else if(source.equals(cutStr)) {
	    subdigraph.cut();
	} else if(source.equals(copyStr)) {
	    subdigraph.copy();
	} else if(source.equals(pasteStr)) {
	    subdigraph.paste();
	} else if(source.equals(insArcStr)) {
	    subdigraph.enableArcInsertion = true;
	} else if(source.equals(delArcStr)) {
	    subdigraph.enableArcDeletion = true;
	} else {
	    subdigraph.createAlgorithm(e.getActionCommand());
	}
    }

    /**
     * this method creates the constraints of a component in a container
     * and then adds the component to the container.  it is based on 
     * code from "Java in a Nutshell" by David Flanagan. published by
     * O'Reilly and Associates incorporated.
     *
     * @param container    container to place the component
     * @param component    the component to be constrained
     * @param grid_x       horizontal position of component
     * @param grid_y       vertical position of component
     * @param grid_width   number of horizontal grid spots to take
     * @param grid_height  number of vertical grid spots to take
     * @param fill         directions to fill when component space is resized
     * @param anchor       anchor position when component space is resized
     * @param weight_x     relative amount of horizontal space 
     *                     component should take when its space is resized
     * @param weight_y     relative amount of vertical space component
     *                     should take when its space is resized
     * @param top          amount of inset space on top
     * @param left         amount of inset space on left
     * @param bottom       amount of inset space on bottom
     * @param right        amount of inset space on right
     *
     * @return true
     */
    public boolean constrain(Container container, Component component, 
			     int grid_x, int grid_y, int grid_width, 
			     int grid_height, int fill, int anchor, 
			     double weight_x, double weight_y, int top,
			     int left, int bottom, int right) {
	
	// set the constraints based on the input variables
	//
	GridBagConstraints c = new GridBagConstraints();
	c.gridx = grid_x;
	c.gridy = grid_y;
	c.gridwidth = grid_width;
	c.gridheight = grid_height;
	c.fill = fill;
	c.anchor = anchor;
	c.weightx = weight_x;
	c.weighty = weight_y;
	
	// if any of the buffer insets are greater than zero, create a new
	// Insets object
	//
	if (top + bottom + left + right > 0)
	    c.insets = new Insets(top, left, bottom, right);
	
	// set the constraints of the component
	//
	((GridBagLayout) container.getLayout()).setConstraints(component, c);
	
	// add the component to the container
	//
	container.add(component);
	
	// exit gracefully
	//
	return true;
    }
}

//
// end of file
