Package lol.chat

Source Code of lol.chat.ContactsWindow

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

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.DefaultListModel;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.packet.Presence;

/**
*
* @author Ricardo
*/
public class ContactsWindow extends javax.swing.JFrame {

    private LoLChatAPI _lolChat;
    private DefaultListModel lstModel;

    /**
     * Creates new form ContactsWindow
     */
    public ContactsWindow(LoLChatAPI lolChat) {
        _lolChat = lolChat;
        initComponents();
        setLocationRelativeTo(null);

        lstModel = new DefaultListModel();
        lstContacts.setModel(lstModel);
        lstContacts.setCellRenderer(new LoLContactCellRenderer());
        lstContacts.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int i = lstContacts.locationToIndex(e.getPoint());
                    LoLContact contact = (LoLContact) lstContacts.getModel().getElementAt(i);
                    if (_lolChat != null) {
                        _lolChat.createChat(contact);
                    }
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });

        lolChat.addRosterListener(new RosterListener() {
            @Override
            public void entriesAdded(Collection<String> addresses) {
                System.out.println("entriesAdded");
                ArrayList<LoLContact> cc = _lolChat.getBuddyList();
                for (LoLContact c : cc) {
                    if (!lstModel.contains(c)) {
                        lstModel.addElement(c);
                    }
                }
            }

            @Override
            public void entriesUpdated(Collection<String> addresses) {
                System.out.println("entriesUpdated");
                ArrayList<LoLContact> cc = _lolChat.getBuddyList();
                for (LoLContact c : cc) {
                    if (!lstModel.contains(c)) {
                        lstModel.addElement(c);
                    }
                }
            }

            @Override
            public void entriesDeleted(Collection<String> addresses) {
                System.out.println("entriesDeleted");
                lstModel.clear();
                ArrayList<LoLContact> cc = _lolChat.getBuddyList();
                for (LoLContact c : cc) {
                    lstModel.addElement(c);
                }
            }

            @Override
            public void presenceChanged(Presence presence) {
                try {
                    System.out.println("presenceChanged");
                    lstModel.clear();
                    ArrayList<LoLContact> cc = _lolChat.getBuddyList();
                    for (LoLContact c : cc) {
                        lstModel.addElement(c);
                    }
                }
                catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        });
    }

    public ContactsWindow() {
        this(null);
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstContacts = new javax.swing.JList();
        btnRefresh = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuChangeStatus = new javax.swing.JMenuItem();

        jScrollPane2.setViewportView(jTree1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LoL Chat");

        jScrollPane1.setViewportView(lstContacts);

        btnRefresh.setText("Atualizar");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        jMenu1.setText("Estado");

        menuChangeStatus.setText("Mudar estado");
        menuChangeStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuChangeStatusActionPerformed(evt);
            }
        });
        jMenu1.add(menuChangeStatus);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        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()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(btnRefresh)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnRefresh)
                .addContainerGap())
        );

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

    private void menuChangeStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuChangeStatusActionPerformed
        new StatusWindow(_lolChat).setVisible(true);
    }//GEN-LAST:event_menuChangeStatusActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        lstModel.removeAllElements();
        ArrayList<LoLContact> cc = _lolChat.getBuddyList();
        for (LoLContact c : cc) {
            lstModel.addElement(c);
        }
    }//GEN-LAST:event_btnRefreshActionPerformed

    /**
     * @param args the command line arguments
     */
    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(ContactsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ContactsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ContactsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ContactsWindow.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 ContactsWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRefresh;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTree jTree1;
    private javax.swing.JList lstContacts;
    private javax.swing.JMenuItem menuChangeStatus;
    // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of lol.chat.ContactsWindow

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.