KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > chat > ui > conversation > ConversationController


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.chat.ui.conversation;
19
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26 import javax.swing.JTabbedPane JavaDoc;
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 /**
45  * Handles all chat panels.
46  *
47  * @author fdietz
48  */

49
50 public class ConversationController extends JTabbedPane JavaDoc implements
51         IConversationController, ActionListener JavaDoc, IConnectionChangedListener {
52
53     private static final Logger JavaDoc LOG = Logger
54             .getLogger("org.columba.chat.ui.conversation");
55
56     private Map JavaDoc<String JavaDoc, IChatMediator> chatMap = new Hashtable JavaDoc<String JavaDoc, IChatMediator>();
57
58     private MessageListener messageListener = new MessageListener();
59
60     /**
61      *
62      */

63     public ConversationController() {
64         super();
65
66         chatMap = new Hashtable JavaDoc<String JavaDoc, IChatMediator>();
67
68         MainInterface.connection.addConnectionChangedListener(this);
69
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.columba.chat.ui.conversation.IConversationController#addChat(java.lang.String)
76      */

77     public IChatMediator addChat(String JavaDoc jabberId, Chat chat) {
78         if ( jabberId == null ) throw new IllegalArgumentException JavaDoc("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     /*
98      * (non-Javadoc)
99      *
100      * @see org.columba.chat.ui.conversation.IConversationController#getSelected()
101      */

102     public IChatMediator getSelected() {
103         int index = getSelectedIndex();
104
105         return get(index);
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see org.columba.chat.ui.conversation.IConversationController#get(int)
112      */

113     public IChatMediator get(int index) {
114         return null;
115
116         // return (ChatMediator) chatList.get(index);
117
}
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see org.columba.chat.ui.conversation.IConversationController#closeSelected()
123      */

124     public void closeSelected() {
125         int index = getSelectedIndex();
126         remove(index);
127     }
128
129     private IChatMediator getMediator(String JavaDoc jabberId) {
130         if (jabberId == null)
131             throw new IllegalArgumentException JavaDoc(jabberId);
132
133         return chatMap.get(jabberId);
134     }
135
136     public void actionPerformed(ActionEvent JavaDoc 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         /**
149          * @see org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware.smack.packet.Packet)
150          */

151         public void processPacket(Packet packet) {
152             final Message message = (Message) packet;
153
154             LOG.finest("message" + message.toString());
155             // log.info(message.toString());
156

157             if ((message.getType() != Message.Type.NORMAL)
158                     && (message.getType() != Message.Type.CHAT))
159                 return;
160
161             String JavaDoc from = message.getFrom();
162
163             LOG.info("From=" + from);
164
165             // example: fdietz@jabber.org/Jabber-client
166
// -> remove "/Jabber-client"
167
final String JavaDoc normalizedFrom = Parser.normalizeFrom(from);
168             final IBuddyStatus buddyStatus = BuddyList.getInstance().getBuddy(
169                     normalizedFrom);
170
171             if (buddyStatus != null) {
172
173                 // awt-event-thread
174
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc jabberId) {
194         if ( jabberId == null ) throw new IllegalArgumentException JavaDoc("jabberId == null");
195         
196         if (chatMap.containsKey(jabberId))
197             return true;
198
199         return false;
200     }
201
202     /**
203      * @see org.columba.chat.conn.api.IConnectionChangedListener#connectionChanged(org.columba.chat.conn.api.ConnectionChangedEvent)
204      */

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