// file: MainMenu.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: MainMenu
//
// implement the menu driven system that drives the applet
//
// hierarchy: MainMenu
//
class MainMenu 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 MainWindow
    //
    public MainWindow mainWnd = 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 a frame to display the help message
    //
    JFrame helpFrame = null;

    // declare a text area to display documentation
    //
    public JTextArea textArea = null;
    
    // declare a scrollable pane for the text area
    //
    public JScrollPane scrollPane = null;

    // declare the layout manager
    //
    public GridBagLayout gridbag = new GridBagLayout();
    
    // ---------------------------------------------------        
    //
    // declare class constructors
    //
    // ---------------------------------------------------    
   
    // method: MainMenu
    //
    // arguments:
    //
    // returns  : none
    //
    // constructor initializes objects and containers
    //
    MainMenu(MainWindow mainWnd_arg) {
	
	super();

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

	// set the reference to vertex box
	//
	mainWnd = mainWnd_arg;

	// instantiate the frame
	//
	helpFrame = new JFrame("Help");

	// instantiate the text area to display documentation
	//
	textArea = new JTextArea();
	textArea.setLineWrap(true);
	textArea.setWrapStyleWord(true);
	textArea.setEditable(false);
	
	// instantiate the scrollable pane for the text area
	//
	scrollPane = new JScrollPane(textArea);

	// set the scroll pane dismensions
	//
	scrollPane.setSize(new Dimension(HELP_WIDTH, HELP_HEIGHT));
	scrollPane.setPreferredSize(new Dimension(HELP_WIDTH, HELP_HEIGHT));
	scrollPane.setMinimumSize(new Dimension(HELP_WIDTH, HELP_HEIGHT));
	scrollPane.setMaximumSize(new Dimension(HELP_WIDTH, HELP_HEIGHT));

	// 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(clearStr);
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 
						       ActionEvent.ALT_MASK));
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(clearStr);
	menu.add(menuItem);

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

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

	menuItem = new JMenuItem(saveAsStr);
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(saveAsStr);
	menu.add(menuItem);

	menu.addSeparator();

	menuItem = new JMenuItem(quitStr);
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 
						       ActionEvent.ALT_MASK));
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(quitStr);
	menu.add(menuItem);
	
	// create the help menu
	//
	menu = new JMenu(helpStr);
	menu.setFont(newFont);
	menuBar.add(Box.createHorizontalGlue());
	menuBar.add(menu);

	menuItem = new JMenuItem(overviewStr);
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(overviewStr);
	menu.add(menuItem);

	menu.addSeparator();

	menuItem = new JMenuItem(tutorialStr);
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(tutorialStr);
	menu.add(menuItem);

	// set the dimensions of the menu
	//
	this.setSize(new Dimension(mainWnd.MAX_WIDTH, 30));
	this.setMinimumSize(new Dimension(mainWnd.MAX_WIDTH, 30));
	this.setMaximumSize(new Dimension(mainWnd.MAX_WIDTH, 30));
	this.setPreferredSize(new Dimension(mainWnd.MAX_WIDTH, 30));
    }
    
    // ---------------------------------------------------
    //
    // declare class methods
    //
    // ---------------------------------------------------
    
    // method: add_components
    //
    // arguments: none
    // return   : none
    //
    // 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: actionPerformed
    //
    // arguments:
    //    ActionEvent e: action command that is fired
    //
    // return   : none
    //
    // method listens for actions taking place on text only menu items
    //
    public void actionPerformed(ActionEvent e) {

	// get the source of the action that just occured
	//
	String source = e.getActionCommand();
	
	// depending on the source of the event take the appropriate action
	//
	if(source.equals(openStr)) {
	    
	    JFileChooser loadChooser=null;
	    
	    //    if(digraph.paramFile != null) {
	    //		loadChooser = new JFileChooser(digraph.paramFile);
	    // }
	    // else {
	    String cwd = System.getProperty("user.dir");
	    loadChooser = new JFileChooser(cwd);
	    // }
	    
	    // set the font and button text for the load chooser
	    //
	    loadChooser.setFont(newFont);
	    String loadApprove = new String("Load");
	    loadChooser.setApproveButtonText(loadApprove);
	    
	    
	    // display the file chooser object
	    //
	    int returnVal = loadChooser.showOpenDialog(mainWnd);
	    
	    // check to see if a file was selected by the user
	    //
	    if (returnVal == JFileChooser.APPROVE_OPTION) {
		
		// retrieve the selected file
		//
		//	digraph.paramFile = loadChooser.getSelectedFile();
		
		// read the parameters to file
		//
		//	digraph.read();
	    } 
	    
	} else if(source.equals(saveStr)) {
	    
	    // write the parameters to file
	    //
	    if (mainWnd.paramFile != null) {
		mainWnd.write();
	    }
	    else {
		
		// initialize the file chooser object
		//
		String cwd = System.getProperty("user.dir");
		
		JFileChooser saveChooser = new JFileChooser(cwd);
		
		// set the font and button text for the save chooser
		//
		saveChooser.setFont(newFont);
		String saveApprove = new String("Save");
		saveChooser.setApproveButtonText(saveApprove);
		
		// display the file chooser object
		//
		int returnVal = saveChooser.showOpenDialog(mainWnd);
		
		// check to see if a file was selected by the user
		//
		if (returnVal == JFileChooser.APPROVE_OPTION) {
		    
		    // retrieve the selected file
		    //
		    mainWnd.paramFile = saveChooser.getSelectedFile();
		    
		    // write the parameters to file
		    //
		    mainWnd.write();
		}
	    }
	    
	} else if(source.equals(saveAsStr)) {
	    
	    JFileChooser saveChooser = null;
	    
	    // initialize the file chooser object
	    //
	    //   if(digraph.paramFile != null) {
	    //		saveChooser = new JFileChooser(digraph.paramFile);
	    // }
	    // else {
	    String cwd = System.getProperty("user.dir");
	    saveChooser = new JFileChooser(cwd);
	    // }    
	    
	    // set the font and button text for the save chooser
	    //
	    saveChooser.setFont(newFont);
	    String saveApprove = new String("Save");
	    saveChooser.setApproveButtonText(saveApprove);
	    
	    // display the file chooser object
	    //
	    int returnVal = saveChooser.showOpenDialog(mainWnd);
	    
	    // check to see if a file was selected by the user
	    //
	    if (returnVal == JFileChooser.APPROVE_OPTION) {
		
		// retrieve the selected file
		//
		//		digraph.paramFile = saveChooser.getSelectedFile();
		
		// write the parameters to file
		//
		//	digraph.write();
	    } 

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

	    // create and display the configuration panel
	    // *** why not use ConfigBox?
	    // ConfigBoxIn systemInConfig = new ConfigBoxIn(mainWnd, mainWnd.data, mainWnd.vertex, mainWnd.cfg_panel, 0);
	    //	    systemInConfig.pack();
	    // systemInConfig.setVisible(true);
	} else if(source.equals(clearStr)) {

	    // reset the param file name
	    //
	    //  digraph.paramFile = null;

	    // restore to defaults
	    //
	    mainWnd.config_box.defaultSettings();

	    // refresh the work area
	    //
	    //	    mainWnd.cfg_panel.repaint();

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

	    // exit without saving if nothing has changed
	    //
	    // if (!mainWnd.saveWork) {
		System.exit(0);
		// }

	    // popup a dialogue that prompts the user to save his/her changes
	    //
	    JFrame frame = new JFrame("Warning");
	    int ans = JOptionPane.showConfirmDialog(frame, "Would you like to save your current changes?", "Warning", JOptionPane.YES_NO_CANCEL_OPTION);

	    // get the users choice
	    //
	    if (ans == JOptionPane.YES_OPTION) {

		// initialize the file chooser object
		//
		JFileChooser saveChooser = new JFileChooser();
		
		// set the font and button text for the save chooser
		//
		saveChooser.setFont(newFont);
		String saveApprove = new String("Save");
		saveChooser.setApproveButtonText(saveApprove);
		
		// display the file chooser object
		//
		int returnVal = saveChooser.showOpenDialog(mainWnd);
		
		// check to see if a file was selected by the user
		//
		if (returnVal == JFileChooser.APPROVE_OPTION) {
		    
		    // retrieve the selected file
		    //
		    // digraph.paramFile = saveChooser.getSelectedFile();
		    
		    // write the parameters to file
		    //
		    // digraph.write();
		}

		// close the application
		//
		System.exit(0);
	    }
	    
	}
    }

    // method: constrain
    //
    // argument: 
    //     Container container: (input) container to place the component
    //     Component component: (input) the component to be constrained
    //     int grid_x: (input) horizontal position of component
    //     int grid_y: (input) vertical position of component
    //     int grid_width: (input) number of horizontal grid spots to take
    //     int grid_height: (input) number of vertical grid spots to take
    //     int fill: (input) directions to fill when component space is resized
    //     int anchor: (input) anchor position when component space is resized
    //     double weight_x: (input)relative amount of horizontal space 
    //                      component should take when its space is resized
    //     double weight_y: (input) relative amount of vertical space component
    //                      should take when its space is resized
    //     int top: (input) amount of inset space on top
    //     int left: (input) amount of inset space on left
    //     int bottom: (input) amount of inset space on bottom
    //     int right: (input) amount of inset space on right
    // 
    // returns: boolean flag indicating status
    //
    // 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.
    //
    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





