ContactItemHandler Question

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

Hi Ryan,

Are you sure the presence of the contact item is changing? I’'m using this code directly as well and it is working fine. Have you tried just adding a default listener?

ex.

contactItem.setHandler(new ContactItemHandler() {

public void handlePresence(Presence presence) {

// Do nothing unless offline.

if (presence == null) {

contactItem.setIcon(null);

}

}

public boolean handleDoubleClick() {

// Retrieve dataform and display.

try {

showWorkgroup(contactItem);

}

catch (Exception e) {

e.printStackTrace();

}

return true;

}

});

Hi Derek,

You were right, it turned out to be a client issue - I had the exact same though occur to me while I was driving home thought a snow storm last night.

Thanks for the reply,

Ryan

Hi Ryan,

I wonder why you did post your question here, very uncommon that dev’'s use a public forum Or are you more someone like an external dev?

LG

Hi LG,

I know, it’'s crazy that a developer has to ask for help.

I do not work for Jive, rather I was made a developer in recognition of my involvement in the Jivesoftware.org community both in terms of my participation on these forums and code I’‘ve contributed. For a long time I’‘ve been interested in the software that helps build communities, forums, IM, wiki’‘s, etc., so I’'m real excited to be part of the Wildfire project.

Unfortunately, since I haven’‘t yet figured out a way to turn this sort of work into a paying, full-time job I’‘ve been working on some other projects (some related) so my participation here has dropped off somewhat while my list of things I want to do continues to grow. Hopefully my schedule will soon clear up a bit and I’'ll be able to jump in some more.

Cheers,

Ryan