/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chatserver;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;

public class MenuPrincipal extends JFrame implements Runnable {

    private ServerSocket serverSocket = null;
    private boolean mensagem = false;

    public MenuPrincipal() {
        initComponents();
    }

    @Override
    public void run() {
        try {
            serverSocket = new ServerSocket(5200);
            Socket cliente = serverSocket.accept();
            DataOutputStream out = new DataOutputStream(cliente.getOutputStream());
            DataInputStream in = new DataInputStream(cliente.getInputStream());
            out.writeUTF("Envie algo");
            while (true) {
                
                if (in.available() > 0) {
                    taRecebe.append(in.readUTF());
                }
                if(mensagem) {
                    out.writeUTF(tfEnvia.getText());
                    mensagem = false;
                }
                Thread.sleep(10);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tbMenu = new javax.swing.JToolBar();
        btServir = new javax.swing.JButton();
        btConectar = new javax.swing.JButton();
        paStatus = new javax.swing.JPanel();
        lbStatus = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taRecebe = new javax.swing.JTextArea();
        tfEnvia = new javax.swing.JTextField();
        btEnvia = new javax.swing.JButton();
        mbMenu = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tbMenu.setRollover(true);

        btServir.setText("Servir");
        btServir.setFocusable(false);
        btServir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btServir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btServir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btServirActionPerformed(evt);
            }
        });
        tbMenu.add(btServir);

        btConectar.setText("Conectar");
        btConectar.setFocusable(false);
        btConectar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btConectar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btConectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConectarActionPerformed(evt);
            }
        });
        tbMenu.add(btConectar);

        lbStatus.setText("Não conectado ...");

        javax.swing.GroupLayout paStatusLayout = new javax.swing.GroupLayout(paStatus);
        paStatus.setLayout(paStatusLayout);
        paStatusLayout.setHorizontalGroup(
            paStatusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paStatusLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbStatus)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        paStatusLayout.setVerticalGroup(
            paStatusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paStatusLayout.createSequentialGroup()
                .addComponent(lbStatus)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        taRecebe.setColumns(20);
        taRecebe.setRows(5);
        jScrollPane1.setViewportView(taRecebe);

        btEnvia.setText("Enviar");
        btEnvia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEnviaActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        mbMenu.add(jMenu1);

        jMenu2.setText("Edit");
        mbMenu.add(jMenu2);

        setJMenuBar(mbMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tbMenu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(paStatus, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfEnvia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEnvia)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tbMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfEnvia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btEnvia))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(paStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

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

    private void btServirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btServirActionPerformed
        new Thread(this).start();
        lbStatus.setText("Servidor ouvindo na porta 5200");
    }//GEN-LAST:event_btServirActionPerformed

    private void btEnviaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEnviaActionPerformed
        mensagem = true;
    }//GEN-LAST:event_btEnviaActionPerformed

    private void btConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConectarActionPerformed
        new ExecutaCliente().setVisible(true);
    }//GEN-LAST:event_btConectarActionPerformed

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MenuPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MenuPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MenuPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MenuPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MenuPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btConectar;
    private javax.swing.JButton btEnvia;
    private javax.swing.JButton btServir;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbStatus;
    private javax.swing.JMenuBar mbMenu;
    private javax.swing.JPanel paStatus;
    private javax.swing.JTextArea taRecebe;
    private javax.swing.JToolBar tbMenu;
    private javax.swing.JTextField tfEnvia;
    // End of variables declaration//GEN-END:variables

}
