Ryan Graham

ContactItemHandler Question

Discussion created by Ryan Graham Champion on Mar 9, 2006
Latest reply on Mar 14, 2006 by Ryan Graham

Hi All,

 

I''ve experimenting with the ContactItemHandler and I''ve run across a problem that has left me a bit stumped. As you can see from the code below I''ve added a ContactItemHandler (via the initContactHandlers() method) that shows a dialog whenever one of contacts presence changes, however the dialog never appears. However, if I "drop-down" and start working with the Smack directly (via the initContactHandlers2() method) the dialog appears whenever the user presence changes. I''d prefer to keep working with the Spark API if at all possible so I''m wondering if there is a problem with my Spark code or if there is an issue with Spark?

 

Thanks,

Ryan

 

package com.version2software.spark.plugin;

 

import java.util.Collection;

import java.util.Iterator;

 

import javax.swing.JOptionPane;

import javax.swing.SwingUtilities;

 

import org.jivesoftware.smack.Roster;

import org.jivesoftware.smack.RosterListener;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.packet.Presence;

 

import com.jivesoftware.spark.SparkManager;

import com.jivesoftware.spark.plugin.Plugin;

import com.jivesoftware.spark.ui.ContactGroup;

import com.jivesoftware.spark.ui.ContactItem;

import com.jivesoftware.spark.ui.ContactItemHandler;

 

public class PresencePlugin implements Plugin {

 

   public void initialize() {

      initContactHandlers();

      //initContactHandlers2();

   }

    

   private void initContactHandlers() {     

      Iterator contactGroupsIter = SparkManager.getWorkspace().getContactList().getContactGroups().iterator();

      while (contactGroupsIter.hasNext()) {

         ContactGroup contactGroup = (ContactGroup) contactGroupsIter.next();

          

         Iterator contactItemsIter = contactGroup.getContactItems().iterator();

         while (contactItemsIter.hasNext()) {

            ContactItem contactItem = (ContactItem) contactItemsIter.next();

             

            contactItem.setHandler(new ContactItemPresenceHandler(contactItem));

         }

      }

   }

    

   private class ContactItemPresenceHandler implements ContactItemHandler {

      private ContactItem contact;

       

      public ContactItemPresenceHandler(ContactItem contact) {

         this.contact = contact;

      }

 

      public void handlePresence(final Presence presence) {        

         SwingUtilities.invokeLater(new Runnable() {

            public void run() {

               JOptionPane.showMessageDialog(SparkManager.getMainWindow(), contact.getFullJID() + " : " + presence,

                     "handlePresence", JOptionPane.INFORMATION_MESSAGE);             

            }

        });        

      }

 

      public boolean handleDoubleClick() {

          return false;

      }

   }

    

   private void initContactHandlers2() {

      XMPPConnection con = SparkManager.getConnection();     

      Roster roster = con.getRoster();     

      roster.addRosterListener(new RosterPresenceListner());

   }

    

   private class RosterPresenceListner implements RosterListener {

      public void rosterModified() {

      }

 

      public void presenceChanged(final String user) {       

         SwingUtilities.invokeLater(new Runnable() {

            public void run() {

               JOptionPane.showMessageDialog(SparkManager.getMainWindow(), user,

                     "presenceChanged", JOptionPane.INFORMATION_MESSAGE);             

            }

         });

      }

 

      public void entriesAdded(Collection c) {        

      }

    

      public void entriesUpdated(Collection c) {        

      }

    

      public void entriesDeleted(Collection c) {

      }

   }

    

   public void shutdown() {

   }  

 

   public boolean canShutDown() {

      return true;

   }

 

   public void uninstall() {

   }

}

/code

Outcomes