import javax.media.*;
import javax.media.format.*;
import java.util.Vector;
public class fPrincipal extends javax.swing.JDialog {

    private Emisor tr;
    private Receptor rc;
    private Vector Dispositivos;
    private CaptureDeviceInfo audioCaptureDeviceInfo;  
 
    /** Creates new form fPrincipal */
    public fPrincipal(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

        initComponents();
        this.setSize(288, 235);        
        jButton2.setEnabled(false);
		Inicializar();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        tfPuertoEsc = new javax.swing.JTextField();
        tfPuertoTrans = new javax.swing.JTextField();
        tfIP = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lEstado = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        getContentPane().setLayout(null);

        setTitle("Audio-Conferencia");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jButton1.setText("Iniciar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton1);
        jButton1.setBounds(10, 170, 69, 26);

        jButton2.setText("Parar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton2);
        jButton2.setBounds(100, 170, 81, 26);

        jPanel1.setLayout(null);

        jPanel1.setBorder(new javax.swing.border.TitledBorder("Datos de comunicacion"));
        jPanel1.add(tfPuertoEsc);
        tfPuertoEsc.setBounds(190, 60, 60, 20);

        jPanel1.add(tfPuertoTrans);
        tfPuertoTrans.setBounds(190, 40, 60, 20);

        jPanel1.add(tfIP);
        tfIP.setBounds(160, 20, 90, 20);

        jLabel3.setText("Puerto de escucha:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(10, 60, 130, 16);

        jLabel2.setText("Puerto de transmision:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 40, 140, 16);

        jLabel1.setText("Direccion IP destino:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 20, 120, 16);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 10, 260, 90);

        jPanel2.setLayout(null);

        jPanel2.setBorder(new javax.swing.border.TitledBorder("Estado"));
        lEstado.setText("Sin conexion");
        lEstado.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jPanel2.add(lEstado);
        lEstado.setBounds(10, 26, 240, 16);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(10, 100, 260, 60);

        jButton3.setText("Cerrar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton3);
        jButton3.setBounds(200, 170, 71, 26);

        pack();
    }//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // Add your handling code here:
        System.exit(0);        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // Add your handling code here:
        eventStop();
        talkStop();
        listenStop();
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Add your handling code here:
        eventStart();
        talkStart();
        listenStart();
       
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
        System.exit(0);
    }//GEN-LAST:event_closeDialog
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new fPrincipal(new javax.swing.JFrame(), true).show();
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField tfPuertoEsc;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel lEstado;
    private javax.swing.JTextField tfPuertoTrans;
    private javax.swing.JTextField tfIP;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
    public void Inicializar(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
	Dispositivos = CaptureDeviceManager.getDeviceList ( null );
	Format [] format;
	for(int j = 0 ; j < Dispositivos.size(); j++){
	    format = ((CaptureDeviceInfo)Dispositivos.elementAt(j)).getFormats();
	    for(int i = 0 ; i < format.length; i++){
		if(format[i] instanceof AudioFormat)
		    audioCaptureDeviceInfo = (CaptureDeviceInfo)Dispositivos.elementAt(j);
		
	    }
	}
	if(audioCaptureDeviceInfo == null){
            lEstado.setText("Dispositivo de audio no encontrado");
	    return;
	}	
    }
    
    public void talkStart(){

	String ipaddress;
	String pb;
	
	if(audioCaptureDeviceInfo == null){
	    lEstado.setText("Dispositivo de audio no encontrado");
	    return;
	}
	ipaddress = tfIP.getText();  
	pb = tfPuertoTrans.getText();
        lEstado.setText("Creando la clase para la transmision");
	try{
	    tr = new Emisor(audioCaptureDeviceInfo.getLocator(), ipaddress, pb, new AudioFormat(AudioFormat.GSM_RTP,8000,4,1));
	} catch(Exception e){
	    lEstado.setText("Error al crear la transmision");
	    return;
	}
	lEstado.setText("Inicializando transmision");
	if(tr.start() != null){
	    lEstado.setText("Error en la transmision");
	    return;
	}
	lEstado.setText("Transmitiendo");
    }

    public void talkStop(){
	lEstado.setText("Parando la transmision");
	if(tr!=null)
	    tr.stop();
	lEstado.setText("Transmision parada");
    }

    public void listenStart(){
	String ipaddress;
	int pb;
	ipaddress = tfIP.getText(); 
	pb = Integer.parseInt(tfPuertoEsc.getText()); 
	rc = new Receptor(ipaddress, pb);
	if(rc == null){
	    lEstado.setText("Error en la escucha. No inicializado");
	    return;
	}
	if(!rc.initialize()){
	    lEstado.setText("Error en la escucha. No inicializado");
	    return;
	}
	lEstado.setText("Conectado");
    }
    public void listenStop(){
	if(rc!=null)
	    rc.close();

	lEstado.setText("No Inicializado");
    }
    public void eventStop(){
        jButton1.setEnabled(true);
        jButton2.setEnabled(false);
    }
    public void eventStart(){
        jButton2.setEnabled(true);
        jButton1.setEnabled(false);
    }
}
