View Javadoc

1   /*
2    * jcurl java curling software framework https://JCurl.mro.name Copyright (C)
3    * 2005-2009 M. Rohrmoser
4    * 
5    * This program is free software; you can redistribute it and/or modify it under
6    * the terms of the GNU General Public License as published by the Free Software
7    * Foundation; either version 2 of the License, or (at your option) any later
8    * version.
9    * 
10   * This program is distributed in the hope that it will be useful, but WITHOUT
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13   * details.
14   * 
15   * You should have received a copy of the GNU General Public License along with
16   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17   * Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  
20  package org.jcurl.demo.smack;
21  
22  import java.awt.BorderLayout;
23  import java.awt.event.MouseAdapter;
24  import java.awt.event.MouseEvent;
25  import java.awt.event.MouseListener;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.Map;
29  import java.util.Vector;
30  import java.util.WeakHashMap;
31  import java.util.regex.Pattern;
32  
33  import javax.swing.JComponent;
34  import javax.swing.JList;
35  
36  import org.apache.commons.logging.Log;
37  import org.jcurl.core.log.JCLoggerFactory;
38  import org.jivesoftware.smack.Chat;
39  import org.jivesoftware.smack.ChatManager;
40  import org.jivesoftware.smack.ChatManagerListener;
41  import org.jivesoftware.smack.Roster;
42  import org.jivesoftware.smack.RosterEntry;
43  import org.jivesoftware.smack.RosterListener;
44  import org.jivesoftware.smack.XMPPConnection;
45  import org.jivesoftware.smack.packet.Presence;
46  
47  /**
48   * A simple {@link JList}-based roster list. New {@link Chat}s are created on
49   * double-clicking the target address.
50   * <p>
51   * Register a {@link ChatManagerListener} with
52   * {@link XMPPConnection#getChatManager()} to get notice of them.
53   * </p>
54   * 
55   * @author <a href="mailto:JCurl@mro.name">M. Rohrmoser </a>
56   * @version $Id: RosterSimpleSwingBean.java 1031 2009-07-23 15:06:05Z mro $
57   */
58  public class RosterSimpleSwingBean extends JComponent implements RosterListener {
59  
60  	private static final Log log = JCLoggerFactory
61  			.getLogger(RosterSimpleSwingBean.class);
62  	private static final long serialVersionUID = 8787616993298867816L;
63  	private ChatManager chatManager;
64  	private final Vector<String> data = new Vector<String>();
65  	private final JList l;
66  	private Roster roster;
67  	private final Map<String, XmppAddress> s2a = new WeakHashMap<String, XmppAddress>();
68  	private Pattern star;
69  
70  	public RosterSimpleSwingBean() {
71  		setLayout(new BorderLayout());
72  		l = new JList(data);
73  		// see
74  		// http://java.sun.com/products/jfc/tsc/tech_topics/jlist_1/jlist.html:
75  		final MouseListener mouseListener = new MouseAdapter() {
76  			@Override
77  			public void mouseClicked(MouseEvent e) {
78  				if (e.getClickCount() == 2) {
79  					final int index = l.locationToIndex(e.getPoint());
80  					final Object o = l.getModel().getElementAt(index);
81  					if (o != null && getChatManager() != null)
82  						getChatManager()
83  								.createChat(s2a.get(o).toString(), null);
84  				}
85  			}
86  		};
87  		l.addMouseListener(mouseListener);
88  		add(l, BorderLayout.CENTER);
89  	}
90  
91  	public void entriesAdded(final Collection<String> arg0) {
92  		log.warn("ignored");
93  	}
94  
95  	public void entriesDeleted(final Collection<String> arg0) {
96  		log.warn("ignored");
97  	}
98  
99  	public void entriesUpdated(final Collection<String> arg0) {
100 		log.warn("ignored");
101 	}
102 
103 	public ChatManager getChatManager() {
104 		return chatManager;
105 	}
106 
107 	public Roster getRoster() {
108 		return roster;
109 	}
110 
111 	public Pattern getStar() {
112 		return star;
113 	}
114 
115 	public void presenceChanged(final Presence presence) {
116 		// strip the resource
117 		final XmppAddress a0 = XmppAddress.parse(presence.getFrom());
118 		if (a0 == null)
119 			return;
120 		String a = a0.toString(null);
121 
122 		// add a mark if the star pattern matches:
123 		if (star != null && star.matcher(presence.getFrom()).matches())
124 			a = "* " + a;
125 
126 		// update data
127 		if (presence.isAvailable() && !presence.isAway()) {
128 			data.add(a);
129 			s2a.put(a, a0);
130 		} else {
131 			data.remove(a);
132 			s2a.remove(a);
133 		}
134 		// update visible list
135 		Collections.sort(data);
136 		l.setListData(data);
137 	}
138 
139 	public void setChatManager(final ChatManager chatManager) {
140 		this.chatManager = chatManager;
141 	}
142 
143 	public void setConn(final XMPPConnection conn) {
144 		setChatManager(conn.getChatManager());
145 		setRoster(conn.getRoster());
146 	}
147 
148 	@Override
149 	public void setEnabled(final boolean enabled) {
150 		l.setEnabled(enabled);
151 		super.setEnabled(enabled);
152 	}
153 
154 	public void setRoster(final Roster roster) {
155 		if (this.roster != null)
156 			this.roster.removeRosterListener(this);
157 		this.roster = roster;
158 		if (this.roster != null)
159 			this.roster.addRosterListener(this);
160 		updateList();
161 	}
162 
163 	public void setStar(final Pattern star) {
164 		this.star = star;
165 		updateList();
166 	}
167 
168 	private void updateList() {
169 		data.clear();
170 		if (roster != null)
171 			for (final RosterEntry elem : roster.getEntries())
172 				presenceChanged(roster.getPresence(elem.getUser()));
173 	}
174 }