
/**
 * SGRBAGui.java
 *
 * @author Wanderson C. Bragança <wanderson.wbc@gmail.com>
 * @link http://www.wanderson.wordpress.com
 * @copyright Copyright © 2009
 * @version  14/04/2009
 */
import javax.swing.DefaultListModel;
import java.util.*;
import jade.core.*;
import jade.gui.GuiEvent;
import javax.swing.JOptionPane;
import javax.swing.event.*;
import javax.swing.table.*;

public class SGRBAGui extends javax.swing.JFrame {

    transient protected SGRBA myAgent;
    private TabelaPosicoes localDisponivel;
    private TabelaPosicoes localVisitado;
    private DefaultListModel listModel;
    private String p = "todas";
    public Iterator lv;

    /** Creates new form SGRBAGui */
    public SGRBAGui(SGRBA a) {
        this.setTitle("SGRBA");
        this.myAgent = a;
        initComponents();

        // Lista agente criados
        listModel = new DefaultListModel();
        listaAgentes.setModel(listModel);

        // Exibe plataformas disponiveis
        localDisponivel = new TabelaPosicoes();
        plataformas.setModel(localDisponivel);

        TableColumn c;
        c = plataformas.getColumn((Object) plataformas.getColumnName(0));
        c.setHeaderValue((Object) (new String("ID")));
        c = plataformas.getColumn((Object) plataformas.getColumnName(1));
        c.setHeaderValue((Object) (new String("Container")));
        c = plataformas.getColumn((Object) plataformas.getColumnName(2));
        c.setHeaderValue((Object) (new String("Protocolo")));
        c = plataformas.getColumn((Object) plataformas.getColumnName(3));
        c.setHeaderValue((Object) (new String("Host")));
        plataformas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        mover.setEnabled(false);
        removerAgente.setEnabled(false);

        pararMonitoramento.setEnabled(false);
        listaAgentes.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                if (listaAgentes.getSelectedIndex() == -1) {
                    mover.setEnabled(false);
                    removerAgente.setEnabled(false);
                } else {
                    mover.setEnabled(true);
                    removerAgente.setEnabled(true);
                }
            }
        });
        // Plataformas visitadas
        localVisitado = new TabelaPosicoes();
        plataformasVisitadas.setModel(localVisitado);
        c = plataformasVisitadas.getColumn((Object) plataformasVisitadas.getColumnName(0));
        c.setHeaderValue((Object) (new String("ID")));
        c = plataformasVisitadas.getColumn((Object) plataformasVisitadas.getColumnName(1));
        c.setHeaderValue((Object) (new String("Container")));
        c = plataformasVisitadas.getColumn((Object) plataformasVisitadas.getColumnName(2));
        c.setHeaderValue((Object) (new String("Protocolo")));
        c = plataformasVisitadas.getColumn((Object) plataformasVisitadas.getColumnName(3));
        c.setHeaderValue((Object) (new String("Host")));


    }

    public void changeLookAndFeel(int index) {
        javax.swing.UIManager.LookAndFeelInfo[] looks = javax.swing.UIManager.getInstalledLookAndFeels();
        try {
            javax.swing.UIManager.setLookAndFeel(looks[index].getClassName());
            javax.swing.SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        atualizar = new javax.swing.JButton();
        sair = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaAgentes = new javax.swing.JList();
        novoAgente = new javax.swing.JButton();
        removerAgente = new javax.swing.JButton();
        mover = new javax.swing.JButton();
        iniciarMonitoramento = new javax.swing.JButton();
        pararMonitoramento = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        plataformasVisitadas = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        plataformas = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        atualizar.setText("Atualizar");
        atualizar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                atualizarMouseClicked(evt);
            }
        });

        sair.setText("Sair");
        sair.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                sairMouseClicked(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel2.setForeground(new java.awt.Color(51, 102, 255));

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Agentes Criados"));

        listaAgentes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane2.setViewportView(listaAgentes);

        novoAgente.setText("Novo Agente");
        novoAgente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                novoAgenteMouseClicked(evt);
            }
        });

        removerAgente.setText("Destruir Agente");
        removerAgente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                removerAgenteMouseClicked(evt);
            }
        });

        mover.setText("Mover Agente");
        mover.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                moverMouseClicked(evt);
            }
        });

        iniciarMonitoramento.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        iniciarMonitoramento.setForeground(new java.awt.Color(0, 0, 204));
        iniciarMonitoramento.setText("Inciar Monitoramento");
        iniciarMonitoramento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                iniciarMonitoramentoMouseClicked(evt);
            }
        });

        pararMonitoramento.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        pararMonitoramento.setForeground(java.awt.Color.red);
        pararMonitoramento.setText("Parar Monitoramento");
        pararMonitoramento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pararMonitoramentoMouseClicked(evt);
            }
        });

        jLabel1.setForeground(new java.awt.Color(51, 51, 255));
        jLabel1.setText("*Para mover! Selecione o Agente e a plataforma de destino!");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(novoAgente, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                        .addComponent(removerAgente, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(mover, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(iniciarMonitoramento)
                        .addGap(35, 35, 35)
                        .addComponent(pararMonitoramento))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(novoAgente)
                    .addComponent(mover)
                    .addComponent(removerAgente))
                .addGap(32, 32, 32)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(iniciarMonitoramento)
                    .addComponent(pararMonitoramento))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Locais visitados"));

        plataformasVisitadas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Título 1", "Título 2", "Título 3", "Título 4"
            }
        ));
        jScrollPane3.setViewportView(plataformasVisitadas);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Plataformas Disponíveis"));

        plataformas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Título 1", "Título 2", "Título 3", "Título 4"
            }
        ));
        jScrollPane1.setViewportView(plataformas);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(atualizar)
                        .addGap(28, 28, 28)
                        .addComponent(sair)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(22, 22, 22)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(19, 19, 19)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sair)
                    .addComponent(atualizar))
                .addContainerGap())
        );

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

    private void pararMonitoramentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pararMonitoramentoMouseClicked
        // Parar monitoramento web
        GuiEvent ev = new GuiEvent((Object) this, myAgent.PARAR_MONITORAMENTO_WEB);
        myAgent.postGuiEvent(ev);
    }//GEN-LAST:event_pararMonitoramentoMouseClicked

    private void moverMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_moverMouseClicked
        // Move Agente
        Location dest;
        int sel = plataformas.getSelectedRow();
        if (sel >= 0) {
            dest = localDisponivel.getElementAt(sel);
            GuiEvent ev = new GuiEvent((Object) this, myAgent.MOVE_AGENTE);
            ev.addParameter(dest);
            ev.addParameter((String) listaAgentes.getSelectedValue());
            ev.addParameter(p);
            myAgent.postGuiEvent(ev);
            
        }
        else{
            JOptionPane.showMessageDialog(null,"Selecione uma plataforma");
        }

    }//GEN-LAST:event_moverMouseClicked

    private void removerAgenteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_removerAgenteMouseClicked
        // mata o agente:
        GuiEvent ge = new GuiEvent(this, myAgent.KILL_AGENTE);
        ge.addParameter((String) listaAgentes.getSelectedValue());
        myAgent.postGuiEvent(ge);
    }//GEN-LAST:event_removerAgenteMouseClicked

    private void novoAgenteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_novoAgenteMouseClicked
        GuiEvent ge = new GuiEvent(this, myAgent.NOVO_AGENTE);
        myAgent.postGuiEvent(ge);
    }//GEN-LAST:event_novoAgenteMouseClicked

    private void atualizarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_atualizarMouseClicked

        GuiEvent ev = new GuiEvent(null, myAgent.ATUALIZAR);
        myAgent.postGuiEvent(ev);
    }//GEN-LAST:event_atualizarMouseClicked

    private void sairMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sairMouseClicked
        GuiEvent ge = new GuiEvent(this, myAgent.SAIR);
        myAgent.postGuiEvent(ge);

    }//GEN-LAST:event_sairMouseClicked

    private void iniciarMonitoramentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_iniciarMonitoramentoMouseClicked
        GuiEvent ev = new GuiEvent((Object) this, myAgent.INICIAR_MONITORAMENTO_WEB);
        myAgent.postGuiEvent(ev);
        pararMonitoramento.setEnabled(true);
    }//GEN-LAST:event_iniciarMonitoramentoMouseClicked

    public void updateLocais(Iterator list) {
        localDisponivel.clear();
        for (; list.hasNext();) {
            Object obj = list.next();
            localDisponivel.add((Location) obj);
        }
        localDisponivel.fireTableDataChanged();
    }

    public void updateLocaisVisitados(Location vi, String w) {
        if (w.equals("todas")) {
            for (; lv.hasNext();) {
                Object obj = lv.next();
                localVisitado.add((Location) obj);

            }
            localVisitado.fireTableDataChanged();
        } else {
            localVisitado.add(vi);
            localVisitado.fireTableDataChanged();
        }
    }

    public void updateList(Vector v) {
        listModel.clear();
        for (int i = 0; i < v.size(); i++) {
            listModel.addElement(v.get(i));
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton atualizar;
    private javax.swing.JButton iniciarMonitoramento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList listaAgentes;
    private javax.swing.JButton mover;
    private javax.swing.JButton novoAgente;
    private javax.swing.JButton pararMonitoramento;
    private javax.swing.JTable plataformas;
    private javax.swing.JTable plataformasVisitadas;
    private javax.swing.JButton removerAgente;
    private javax.swing.JButton sair;
    // End of variables declaration//GEN-END:variables
}
