import java.util.*;
import javax.swing.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 17-08-2011, 07:18:56 PM
 */

/**
 * Clase que crea Frame con informacion de informacion serial.
 * @author Manuel
 */
public class ConfigCOMFrame extends JFrame {
    
    /** Crea nuevo ConfigCOMFrame*/
    public ConfigCOMFrame(GUIFrame GUI, WelcomeFrame Welcome) {
        this.GUI = GUI;
        this.Welcome = Welcome;
        initComponents();
    }

    /** Metodo llamado por el costructor para inicializar los elementos del ConfigCOMFrame.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtLog = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        ConectButton = new javax.swing.JButton();
        DisconectButton = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        PortLabel = new javax.swing.JLabel();
        cboxPorts = new javax.swing.JComboBox();

        setTitle("Comunicator Console");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());

        txtLog.setColumns(80);
        txtLog.setEditable(false);
        txtLog.setRows(15);
        txtLog.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jScrollPane1.setViewportView(txtLog);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel2.setFocusCycleRoot(true);
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jPanel4.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        ConectButton.setText("Conectar");
        ConectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConectButtonActionPerformed(evt);
            }
        });
        jPanel4.add(ConectButton);

        DisconectButton.setText("Desconectar");
        DisconectButton.setEnabled(false);
        DisconectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DisconectButtonActionPerformed(evt);
            }
        });
        jPanel4.add(DisconectButton);

        jPanel2.add(jPanel4);

        jPanel5.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        PortLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        PortLabel.setText("Puerto ");
        PortLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jPanel5.add(PortLabel);

        jPanel5.add(cboxPorts);

        jPanel2.add(jPanel5);

        jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_END);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ConectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConectButtonActionPerformed

        Communicator COM = GUI.getCommunicator();

        COM.connect( (String) cboxPorts.getSelectedItem());
        if (COM.getConnected() == true) {
            if (COM.initIOStream() == true)
                COM.initListener();
                setConectButton(false);
                setDisconectButton(true);
        }
    }//GEN-LAST:event_ConectButtonActionPerformed

    private void DisconectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DisconectButtonActionPerformed
        GUI.getCommunicator().disconnect();
        setConectButton(true);
        setDisconectButton(false);
    }//GEN-LAST:event_DisconectButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ConectButton;
    private javax.swing.JButton DisconectButton;
    private javax.swing.JLabel PortLabel;
    private javax.swing.JComboBox cboxPorts;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtLog;
    // End of variables declaration//GEN-END:variables
    private GUIFrame GUI = null;
    private WelcomeFrame Welcome = null;
	
	/**
	* Obtiene lo escrito en el area de texto txtLog.
	* @return Contenido cuadro de texto.
	*/
    public JTextArea get_txtLog() {
        return txtLog;
    }
	
	/**
	* Obtiene y agrega los puertos de transmision serial disponibles
	*/
    public void setComboBox() {

        ArrayList a = GUI.getCommunicator().getPortNames();
        Iterator iter = a.iterator();
        while (iter.hasNext())
            cboxPorts.addItem(iter.next());

        cboxPorts.setSelectedItem((String) Welcome.getComboBox().getSelectedItem());
    }
	
	/**
	* Ajusta estado de boton de Conectado
	* @param state Estado de conexion a ajustar.
	*/
    public void setConectButton(boolean state) {
        ConectButton.setEnabled(state);
    }
	
	/**
	* Ajusta estado de boton de Desconectado
	* @param state Estado de conexion a ajustar.
	*/
    public void setDisconectButton(boolean state) {
        DisconectButton.setEnabled(state);
    }
}
