Package JabberClient

Source Code of JabberClient.ChatWindow

package JabberClient;

import java.util.Observable;
import java.util.Observer;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;


public class ChatWindow extends javax.swing.JFrame implements Observer {

    private XMPPConnection xmppconnection;
    private ChatManager chatmanager;
    private Chat chat;
    private Contact contact;
   
    public ChatWindow(XMPPConnection xmppconnection, Contact contact) {
        this.xmppconnection = xmppconnection;
        this.contact = contact;
        initComponents();
        this.getRootPane().setDefaultButton(jbtSendMessage);
        changeGUI();
        jbtSendMessage.setMnemonic('s');
      
        chatmanager = xmppconnection.getChatManager();
        chat = chatmanager.createChat(this.getTitle(), new MessageListener() {
            public void processMessage(Chat chat, Message message) {
                txtAreaChat.setText(txtAreaChat.getText() + message.getFrom() + ": " + message.getBody().toString() + "\n");
                setVisible(true);
            }
        });
    }

    public ChatWindow(XMPPConnection xmppconnection, Message message, Contact contact) {
        initComponents();
        this.getRootPane().setDefaultButton(jbtSendMessage);
        String strFrom = message.getFrom();
        this.setTitle(strFrom);
        this.contact = contact;
        this.xmppconnection = xmppconnection;

        changeGUI();
        jbtSendMessage.setMnemonic('s');

        txtAreaChat.setText( strFrom + ": " + message.getBody().toString() + "\n");

        chatmanager = xmppconnection.getChatManager();
        chat = chatmanager.createChat(message.getFrom(), new MessageListener() {
            public void processMessage(Chat chat, Message msg) {
                txtAreaChat.setText(txtAreaChat.getText() + msg.getFrom() + ": " + msg.getBody().toString() + "\n");
                setVisible(true);
            }
        });
    }



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

        jScrollPane1 = new javax.swing.JScrollPane();
        txtAreaChat = new javax.swing.JTextArea();
        jbtSendMessage = new javax.swing.JButton();
        jbtFileTransfer = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtSendMessage = new javax.swing.JTextArea();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        txtAreaChat.setColumns(20);
        txtAreaChat.setEditable(false);
        txtAreaChat.setRows(5);
        jScrollPane1.setViewportView(txtAreaChat);

        jbtSendMessage.setText("Senden");
        jbtSendMessage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSendMessageActionPerformed(evt);
            }
        });

        jbtFileTransfer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/floppy_mount.jpg"))); // NOI18N
        jbtFileTransfer.setToolTipText("Dateitransfer ist nur möglich, wenn der Kontakt online ist.");
        jbtFileTransfer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtFileTransferActionPerformed(evt);
            }
        });

        txtSendMessage.setColumns(20);
        txtSendMessage.setRows(5);
        jScrollPane2.setViewportView(txtSendMessage);

        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(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbtFileTransfer, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jbtSendMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbtFileTransfer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtSendMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

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

    private void jbtSendMessageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSendMessageActionPerformed

        txtAreaChat.setText(txtAreaChat.getText() + xmppconnection.getUser() + ": " + txtSendMessage.getText() + "\n" );

        //hier werden nachricht senden
        try {
            chat.sendMessage(txtSendMessage.getText());
        }
        catch (XMPPException e) {
            System.out.println("Error Delivering block");
        }
        txtSendMessage.setText(null);
    }//GEN-LAST:event_jbtSendMessageActionPerformed


    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing

    }//GEN-LAST:event_formWindowClosing

    private void jbtFileTransferActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtFileTransferActionPerformed
        FileTransfer filetransfer = new FileTransfer(xmppconnection, this);
        filetransfer.sendFile(contact.getFullJID());
    }//GEN-LAST:event_jbtFileTransferActionPerformed


    public void update(Observable o, Object arg) {
        changeGUI();
    }

    private void changeGUI(){
        if (!contact.getPresence().equals("unavailable")){
            jbtFileTransfer.setEnabled(true);
            this.setTitle(contact.getFullJID());}

        else{
            jbtFileTransfer.setEnabled(false);
            this.setTitle(contact.getBareJID());}
    }

    public void setMessage (Message message)
    {
        txtAreaChat.setText(txtAreaChat.getText() + message.getFrom() + ": " + message.getBody().toString() + "\n");
    }
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtFileTransfer;
    private javax.swing.JButton jbtSendMessage;
    private javax.swing.JTextArea txtAreaChat;
    private javax.swing.JTextArea txtSendMessage;
    // End of variables declaration//GEN-END:variables

}
TOP

Related Classes of JabberClient.ChatWindow

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.