// file: $ISIP_IFC/util/demo/isip_demo_selector/ConfigMenu.java
// 2005/05/23 sarnowska


// import necessary java libraries
//
import java.net.*;
import java.io.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import java.util.*;
import javax.swing.border.*;
import java.lang.*;
import java.text.DecimalFormat;

/**
 *  A configuration that is shown inside a dialog.
 */
public class ConfigMenu extends JPanel {  


    //------------------------------------------------------------------------
    //
    // public data
    //
    //------------------------------------------------------------------------
    
    /**
     * Number of arguments parsed from recorder_param file
     */
    public static final int NUMBER_ARGS = 5;  


    //------------------------------------------------------------------------
    //
    // protected data
    //
    //------------------------------------------------------------------------

     /**
     * Apply button.
     */
    protected JButton applyButton;

    /**
     * OK button.
     */
    protected JButton okButton;

    /**
     * Cancel button.
     */
    protected JButton cancelButton;

    /**
     * Reset button.
     */
    protected JButton resetButton;

    /**
     * Default Settings button.
     */
    protected JButton defaultButton;

    /**
     * Boolean value ok to represent the status of the config.
     */
    protected boolean ok;

    /**
     * Dialog box to hold the configution.
     */
    protected JDialog dialog;

    /**
      * Default value of server port
      */
    protected int processPort_DEF = 9000;

    /**
     * filename containing configuration information
     */
    protected String filename;

     /**
      * Default value of first part of server and hub port IP
      */
    protected int ip1_DEF = 127;

    /**
      * Default value of second part of server and hub port IP
      */
    protected int ip2_DEF = 0;

    /**
      * Default value of third part of server and hub port IP
      */
    protected int ip3_DEF = 0;

    /**
      * Default value of fourth part of server and hub port IP
      */
    protected int ip4_DEF = 1;

    /**
      * Default value of hub port
      */
    protected int uiPort_DEF = 25010;

    /**
     * Default value for Mysql Database username
     */
    protected String username_DEF = "dialog";

    /**
     * Default value for Mysql Database password
     */
    protected String password_DEF = "pass123";

    /**
     * Default value for Mysql Database database name
     */
    protected String databaseName_DEF = "flatroute";

    /**
     * Array of argument [][0] tags and corresponding 
     * values [][1] parsed from recorder_param file
     */
    protected String[][] argArray = new String [NUMBER_ARGS][2];

    /**
     * Array of indicators if argument valid values exist from recoder_param file
     */
    protected boolean[][] argErrors = new boolean [NUMBER_ARGS][2];

    /**
     * Array of comma separated IP port values for server and hub
     */
    protected String[][] ipParts = new String [3][4];

     /**
     * Menu fields and labels
     */
    protected JLabel fieldLabel;
    protected JLabel ipSpacerLabel;
    protected JTextField processPortField;
    protected JTextField processIPField1;
    protected JTextField processIPField2;
    protected JTextField processIPField3;
    protected JTextField processIPField4;
    protected JTextField hubIPField1;
    protected JTextField hubIPField2;
    protected JTextField hubIPField3;
    protected JTextField hubIPField4;
    protected JTextField uiPortField; //client_gui port
    protected JComboBox modeDropField;
    protected JRadioButton clientSelect;
    protected JRadioButton serverSelect;
    protected JTextField hostIPField1;
    protected JTextField hostIPField2;
    protected JTextField hostIPField3;
    protected JTextField hostIPField4;
    protected JTextField usernameField;
    protected JTextField passwordField;
    protected JTextField databaseNameField;
    
    /**
     * Constructor.
     */
    public ConfigMenu(String fname) {  
	
	filename = fname;
	setLayout(new BorderLayout());
	
	// create Apply button to update changes
	//
	applyButton = new JButton("Apply");
	applyButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent event) {
		    if (checkInputIntegrity())
			writeToFile(filename);
		    initialFileCheck(filename);
		    loadFileSettings();
		}
	    });
	
	// create Ok buttons that terminate the dialog
	//
	okButton = new JButton("OK");
	okButton.addActionListener(new  ActionListener() 
	    {
		public void actionPerformed(ActionEvent event) 
		{
		    if (checkInputIntegrity()){
			writeToFile(filename);
			
			if (dialog != null) {
			    dialog.setVisible(false);
			}
		    }
		}
	    });
	
	// create Reset button to reset all values to values in file
	//
	resetButton = new JButton("Reset");
	resetButton.addActionListener(new ActionListener() 
	    {
		public void actionPerformed(ActionEvent event) 
		{
		    initialFileCheck(filename);
		    loadFileSettings();		    
		}
	    });

	// create Default Settings button to set all values to default values
	//
	defaultButton = new JButton("Defaults");
	defaultButton.addActionListener(new ActionListener() 
	    {
		public void actionPerformed(ActionEvent event) 
		{
		    setDefaults();		    
		}
	    });
	
	// create Cancel button to terminate the dialog
	//
	cancelButton = new JButton("Cancel");
	cancelButton.addActionListener(new ActionListener() 
	    {
		public void actionPerformed(ActionEvent event) 
		{
		    // exit without saving but check for field validity
		    //
		    if (checkInputIntegrity()){
			
			if (dialog != null) {
			    dialog.setVisible(false);
			}
		    }
		}
	    });
	
	
	// add buttons to southern border
	//
	JPanel buttonPanel = new JPanel();
	buttonPanel.setLayout(new BorderLayout());
	JPanel northbuttonPanel =  new JPanel();
	JPanel southbuttonPanel =  new JPanel();
	northbuttonPanel.add(applyButton);
	northbuttonPanel.add(okButton);
	southbuttonPanel.add(resetButton);
	southbuttonPanel.add(defaultButton);
	northbuttonPanel.add(cancelButton);
	buttonPanel.add(northbuttonPanel, BorderLayout.NORTH);
	buttonPanel.add(southbuttonPanel, BorderLayout.SOUTH);
	add(buttonPanel, BorderLayout.SOUTH);

	//create display panel
	//
	JPanel configPanel = new JPanel();
	
	//  make this config panel use flowlayout
	//
	configPanel.setLayout(new FlowLayout());
	
	// create the border and set the proper font
	//
	BorderUIResource.TitledBorderUIResource border =
	    new BorderUIResource.TitledBorderUIResource("");
	border.setTitleColor(Color.black);

	this.setBorder(border);

	//  make this config panel use grid bag layout
	//
	GridBagLayout gridbag = new GridBagLayout();
	configPanel.setLayout(gridbag);
	this.add(configPanel);

	// label counter
	//
	int k = 0;
	int ipCount;

	// Process IP Address aka demo manager port
       	//
	fieldLabel = new JLabel("Demo Manager IP");
	fieldLabel.setForeground(Color.black);

	// places component into panel according to gridbag layout specs
	//
	constrain(configPanel, fieldLabel, gridbag, 0, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);

	// add process IP Address input fields aka Server IP input fields
	//
	ipCount = 7;
       	
	ipSpacerLabel = new JLabel(".");
	ipSpacerLabel.setForeground(Color.black);
	
      	processIPField1 = new JTextField(Integer.toString(ip1_DEF),3);	
	constrain(configPanel, processIPField1, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;
	ipSpacerLabel = new JLabel(".");
	constrain(configPanel, ipSpacerLabel, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;

	processIPField2 = new JTextField(Integer.toString(ip2_DEF),3);	
	constrain(configPanel, processIPField2, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;
	ipSpacerLabel = new JLabel(".");
	constrain(configPanel, ipSpacerLabel, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;

      	processIPField3 = new JTextField(Integer.toString(ip3_DEF),3);	
	constrain(configPanel, processIPField3, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;
	ipSpacerLabel = new JLabel(".");
	ipSpacerLabel.setForeground(Color.black);
	constrain(configPanel, ipSpacerLabel, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;

	processIPField4 = new JTextField(Integer.toString(ip4_DEF),3);		
	constrain(configPanel, processIPField4, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);

	k++;

	// Process Port aka Demo Manager Port
       	//
	fieldLabel = new JLabel("Demo Manager Port");
	fieldLabel.setForeground(Color.black);

	constrain(configPanel, fieldLabel, gridbag, 0, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);

	// add process port input field aka server port input field
	//
	processPortField = new JTextField(Integer.toString(processPort_DEF), 4);	
	constrain(configPanel, processPortField, gridbag, 13, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	k++;

	// Mode (indicate default here)
       	//
	fieldLabel = new JLabel("Mode");
	fieldLabel.setForeground(Color.black);

	constrain(configPanel, fieldLabel, gridbag, 0, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);

	serverSelect = new JRadioButton("Server");
       	clientSelect = new JRadioButton("Client", true);
	
	ButtonGroup modeGroup = new ButtonGroup();
	modeGroup.add(serverSelect);
	modeGroup.add(clientSelect);
    
	constrain(configPanel, serverSelect, gridbag, 7, k,
		  3, 1, 1, 0, GridBagConstraints.WEST);
	constrain(configPanel, clientSelect, gridbag, 11, k,
		  3, 1, 1, 0, GridBagConstraints.WEST);

	k++;

	// Hub IP Address
       	//
	fieldLabel = new JLabel("Hub IP           ");
	fieldLabel.setForeground(Color.black);

	constrain(configPanel, fieldLabel, gridbag, 0, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);

	// add hub IP Address input fields
	//
	ipCount = 7;
       	
      	hubIPField1 = new JTextField(Integer.toString(ip1_DEF),3);	
	constrain(configPanel, hubIPField1, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;
	ipSpacerLabel = new JLabel(".");
	constrain(configPanel, ipSpacerLabel, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;

	hubIPField2 = new JTextField(Integer.toString(ip2_DEF),3);	
	constrain(configPanel, hubIPField2, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;
	ipSpacerLabel = new JLabel(".");
	constrain(configPanel, ipSpacerLabel, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;

      	hubIPField3 = new JTextField(Integer.toString(ip3_DEF),3);	
	constrain(configPanel, hubIPField3, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;
	ipSpacerLabel = new JLabel(".");
	ipSpacerLabel.setForeground(Color.black);
	constrain(configPanel, ipSpacerLabel, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	ipCount++;

	hubIPField4 = new JTextField(Integer.toString(ip4_DEF),3);		
	constrain(configPanel, hubIPField4, gridbag, ipCount, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);
	k++;

	// UI Port aka Client Gui Port
       	//
	fieldLabel = new JLabel("Client GUI Port");
	fieldLabel.setForeground(Color.black);

	constrain(configPanel, fieldLabel, gridbag, 0, k,
		  1, 1, 1, 0, GridBagConstraints.WEST);

	// add process port input field
	//
	uiPortField = new JTextField(Integer.toString(uiPort_DEF),4);	
	constrain(configPanel, uiPortField, gridbag, 13, k,
		  2, 1, 1, 0, GridBagConstraints.WEST);
	k++;
    }
    
    /**
     * Place object into a GridBagLayout format. 
     *
     *@param container_a Panel component belongs to
     *@param button_a Component
     *@param gridbag_a Name of GridBagLayout component belongs to
     *@param gridx_a Cell containing leading edge of component's display area
     *@param gridy_a Cell at top of component's display area
     *@param gridwidth_a Number of cells in row for component's display area
     *@param gridheight_a Number of cells in column for component's display area
     *@param weightx_a How to distribute extra horizontal space
     *@param weighty_a How to distribute extra vertical space
     *@param anchor_a Determines where to place it component in display area
     *
     *@return a boolean value always true
     */
    public boolean constrain(JPanel container_a, Component button_a, 
			  GridBagLayout gridbag_a, 
			  int gridx_a, int gridy_a, 
			  int gridwidth_a, int gridheight_a,
			  int weightx_a, int weighty_a, 
			  int anchor_a) {

	GridBagConstraints c = new GridBagConstraints();
	c.gridx = gridx_a;
	c.gridy = gridy_a;
	c.gridwidth = gridwidth_a;
	c.gridheight = gridheight_a;
	c.weightx = weightx_a;
	c.weighty = weighty_a;
	c.anchor = anchor_a;
	gridbag_a.setConstraints(button_a, c);
	container_a.add(button_a);

	// exit gracefully
	//
	return true;
    }

    /**
     * Set menu fields to contain default values.
     */
    public void setDefaults(){
	
	//set values in input fields to defaults
	processPortField.setText(Integer.toString(processPort_DEF));
	processIPField1.setText(Integer.toString(ip1_DEF));
	processIPField2.setText(Integer.toString(ip2_DEF));
	processIPField3.setText(Integer.toString(ip3_DEF));
	processIPField4.setText(Integer.toString(ip4_DEF));
	hubIPField1.setText(Integer.toString(ip1_DEF));
	hubIPField2.setText(Integer.toString(ip2_DEF));
	hubIPField3.setText(Integer.toString(ip3_DEF));
	hubIPField4.setText(Integer.toString(ip4_DEF));
	uiPortField.setText(Integer.toString(uiPort_DEF));
   	clientSelect.setSelected(true);
    }

    /**
     * Write menu field values to file.
     *
     *@param filename file containing configuration info
     *
     *@return boolean value that is true if file write successful
     */

    public boolean writeToFile(String filename) {

	ObjectOutputStream output;
	boolean success = true;
	
	// create updated recorder_param file
	//
	try {
	    BufferedWriter out = new BufferedWriter(new FileWriter(filename));

	    out.write("-demo_manager_port ");
	    out.write(processPortField.getText());

	    out.write("\n-demo_manager_ip_address ");
	    out.write(processIPField1.getText());
	    out.write(".");
	    out.write(processIPField2.getText());
	    out.write(".");
	    out.write(processIPField3.getText());
	    out.write(".");
	    out.write(processIPField4.getText());

	    out.write("\n-hub_ip ");
	    out.write(hubIPField1.getText());
	    out.write(".");
	    out.write(hubIPField2.getText());
	    out.write(".");
	    out.write(hubIPField3.getText());
	    out.write(".");
	    out.write(hubIPField4.getText());

	    out.write("\n-mode ");
	    if (clientSelect.isSelected())
		out.write("client");
	    else
		out.write("server");

	    out.write("\n-client_port ");
	    out.write(uiPortField.getText());
	    out.close();
	} 
	catch (IOException e) {
	    System.out.println("error while writing updated "+filename+" file");
	    success = false;
	}
	return success;
    }

    /**
     * Checks validity of menu values
     */
    public boolean checkInputIntegrity(){
	
	boolean success = true;
	int i;
	int j;
	boolean[] isInvalidInput = new boolean[NUMBER_ARGS];
	String error = "Invalid Input Values\n";

	for (i = 0; i < NUMBER_ARGS; i++) {
		isInvalidInput[i] = false;	   
	}
	
	// valid process port between 1 and 65536?
	//
	try {
	    if ( Integer.parseInt(processPortField.getText()) <= 0 
		 || Integer.parseInt(processPortField.getText()) > 65536 ){
		success = false;
		isInvalidInput[0] = true;
		error += "  Server port must be between 1 and 65536\n"; 
	    }
	}
	catch (NumberFormatException e) {
	    isInvalidInput[0] = true;
	    success = false;
	    error += "  Server port must be an integer value\n"; 
	}

	// valid ui port between 1 and 65536?	
	//
	try {
	    if ( Integer.parseInt(uiPortField.getText()) <= 0 
		 || Integer.parseInt(uiPortField.getText()) > 65536 ){
		success = false;
		isInvalidInput[4] = true;
		error += " Hub port must be between 1 and 65536\n"; 
	    }
	    
	}
	catch (NumberFormatException e) {
	    isInvalidInput[4] = true;
	    success = false;
	    error += "  Hub port must be an integer value\n"; 
	}
	
	// valid process ip address?
	//
	try {
	    if ( Integer.parseInt(processIPField1.getText()) < 0 
		 || Integer.parseInt(processIPField1.getText()) > 255) {
		success = false;
		isInvalidInput[1] = true;
	    }
	    if ( Integer.parseInt(processIPField2.getText()) < 0 
		 || Integer.parseInt(processIPField2.getText()) > 255) {
		success = false;
		isInvalidInput[1] = true;
	    }
	    if ( Integer.parseInt(processIPField3.getText()) < 0 
		 || Integer.parseInt(processIPField3.getText()) > 255) {
		success = false;
		isInvalidInput[1] = true;
	    }
	    if ( Integer.parseInt(processIPField4.getText()) < 0 
		 || Integer.parseInt(processIPField4.getText()) > 255) {
		success = false;
		isInvalidInput[1] = true;
	    }
	
	    if ( Integer.parseInt(processIPField1.getText()) == 0 ){
		success = false;
		isInvalidInput[1] = true;
	    }

	    
	}
	catch (NumberFormatException e) {
	    success = false;
	    isInvalidInput[1] = true;
	}

	if (isInvalidInput[1])
		error += "  Invalid server IP\n";
	
	// valid hub ip address?
	//
	try {
	    if ( Integer.parseInt(hubIPField1.getText()) < 0 
		 || Integer.parseInt(hubIPField1.getText()) > 255) {
		success = false;
		isInvalidInput[2] = true;
	    }
	    if ( Integer.parseInt(hubIPField2.getText()) < 0 
		 || Integer.parseInt(hubIPField2.getText()) > 255) {
		success = false;
		isInvalidInput[2] = true;
	    }
	    if ( Integer.parseInt(hubIPField3.getText()) < 0 
		 || Integer.parseInt(hubIPField3.getText()) > 255) {
		success = false;
		isInvalidInput[2] = true;
	    }
	    if ( Integer.parseInt(hubIPField4.getText()) < 0 
		 || Integer.parseInt(hubIPField4.getText()) > 255) {
		success = false;
		isInvalidInput[2] = true;
	    }
	
	    if ( Integer.parseInt(hubIPField1.getText()) == 0 ){
		success = false;
		isInvalidInput[2] = true;
	    }
	}
	catch (NumberFormatException e) {
	    success = false;
	    isInvalidInput[2] = true;
	}
  
	if (isInvalidInput[2])
		error += "  Invalid hub IP\n";

	if (!success) {
	    // pop up warning
	    //
	    JOptionPane.showMessageDialog(null, error, 
					  "Input Error", 
					  JOptionPane.PLAIN_MESSAGE);
	}

	return success;
    }
  
    /**
     * Updates values shown in configuration menu
     * to current values existing in configuration file
     */
  
    public void loadFileSettings(){
	// set process field
	//   
	processPortField.setText(argArray[0][1]);
       
	// set process ip
	//    
	processIPField1.setText(ipParts[0][0]);
	    processIPField2.setText(ipParts[0][1]);
	    processIPField3.setText(ipParts[0][2]);
	    processIPField4.setText(ipParts[0][3]);
	    
	    // set hub ip
	    //
	    hubIPField1.setText(ipParts[1][0]);
	    hubIPField2.setText(ipParts[1][1]);
	    hubIPField3.setText(ipParts[1][2]);
	    hubIPField4.setText(ipParts[1][3]);
	    
	    // set mode
	    //
	    if (argArray[3][1].equals("client"))
		clientSelect.setSelected(true);
	    else
		serverSelect.setSelected(true);
	    
	    // set ui port
	    //
	    uiPortField.setText(argArray[4][1]);
	    
    }
    
    /**
     *Ran upon startup of dialog menu 
     *Extracts configuration setting from file 
     *Checks if all values are present and valid
     *Otherwise pops up warning message
     *
     *@param filename file containing configuration info
     */

     public boolean initialFileCheck(String filename) {
	
	boolean totalSuccess = true; // true if all valid and complete inputs
	boolean success = true; // true if all inputs are valid
	boolean isLackingInput = false;//true if lacking any input values
	String error = "";
	boolean[] isInvalidInput = new boolean[NUMBER_ARGS];
	FileReader paramFile;

	try{
	    // create file if and only if file does not exist
	    //
	    File file = new File(filename);

	    file.createNewFile();
	    BufferedReader readFile = new BufferedReader(new FileReader(filename));
	    String line;
	    String paramStr = "";
	    int i;
	    int j;
	    int loc;
	    int start;
	    int end;
	    int countDot = 0;
	    String space = " ";
	    String dot = ".";

	    // tags for headers
	    //
	    argArray[0][0] = "-demo_manager_port";
	    argArray[1][0] = "-demo_manager_ip_address";
	    argArray[2][0] = "-hub_ip";
	    argArray[3][0] = "-mode";
	    argArray[4][0] = "-client_port";

	    for (i = 0; i < java.lang.reflect.Array.getLength(argArray); i++) {
		argArray[i][1] = "";
		for (j = 0; j < 2; j++){
		    argErrors[i][j] = false;
		}
	    }
	    	    	    
	    // process each line in file until end of file
	    //
	    while ((line = readFile.readLine()) != null) {
		paramStr = line;

		// find tags: if exist store value
		//
		for (i = 0; i < java.lang.reflect.Array.getLength(argArray); i++) {
		    
		    if ( (loc = line.indexOf(argArray[i][0])) != -1 ) {
			argErrors[i][0] = true;
			
			start = (loc + argArray[i][0].length());
			
			end = line.indexOf(space, start+1);
			if (end == -1) 
			    end = line.length();
			    
			if ( (start+1) > line.length()) {
			    
			    System.out.println("no value for arg "+i);
			    // end of line 
			}
			else { // check for value associated with tag
			    argArray[i][1] = line.substring(start+1, end);

			    if ( (argArray[i][1].length() == 0 ) 
				 || argArray[i][1].equals(" "))
				System.out.println("  no value");		   
			    else
				argErrors[i][1] = true;
			    
			    // false parameter reading of next tag
			    //
			    if (argArray[i][1].startsWith("-")){
				argArray[i][1] = "";
				argErrors[i][0] = false;
				argErrors[i][1] = false;
			    }
			}
			    
		    }
		}
		
	    }

	    if (argErrors[1][1] == true){

		// check to make sure format is valid containing 3 periods
		//
		countDot = 0;
		if ((loc = argArray[1][1].indexOf(dot)) != -1) {
		    countDot++;
		    if ((loc = argArray[1][1].indexOf(dot)) != -1) {
			countDot++;
			if ((loc = argArray[1][1].indexOf(dot)) != -1) {
			    countDot++;
			}
		    }
		}
		    
		if (countDot == 3) {
		    // get process ip
		    //
		    start = 0;
		    end = argArray[1][1].indexOf(dot);	
		    ipParts[0][0] = argArray[1][1].substring(start, end);
		    
		    start = argArray[1][1].indexOf(dot, end);
		    end = argArray[1][1].indexOf(dot, start+1);
		    ipParts[0][1] = argArray[1][1].substring(start+1, end);
		    
		    start = argArray[1][1].indexOf(dot, end);
		    end = argArray[1][1].indexOf(dot, start+1);
		    ipParts[0][2] = argArray[1][1].substring(start+1, end);
		    
		    start = argArray[1][1].indexOf(dot, end);
		    end = argArray[1][1].length();
		    ipParts[0][3] = argArray[1][1].substring(start+1, end);
		}
		
	    }
	    
	    if (argErrors[2][1] == true){
		
		// check to make sure format is valid containing 3 periods
		//
		countDot = 0;
		if ((loc = argArray[1][1].indexOf(dot)) != -1) {
		    countDot++;
		    if ((loc = argArray[1][1].indexOf(dot)) != -1) {
			countDot++;
			if ((loc = argArray[1][1].indexOf(dot)) != -1) {
			    countDot++;
			}
		    }
		}
		    
		if (countDot == 3) {
		// get hub ip
		//
		    start = 0;
		end = argArray[2][1].indexOf(dot);
		ipParts[1][0] = argArray[2][1].substring(start, end);

		start = argArray[2][1].indexOf(dot, end);
		end = argArray[2][1].indexOf(dot, start+1);
		ipParts[1][1] = argArray[2][1].substring(start+1, end);
	    
		start = argArray[2][1].indexOf(dot, end);
		end = argArray[2][1].indexOf(dot, start+1);
		ipParts[1][2] = argArray[2][1].substring(start+1, end);
	    
		start = argArray[2][1].indexOf(dot, end);
		end = argArray[2][1].length();
		ipParts[1][3] = argArray[2][1].substring(start+1, end);
		}
	    		
	    }

	    // check if any fields or values are missing
	    //
	    for (i = 0; i < java.lang.reflect.Array.getLength(argErrors); i++) {
		for (j = 0; j < 2; j++){
		    
		    if( argErrors[i][j] == false){
			isLackingInput = true;
		    }
		}
	    }

	    if (isLackingInput){
		error += "Configuration file recorder_param is lacking values.\n";
	    }
	    
	    
	    
	    for (i = 0; i < 5; i++) {
		isInvalidInput[i] = false;	   
	    }
	    
	    // check file data integrity
	    //
	    for (i = 0; i < java.lang.reflect.Array.getLength(argArray); i++) {
		
		if( argErrors[i][1] == true) {
		    
		    switch(i){
		    case 0:
		    case 4:
			// valid port between 1 and 65536?
			try {
			    if ( Integer.parseInt(argArray[i][1]) <= 0 
				 || Integer.parseInt(argArray[i][1]) > 65536 ){
				success = false;
				isInvalidInput[i] = true;
			    }
			}
			catch (NumberFormatException e) {
			    isInvalidInput[i] = true;
			    success = false;
			}
			break;
		    case 1:
		    case 2:
			for (j = 0; j < 4; j++) {
			    try {
				if ( Integer.parseInt(ipParts[i-1][j]) < 0 
				     || Integer.parseInt(ipParts[i-1][j]) > 255) {
				    success = false;
				    isInvalidInput[i] = true;
				}
				
				if ( j == 0 )
				    if (Integer.parseInt(ipParts[i-1][j]) == 0 ){
					success = false;
					isInvalidInput[i] = true;
				    }
			    }
			    catch (NumberFormatException e) {
				success = false;
				isInvalidInput[i] = true;
			    }
			}
			break;
		    }
		}
	    }
	    
	    readFile.close();
	} 
	catch (IOException e) {
	    
	    System.out.println("error while processing recorder_param file");
	    success = false;
	}
	
	if (!success){
	    error += "Invalid values in configuration file recorder_param.\n";
	}
	
	for (int i=0; i< NUMBER_ARGS; i++){
	    if (isInvalidInput[i]){
		totalSuccess = false;
	    }
	}
	
	if ( ((!success) || (isLackingInput)) && (!isShowing()) ){
	    JOptionPane.showMessageDialog(null, error, "Error",
					  JOptionPane.PLAIN_MESSAGE);
	    totalSuccess = false;
	}
	
	return totalSuccess;
    }

    /**
     *Makes pop up menu dialog visible
     *
     *@param parent Component menu belongs to
     *@param title Menu title to be displayed 
     */
    public boolean showDialog(Component parent, String title) {
	
	ok = false;
	
	// locate the owner frame
	//
	Frame owner = null;
	if (parent instanceof Frame)
	    owner = (Frame) parent;
	else 
	    owner = (Frame)SwingUtilities.getAncestorOfClass(
							     Frame.class, parent);
	
	// if first time, or if owner has changed, make new dialog
	//
	if (dialog == null || dialog.getOwner() != owner)  {
	    
	    dialog = new JDialog(owner, true);
	    dialog.getContentPane().add(this);
	    dialog.getRootPane().setDefaultButton(okButton);
	    dialog.pack();
	}
	
	// set title and show dialog
	//
	dialog.setTitle(title);

	Toolkit tk = Toolkit.getDefaultToolkit();
	Dimension d = tk.getScreenSize();
	int screenHeight = d.height;
  	int screenWidth = d.width;

	dialog.setLocation( screenWidth / 4, screenHeight / 4);
	dialog.setVisible(true);

	return ok;
    }

    /** 
    * Main driver program. Used to test this class separately.
    *
    * @param args arguments from the command line.
    */
    public static void main(String args[]) {
	
        final ConfigMenu splot = new ConfigMenu("recorder_param.txt");
	
        WindowListener l = new WindowAdapter() {
                public void windowClosing(WindowEvent e) { System.exit(0); }
            };
	
        JFrame f = new JFrame("Configuration");

	f.addWindowListener(l);
        f.getContentPane().add("Center", splot);
        f.pack();
        f.setVisible(true);

    }

}
