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.event.KeyAdapter;
23  import java.awt.event.KeyEvent;
24  import java.awt.event.KeyListener;
25  
26  import javax.swing.BoxLayout;
27  import javax.swing.JComponent;
28  import javax.swing.JTextField;
29  import javax.swing.SwingUtilities;
30  
31  import org.apache.commons.logging.Log;
32  import org.jcurl.core.log.JCLoggerFactory;
33  import org.jivesoftware.smack.Chat;
34  import org.jivesoftware.smack.ChatManagerListener;
35  import org.jivesoftware.smack.XMPPException;
36  import org.jivesoftware.smack.packet.Message;
37  import org.jivesoftware.smack.packet.Message.Type;
38  
39  /**
40   * A simple chat-message sender GUI component. Implements
41   * {@link ChatManagerListener} to keep track of the current chat to send to.
42   * <p>
43   * Locally created {@link Chat}s typically come from a
44   * {@link RosterSimpleSwingBean}.
45   * </p>
46   * 
47   * @author <a href="mailto:JCurl@mro.name">M. Rohrmoser </a>
48   * @version $Id: ChatSimpleSwingBean.java 1031 2009-07-23 15:06:05Z mro $
49   */
50  public class ChatSimpleSwingBean extends JComponent implements
51  		ChatManagerListener {
52  
53  	private static final Log log = JCLoggerFactory
54  			.getLogger(ChatSimpleSwingBean.class);
55  
56  	private static final long serialVersionUID = 3756526957709716506L;
57  	private Chat chat = null;
58  	private final JTextField text;
59  
60  	public ChatSimpleSwingBean() {
61  		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
62  		text = new JTextField();
63  		// see
64  		// http://java.sun.com/products/jfc/tsc/tech_topics/jlist_1/jlist.html:
65  		final KeyListener keyListener = new KeyAdapter() {
66  			@Override
67  			public void keyTyped(KeyEvent evt) {
68  				if ('\n' == evt.getKeyChar())
69  					try {
70  						send(text.getText());
71  						text.setText("");
72  					} catch (XMPPException e) {
73  						log.error("Sending \"" + text.getText() + "\"", e);
74  					}
75  			}
76  		};
77  		text.addKeyListener(keyListener);
78  		add(text);
79  		setEnabled(false);
80  	}
81  
82  	public void chatCreated(final Chat chat2, final boolean flag) {
83  		if (chat != null
84  				&& !chat.getParticipant().equals(chat2.getParticipant())) {
85  			log.error("Ingore the intruder " + chat2.getParticipant());
86  			return;
87  		}
88  		chat = chat2;
89  		SwingUtilities.invokeLater(new Runnable() {
90  			public void run() {
91  				setEnabled(true);
92  			}
93  		});
94  	}
95  
96  	private void send(final CharSequence txt) throws XMPPException {
97  		if (chat == null || txt == null || txt.length() == 0)
98  			return;
99  		final Message msg = new Message(chat.getParticipant(), Type.chat);
100 		msg.setBody(txt.toString());
101 		chat.sendMessage(msg);
102 	}
103 
104 	@Override
105 	public void setEnabled(final boolean enabled) {
106 		if (isEnabled() == enabled)
107 			return;
108 		log.info(enabled);
109 		text.setEnabled(enabled);
110 		super.setEnabled(enabled);
111 	}
112 }