/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lol.chat;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
/**
*
* @author Ricardo
*/
public class ChatWindow extends javax.swing.JFrame {
private LoLChatAPI _lolChat;
private Chat _chat;
private MessageListener l;
/**
* Creates new form ChatWindow
*/
public ChatWindow(LoLChatAPI lolChat, Chat chat) {
initComponents();
setSize(new Dimension(500, 400));
setLocationRelativeTo(null);
_lolChat = lolChat;
this._chat = chat;
setTitle(lolChat.getUserName(chat.getParticipant()));
l = new MessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
if (message.getType() == Message.Type.chat) {
txtChat.append(getTitle() + ": " + message.getBody() + "\n\n");
} else {
txtChat.append(message.getBody() + "\n\n");
}
}
};
chat.addMessageListener(l);
addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
_chat.removeMessageListener(l);
_lolChat.deleteChat(_chat.getParticipant());
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
});
}
public ChatWindow() {
this(null, 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() {
jScrollPane3 = new javax.swing.JScrollPane();
txtChat = new javax.swing.JTextArea();
jPanel2 = new javax.swing.JPanel();
btnSend = new javax.swing.JButton();
jScrollPane4 = new javax.swing.JScrollPane();
txtSend = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
txtChat.setColumns(20);
txtChat.setRows(5);
jScrollPane3.setViewportView(txtChat);
getContentPane().add(jScrollPane3, java.awt.BorderLayout.CENTER);
jPanel2.setLayout(new java.awt.BorderLayout());
btnSend.setText("Enviar");
btnSend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSendActionPerformed(evt);
}
});
jPanel2.add(btnSend, java.awt.BorderLayout.LINE_END);
txtSend.setColumns(20);
txtSend.setRows(5);
txtSend.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
txtSendKeyPressed(evt);
}
});
jScrollPane4.setViewportView(txtSend);
jPanel2.add(jScrollPane4, java.awt.BorderLayout.CENTER);
getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
if (_chat != null) {
try {
_chat.sendMessage(txtSend.getText());
txtChat.append("Eu: " + txtSend.getText() + "\n\n");
txtSend.setText(null);
} catch (XMPPException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
}//GEN-LAST:event_btnSendActionPerformed
private void txtSendKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSendKeyPressed
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
if (!evt.isShiftDown()) {
btnSend.doClick();
}
else {
txtSend.append("\n");
}
}
}//GEN-LAST:event_txtSendKeyPressed
/**
* @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(ChatWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ChatWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ChatWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ChatWindow.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 ChatWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnSend;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTextArea txtChat;
private javax.swing.JTextArea txtSend;
// End of variables declaration//GEN-END:variables
}