// file: MainMenu.java
//

// import system files
//
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
//
// hierarchy: JPanel->SubPanel->MainMenu
//

//import Designer;
//import Constants;

class MainMenu extends SubPanel implements ActionListener, ItemListener, Constants {
    
    // declare components
    //
    private final static int FRAME_SEPARATION = 2;
    private int design_type;
    private Designer designer;
    
    // set clear flag
    //
    public boolean clear_flag = true;
    
    // declare the card layout identifier names
    //
    String magnitudeParams = new String("Magnitude");
    String impulseParams = new String("Impulse");
    String phaseParams = new String("Phase");
    String datainputParams = new String ("Input");

    // declare varaibles to read the help file
    //
    URL url;
    Vector urlvec;
    String document;
    BufferedReader buff;    
    AppletContext context;

    // declare the menu font layout
    //
    Font currentFont = getFont();
    Font newFont = new Font(currentFont.getName(), currentFont.getStyle(), 12);
    
    // declare the menu layout
    //
    JMenuBar menuBar;
    JMenu menu;
    JMenuItem menuItem;
    JMenuItem prevMenuItem;
    JMenuItem nextMenuItem;
    JRadioButtonMenuItem rbMenuItem;
    
    // declare components for the help box
    //
    JFrame frame;     
    JTextArea textArea;
    JScrollPane scrollPane;
    JButton dismiss;
    
    // declare menu title strings
    //
    String modeOption = new String("Mode");
    String editOption = new String("Edit");
    String goOption = new String("Go");
    
    String add_menuItem = new String("Add");
    String pole_menuItem = new String("Add Pole");
    String zero_menuItem = new String("Add Zero");
    String analog_menuItem = new String("Analog");
    String digital_menuItem = new String("Digital");
    String clear_menuItem = new String("Clear");
    String update_menuItem = new String("Update");
    String start3d_menuItem = new String("Start3D");
    String impulse_response_menuItem = new String("Impulse Response");
    String frequency_response_menuItem = new String("Frequency Response");
    
    // declare strings for the Parameters menu
    //
    String viewOption = new String("Parameters");
    String magnitudeCard = new String("Magnitude");
    String phaseCard = new String("Phase");
    String impulseCard = new String("Impulse");
    String datainputCard = new String("Input");
    
    //declare strings for the Help menu
    //
    String helpOption = new String("Help");
    String tutorial = new String("Applet Tutorial");
    String overview = new String("Applet Overview");
    String release = new String("Release Notes");
    String info = new String("Information and Support");
    String plugins = new String("About Plug-ins");
    String done = new String("Dismiss");

    // declare menu description strings
    //
    String newline = new String("\n");

    
    
    // *********************************************************************
    //
    // class constructor
    //
    // *********************************************************************
    
    MainMenu(Designer designer_a, int type, Vector vec, AppletContext con) {
        super();
	
	// initialize the input and output panel objects
        //
	urlvec = vec;
	context = con;
	designer = designer_a;
	design_type = type;
	
	// create a border around the mainmenu
        //
        setBorder(BorderFactory.createEtchedBorder());
	
        // create components for the help box
        //
        frame = new JFrame();
	
        // create the textbox to view the help message
        //
        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true); 
        textArea.setEditable(false);

	// make the text area scrollable
	//
	scrollPane = new JScrollPane(textArea);
	scrollPane.setPreferredSize(new Dimension(400, 450));

	// create a dismiss button for the text area
	//
	dismiss = new JButton(done);
	dismiss.setActionCommand(done);
	dismiss.addActionListener(this);
	
        // add the components to the frame
        //
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.getContentPane().add(dismiss, BorderLayout.SOUTH);
	
        // create the menu bar item
        //
        menuBar = new JMenuBar();
	
        // bulid the Mode menu
        //
        menu = new JMenu("Mode");
        menu.setFont(newFont);
        menu.getAccessibleContext().setAccessibleDescription(modeOption);
        menuBar.add(menu);
	
	// Add 'Analog' Option to Mode menu
	menuItem = new JMenuItem(analog_menuItem);
	menuItem.setFont(newFont);
        menuItem.addActionListener(this);
        menuItem.setActionCommand(analog_menuItem);
	menu.add(menuItem);
	
	// Add 'Digital' Option to Mode menu
	//
	menuItem = new JMenuItem(digital_menuItem);
	menuItem.setFont(newFont);
        menuItem.addActionListener(this);
        menuItem.setActionCommand(digital_menuItem);
	menu.add(menuItem);
	
	// Build Edit menu
	//
	menu = new JMenu("Edit");
	menu.setFont(newFont);
        menu.getAccessibleContext().setAccessibleDescription(editOption);
        menuBar.add(menu);
	
	// add pole menu item to 'Edit' menu
	//
	menuItem = new JMenuItem(pole_menuItem);
	menuItem.setFont(newFont);
        menuItem.addActionListener(this);
        menuItem.setActionCommand(pole_menuItem);
	menu.add(menuItem);
	
	// add zero menu item to 'Edit' menu
	//
	menuItem = new JMenuItem(zero_menuItem);
	menuItem.setFont(newFont);
        menuItem.addActionListener(this);
        menuItem.setActionCommand(zero_menuItem);
	menu.add(menuItem);
	
	// bulid the Parameters menu
	//
	menu = new JMenu("Parameters");
	menu.setFont(newFont);
	menu.getAccessibleContext().setAccessibleDescription(viewOption);
	menuBar.add(menu);
	
	// add the scale option to the sub menu
	//
	menuItem = new JMenuItem("Input");
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(datainputCard);
	menu.add(menuItem);
	
	// add the scale option to the sub menu
	//
	menuItem = new JMenuItem("Magnitude");
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(magnitudeCard);
	menu.add(menuItem);
	
	// add the scale option to the sub menu
	//
	menuItem = new JMenuItem("Phase");
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(phaseCard);
	menu.add(menuItem);
	
	// add the scale option to the sub menu
	//
	menuItem = new JMenuItem("Impulse");
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(impulseCard);
	menu.add(menuItem);
	
        // bulid the Go menu
        //
        menu = new JMenu("Go");
        menu.setFont(newFont);
        menu.getAccessibleContext().setAccessibleDescription(goOption);
        menuBar.add(menu);
	
	// Add Frequency Response menuitem to 3D Viewer
	//
	menuItem = new JMenuItem(frequency_response_menuItem);
	menuItem.setFont(newFont);
        menuItem.addActionListener(this);
        menuItem.setActionCommand(frequency_response_menuItem);
	menu.add(menuItem);
	
	// Add Clear menu item to Go
	//
	menuItem = new JMenuItem(clear_menuItem);
	menuItem.setFont(newFont);
        menuItem.addActionListener(this);
        menuItem.setActionCommand(clear_menuItem);
	menu.add(menuItem);
	
	
        // bulid the Help menu
        //
        menu = new JMenu("Help");
        menu.setFont(newFont);
        menu.getAccessibleContext().setAccessibleDescription(helpOption);
        menuBar.add(Box.createHorizontalGlue());
        menuBar.add(menu);
	
	// add the Release option to the menu
	//
	menuItem = new JMenuItem("Release Notes");
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(release);
	menu.add(menuItem);

	// add the Information option to the menu
	//
	menuItem = new JMenuItem("Information and Support");
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(info);
	menu.add(menuItem);

	menu.addSeparator();

	// add the Overview option to the menu
	//
	menuItem = new JMenuItem("Applet Overview");
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(overview);
	menu.add(menuItem);

	// add the Tutorial option to the menu
	//
	menuItem = new JMenuItem("Applet Tutorial");
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(tutorial);
	menu.add(menuItem);

	menu.addSeparator();

	// add the Plug-ins option to the menu
	//
	menuItem = new JMenuItem("About Plug-ins");
	menuItem.setFont(newFont);
	menuItem.addActionListener(this);
	menuItem.setActionCommand(plugins);
	menu.add(menuItem);
    }
    
    // method: add_components
    //
    // arguments: none
    // return   : none
    //
    // adds components to the mainmenu
    //
    void add_components () {
        
	constrain(this, menuBar, 0, 0, 1, 1, GridBagConstraints.BOTH,
		  GridBagConstraints.NORTH, 1.0, 0.0, 0,0,0,0);//FRAME_SEPARATION, 
	//	      FRAME_SEPARATION, FRAME_SEPARATION, FRAME_SEPARATION);
    }
    
    // method: itemStateChanged
    //
    // arguments: none
    // return   : none
    //
    // method that determines if the hold radio button is set or not and 
    // if so it sets a flag that save the decision region points
    //
    public void itemStateChanged(ItemEvent e) {
        JMenuItem source = (JMenuItem)(e.getSource());
        if(e.getStateChange() == ItemEvent.SELECTED) {
            //output.holdFlag = 1;
        } else {
            //output.holdFlag = 0;
        }
    }
    
    
    
    // method: actionPerformed
    //
    // arguments: (ActionEvent) e
    // 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();
	int pole_zero_indicator;
	double frequency, bandwidth;
	
	if(source .equals(clear_menuItem)) {
	    //designer.output_panel.phase_response.clear_flag = true;
	    designer.output_panel.impulse_response.clear_flag = true;
	    //designer.output_panel.mag_response.clear_flag = true;
	    designer.input_panel.pz_map.clear_all();
	}
	
	else if(source .equals(pole_menuItem)) {
	    frequency = Double.valueOf(designer.input_params.entry_text_0.
				       getText()).doubleValue();
	    bandwidth = Double.valueOf(designer.input_params.entry_text_1.
				       getText()).doubleValue();
	    designer.input_panel.data_input.type_ret(0);
	    designer.input_panel.data_input.add_pz(frequency,bandwidth);
	}
	
	else if(source .equals(zero_menuItem)) {
	    frequency = Double.valueOf(designer.input_params.entry_text_0.
				       getText()).doubleValue();
	    bandwidth = Double.valueOf(designer.input_params.entry_text_1.
				       getText()).doubleValue(); 
	    designer.input_panel.data_input.type_ret(1);
	    designer.input_panel.data_input.add_pz(frequency,bandwidth); 
	}
	
	else if(source .equals(analog_menuItem)) {
	    int type;
	    type = designer.get_design_type();
	    if(type != Constants.ANALOG) {
		designer.output_panel.impulse_response.clear_flag = true;
		designer.set_design_type(Constants.ANALOG);
	    }
	}
	
	else if(source .equals(digital_menuItem)) {
	    int type;
	    type = designer.get_design_type();
	    if(type != Constants.DIGITAL) {
		designer.output_panel.impulse_response.clear_flag = true;
		designer.set_design_type(Constants.DIGITAL);
	    }
	}
	
	else if(source .equals(frequency_response_menuItem)) {
	    designer.input_panel.pz_map.set_start3d();
	}
	
	else if(source.equals(magnitudeCard)) {
	    CardLayout cl = (CardLayout)(designer.input_params.cards.getLayout(                         ));
            cl.show(designer.input_params.cards, magnitudeParams);
	}
	
	else if(source.equals(phaseCard)) {
	    CardLayout cl = (CardLayout)(designer.input_params.cards.getLayout());
            cl.show(designer.input_params.cards, phaseParams);
	}
	
	else if(source.equals(impulseCard)) {
	    CardLayout cl = (CardLayout)(designer.input_params.cards.getLayout());
            cl.show(designer.input_params.cards, impulseParams);
	}
	
	else if(source.equals(datainputCard)) {
	    CardLayout cl = (CardLayout)(designer.input_params.cards.getLayout());
            cl.show(designer.input_params.cards, datainputParams);
	}
	
	else if(source.equals(release)) {

	    // clear the text in the text area
            //
            textArea.setText("");

	    // read the help file contents and append them to the text area
	    //
	    try { 

		// get the URL component
		//
		url = (URL)urlvec.elementAt(0);

		// open a file descriptor to read the help file
		//
		InputStreamReader rd = new InputStreamReader(url.openStream());

		// initialize the buffer to read the contents of the help file
		//
		buff = new BufferedReader(rd); 

		// read each line fo the file
		//
		while((document = buff.readLine()) != null) { 

		    // add the help message line to the text area
		    //
		    textArea.append(document + newline);
		}
	    } 
	    catch(IOException ioe) { }

            // display the text area and dismiss button
            //
            frame.pack();
            frame.setVisible(true);
            
	} else if(source.equals(info)) {

	    // clear the text in the text area
            //
            textArea.setText("");

	    // read the help file contents and append them to the text area
	    //
	    try { 

		// get the URL component
		//
		url = (URL)urlvec.elementAt(1);

		// open a file descriptor to read the help file
		//
		InputStreamReader rd = new InputStreamReader(url.openStream());

		// initialize the buffer to read the contents of the help file
		//
		buff = new BufferedReader(rd); 

		// read each line fo the file
		//
		while((document = buff.readLine()) != null) { 

		    // add the help message line to the text area
		    //
		    textArea.append(document + newline);
		}
	    } 
	    catch(IOException ioe) { }

            // display the text area and dismiss button
            //
            frame.pack();
            frame.setVisible(true);

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

	    // clear the text in the text area
            //
            textArea.setText("");

	    // read the help file contents and append them to the text area
	    //
	    try { 

		// get the URL component
		//
		url = (URL)urlvec.elementAt(2);

		// open a file descriptor to read the help file
		//
		InputStreamReader rd = new InputStreamReader(url.openStream());

		// initialize the buffer to read the contents of the help file
		//
		buff = new BufferedReader(rd); 

		// read each line fo the file
		//
		while((document = buff.readLine()) != null) { 

		    // add the help message line to the text area
		    //
		    textArea.append(document + newline);
		}
	    } 
	    catch(IOException ioe) { }

            // display the text area and dismiss button
            //
            frame.pack();
            frame.setVisible(true);
            
	} else if(source.equals(tutorial)) {

	    // display the document
	    //
	    try { 

		// get the URL component
		//
		url = (URL)urlvec.elementAt(3);

		// display the url on a seperate browser
		//
		context.showDocument(url, "_blank");
	    } 
	    catch(Exception ae) { }

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

	    // clear the text in the text area
            //
            textArea.setText("");
            
	    // read the help file contents and append them to the text area
	    //
	    try { 

		// get the URL component
		//
		url = (URL)urlvec.elementAt(4);

		// open a file descriptor to read the help file
		//
		InputStreamReader rd = new InputStreamReader(url.openStream());

		// initialize the buffer to read the contents of the help file
		//
		buff = new BufferedReader(rd); 

		// read each line fo the file
		//
		while((document = buff.readLine()) != null) { 

		    // add the help message line to the text area
		    //
		    textArea.append(document + newline);
		}
	    } 
	    catch(IOException ioe) { }

            // display the text area and dismiss button
            //
            frame.pack();
            frame.setVisible(true);

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

	    // remove the frame form the screen
            //
            frame.setVisible(false);
	}
    }
    
}

//
// end of file
