// file: $ISIP_IFC/util/demo/isip_demo_selector/DemoSelector.java
// version: $Id: DemoSelector.java 10235 2005-09-13 15:15:42Z stanley $
//

// import necessary java libraries
//
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

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

/**
 * This class is a program that will allow the user to select a demo
 * to run.  The demo select screen will become invisible when a demo
 * is selected and remain that way until the chosen demo exits. <P>
 */
public class DemoSelector extends JFrame {
    
    //-------------------------------------------------------------------------
    //
    // public constants
    //
    //-------------------------------------------------------------------------

    /** 
     * Default canvas frame width.
     */
    public static final int WM_CANVAS_WIDTH = 800;

    /**
     * Default canvas frame height.
     */
    public static final int WM_CANVAS_HEIGHT = 600;
    
    /**
     * Default upper title panel width
     */
    public static final int TITLE_PANEL_WIDTH = 775;

    /**
     * Default upper title panel height
     */
    public static final int TITLE_PANEL_HEIGHT = 48;

    /**
     * Default lower title panel width
     */
    public static final int TITLE_LOWER_WIDTH = 775;

    /**
     * Default lower title panel height
     */
    public static final int TITLE_LOWER_HEIGHT = 54;

    /**
     * Default description label width
     */
    public static final int DESC_LABEL_WIDTH = 450;

    /**
     * Default description lable height
     */
    public static final int DESC_LABEL_HEIGHT = 105;

    /**
     * Default title panel width
     */
    public static final int DEMO_PANEL_WIDTH = 775;

    /**
     * Default title panel height
     */
    public static final int DEMO_PANEL_HEIGHT = 105;

    /**
     * The blue color defined for font color.
     */
    public Color COLOR_BLUE = new Color(0, 0, 200, 255);

    /**
     * The light blue color defined for font color.
     */
    public Color COLOR_LIGHT_BLUE = new Color(0, 0, 200, 100);
    
    /**
     * The dark blue color for button backgrounds
     */
    public Color COLOR_BTN_BLUE = new Color(20, 34, 95);
    
    /**
     * The color maroon
     */
    public Color COLOR_MAROON = new Color(155, 0, 40);

    /**
     * The title font size.
     */
    public static final int TITLE_FONT_SIZE = 36;

    /**
     * Number of arguments allowed.
     */
    public static final int MAX_NUMBER_ARGS = 20;

    //------------------------------------------------------------------------
    //
    // protected data
    //
    //-----------------------------------------------------------------------
  
    /**
     * A string for error messages.
     */
    protected static String error_message_d;             
    
    /**
     * Speech Analysis application UI frame.
     */    
    protected static SpeechAnalysisUIFrame analysis_frame_d; 

    /**
     * Speech Recognition application UI frame.
     */    
    protected static SpeechRecognitionUIFrame recognition_frame_d; 

    /**
     * Dialog System application UI frame.
     */    
    protected static DialogNavigationUIFrame dialog_frame_d; 

     /**
      * Speaker Verification application UI frame.
     */    
    protected static SpeakerVerificationUIFrame verification_frame_d;

    /**
     * Top level JPanel.
     */
    protected final JPanel main_panel_d;                  

    /**
     * Command line arguments.
     */
    protected static String[] server_args_d;

    /**
     * Static User Interface port number.
     */
    protected static int ui_port_d = 15000;

    /**
     * Number of connection for this session.
     */
    protected static int num_connection_d = 0;

    /**
     * The Configuration pop up window
     */

    protected ConfigMenu dialog = null;

    /**
     * File holding initial configuration settings
     */
    protected String configFilename = "recorder_param.txt";

    /**
     * Dialog Navigation button path string
     */ 
    protected String dialog_img_string = "./images/dialog_button.jpg";

    /**
     * Speech Verification button path string
     */ 
    protected String verify_img_string = "./images/verification_button.jpg";

    /**
     * Speech Recognition button path string
     */ 
    protected String recognition_img_string =
	"./images/recognition_button.jpg";

    /**
     * Speech Analysis button path string
     */ 
    protected String analysis_img_string = "./images/analysis_button.jpg";

    /**
     * Background path string
     */ 
    protected String background_img_string = "./images/background.jpg";

       
    //-------------------------------------------------------------------------
    //
    // public methods
    //
    //-------------------------------------------------------------------------

    /**
     * Constructor. Creates the visual interface for the demo
     * selector.
     */
    public DemoSelector() {
        
        super("Demonstration Selector");
        
        final JFrame frame = this;
        
        // Try and set the LAF to that of the system
        //
        try {
            UIManager.setLookAndFeel
		(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception eNative) { // If Native LAF fails set to Java LAF
            try {
                UIManager.setLookAndFeel
		    (UIManager.getCrossPlatformLookAndFeelClassName());
            }
            catch (Exception eJava) {
                System.out.println("Error setting LAF: " + eJava);
            }
        }
        
	// Create all necessary images
        //
        final ImageIcon dialogImg = new ImageIcon(dialog_img_string);
        final ImageIcon verifyImg = new ImageIcon(verify_img_string);
        final ImageIcon analysisImg = new ImageIcon(analysis_img_string);
        final ImageIcon recognitionImg = new ImageIcon(recognition_img_string);
        final Image bgImage = getToolkit().getImage(background_img_string);
        
        // Add media tracker to insure images are loaded before app starts
        //
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(bgImage, 0);

        // Add listener for cleanup when closing program
        //
        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        
        // Divide main screen into 3 sections; NORTH, SOUTH, and CENTER.
        // The NORTH section will contain the title.  The SOUTH section
        // will contain the instructions and copyright info.  The
        // CENTER section will contain all the buttons and labels
        // for the various demos.
        //
        Container content = getContentPane();
        content.setLayout(new BorderLayout());
        main_panel_d = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)) {
                
                // Paint the background image
                //
                public void paintComponent(Graphics g) {

                    super.paintComponent(g);
                    
                    Dimension sz = getSize();

                    g.drawImage(bgImage, 0, 0, 
                                (int)sz.getWidth(), (int)sz.getHeight(), this);
                }
            };

        main_panel_d.setBorder(BorderFactory.createEtchedBorder());

        GraphicsEnvironment ge = GraphicsEnvironment
	    .getLocalGraphicsEnvironment();

        Font[] f = ge.getAllFonts();

	// Setup menu bar
	// Create Config menu and its items
	//
	JMenuBar bar = new JMenuBar();
       	setJMenuBar( bar );
	
	JMenu configMenu = new JMenu ( "Configuration" );
	configMenu.setMnemonic( 'C' );
	
	bar.add( configMenu );

	JMenuItem editOption = new JMenuItem( "Edit" );
	editOption.setMnemonic( 'E' );
	configMenu.add( editOption);

	// check for initial configuration from recorder_param file
	//
	dialog = new ConfigMenu(configFilename);
        
	if (!dialog.initialFileCheck(configFilename)){
	    dialog.loadFileSettings();	
	    dialog.showDialog(DemoSelector.this, "Network Configurations");
	}

	editOption.addActionListener(new ActionListener() {
		
		public void actionPerformed(ActionEvent e){

		    if (dialog == null) {
			dialog = new ConfigMenu(configFilename);
		    }
	
  	    	    dialog.initialFileCheck(configFilename);	    
		    dialog.loadFileSettings();
		    dialog.showDialog(DemoSelector.this, 
				      "Network Configurations");
		}
		
	    });
                                          
        // Create content for section 1 (the title)
        //
        JPanel sec1 = new JPanel(new FlowLayout());

        JPanel title = new JPanel(new FlowLayout()) {

                public Dimension getPreferredSize() {
                    return new Dimension(TITLE_PANEL_WIDTH, 
                                         TITLE_PANEL_HEIGHT);
                }
                
                public void paintComponent(Graphics g) {
                    
                    Dimension sz = getSize();
                    
                    enableEvents(MouseEvent.MOUSE_CLICKED);
                    
                    Graphics2D g2;
                    g2 = (Graphics2D) g;

                    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                                        RenderingHints.VALUE_ANTIALIAS_ON);

                    g2.setRenderingHint(RenderingHints.KEY_RENDERING, 
                                        RenderingHints.VALUE_RENDER_QUALITY);
                    
                    FontRenderContext frc = g2.getFontRenderContext();

                    Font f = new Font("Times New Roman", 
                                      Font.BOLD|Font.ITALIC, 
                                      TITLE_FONT_SIZE);

                    String s1 = new String("Human Language " +
					   "Technology Demonstrations");
                    TextLayout t1 = new TextLayout(s1, f, frc);
                    Rectangle2D t1Rect = t1.getBounds();

                    g2.setColor(COLOR_BLUE);

                    t1.draw(g2, (float)(sz.getWidth() - 
                                        t1Rect.getWidth()) / 2,
                            (float)t1Rect.getHeight());
                }
        };

        sec1.setBackground(Color.WHITE);
        sec1.setOpaque(false);
        sec1.add(title);
        
        // Create a default size for all the demo panels
        //
        Dimension demoSize = new Dimension(DEMO_PANEL_WIDTH,
                                           DEMO_PANEL_HEIGHT);
        
        // Create the panels to hold the button and label for each demo.
        // The name of the panel corresponds to its location where
        // pnl# will be the # is the number of the row in which that
        // button and label are contained.
        //
        JPanel pnl1 = new JPanel(new FlowLayout());
        JPanel pnl2 = new JPanel(new FlowLayout());
        JPanel pnl3 = new JPanel(new FlowLayout());
        JPanel pnl4 = new JPanel(new FlowLayout());

        // Make the panels transparent so the background image can be seen
        //
        pnl1.setOpaque(false);
        pnl2.setOpaque(false);
        pnl3.setOpaque(false);
        pnl4.setOpaque(false);
        
        // Set the preferred size of the demo panels
        //
        pnl1.setPreferredSize(demoSize);
        pnl2.setPreferredSize(demoSize);
        pnl3.setPreferredSize(demoSize);
        pnl4.setPreferredSize(demoSize);

        // Create a default size for all the demo description labels
        //
        Dimension lblSize = new Dimension(DESC_LABEL_WIDTH,
                                          DESC_LABEL_HEIGHT);
        
	 // Create the speech analysis button
        //
        JButton analysisBtn = new JButton(analysisImg);

        analysisBtn.setDefaultCapable(false);
        analysisBtn.setBackground(COLOR_BTN_BLUE);
        analysisBtn.setBorder(BorderFactory.createEmptyBorder());
        analysisBtn.setHorizontalTextPosition(SwingConstants.CENTER);
        analysisBtn.setVerticalTextPosition(SwingConstants.BOTTOM);

	
        analysisBtn.addActionListener(new ActionListener() {

                // Display audio recorder demo when pressed
                //
                public void actionPerformed(ActionEvent e) {

                    // Create the AR main server
                    //
                    File config_file_obj = new File(configFilename);
                    server_args_d = getArgs(config_file_obj);
                    final SpeechAnalysisMainServer mainServer = 
			new SpeechAnalysisMainServer(server_args_d);
                    
                    // Get the AR frame from the main server and show it
                    //
                    analysis_frame_d = mainServer.getSpeechAnalysisUIFrame();
                    
                    // Add a window listener for cleanup and to allow
                    // the demo selector to become visible again after
                    // the demo is closed.
                    //
                    analysis_frame_d.addWindowListener(new WindowAdapter() {
                            public void windowClosing(WindowEvent e) {
                                if (!analysis_frame_d.getRecording()) {
                                    analysis_frame_d.setVisible(false);
                                    setVisible(true);  // reveal Selector
                                    analysis_frame_d.getConnectionManager()
					.disconnect();
                                    analysis_frame_d.setConnection(false);
                                    analysis_frame_d = null;
                                    mainServer.stop();
                                }
                            }
                            
                            public void windowClosed(WindowEvent e) {
                                if (!analysis_frame_d.getRecording()) {
                                    if (analysis_frame_d != null) {
					analysis_frame_d.setVisible(false);
					analysis_frame_d.getConnectionManager()
					    .disconnect(); 
					analysis_frame_d.setConnection(false); 
					analysis_frame_d = null;
				    }
                                    setVisible(true);  // reveal Selector
                                    mainServer.stop();
                                }
                            }
                        }); // end WindowAdapter/addWindowListener

                    analysis_frame_d.setVisible(true);                   
                    System.out.println("speech analysis started");
                    
                    try {
                        mainServer.start();
                        System.out.println("speech analysis be started");
                        setVisible(false); // hide Selector
                    } 
                    catch (Exception ex) {
                        error_message_d = ex.toString();
                    } // end try/catch
                    main_panel_d.repaint();
                }
            });

        // Create the analysis label
        //
        String analysisStr =
            "<html><font size=\"5\" color=\"#0000C8\">"+
            "<ul><li>" +
            "Speech Analysis: " +
            "<font color=\"#9B0028\">Learn about basic digital voice<br>" + 
            "speech processing by recording and visualizing<br>" +
            "your voice patterns</font>" +
            "</li></ul>" +
            "</font></html>";

        JLabel analysisLbl = new JLabel(analysisStr);
        analysisLbl.setPreferredSize(lblSize);
        analysisLbl.setVerticalAlignment(SwingConstants.TOP);
        
        // Create the verify button
        //
        JButton verifyBtn = new JButton(verifyImg);

        verifyBtn.setDefaultCapable(false);
        verifyBtn.setBackground(COLOR_BTN_BLUE);
        verifyBtn.setBorder(BorderFactory.createEmptyBorder());
        verifyBtn.setHorizontalTextPosition(SwingConstants.CENTER);
        verifyBtn.setVerticalTextPosition(SwingConstants.BOTTOM);
        verifyBtn.addActionListener(new ActionListener() {
                
                // Display verify demo when button is pressed
                //
                public void actionPerformed(ActionEvent e) {
                    
                    // Create the AR main server
                    //
                    File config_file_obj = new File(configFilename);
                    server_args_d = getArgs(config_file_obj);
                    final SpeakerVerificationMainServer mainSVServer
			= new SpeakerVerificationMainServer
			(server_args_d);
                    
                    // Get the AR frame from the main server and show it
                    //
                    verification_frame_d = mainSVServer
			.getSpeakerVerificationUIFrame();
                    
                    // Add a window listener for cleanup and to allow
                    // the demo selector to become visible again after
                    // the demo is closed.
                    //
                    verification_frame_d.addWindowListener
			(new WindowAdapter() {
                            public void windowClosing(WindowEvent e) {
                                if (!verification_frame_d.getRecording()) {
                                    verification_frame_d.setVisible(false);
                                    setVisible(true);  // reveal Selector
                                    verification_frame_d
					.getConnectionManager().disconnect();
                                    verification_frame_d.setConnection(false);
                                    verification_frame_d = null;
                                    mainSVServer.stop();
                                }
                            }
                            
                            public void windowClosed(WindowEvent e) {
                                if (!verification_frame_d.getRecording()) {
                                    verification_frame_d.setVisible(false);
                                    setVisible(true);  // reveal Selector
                                    verification_frame_d
					.getConnectionManager().disconnect(); 
                                    verification_frame_d.setConnection(false); 
                                    verification_frame_d = null;
                                    mainSVServer.stop();
                                }
                            }
                        }); // end WindowAdapter/addWindowListener
                    
                    verification_frame_d.setVisible(true); 
		    // reveal Selector;
                    //
		    System.out.println("speaker verification demo started");
                    
                    try {
                        mainSVServer.start();
                        System.out.println("speaker verification " +
					   "demo started");
                        setVisible(false); // hide Selector
                    } 
                    catch (Exception ex) {
                        error_message_d = ex.toString();
                    } // end try/catch
                    main_panel_d.repaint();
                } // end actionPerformed
            }); // end ActionListener/addActionListener
        
        // Create the verify label
        //
        String verifyStr =
            "<html><font size=\"5\" color=\"#0000C8\">"+
            "<ul><li>" +
            "Speaker Verification: " +
            "<font color=\"#9B0028\">Enroll and verify your<br>" + 
            "identity using a text-independent verification<br>" +
            "system that minimizes enrollment time</font>" +
            "</li></ul>" +
            "</font></html>";

        JLabel verifyLbl = new JLabel(verifyStr);
        verifyLbl.setPreferredSize(lblSize);
        verifyLbl.setVerticalAlignment(SwingConstants.TOP);
        
        // Create the verify button
        //
        JButton recognitionBtn = new JButton(recognitionImg);

        recognitionBtn.setDefaultCapable(false);
        recognitionBtn.setBackground(COLOR_BTN_BLUE);
        recognitionBtn.setBorder(BorderFactory.createEmptyBorder());
        recognitionBtn.setHorizontalTextPosition(SwingConstants.CENTER);
        recognitionBtn.setVerticalTextPosition(SwingConstants.BOTTOM);
       	recognitionBtn.addActionListener(new ActionListener() {
		
                // Display audio recorder demo when pressed
                //
                public void actionPerformed(ActionEvent e) {
		    
                    // Create the AR main server
                    //
                    File config_file_obj = new File(configFilename);
                    server_args_d = getArgs(config_file_obj);
                    final SpeechRecognitionMainServer mainSRServer
			= new SpeechRecognitionMainServer(server_args_d);
                    
                    // Get the AR frame from the main server and show it
                    //
                    recognition_frame_d = mainSRServer
			.getSpeechRecognitionUIFrame();
                    
                    // Add a window listener for cleanup and to allow
                    // the demo selector to become visible again after
                    // the demo is closed.
                    //
                    recognition_frame_d.addWindowListener(new WindowAdapter() {
                            public void windowClosing(WindowEvent e) {
                                if (!recognition_frame_d.getRecording()) {
                                    recognition_frame_d.setVisible(false);
                                    setVisible(true);  // reveal Selector
                                    recognition_frame_d
					.getConnectionManager().disconnect();
                                    recognition_frame_d.setConnection(false);
                                    recognition_frame_d = null;
                                    mainSRServer.stop();
                                }
                            }
                            
                            public void windowClosed(WindowEvent e) {
                                if (!recognition_frame_d.getRecording()) {
                                    recognition_frame_d.setVisible(false);
                                    setVisible(true);  // reveal Selector
                                    recognition_frame_d.
					getConnectionManager().disconnect(); 
                                    recognition_frame_d.setConnection(false); 
                                    recognition_frame_d = null;
                                    mainSRServer.stop();
                                }
                            }
                        }); // end WindowAdapter/addWindowListener

                    recognition_frame_d.setVisible(true);                    
                    System.out.println("speech recognition started");
                    
                    try {
                        mainSRServer.start();
                        System.out.println("speech recognition be started");
                        setVisible(false); // hide Selector
                    } 
                    catch (Exception ex) {
                        error_message_d = ex.toString();
                    } // end try/catch
                    main_panel_d.repaint();
                }
            });
	
        // Create the audio recorder label
        //
        String recognitionStr =
            "<html><font size=\"5\" color=\"#0000C8\">"+
            "<ul><li>" +
            "Speech Recognition: " +
            "<font color=\"#9B0028\">Convert your speech to text<br>" + 
            "using our public domain conversational speech<br>" +
            "recognition system</font>" +
            "</li></ul>" +
            "</font></html>";
        
        JLabel recognitionLbl = new JLabel(recognitionStr);
        recognitionLbl.setPreferredSize(lblSize);
        recognitionLbl.setVerticalAlignment(SwingConstants.TOP);
        
        // Create the recognition button
        //
        JButton dialogBtn = new JButton(dialogImg);
        
        dialogBtn.setDefaultCapable(false);
        dialogBtn.setBackground(COLOR_BTN_BLUE);
        dialogBtn.setBorder(BorderFactory.createEmptyBorder());
        dialogBtn.setHorizontalTextPosition(SwingConstants.CENTER);
        dialogBtn.setVerticalTextPosition(SwingConstants.BOTTOM);
        dialogBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    // Create the DS main server
                    //
                    File config_file_obj = new File(configFilename);
                    server_args_d = getArgs(config_file_obj);
		    final DialogNavigationMainServer mainDNServer 
			= new DialogNavigationMainServer(server_args_d);
                    
                    // Get the DS frame from the main server and show it
                    //
		    dialog_frame_d = mainDNServer.getDialogNavigationUIFrame();
                    
                    // Add a window listener for cleanup and to allow
                    // the demo selector to become visible again after
                    // the demo is closed.
                    //
                    dialog_frame_d.addWindowListener(new WindowAdapter() {
                            public void windowClosing(WindowEvent e) {
                                if (!dialog_frame_d.getRecording()) {
                                    dialog_frame_d.setVisible(false);
                                    setVisible(true);  // reveal Selector
                                    dialog_frame_d
					.getConnectionManager().disconnect();
                                    dialog_frame_d.setConnection(false);
                                    dialog_frame_d = null;
                                    mainDNServer.stop();
                                }
                            }
                            
                            public void windowClosed(WindowEvent e) {
                                if (!recognition_frame_d.getRecording()) {
                                    dialog_frame_d.setVisible(false);
                                    setVisible(true);  // reveal Selector
                                    dialog_frame_d
					.getConnectionManager().disconnect(); 
                                    dialog_frame_d.setConnection(false); 
                                    dialog_frame_d = null;
                                    mainDNServer.stop();
                                }
                            }
                        }); // end WindowAdapter/addWindowListener
                    
		    dialog_frame_d.setVisible(true);
                    System.out.println("dialog system started");
                    
                    try {
                        mainDNServer.start();
                        System.out.println("dialog system is started");
                        setVisible(false); // hide Selector
                    } 
                    catch (Exception ex) {
                        error_message_d = ex.toString();
                    } // end try/catch
                    main_panel_d.repaint();
  
                } // end actionPerformed
            }); // end ActionListener/addActionListener
        
        // Create the dialog label
        //
        String dialogStr =
            "<html><font size=\"5\" color=\"#0000C8\">"+
            "<ul><li>" +
            "In-Vehicle Dialog Systems: " +
            "<font color=\"#9B0028\">Learn to navigate<br>" + 
            "the Mississippi State University community<br>" +
            "using our conversational speech agent</font>" +
            "</li></ul>" +
            "</font></html>";

        JLabel dialogLbl = new JLabel(dialogStr);
        dialogLbl.setPreferredSize(lblSize);
        dialogLbl.setVerticalAlignment(SwingConstants.TOP);
        
        // Add the buttons and labels to their corresponding panels
        //
        pnl1.add(analysisBtn);
        pnl1.add(analysisLbl);
        pnl2.add(verifyBtn);
        pnl2.add(verifyLbl);
        pnl3.add(recognitionBtn);
        pnl3.add(recognitionLbl);
        pnl4.add(dialogBtn);
        pnl4.add(dialogLbl);
        
        // Create content for section 3 (instructions)
        //
        JPanel sec3 = new JPanel(new FlowLayout());

        JPanel bottom = new JPanel(new FlowLayout()) {
                public Dimension getPreferredSize() {
                        return new Dimension(TITLE_LOWER_WIDTH, 
                                             TITLE_LOWER_HEIGHT);
                }
                
                public void paintComponent(Graphics g) {
                    
                    // Get the size of this panel
                    //
                    Dimension sz = getSize();
		    
                    enableEvents(MouseEvent.MOUSE_CLICKED);
                    
                    Graphics2D g2;
                    g2 = (Graphics2D) g;
		    		    
		    GradientPaint gradient = new GradientPaint(0, 0, 
							       COLOR_MAROON,
							       765, 0,
							       Color.WHITE);
		    g2.setPaint(gradient);
		    g2.fill(new Rectangle2D.Double(5, 10, 765, 5));
		    
		    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                                        RenderingHints.VALUE_ANTIALIAS_ON);

		    
		    g2.setRenderingHint(RenderingHints.KEY_RENDERING, 
                                        RenderingHints.VALUE_RENDER_QUALITY);
		    
                    FontRenderContext frc1 = g2.getFontRenderContext();
		    
                    Font f1 = new Font("Times New Roman", 
                                       Font.BOLD|Font.ITALIC, 
                                       TITLE_FONT_SIZE);
                    
                    String s1 = new String("Center for Advanced " +
					   "Vehicular Systems");
                    TextLayout t1 = new TextLayout(s1, f1, frc1);
                    Rectangle2D t1Rect = t1.getBounds();
                    g2.setColor(COLOR_MAROON);
                    t1.draw(g2, 6, 
                            (float)(sz.getHeight() - 10));

		    String s2 = new String("CAVS");
                    TextLayout t2 = new TextLayout(s2, f1, frc1);
                    Rectangle2D t2Rect = t2.getBounds();
		    int sw = (int) t2.getBounds().getWidth();
		    int sh = (int) t2.getBounds().getHeight();
		    Shape sha = t2.getOutline(AffineTransform
					      .getTranslateInstance
					      (800 - 129, 46));		    
		    
		   
		    g2.setColor(COLOR_MAROON);
		    g2.setStroke(new BasicStroke(3));

		    g2.draw(sha);
		    g2.setPaint(Color.white);
		    g2.fill(sha);
		}
        };
        
        sec3.setBackground(Color.WHITE);
        sec3.setOpaque(false);
        sec3.add(bottom);
        
        // Add content (sections 1, 2, and 3) to main frame and set size
        //
        main_panel_d.add(sec1);

        main_panel_d.add(pnl1);
        main_panel_d.add(pnl2);
        main_panel_d.add(pnl3);
        main_panel_d.add(pnl4);

        main_panel_d.add(sec3);

        content.add(main_panel_d, BorderLayout.CENTER);

        setSize(WM_CANVAS_WIDTH, WM_CANVAS_HEIGHT);
        
        // Make sure all images are loaded before displaying app
        //
        try {
            tracker.waitForAll();
        }
        catch (InterruptedException ie) {}
        
        // Show app
        //
        setVisible(true);
    }
    
    //-------------------------------------------------------------------------
    //
    // class-specific other public methods:
    //  
    //
    //-------------------------------------------------------------------------

    /**
     * This method reads the parameters from a text file and passes it
     * as string arguments to the argument parser.
     *
     *@param param_file_a The text file containing the arguments.
     *@return an array of strings containing the arguments. 
     */
    public static String[] getArgs(File param_file_a) {
        
        BufferedReader input = null;
        String[] contents = new String [MAX_NUMBER_ARGS];

        try {
	    // Attempt to open the parameter file
	    //
            input = new BufferedReader(new FileReader(param_file_a));
            String all_lines = "";
	    String line = "";
	    
	    while ((line = input.readLine()) != null) {
		all_lines += " " + line;
	    }
	    // remove any opening and closing whitespace
	    //
	    all_lines = all_lines.trim();
	    
	    // Split it all on whitespace
	    //
            contents = all_lines.split("\\s");  

            System.out.println("DEBUG: All lines from parameter file [" + 
			       all_lines + "]");
        }
        catch (Exception ex) {
            System.out.println("Error while reading parameter file in " +
			       "getArgs()");
	    ex.printStackTrace();
	    // TODO, popup box and exit
	    //
	    System.exit(1);
        }

        finally {

            try {
                if (input != null) {
                    input.close();
                }
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        System.out.println("========= args start ========================");
        for (int i = 0; i < java.lang.reflect.Array.getLength(contents); i++)
            System.out.println(contents[i]);
        System.out.println("========== args end =======================");

	// create demo arg parser
	//
	DemoArgParser parser = new DemoArgParser(contents);
        
	// get the working mode
	//
	String mode = parser.getMode();

	System.out.println("mode=" + mode);

	// Things this section does
	// 1) replace "hub_ip" with "contact_hub" 
	// ^^ REMOVED - remains as "hub_ip" now
	//
	// 2) replace "hub_ip\contact_hub" value with "IP:new_port"
	// ^^ REMOVED
	//
	// 3) replace "ui_port" with "speech_port"
	// ^^ REMOVED
	//
	// 4) replace "ui_port" value with "new_port"
	//
	int index = 0;

	// replace the value of -hub_ip to -contact_hub
	// format:   IP:PORT
	//
	int new_port = parser.getClientPort();
		
	// make a new port number so no clients conflict
	//
	new_port = new_port + 2 * num_connection_d++;
	String client_port_string_d = "-client_port";
	
	// find the index of -ui_port
	//
	for (index = 0; index < contents.length; index++) {
	    if (contents[index].equalsIgnoreCase
		(client_port_string_d)) {
		break;
	    }
	}

	// We found the tag at this location, but we want to replace
	// the next one, so go one more
	//	
	index++;

	// error checking
	//
	if (index >= contents.length) {
	    
	    // TODO popup box
	    //
	    System.out.println("Could not find [" + 
			       client_port_string_d +
			       "] argument in parameter file");
	    System.exit(1);
	}
	
	// replace the -hub_ip with -contact_hub arg
	//
	contents[index] = new_port + "";
	
        System.out.println("========= args after modification==========");
        for (int i = 0; i < java.lang.reflect.Array.getLength(contents); i++)
            System.out.println(contents[i]);
        System.out.println("========== args end =======================");
        
        return contents;        
    }   
    
    //-------------------------------------------------------------------------
    //
    // main method for this class
    //
    //-------------------------------------------------------------------------
    
    /**
     * Creates a new DemoSelector.
     *
     * @param args arguments from the command line.
     */
    public static void main(String[] args) {
        new DemoSelector();
    }
    
}
