package JabberClient;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.NotFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.OfflineMessageManager;
public class MainWindow extends javax.swing.JFrame {
private XMPPConnection xmppconnection;
private Roster roster;
private Contacts contacts;
private Contact contact;
private FileTransfer filetransfer;
private DefaultListModel defaultlistmodel = new DefaultListModel();
private SearchWindow searchwindow;
private LoginWindow loginwindow;
private Presence presence = new Presence(Presence.Type.available);
private String strDelRosterItem;
public MainWindow(final XMPPConnection xmppconnection, LoginWindow loginwindow) {
this.xmppconnection = xmppconnection;
this.loginwindow = loginwindow;
contacts = new Contacts();
OfflineMessageManager offlineMgr = new OfflineMessageManager(xmppconnection);
try {
Iterator<Message> it = offlineMgr.getMessages();
while(it.hasNext()){
System.out.println("Offline Message: " + it.next().getBody());
}
} catch (XMPPException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
final JPopupMenu popup = new JPopupMenu();
JMenuItem popupItem = new JMenuItem("Eintrag löschen");
popup.add(popupItem);
filetransfer = new FileTransfer(xmppconnection);
filetransfer.receiveFile();
initComponents();
jlblUsername.setText(xmppconnection.getUser());
ComboBoxCellRenderer renderer = new ComboBoxCellRenderer();
jComboBoxStatus.setModel(loginwindow.getComboBoxItems());
jComboBoxStatus.setRenderer(renderer);
jComboBoxStatus.setSelectedIndex(loginwindow.getComboBoxSelectedItem());
jComboBoxStatus.setForeground(((ComboItem)jComboBoxStatus.getSelectedItem()).getColor());
final AndFilter andFilter = new AndFilter(new PacketTypeFilter(Message.class));
//Listener für eingehende Messages, dass dann ein Fenster geöffnet wird
final PacketListener pktListener = new PacketListener() {
public void processPacket(Packet pktPacket) {
boolean bWindow = false;
if (pktPacket instanceof Message) {
Message msg = (Message) pktPacket;
//String strFrom = msg.getFrom().substring(0, msg.getFrom().indexOf("/"));
String strFrom = msg.getFrom();
//System.out.println("Message: " + msg.getFrom() + " " + msg.getBody());
//System.out.println("geoeffnete Fenster:");
for(Frame frame : JFrame.getFrames()){
//if(frame.isVisible()){
//System.out.println(frame.getTitle());
//}
if (frame.getTitle().equals(strFrom))
{
//es ist schon ein ChatWindow für diesen Kontakt da
//wie sende ich die machricht am geschicktesten da hin???
//(ChatWindow) frame.setMessage(msg);
bWindow = true;
System.out.println("FENSTER offen!");
// xmppConnection.getChatManager().getThreadChat(msg.getThread()).
break;
}
}
if (bWindow == false)
{
ChatWindow wndChat1 = new ChatWindow(xmppconnection, msg, contacts.getContact(msg.getFrom(), false));
wndChat1.setVisible(true);
andFilter.addFilter(new NotFilter(new FromContainsFilter(msg.getFrom())));
xmppconnection.addPacketListener(this, andFilter);
}
}
}
};
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
String strTitle = null;
boolean bChatWindow = false;
//int index = lstContacts.locationToIndex(e.getPoint());
if (lstView.getSelectedValue() instanceof ListItem )
{
strTitle = ((ListItem) lstView.getSelectedValue()).getText();
//System.out.println("DRIN!");
for(Frame frame : JFrame.getFrames()){
if (frame.getTitle().equals(strTitle))
{
bChatWindow = true;
frame.setVisible(bChatWindow);
break;
}
}
if (bChatWindow == false)
{
if(contacts.getContact(strTitle,false) != null){
contact = contacts.getContact(strTitle,false);}
else{
contact = contacts.getContact(strTitle,true);}
ChatWindow wndChat = new ChatWindow(xmppconnection, contact);
Observer observer = wndChat;
contact.addObserver(observer);
andFilter.addFilter(new NotFilter(new FromContainsFilter(contact.getFullJID())));
xmppconnection.addPacketListener(pktListener, andFilter);
wndChat.setVisible(true);
}
}
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
{
popup.show(e.getComponent(), e.getX(), e.getY());
strDelRosterItem = ((ListItem) defaultlistmodel.getElementAt(lstView.locationToIndex(e.getPoint()))).getText();
}
}
};
MouseListener mousePopupListener = new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
// Hier den ausgewählten Eintrag löschen!!!!!!
System.out.println("Eintrag löschen geklickt");
System.out.println("Eintrag: " + strDelRosterItem.trim() );
strDelRosterItem = StringUtils.parseBareAddress(strDelRosterItem.trim());
//System.out.println("bareJID: " + StringUtils.parseBareAddress(strDelRosterItem.trim()));
try {
System.out.println(roster.toString());
System.out.println("*** vor roster.removeEntry(...) ***");
roster.removeEntry(roster.getEntry(strDelRosterItem));
//FullJID und BareJID!!!
try {
System.out.println("*** im try zweig ***");
contacts.deleteContact(contacts.getContact(strDelRosterItem, true));
}
catch (Exception ex)
{
System.out.println("*** im catch zweig ***");
contacts.deleteContact(contacts.getContact(strDelRosterItem, false));
}
//contacts.deleteContact(contacts.getContact(strDelRosterItem, true));
setContactList();
} catch (XMPPException ex) {
System.out.println(ex.getXMPPError());
}
}
};
popupItem.addMouseListener(mousePopupListener);
lstView.addMouseListener(mouseListener);
lstView.setCellRenderer(new ContactListCellRenderer());
lstView.setModel(defaultlistmodel);
xmppconnection.addPacketListener(pktListener, andFilter);
makeRoster();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jlblUsername = new javax.swing.JLabel();
jComboBoxStatus = new javax.swing.JComboBox();
label1 = new java.awt.Label();
jScrollPane1 = new javax.swing.JScrollPane();
lstView = new javax.swing.JList();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Herzlich Willkommen", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
jlblUsername.setFont(new java.awt.Font("Arial", 1, 12));
jlblUsername.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/__AutumnLeaves.jpg"))); // NOI18N
jlblUsername.setText("Benutzername");
jComboBoxStatus.setForeground(new java.awt.Color(0, 204, 51));
jComboBoxStatus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBoxStatusActionPerformed(evt);
}
});
label1.setFont(new java.awt.Font("Dialog", 1, 12));
label1.setText("Status:");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBoxStatus, 0, 237, Short.MAX_VALUE))
.addComponent(jlblUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jlblUsername)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jComboBoxStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jScrollPane1.setViewportView(lstView);
jMenu1.setText("Hauptmenü");
jMenuItem1.setText("Optionen");
jMenu1.add(jMenuItem1);
jMenuItem5.setText("Abmelden");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem5ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem5);
jMenuItem4.setText("Beenden");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem4);
jMenuBar1.add(jMenu1);
jMenu2.setText("Kontakte");
jMenuItem3.setText("Kontakte hinzufügen");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchUser(evt);
}
});
jMenu2.add(jMenuItem3);
jMenuBar1.add(jMenu2);
jMenu3.setText("Hilfe");
jMenuItem2.setText("Messengerhilfe");
jMenu3.add(jMenuItem2);
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
// hier muss die Usersuche implementiert werden
private void searchUser(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchUser
searchwindow= new SearchWindow(xmppconnection);
searchwindow.setVisible(true);
}//GEN-LAST:event_searchUser
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
this.dispose();
loginwindow.dispose();
}//GEN-LAST:event_jMenuItem4ActionPerformed
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
this.dispose();
loginwindow.disconnect();
loginwindow.setVisible(true);
}//GEN-LAST:event_formWindowClosing
private void jComboBoxStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxStatusActionPerformed
jComboBoxStatus.setForeground(((ComboItem)jComboBoxStatus.getSelectedItem()).getColor());
if (((ComboItem)jComboBoxStatus.getSelectedItem()).getText().equals("online"))
{
presence.setMode(Presence.Mode.available);
presence.setStatus("online");
}
else if (((ComboItem)jComboBoxStatus.getSelectedItem()).getText().equals("nicht verfügbar"))
{
presence.setMode(Presence.Mode.xa);
presence.setStatus("nicht verfügbar");
}
else if (((ComboItem)jComboBoxStatus.getSelectedItem()).getText().equals("abwesend"))
{
presence.setMode(Presence.Mode.away);
presence.setStatus("abwesend");
}
else if (((ComboItem)jComboBoxStatus.getSelectedItem()).getText().equals("beschäftigt"))
{
presence.setMode(Presence.Mode.dnd);
presence.setStatus("beschäftigt");
}
else
{
presence.setMode(Presence.Mode.xa);
presence.setStatus("nicht verfügbar");
}
if (xmppconnection.isConnected())
{
xmppconnection.sendPacket(presence);
}
}//GEN-LAST:event_jComboBoxStatusActionPerformed
private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
this.dispose();
loginwindow.disconnect();
loginwindow.setVisible(true);
}//GEN-LAST:event_jMenuItem5ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JComboBox jComboBoxStatus;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel jlblUsername;
private java.awt.Label label1;
private javax.swing.JList lstView;
// End of variables declaration//GEN-END:variables
public void updateContacts() {
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
System.out.println("*** in FOR Schleife ***");
if ( contacts.getContact(entry.getUser(), "") == null && contacts.getContact(entry.getUser(), roster.getPresence(entry.getUser()).getFrom()) == null )
{
if (roster.getPresence(entry.getUser()).isAvailable())
{
contacts.addContact(new Contact(entry.getUser(),roster.getPresence(entry.getUser()).getFrom(),entry.getName(),roster.getPresence(entry.getUser()).toString()));
}
else
{
contacts.addContact(new Contact(entry.getUser(),"",entry.getName(),roster.getPresence(entry.getUser()).toString()));
}
}
}
}
public Roster makeRoster() {
roster = xmppconnection.getRoster();
roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
roster.addRosterListener(new RosterListener() {
public void entriesDeleted(Collection<String> addresses) {
setContactList();
}
public void entriesUpdated(Collection<String> addresses) {
//muss noch in Contacts integriert werden
}
public void presenceChanged(Presence presence) {
System.out.println("im Listener:");
String strFullJID = presence.getFrom();
//System.out.println("strFullJID: " + strFullJID);
String strBareJID = strFullJID.substring( 0, strFullJID.lastIndexOf("/"));
//System.out.println("strBareJID: " + strBareJID);
Contact tmpContact = null;
if(contacts.getContact(strBareJID, "") != null){
//wenn offline-Kontakt schon von Roster erzeugt wurde --> FullJID eintragen und Status ändern
System.out.println("erster IF");
tmpContact = contacts.getContact(strBareJID, "");
tmpContact.setFullJID(strFullJID);
tmpContact.setPresence(roster.getPresence(strFullJID).toString());
//contacts.print();
}
else{
System.out.println("erster ELSE");
if (contacts.getContact(strBareJID, strFullJID) != null){
System.out.println("zweiter IF");
tmpContact = contacts.getContact(strBareJID, strFullJID);
//Wenn offline gegangen --> status auf offline und fullJID entfernen
if(!roster.getPresence(strFullJID).isAvailable()){
System.out.println("dritter IF");
tmpContact.setFullJID("");
tmpContact.setPresence(roster.getPresence(strFullJID).toString());
}
//andernfalls --> nur den Status ändern
else{
System.out.println("dritter ELSE");
tmpContact.setPresence(roster.getPresence(strFullJID).toString());
}
}
else{
System.out.println("zweiter ELSE");
//wenn keine einfache BareJID (offline-Kontakt) und keine BareJID+FullJID (online-Kontakt) gefunden wurde, komplett neuen Contact anlegen
contacts.addContact(new Contact(strBareJID,strFullJID,roster.getEntry(strFullJID).getName(),roster.getPresence(strFullJID).toString()));
}
}
contacts.print();
setContactList();
}
public void entriesAdded(Collection<String> arg0) {
for (Iterator it = arg0.iterator(); it.hasNext();) {
String address = (String) it.next();
System.out.println("Adresse zum Hinzufügen: " + address);
RosterEntry entry = xmppconnection.getRoster().getEntry(address);
//When the entry is only from the other user, then send a subscription request
if (entry != null && entry.getType() == RosterPacket.ItemType.from) {
System.out.println("Eintrag durchführen für from-ItemType");
try {
System.out.println("Erstelle Eintrag to: " + entry.getUser());
xmppconnection.getRoster().createEntry(entry.getUser(), entry.getName(), new String[0]);
} catch (XMPPException e) {
e.printStackTrace();
}
}
}
updateContacts();
setContactList();
}
});
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
//System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: " + entry.toString());
if (entry != null && entry.getType() == RosterPacket.ItemType.to) {
//System.out.println("aaaaaaaaaaaaaaaaaaa- Eintrag durchführen für to-ItemType");
try {
//System.out.println("Erstelle Eintrag from: " + entry.getUser());
xmppconnection.getRoster().createEntry(entry.getUser(), entry.getName(), new String[0]);
} catch (XMPPException e) {
e.printStackTrace();
}
}
if (entry != null && entry.getType() == RosterPacket.ItemType.from) {
//System.out.println("bbbbbbbbbbbbbbbbbbbbbbb- Eintrag durchführen für to-ItemType");
try {
//System.out.println("Erstelle Eintrag from: " + entry.getUser());
xmppconnection.getRoster().createEntry(entry.getUser(), entry.getName(), new String[0]);
} catch (XMPPException e) {
e.printStackTrace();
}
}
String strBareJID = entry.getUser();
if(contacts.getContact(strBareJID, "") == null){
if (roster.getPresence(entry.getUser()).isAvailable()) {
//System.out.println(entry.getUser() + " mode: " + roster.getPresence(entry.getUser()).getMode().toString());
contacts.addContact(new Contact(entry.getUser(),roster.getPresence(entry.getUser()).getFrom(),entry.getName(),roster.getPresence(entry.getUser()).toString()));
}
else {
contacts.addContact(new Contact(entry.getUser(),"",entry.getName(),roster.getPresence(entry.getUser()).toString()));
//contacts.addContact(new Contact(entry.getUser(),roster.getPresence(entry.getUser()).getFrom(),entry.getName(),roster.getPresence(entry.getUser()).toString()));
}
}
}
System.out.println("in der Roster:");
contacts.print();
setContactList();
return roster;
}
public void setContactList ()
{
ListItem liItem = null;
Contact cTemp = null;
Iterator<Contact> iter = contacts.getList().iterator();
defaultlistmodel.clear();
while (iter.hasNext())
{
cTemp = iter.next();
//liItem = new ListItem("available", "huhu");
if (cTemp.getFullJID().equals("")) {
liItem = new ListItem(cTemp.getPresence(), cTemp.getBareJID());
}
else {
liItem = new ListItem(cTemp.getPresence(), cTemp.getFullJID());
}
defaultlistmodel.addElement(liItem);
}
//lstView.setModel(lstModel);
lstView.updateUI();
}
}