// file: $ISIP_IFC/util/demo/isip_comm_client_speech_analysis/SpeechAnalysisUIFrame.java
// version: $Id: SpeechAnalysisUIFrame.java 10234 2005-09-13 15:15:36Z stanley $ 
//

// 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 java.util.*;
import javax.swing.border.*;

// import galaxy libraries
//
import galaxy.lang.*;
import galaxy.server.*;
import galaxy.server.ui.ServerUI;

/**
 * This class is used as the user interface for an <code>
 * SpeechAnalysisServer </code> object.
 */
public class SpeechAnalysisUIFrame extends JFrame implements
						      ComponentListener, 
						      ActionListener, 
						      MenuListener {
    
    //-------------------------------------------------------------------------
    //
    // public constants
    //
    //-------------------------------------------------------------------------
    
    /**
     * UI server volume index.
     */
    public static final int UISERVER_VOLUME_INDEX = 0;
    
    /**
     * UI server energy index.
     */
    public static final int UISERVER_ENERGY_INDEX = 1;

    /**
     * Maximum number of characters in the display window.
     */
    public static final int MAX_CHARACTERS = 3000;

    /**
     * Default UI height.
     */
    public static final int MAX_UI_HEIGHT = 700;

    /**
     * Default UI width.
     */
    public static final int MAX_UI_WIDTH = 1000;

    /**
     * Append the text.
     */
    public static final int APPEND = 0;
    
    /**
     * Clear the document.
     */
    public static final int CLEAR = 1;

    /**
     * Append a new line ("\n") at the end of file.
     */
    public static final int APPEND_NEWLINE = -1;
    
     /**
     * Speech display plot.
     */
    public SpeechDisplay speech_plot_d;
    
    /**
     * Card panel to hold all monitor. 
     */
    public JPanel cards_d;
    
    /**
     * Text field to input data.
     */
    public JTextField text_field_d;

    /**
     * Push to talk button.
     */
    public JButton push_to_talk_d;
        
    /**
     * Play back button.
     */
    public JButton play_back_d;
    
    /**
     * Stop button.
     */
    public JButton stop_d;

    /**
     * quit button.
     */
    public JButton quit_button_d;
    
   
    //------------------------------------------------------------------------
    //
    // protected data
    //
    //-----------------------------------------------------------------------
    
    /**
     * Test if this client is get connected with control server
     */
    protected boolean connected_d = true;
   
    /**
     * client object to take care connection with control server.
     */
    protected ConnectionManager conn_d;
    
    /**
     * Indicate if the recording is progrss or not.
     */
    protected static boolean recording_d = false;  
    
    /**
     * text pane.
     */
    public JTextPane rows_d;
    
    /**
     *  Control the style of document.
     */
    protected LimitedStyledDocument lsd_d; 
    
    /**
     * Scroll pane.
     */
    protected JScrollPane scrollPane_d; 
    
    /**
     * Holds the the document. 
     */
    protected Document doc_d;  
    
    /**
     * Attribute set for document.
     */
    protected MutableAttributeSet mas_d; 
    
     /**
      * Text data. 
      */
    protected Vector<String> textData_d;
    
    /**
     * Text buffer.
     */
    protected Vector<String> textBuffer_d;

    /**
     * Control the style of document. 
     */
    protected LimitedStyledDocument lsd1_d; 
   
    /**
     * Text pane.
     */
    protected JTextPane rows1_d; 
    
     /**
      * Scroll pane.
      */
    protected JScrollPane scrollPane1_d;
    
    /**
     * Holds the the document. 
     */
    protected Document doc1_d;   
    
     /**
     *  Attribute set for document.
     */
    protected MutableAttributeSet mas1_d; 
    
    /**
     *  Text data.
     */
    protected Vector textData1_d; 
    
    /**
     * Limited styled document object.
     */
    protected LimitedStyledDocument lsd2_d;
    
    /**
     * text pane.
     */
    protected JTextPane rows2_d;    
    
    /**
     * scroll pane.
     */
    protected JScrollPane scrollPane2_d; 
    
     /**
     * Holds the the document.
     */
    protected Document doc2_d; 
    
     /**
      * attribute set for the document.
      */
    protected MutableAttributeSet mas2_d; 
    
    /**
     * Text data.
     */
    protected Vector textData2_d; 

    /**
     * Replace label.
     */
    protected JLabel replaceLabel_d;  
    
    /**
     * Input panel.  
     */
    protected JPanel input_window_d;   
    
    /**
     * Speech panel.
     */
    protected JPanel speech_window_d;  
    
     /**
      * Dialog panel.
      */
    protected JPanel dialog_window_d; 
    
     /**
      *  Volume panel.
      */
    protected JPanel volume_window_d;   
    
     /**
     * Energy panel.
     */
    protected JPanel energy_window_d;
    
    /**
     * Input panel.
     */
    protected JPanel input_method_d;

    /**
     * Scroll panel.
     */
    protected JScrollPane scrollPane4_d;  

    /**
     * Panels to hold different partitions of the screen.
     */
    protected JPanel p11_d;
    
    /**
     * Panels to hold different partitions of the screen.
     */
    protected JPanel p12_d;
    
    /**
     *  Panels to hold different partitions of the screen.
     */
    protected JPanel p21_d;
    
    /**
     *  Panels to hold different partitions of the screen. 
     */
    protected JPanel p22_d;
    
    /**
     *  Panels to hold different partitions of the screen.
     */
    protected JPanel p1_d;
    
    /**
      *  Panels to hold different partitions of the screen.
      */
    protected JPanel p2_d;
    
    /**
     *  Panels to hold different partitions of the screen. 
     */
    protected JPanel p_d;

    // declare private variables and components
    //

    /**
     *  Text data type.
     */
    protected Vector textDataType;        
    
    /**
     * Text data color.
     */
    protected Vector textDataColor; 
   
    /**
     *  Menu items. 
     */
    protected JMenuItem ShowTextField, HideTextField;
    
    /**
     * Menu items.
     */
    protected JMenuItem ShowDialogItem, HideDialogItem;
    
    /**
     * Menu items.
     */
    protected JMenuItem ShowSpeechItem, HideSpeechItem;
    
    /**
     * Menu items.
     */
    protected JMenuItem ConfigAudio;

    /**
     * Menu items.
     */
    protected JMenuItem ConfigEnergy;
    
    /**
     * Menu items.
     */
    protected JMenuItem ConfigWaveform;
    
    /**
     * Menu items.
     */
    protected JMenuItem ConfigSpectrogram;
    
    /**
     * Menu items.
     */
    protected JMenuItem ConfigDisplay;
    
    /**
     * Menu items.
     */
    protected JMenuItem ConfigDetector;

    /**
     * The Configuration pop up window.
     */    
    protected Configuration dialog = null;

    /**
     * The port number of the speech_analysis. 
     */
    protected int speech_analysis_port_d;
    
    /**
     *  The port number of the process manager. 
     */
    protected int process_manager_port_d;
    
    /**
     *  The ip address of the process manager. 
     */    
    protected String process_manager_ip_d;

    /**
     * Configuration data for audio parameters.
     */
    protected ConfigData cfg_audio_d;
    
    /**
     * Configuration data for energy parameters.
     */
    protected ConfigData cfg_energy_d;

    /**
     * Configuration data for spectrogram parameters.
     */
    protected ConfigData cfg_spectrogram_d;

    /**
     * Configuration data for  waveform parameters.
     */
    protected ConfigData cfg_waveform_d;

    /**
     * Configuration data for display parameters.
     */
    protected ConfigData cfg_display_d;

    /**
     * Configuration data for detector parameters.
     */
    protected ConfigData cfg_detector_d;

    
    //-------------------------------------------------------------------------
    //
    // public methods
    //
    //-------------------------------------------------------------------------
  
    /**
     * Creates the Recorder window which includes the input window,
     * Energy monitor window, and the waveform window. It also
     * establishes the connection with the control server. This is
     * program has user interface for the speech analysis application.
     */
    public SpeechAnalysisUIFrame(int speech_port_a, 
				 int process_port_a, 
				 String process_ip_a,
				 boolean server_mode_a) {
	
	speech_analysis_port_d = speech_port_a;
	process_manager_port_d = process_port_a;
	process_manager_ip_d = process_ip_a;
	
	final SpeechAnalysisUIFrame thisAR = this;
	
	// main panel properties
	//
	setTitle("Speech Analysis");
	
	// set the size for this frame
	//
	setSize(MAX_UI_WIDTH,  MAX_UI_HEIGHT);
	String startString = "";
	
	// main panel menu
	//
	JMenuBar mbar = new JMenuBar();
	setJMenuBar(mbar);
	
	// file menu with "exit" in it
	//
	JMenu fileMenu = new JMenu("File");
	fileMenu.addMenuListener(this);
	
	// add the exit option
	//
	mbar.add(makeMenu(fileMenu,
			  new Object[]
	    {
		null,
		"Exit"
	    },
			  this));

	
	// initialise other menu items
	//
	ShowSpeechItem = new JMenuItem("Show Speech");
       	HideSpeechItem = new JMenuItem("Hide Speech");
	ShowTextField = new JMenuItem("Show Text");
	HideTextField = new JMenuItem("Hide Text");
	
	// set the values to false
	//
	ShowSpeechItem.setEnabled(false);
	ShowSpeechItem.setVisible(false);
	HideSpeechItem.setEnabled(false);
	HideSpeechItem.setVisible(false);
	ShowTextField.setEnabled(true);
	HideTextField.setEnabled(false);
	HideTextField.setVisible(false);
	
	// create the configuration menu
	//
	JMenu fileMenu3 = new JMenu("Configuration");

	// set the menu item to "configuration"
	//
	ConfigAudio = new JMenuItem("Audio");
	ConfigEnergy = new JMenuItem("Energy");
	ConfigWaveform = new JMenuItem("Waveform");
	ConfigSpectrogram = new JMenuItem("Spectrogram");
	ConfigDisplay = new JMenuItem("Display");
	ConfigDetector = new JMenuItem("Detector");
	
	// add the required action listeners
	//
	ConfigAudio.addActionListener(new ConnectAction(ConfigTabPanel.AUDIO));
	ConfigEnergy.addActionListener(new ConnectAction(ConfigTabPanel.ENERGY));
	ConfigWaveform.addActionListener(new ConnectAction(ConfigTabPanel.WAVEFORM));
	ConfigSpectrogram.addActionListener(new ConnectAction(ConfigTabPanel.SPECTROGRAM));
	ConfigDisplay.addActionListener(new ConnectAction(ConfigTabPanel.DISPLAY));
	ConfigDetector.addActionListener(new ConnectAction(ConfigTabPanel.DETECTOR));

	// add all the configuration tabs to the menu
 	//
	mbar.add(makeMenu(fileMenu3,
			  new Object[]
	    { 
		null,
		ConfigAudio,
		ConfigEnergy,
		ConfigWaveform,
		ConfigSpectrogram,
		ConfigDisplay,
		ConfigDetector
	    },
			  this));
	
	// add a help menu
	//
	JMenu fileMenu4 = new JMenu("Help");
	fileMenu4.addMenuListener(this);
	
	// help has no item in it
	//
	mbar.add(makeMenu(fileMenu4,
			  new Object[]
	    { 
		null
	    },
			  this));
	
	// create the document for the input window
	//
	textData_d = new Vector<String>();
	textDataType = new Vector();
	textDataColor = new Vector();
	textBuffer_d = new Vector<String>();
	lsd_d = new LimitedStyledDocument(MAX_CHARACTERS);
	rows_d = new JTextPane(lsd_d);
	rows_d.setCaretPosition(0);
	rows_d.setMargin(new Insets(5,5,5,5));
	doc_d = rows_d.getDocument();
	mas_d = new SimpleAttributeSet();
	scrollPane_d = new JScrollPane(rows_d,
				       JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				       JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	scrollPane_d.setPreferredSize(new Dimension(MAX_UI_WIDTH / 2,
						    (int)(0.95 * MAX_UI_HEIGHT)));
	
	// the speech display plot
	//
	speech_plot_d = new SpeechDisplay();

	// add component listener
	// 
        addComponentListener(this);
	
	// add window listener
	//
	addWindowListener(new WindowAdapter() {
		
		public void windowActivated(WindowEvent e) {
		    speech_plot_d.repaint();
		}
		
		public void windowDeactivated(WindowEvent e) {
		    speech_plot_d.repaint();
		}
	    });
	
	JPanel text_input = new JPanel();
	text_input.setLayout(new BorderLayout());
	
	// set the record button
	//
	push_to_talk_d = new JButton("Record");
	text_input.add(push_to_talk_d, "West");
	
	// set the playback button
	//
	play_back_d = new JButton("Playback");
	text_input.add(play_back_d, "Center");
	
	// set the stop button
	//
	stop_d = new JButton("Stop");
	text_input.add(stop_d, "East");
	
	// set the speech input panel
	//
	JPanel speech_input = new JPanel();
	speech_input.setLayout(new BorderLayout());
	
	// set the clear button and add an action listener
	//
	JButton clearButton = new JButton("Clear");
	clearButton.addActionListener(this);
	speech_input.add(clearButton, "Center");
	
	// set the quit button and add an action listener
	//
	quit_button_d = new JButton("Quit");
	quit_button_d.addActionListener(this);
	speech_input.add(quit_button_d, "East");
	
	// add the input panel
	//
	input_method_d = new JPanel();
	input_method_d.setLayout(new BorderLayout());
	input_method_d.setPreferredSize(new Dimension(MAX_UI_WIDTH / 2,
						      (int)(0.05 * MAX_UI_HEIGHT)));
	input_method_d.add(text_input,"West");
	input_method_d.add(speech_input,"East");
	
	// create the input window
	//
	input_window_d = new JPanel();
	input_window_d.setLayout(new BorderLayout());
	input_window_d.setBorder(new TitledBorder(new EtchedBorder(), 
						  "Message Window"));
	input_window_d.setPreferredSize(new Dimension(MAX_UI_WIDTH / 2,  
						      MAX_UI_HEIGHT / 2));
	input_window_d.add(scrollPane_d, "Center");
	input_window_d.add(input_method_d, "South");
	

	// create the energy monitor
	//
	energy_window_d = new JPanel();
	energy_window_d.setLayout(new BorderLayout());
	energy_window_d.setBorder(new TitledBorder(new EtchedBorder(), 
						   "Speech Analysis"));
	energy_window_d.setPreferredSize(new Dimension(MAX_UI_WIDTH,  
						       MAX_UI_HEIGHT));
	scrollPane4_d = new JScrollPane(speech_plot_d,
					JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	energy_window_d.add(scrollPane4_d, "Center");
	
	// create the card layout
	//
	cards_d = new JPanel();
	cards_d.setLayout(new CardLayout());
	cards_d.add(energy_window_d, "Energy Monitor");
	
	// create the different panels and add the components
	//
	p11_d = new JPanel();
	p11_d.setLayout(new BorderLayout());
	p11_d.setPreferredSize(new Dimension(MAX_UI_WIDTH / 2, 
				     MAX_UI_HEIGHT / 2));
	p11_d.add(input_window_d);
	
	p1_d = new JPanel();
	p1_d.setLayout(new BorderLayout());
	
	p21_d = new JPanel();
	p21_d.setLayout(new GridLayout(1, 1));
	p21_d.setPreferredSize(new Dimension(MAX_UI_WIDTH / 2, 
					     MAX_UI_HEIGHT / 2));
	p21_d.add(cards_d);
	
	p2_d = new JPanel();
	p2_d.setLayout(new BorderLayout());
	
	p1_d.add(p21_d, "Center");
	p2_d.add(p11_d, "Center");
	
	p_d = new JPanel();
	p_d.setLayout(new GridLayout(2,1));
	
	p_d.add(p1_d);
	p_d.add(p2_d);
	
	// Add the main panel to the containerPane
	//
	Container contentPane = getContentPane();
	contentPane.add(p_d, "Center");
	
	contentPane.addMouseListener(new MouseAdapter() {
		public void mouseReleased(MouseEvent evt) {
		}
	    });
	
	setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	
	// connection to control server and start the support servers 
	// in the main frame server side
	//
	conn_d = new ConnectionManager(speech_analysis_port_d, 
				       process_manager_port_d, 
				       process_manager_ip_d,
				       server_mode_a,
				       (String) "SPEECH_ANALYSIS");

	conn_d.addWindowListener(new WindowAdapter() {
		
		public void windowClosed(WindowEvent e) {
		    if (connected_d) {
			connected_d = false;
			
			// Get the error message from the connection manager
			// and display and exit
			//
			JOptionPane.showMessageDialog
			    (thisAR, conn_d
			     .getDisconnectErrorMessage());
			
			thisAR.actionPerformed(new
					       ActionEvent(this, 0, "Quit"));
		    }//end if connected_d
		}
	    });
	
	conn_d.setThread(new Thread(conn_d));
	conn_d.getThread().start();
    }
    
    // these methods are needed if the class implements an component
    // listener. These methods need to be overrided by the UIFrame
    // class eventhough we dont use it. As we dont use any of these
    // functions its left blank..
    //
    
    public void componentHidden(ComponentEvent e) {
	
    }

    public void componentMoved(ComponentEvent e) {
        
    }

    public void componentShown(ComponentEvent e) {
 
    }

    /**
     * This method detects resizing of the UIFrame and recalcultes the
     * energy and waveform display and repaints the display.
     * 
     * @param e The event which is generated when the
     * component is resized
     *
     */
    public void componentResized(ComponentEvent e) {
	    
	// if the window is resized, the speech display is 
	// replotted
	//
	if(!speech_plot_d.realtime_d) {
	    speech_plot_d.redrawOverallPlot();
	speech_plot_d.repaint();
	System.out.println("Component resized");
	}
    }
	

    /**
     * This method detects a new user request, clears the current
     * display history, and displays the current request.
     *
     * @param request_a The request to add to the display.
     * @param mode_a The mode in which to add the request.
     *
     * @return a boolean value indicating status.
     */
    public boolean setText(String request_a, int mode_a) {

	SwingUtilities.invokeLater(new RunnableJTextPane(textData_d,
							 textBuffer_d,
							 doc_d,
							 rows_d,
							 request_a,
							 mode_a));

	// exit gracefully
	//
	return true;
    }
    
    /**
     * This performs the respective actions whenever a menu item
     * or button is pressed.
     *
     * @param evt event the represents the chosen button or menu item.
     *
     */
    public void actionPerformed(ActionEvent evt) {
	
	String arg = evt.getActionCommand();
	System.out.println(arg);
	
	int height = MAX_UI_HEIGHT;
	int width = MAX_UI_WIDTH;
	
	// if the button pressed was exit then exit the window
	//
	if (arg.equals("Exit")) {
	    if (!recording_d) {
		setVisible(false);
		dispose();
	    }
	}
	
	// if quit button was pressed then dispose the window
	//
	if (arg.equals("Quit")) {
	    if (!recording_d) {
		setVisible(false);
		dispose();
	    }
	}

	// it it is clear button then clear the message window and the
	// display
	//
	if (arg.equals("Clear")) {
	    try {
		speech_plot_d.reset();
		doc_d.remove(0, doc_d.getLength());
		rows_d.setCaretPosition(doc_d.getLength());
		textData_d.clear();
		textDataType.clear();
		textDataColor.clear();
		textBuffer_d.clear();

		doc1_d.remove(0, doc1_d.getLength());
		rows1_d.setCaretPosition(doc1_d.getLength());
		textData1_d.clear();

		doc2_d.remove(0, doc2_d.getLength());
		rows2_d.setCaretPosition(doc2_d.getLength());
		textData2_d.clear();

	    } catch (Exception ignore) { }
	}
	
	// if "Show Speech" is pressed
	//
	if (arg.equals("Show Speech")) {
	    
	    speech_window_d.setVisible(true);
	    ShowSpeechItem.setEnabled(false);
	    ShowSpeechItem.setVisible(false);
	    HideSpeechItem.setEnabled(true);
	    HideSpeechItem.setVisible(true);
	}
	
	// if "Hide Speech" is pressed
	//
	if (arg.equals("Hide Speech")) {
	    
	    speech_window_d.setVisible(false);
	    ShowSpeechItem.setEnabled(true);
	    ShowSpeechItem.setVisible(true);
	    HideSpeechItem.setEnabled(false);
	    HideSpeechItem.setVisible(false);
	}
	
	// if "Show Text" is pressed
	//
	if (arg.equals("Show Text")) {
	    
	    replaceLabel_d.setVisible(true);
	    text_field_d.setVisible(true);
	    ShowTextField.setEnabled(false);
	    ShowTextField.setVisible(false);
	    HideTextField.setEnabled(true);
	    HideTextField.setVisible(true);
	}

	// if "Hide Text" is pressed
	//
	if (arg.equals("Hide Text")) {
	    
	    replaceLabel_d.setVisible(false);
	    text_field_d.setVisible(false);
	    ShowTextField.setEnabled(true);
	    ShowTextField.setVisible(true);
	    HideTextField.setEnabled(false);
	    HideTextField.setVisible(false);
	}
    }

    /**
     * Method required by MenuListener, but do not use in this class
     * 
     */  
    public void menuSelected(MenuEvent evt) {
    }
   
    /**
     * Method required by MenuListener, but do not use in this class
     *
     */  
    public void menuDeselected(MenuEvent evt) {
    }
   
    /**
     * Method required by MenuListener, but do not use in this class
     *
     */  
    public void menuCanceled(MenuEvent evt) {
    }
        
    /** 
     * This method is used to either add items to an existing
     * <code>JMenu</code> or to create a new <code>JMenu</code>
     * using the supplied <code>String</code> and then add the items.
     *
     * @param parent Existing <code>JMenu</code> to add items to or
     *               <code>String</code> that contains the text for a
     *               new <code>JMenu</code>.
     * @param items Array of <code>String</code>s and/or
     *              <code>JMenuItem</code>s to be added to the menu.
     * @param target Action listener to use for the added menu items.
     *
     * @return JMenu item.
     */
    public static JMenu makeMenu(Object parent,
				 Object[] items, 
				 Object target) {
	JMenu m = null;
	if (parent instanceof JMenu)
	    m = (JMenu)parent;
	else if (parent instanceof String)
	    m = new JMenu((String)parent);
	else
	    return null;
	
	for (int i = 0; i < items.length; i++) {
	    if (items[i] == null)
		m.addSeparator();
	    else
		m.add(makeMenuItem(items[i], target));
	}
	
	return m;
    }
    
    /** 
     * This method is used by the <code> makeMenu() </code> method to
     * create <code> JMenuItem </code>s with a specified <code>
     * ActionListener </code>.
     * 
     * @param item a java object
     * @param target a java object
     */
    public static JMenuItem makeMenuItem(Object item,
					 Object target) {
	JMenuItem r = null;
	if (item instanceof String)
	    r = new JMenuItem((String)item);
	else if (item instanceof JMenuItem)
	    r = (JMenuItem)item;
	else return null;
	
	if (target instanceof ActionListener)
	    r.addActionListener((ActionListener)target);
	return r;
    }

    //-------------------------------------------------------------------------
    //
    // class-specific public methods:
    //  set methods
    //
    //-------------------------------------------------------------------------

    /**
     * Set the recording to specified value.
     *
     * @param recording_a boolean value for recording flag.
     *
     * @return a boolean value indicating status.
     */
    public static boolean setRecording(boolean recording_a) {

	recording_d = recording_a;

	// exit gracefully
	//
	return true;
    }

    /**
     * Set the connection flag with process manager.
     *
     * @param connected_a boolean value for connection flag.
     *
     * @return a boolean value indicating status.
     */
    public boolean setConnection(boolean connected_a) {

	connected_d = connected_a;

	// exit gracefully
	//
	return true;
    }

  

    //-------------------------------------------------------------------------
    //
    // class-specific public methods:
    //  get methods
    //
    //-------------------------------------------------------------------------
 
    /**
     * Get the audio data after the user sets the configuration.
     *
     * @return a ConfigData object 
     */
    public ConfigData getAudioData() {
	
	return cfg_audio_d;
    }
    
    /**
     * Get the signal detector after the user sets the configuration.
     *
     * @return a ConfigData object 
     */
    public ConfigData getDetectorData() {
	
	return cfg_detector_d;
    }
    
    /**
     * Get the recording flag.
     *
     * @return a boolean value indicating to recording flag.
     */
    public static boolean getRecording() {
	return recording_d;
    }

    /**
     * Get the connection flag.
     *
     * @return a boolean value indicating to connection flag.
     */
    public boolean getConnection() {
	return connected_d;
    }

    /**
     * Get the connection manager object.
     *
     * @return a ConnectionManager object.
     */
    public ConnectionManager getConnectionManager() {
	return conn_d;
    }
    

    /**
     * This class is used to pop up the configuration menu which is
     * used to configure the parameters fo all the servers and the
     * energy, waveform and spectrogram displays..
     */    
    protected class ConnectAction implements ActionListener {

	// public variable
	//
	public int mode = 0;

	/**
	 * constructor of the ConnectAction class. Just sets the mode.
	 */
	public ConnectAction(int mode_a) {
	    mode = mode_a;
	}
	
	/**
	 * This is invoked when an action is performed..
	 *
	 * @param evt the action event invoked.
	 *
	 */
	public void actionPerformed(ActionEvent event) {
	    
	    // define config tab object
	    //
	    ConfigTabPanel cfg_tab_d;
	    
	    // if first time, construct dialog
	    //
	    if (dialog == null) 
		dialog = new  Configuration();
	    else {
		cfg_tab_d = dialog.getPanel();
		cfg_tab_d.setSelectedIndex(mode);
	    }

	    // pop up dialog
	    //
	    if (dialog.showDialog(SpeechAnalysisUIFrame.this, 
				  "Configuration")) {
		
		cfg_tab_d = dialog.getPanel();
		cfg_audio_d = cfg_tab_d.getAudioData();
		cfg_energy_d = cfg_tab_d.getEnergyData();
		cfg_spectrogram_d = cfg_tab_d.getSpectrogramData();
		cfg_waveform_d = cfg_tab_d.getWaveformData();
		cfg_display_d = cfg_tab_d.getDisplayData();
		cfg_detector_d = cfg_tab_d.getDetectorData();

		speech_plot_d.setConfigData(cfg_display_d);
		speech_plot_d.setConfigData(cfg_energy_d);
		speech_plot_d.setConfigData(cfg_waveform_d);

		// if the configuration is changed replot the display
		// and repaint it.
		// 
		if(!speech_plot_d.realtime_d) {
		speech_plot_d.redrawOverallPlot();
		speech_plot_d.repaint();
		}
		else {
		    speech_plot_d.setData(null);
		    speech_plot_d.run();
		    speech_plot_d.repaint();
		}
		
		printData(cfg_audio_d);
		printData(cfg_detector_d);
		printData(cfg_energy_d);
		printData(cfg_spectrogram_d);
		printData(cfg_waveform_d);
		printData(cfg_display_d);
		
		cfg_tab_d.setSelectedIndex(mode);
	    }
	}
	
	/**
	 * Print data inside this config data
	 *
	 * @param cfg_data_a a ConfigData object.
	 *
	 * @return a boolean value indicating status.
	 */
	public boolean printData(ConfigData cfg_data_a) {
	    
	    for (int i = 0; i < cfg_data_a.getAssociation().size(); i++) {
		
		System.out.println("ith item=" + i);
		System.out.println(cfg_data_a.getAssociation().get(i));
		System.out.println(cfg_data_a.getName().get(i));
		System.out.println(cfg_data_a.getType().get(i));
		System.out.println(cfg_data_a.getValue().get(i));
	    }
	    
	    return true;

	}
    

    }
}

