/* MainForLab.java */

package distributedwave;

import java.awt.Rectangle;

/** Main program for the distributed wave demo/lab
 *
 *  copyright (c) 2010, 2011 - Russell C. Bjork
 */
public class Main {
    
    /** The main program
     *
     * @param args the command line arguments
     */
    public static void main(final String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SocketType socketType; 
                try
                {
                    if (args.length > 0)
                        socketType = SocketType.valueOf(SocketType.class, args[0]);
                    else
                    {
                        int choice = javax.swing.JOptionPane.showOptionDialog(
                                null,
                                "Socket Type",
                                "Choose Socket Type",
                                javax.swing.JOptionPane.DEFAULT_OPTION,
                                javax.swing.JOptionPane.QUESTION_MESSAGE,
                                null,
                                SocketType.values(),
                                null);
                        if (choice < 0)
                            System.exit(0);
                        socketType = SocketType.values()[choice];
                    }
                    ParametersDialog parametersDialog =
                            new ParametersDialog(null, true, args.length == 0);
                    parametersDialog.requestParameters();

                    DistributedWave wave;
                    switch(socketType)
                    {
                        case UDP:
                            wave = new DistributedWaveUDP(
                                parametersDialog.getLeftNeighbor(),
                                parametersDialog.getRightNeighbor());
                            break;
                        case TCP:
                        default:
                            wave = new DistributedWaveTCP(
                                parametersDialog.isSingleSystem()
                                    ? null :
                                    parametersDialog.getLeftNeighbor(),
                                parametersDialog.getRightNeighbor());
                            break;
                    }
                    new MainFrame(wave).setVisible(true);
                    wave.startListeners();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        });
    }
    
    private enum SocketType
    {
        TCP,
        UDP;
    }  
}
