
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JComboBox;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIFrame.java
 *
 * Created on 11-08-2011, 07:50:39 PM
 */
/**
 * Clase que crea el Frame de bienvenida de la GUI
 * @author USM
 */
public class WelcomeFrame extends javax.swing.JFrame {
    /** Crea Frame de bienvenida del Frame */
    public WelcomeFrame(GUIFrame gui) {
        GUI = gui;
        initComponents();
    }

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

        jPanel1 = new javax.swing.JPanel();
        NameLabel = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        Logo = new javax.swing.JLabel();
        Title = new javax.swing.JLabel();
        cboxPorts = new javax.swing.JComboBox();
        PortsLabel = new javax.swing.JLabel();
        NameField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Pantalla de Bienvenida");
        setBackground(new java.awt.Color(255, 255, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("InitFrame"); // NOI18N
        setResizable(false);

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

        NameLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        NameLabel.setText("Ingrese su nombre:");

        btnLogin.setFont(new java.awt.Font("Tahoma", 0, 13));
        btnLogin.setText("Entrar");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        Logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/logo.png"))); // NOI18N

        Title.setFont(new java.awt.Font("Tahoma", 0, 20));
        Title.setText("Bienvenidos");

        cboxPorts.setFont(new java.awt.Font("Tahoma", 0, 12));
        cboxPorts.setSelectedItem(cboxPorts);

        PortsLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        PortsLabel.setText("Elija el puerto COM:");

        NameField.setFont(new java.awt.Font("Tahoma", 0, 12));
        NameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                NameFieldKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(156, Short.MAX_VALUE)
                .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(171, 171, 171))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(153, 153, 153)
                .addComponent(Title)
                .addContainerGap(140, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(NameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(NameField, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(PortsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cboxPorts, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(71, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(Logo)
                .addContainerGap(26, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addComponent(Logo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Title)
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PortsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboxPorts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(btnLogin)
                .addGap(42, 42, 42))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-416)/2, (screenSize.height-338)/2, 416, 338);
    }// </editor-fold>//GEN-END:initComponents

	private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
		setVisible(false);

		GUI.getConfigCOMPanel().setComboBox();
		GUI.setVisible(true);
		GUI.setfocustoPanels();

		Communicator COM = GUI.getCommunicator();
		COM.connect((String) cboxPorts.getSelectedItem());

		if (COM.getConnected() == true) {
			if (COM.initIOStream() == true) {
				COM.initListener();
				GUI.getConfigCOMPanel().setConectButton(false);
				GUI.getConfigCOMPanel().setDisconectButton(true);
			}
		}
	}//GEN-LAST:event_btnLoginActionPerformed

    private void NameFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NameFieldKeyPressed
    if (evt.getKeyCode() == evt.VK_ENTER) {
        setVisible(false);

        GUI.getConfigCOMPanel().setComboBox();
        GUI.setVisible(true);
        GUI.setfocustoPanels();

        Communicator COM = GUI.getCommunicator();
        COM.connect((String) cboxPorts.getSelectedItem());

        if (COM.getConnected() == true) {
            if (COM.initIOStream() == true) {
                COM.initListener();
                GUI.getConfigCOMPanel().setConectButton(false);
                GUI.getConfigCOMPanel().setDisconectButton(true);
            }
        }
    }
    // TODO add your handling code here:
    }//GEN-LAST:event_NameFieldKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Logo;
    private javax.swing.JTextField NameField;
    private javax.swing.JLabel NameLabel;
    private javax.swing.JLabel PortsLabel;
    private javax.swing.JLabel Title;
    private javax.swing.JButton btnLogin;
    private javax.swing.JComboBox cboxPorts;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    private GUIFrame GUI = null;
    /**
	* Retorna los puertos disponibles en el JComboBox
	* @return Puertos serial
	*/
    public JComboBox getComboBox() {
		return cboxPorts;
    }
	/**
	* 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());
    }
}
