1 package org.columba.chat.ui.conversation; 19 20 import java.awt.event.ActionEvent ; 21 import java.awt.event.ActionListener ; 22 import java.util.Hashtable ; 23 import java.util.Map ; 24 import java.util.logging.Logger ; 25 26 import javax.swing.JTabbedPane ; 27 28 import org.columba.chat.Connection; 29 import org.columba.chat.MainInterface; 30 import org.columba.chat.base.Parser; 31 import org.columba.chat.conn.api.ConnectionChangedEvent; 32 import org.columba.chat.conn.api.IConnectionChangedListener; 33 import org.columba.chat.conn.api.IConnection.STATUS; 34 import org.columba.chat.model.BuddyList; 35 import org.columba.chat.model.api.IBuddyStatus; 36 import org.columba.chat.ui.conversation.api.IChatMediator; 37 import org.columba.chat.ui.conversation.api.IConversationController; 38 import org.jivesoftware.smack.Chat; 39 import org.jivesoftware.smack.PacketListener; 40 import org.jivesoftware.smack.filter.PacketTypeFilter; 41 import org.jivesoftware.smack.packet.Message; 42 import org.jivesoftware.smack.packet.Packet; 43 44 49 50 public class ConversationController extends JTabbedPane implements 51 IConversationController, ActionListener , IConnectionChangedListener { 52 53 private static final Logger LOG = Logger 54 .getLogger("org.columba.chat.ui.conversation"); 55 56 private Map <String , IChatMediator> chatMap = new Hashtable <String , IChatMediator>(); 57 58 private MessageListener messageListener = new MessageListener(); 59 60 63 public ConversationController() { 64 super(); 65 66 chatMap = new Hashtable <String , IChatMediator>(); 67 68 MainInterface.connection.addConnectionChangedListener(this); 69 70 } 71 72 77 public IChatMediator addChat(String jabberId, Chat chat) { 78 if ( jabberId == null ) throw new IllegalArgumentException ("jabberId == null"); 79 80 81 ChatMediator m = null; 82 if (chatMap.containsKey(jabberId)) { 83 m = (ChatMediator) chatMap.get(jabberId); 84 } else { 85 86 m = new ChatMediator(chat); 87 m.registerCloseActionListener(this); 88 89 chatMap.put(jabberId, m); 90 } 91 92 addTab("Chatting with " + m.getChat().getParticipant(), m); 93 94 return m; 95 } 96 97 102 public IChatMediator getSelected() { 103 int index = getSelectedIndex(); 104 105 return get(index); 106 } 107 108 113 public IChatMediator get(int index) { 114 return null; 115 116 } 118 119 124 public void closeSelected() { 125 int index = getSelectedIndex(); 126 remove(index); 127 } 128 129 private IChatMediator getMediator(String jabberId) { 130 if (jabberId == null) 131 throw new IllegalArgumentException (jabberId); 132 133 return chatMap.get(jabberId); 134 } 135 136 public void actionPerformed(ActionEvent event) { 137 if (event.getActionCommand().equals("CLOSE")) { 138 closeSelected(); 139 } 140 141 } 142 143 class MessageListener implements PacketListener { 144 145 public MessageListener() { 146 } 147 148 151 public void processPacket(Packet packet) { 152 final Message message = (Message) packet; 153 154 LOG.finest("message" + message.toString()); 155 157 if ((message.getType() != Message.Type.NORMAL) 158 && (message.getType() != Message.Type.CHAT)) 159 return; 160 161 String from = message.getFrom(); 162 163 LOG.info("From=" + from); 164 165 final String normalizedFrom = Parser.normalizeFrom(from); 168 final IBuddyStatus buddyStatus = BuddyList.getInstance().getBuddy( 169 normalizedFrom); 170 171 if (buddyStatus != null) { 172 173 javax.swing.SwingUtilities.invokeLater(new Runnable () { 175 public void run() { 176 177 IChatMediator mediator = getMediator(normalizedFrom); 178 if (mediator != null) { 179 mediator.displayReceivedMessage(message, 180 buddyStatus); 181 182 mediator.sendTextFieldRequestFocus(); 183 } 184 185 } 186 }); 187 188 } 189 190 } 191 } 192 193 public boolean exists(String jabberId) { 194 if ( jabberId == null ) throw new IllegalArgumentException ("jabberId == null"); 195 196 if (chatMap.containsKey(jabberId)) 197 return true; 198 199 return false; 200 } 201 202 205 public void connectionChanged(ConnectionChangedEvent object) { 206 STATUS status = object.getStatus(); 207 208 if (status == STATUS.ONLINE) { 209 setEnabled(true); 210 211 messageListener = new MessageListener(); 212 Connection.XMPPConnection.addPacketListener(messageListener, 213 new PacketTypeFilter(Message.class)); 214 215 } else if (status == STATUS.OFFLINE) { 216 setEnabled(false); 217 Connection.XMPPConnection.removePacketListener(messageListener); 218 } 219 } 220 221 } | Popular Tags |