001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.chat.ui.conversation;
019:
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.util.Hashtable;
023: import java.util.Map;
024: import java.util.logging.Logger;
025:
026: import javax.swing.JTabbedPane;
027:
028: import org.columba.chat.Connection;
029: import org.columba.chat.MainInterface;
030: import org.columba.chat.base.Parser;
031: import org.columba.chat.conn.api.ConnectionChangedEvent;
032: import org.columba.chat.conn.api.IConnectionChangedListener;
033: import org.columba.chat.conn.api.IConnection.STATUS;
034: import org.columba.chat.model.BuddyList;
035: import org.columba.chat.model.api.IBuddyStatus;
036: import org.columba.chat.ui.conversation.api.IChatMediator;
037: import org.columba.chat.ui.conversation.api.IConversationController;
038: import org.jivesoftware.smack.Chat;
039: import org.jivesoftware.smack.PacketListener;
040: import org.jivesoftware.smack.filter.PacketTypeFilter;
041: import org.jivesoftware.smack.packet.Message;
042: import org.jivesoftware.smack.packet.Packet;
043:
044: /**
045: * Handles all chat panels.
046: *
047: * @author fdietz
048: */
049:
050: public class ConversationController extends JTabbedPane implements
051: IConversationController, ActionListener,
052: IConnectionChangedListener {
053:
054: private static final Logger LOG = Logger
055: .getLogger("org.columba.chat.ui.conversation");
056:
057: private Map<String, IChatMediator> chatMap = new Hashtable<String, IChatMediator>();
058:
059: private MessageListener messageListener = new MessageListener();
060:
061: /**
062: *
063: */
064: public ConversationController() {
065: super ();
066:
067: chatMap = new Hashtable<String, IChatMediator>();
068:
069: MainInterface.connection.addConnectionChangedListener(this );
070:
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see org.columba.chat.ui.conversation.IConversationController#addChat(java.lang.String)
077: */
078: public IChatMediator addChat(String jabberId, Chat chat) {
079: if (jabberId == null)
080: throw new IllegalArgumentException("jabberId == null");
081:
082: ChatMediator m = null;
083: if (chatMap.containsKey(jabberId)) {
084: m = (ChatMediator) chatMap.get(jabberId);
085: } else {
086:
087: m = new ChatMediator(chat);
088: m.registerCloseActionListener(this );
089:
090: chatMap.put(jabberId, m);
091: }
092:
093: addTab("Chatting with " + m.getChat().getParticipant(), m);
094:
095: return m;
096: }
097:
098: /*
099: * (non-Javadoc)
100: *
101: * @see org.columba.chat.ui.conversation.IConversationController#getSelected()
102: */
103: public IChatMediator getSelected() {
104: int index = getSelectedIndex();
105:
106: return get(index);
107: }
108:
109: /*
110: * (non-Javadoc)
111: *
112: * @see org.columba.chat.ui.conversation.IConversationController#get(int)
113: */
114: public IChatMediator get(int index) {
115: return null;
116:
117: // return (ChatMediator) chatList.get(index);
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see org.columba.chat.ui.conversation.IConversationController#closeSelected()
124: */
125: public void closeSelected() {
126: int index = getSelectedIndex();
127: remove(index);
128: }
129:
130: private IChatMediator getMediator(String jabberId) {
131: if (jabberId == null)
132: throw new IllegalArgumentException(jabberId);
133:
134: return chatMap.get(jabberId);
135: }
136:
137: public void actionPerformed(ActionEvent event) {
138: if (event.getActionCommand().equals("CLOSE")) {
139: closeSelected();
140: }
141:
142: }
143:
144: class MessageListener implements PacketListener {
145:
146: public MessageListener() {
147: }
148:
149: /**
150: * @see org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware.smack.packet.Packet)
151: */
152: public void processPacket(Packet packet) {
153: final Message message = (Message) packet;
154:
155: LOG.finest("message" + message.toString());
156: // log.info(message.toString());
157:
158: if ((message.getType() != Message.Type.NORMAL)
159: && (message.getType() != Message.Type.CHAT))
160: return;
161:
162: String from = message.getFrom();
163:
164: LOG.info("From=" + from);
165:
166: // example: fdietz@jabber.org/Jabber-client
167: // -> remove "/Jabber-client"
168: final String normalizedFrom = Parser.normalizeFrom(from);
169: final IBuddyStatus buddyStatus = BuddyList.getInstance()
170: .getBuddy(normalizedFrom);
171:
172: if (buddyStatus != null) {
173:
174: // awt-event-thread
175: javax.swing.SwingUtilities.invokeLater(new Runnable() {
176: public void run() {
177:
178: IChatMediator mediator = getMediator(normalizedFrom);
179: if (mediator != null) {
180: mediator.displayReceivedMessage(message,
181: buddyStatus);
182:
183: mediator.sendTextFieldRequestFocus();
184: }
185:
186: }
187: });
188:
189: }
190:
191: }
192: }
193:
194: public boolean exists(String jabberId) {
195: if (jabberId == null)
196: throw new IllegalArgumentException("jabberId == null");
197:
198: if (chatMap.containsKey(jabberId))
199: return true;
200:
201: return false;
202: }
203:
204: /**
205: * @see org.columba.chat.conn.api.IConnectionChangedListener#connectionChanged(org.columba.chat.conn.api.ConnectionChangedEvent)
206: */
207: public void connectionChanged(ConnectionChangedEvent object) {
208: STATUS status = object.getStatus();
209:
210: if (status == STATUS.ONLINE) {
211: setEnabled(true);
212:
213: messageListener = new MessageListener();
214: Connection.XMPPConnection.addPacketListener(
215: messageListener,
216: new PacketTypeFilter(Message.class));
217:
218: } else if (status == STATUS.OFFLINE) {
219: setEnabled(false);
220: Connection.XMPPConnection
221: .removePacketListener(messageListener);
222: }
223: }
224:
225: }
|