aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/chat/ChatWindowManager.java
blob: c53d890fadb2f40dc98ea7c3b0b6241e0da41d8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.gui.main.chat;

import java.util.*;

import javax.swing.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.customcontrols.*;
import net.java.sip.communicator.impl.gui.main.chat.conference.*;
import net.java.sip.communicator.impl.gui.main.chatroomslist.*;
import net.java.sip.communicator.impl.gui.main.contactlist.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.service.contactlist.*;
import net.java.sip.communicator.service.protocol.*;

/**
 * Manages chat windows and panels.
 *
 * @author Yana Stamcheva
 */
public class ChatWindowManager
{
    private final java.util.List<ChatPanel> chatPanels =
        new ArrayList<ChatPanel>();

    private final Object syncChat = new Object();

    /**
     * Opens a the specified chatPanel and brings it to the front if so
     * specified.
     *
     * @param chatPanel the chat panel that we will be opening
     * @param setSelected specifies whether we should bring the chat to front
     * after creating it.
     */
    public void openChat(final ChatPanel chatPanel, final boolean setSelected)
    {
        if(!SwingUtilities.isEventDispatchThread())
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    openChat(chatPanel, setSelected);
                }
            });
            return;
        }

        synchronized (syncChat)
        {
            ChatWindow chatWindow = chatPanel.getChatWindow();

            boolean isChatVisible = chatPanel.isShown();

            if(!isChatVisible)
                chatWindow.addChat(chatPanel);

            if(chatWindow.isVisible())
            {
                if(chatWindow.getExtendedState() != JFrame.ICONIFIED)
                {
                    if(ConfigurationManager.isAutoPopupNewMessage()
                        || setSelected)
                        chatWindow.toFront();
                }
                else
                {
                    if(setSelected)
                    {
                        chatWindow.setExtendedState(JFrame.NORMAL);
                        chatWindow.toFront();
                    }

                    if(!chatWindow.getTitle().startsWith("*"))
                        chatWindow.setTitle(
                            "*" + chatWindow.getTitle());
                }

                if(setSelected)
                {
                    chatWindow.setCurrentChatPanel(chatPanel);
                }
                else if(!chatWindow.getCurrentChatPanel().equals(chatPanel)
                    && chatWindow.getChatTabCount() > 0)
                {
                    chatPanel.getChatWindow().highlightTab(chatPanel);

                    chatPanel.setCaretToEnd();
                }
            }
            else
            {
                chatWindow.setVisible(true);

                chatWindow.setCurrentChatPanel(chatPanel);

                chatPanel.setCaretToEnd();
            }
        }
    }

    /**
     * Returns TRUE if there is an opened <tt>ChatPanel</tt> for the given
     * <tt>MetaContact</tt>.
     * @param metaContact the <tt>MetaContact</tt>, for which the chat is about
     * @return TRUE if there is an opened <tt>ChatPanel</tt> for the given
     * <tt>MetaContact</tt>
     */
    public boolean isChatOpenedForContact(MetaContact metaContact)
    {
        synchronized (syncChat)
        {
            ChatSession chatSession = findChatSessionForDescriptor(metaContact);

            return (chatSession != null
                && getChat(chatSession).isShown());
        }
    }

    /**
     * Returns TRUE if there is an opened <tt>ChatPanel</tt> for the given
     * <tt>ChatRoom</tt>.
     * @param chatRoomWrapper the <tt>ChatRoomWrapper</tt>, for which the chat
     * is about
     * @return TRUE if there is an opened <tt>ChatPanel</tt> for the given
     * <tt>ChatRoom</tt>
     */
    public boolean isChatOpenedForChatRoom(ChatRoomWrapper chatRoomWrapper)
    {
        synchronized (syncChat)
        {
            ChatSession chatSession
                = findChatSessionForDescriptor(chatRoomWrapper);

            return (chatSession != null
                && getChat(chatSession).isShown());
        }
    }

    /**
     * Returns TRUE if there is an opened <tt>ChatPanel</tt> for the given
     * <tt>ChatRoom</tt>.
     * @param chatRoom the <tt>ChatRoom</tt>, for which the chat is about
     * @return TRUE if there is an opened <tt>ChatPanel</tt> for the given
     * <tt>ChatRoom</tt>
     */
    public boolean isChatOpenedForChatRoom(ChatRoom chatRoom)
    {
        synchronized (syncChat)
        {
            for (ChatPanel chatPanel : chatPanels)
            {
                ChatSession chatSession = chatPanel.getChatSession();

                Object descriptor = chatSession.getDescriptor();

                if(descriptor instanceof ChatRoomWrapper)
                {
                    ChatRoomWrapper chatRoomWrapper
                        = (ChatRoomWrapper) descriptor;

                    if(chatRoomWrapper.getChatRoomID().equals(chatRoom.getIdentifier())
                        && getChat(chatSession).isShown())
                    {
                        return true;
                    }
                }
            }

            return false;
        }
    }

    /**
     * Closes the given chat panel.
     *
     * @param chatPanel the chat panel to close
     */
    public void closeChat(ChatPanel chatPanel)
    {
        synchronized (syncChat)
        {
            if(containsChat(chatPanel))
            {
                ChatWindow chatWindow = chatPanel.getChatWindow();

                if (!chatPanel.isWriteAreaEmpty())
                {
                    SIPCommMsgTextArea msgText = new SIPCommMsgTextArea(
                        GuiActivator.getResources().getI18NString(
                            "service.gui.NON_EMPTY_CHAT_WINDOW_CLOSE"));

                    int answer = JOptionPane.showConfirmDialog(
                        chatWindow,
                        msgText,
                        GuiActivator.getResources().getI18NString(
                            "service.gui.WARNING"),
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.WARNING_MESSAGE);

                    if (answer == JOptionPane.OK_OPTION)
                    {
                        closeChatPanel(chatPanel);
                    }
                }
                else if (System.currentTimeMillis() - chatWindow
                    .getLastIncomingMsgTimestamp(chatPanel) < 2 * 1000)
                {
                    SIPCommMsgTextArea msgText
                        = new SIPCommMsgTextArea(GuiActivator.getResources()
                            .getI18NString(
                                "service.gui.CLOSE_CHAT_AFTER_NEW_MESSAGE"));

                    int answer = JOptionPane.showConfirmDialog(
                        chatWindow,
                        msgText,
                        GuiActivator.getResources()
                            .getI18NString("service.gui.WARNING"),
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.WARNING_MESSAGE);

                    if (answer == JOptionPane.OK_OPTION)
                        closeChatPanel(chatPanel);
                }
                else {
                    closeChatPanel(chatPanel);
                }
            }
        }
    }

    /**
     * Closes the chat window. Removes all contained chats and invokes
     * setVisible(false) to the window.
     */
    public void closeWindow(ChatWindow chatWindow)
    {
        synchronized (syncChat)
        {
            ChatPanel chatPanel = chatWindow.getCurrentChatPanel();

            if (!chatPanel.isWriteAreaEmpty())
            {
                SIPCommMsgTextArea msgText = new SIPCommMsgTextArea(
                    GuiActivator.getResources().getI18NString(
                        "service.gui.NON_EMPTY_CHAT_WINDOW_CLOSE"));
                int answer = JOptionPane.showConfirmDialog(
                    chatWindow,
                    msgText,
                    GuiActivator.getResources()
                        .getI18NString("service.gui.WARNING"),
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.WARNING_MESSAGE);

                if (answer == JOptionPane.OK_OPTION)
                {
                    this.disposeChatWindow(chatWindow);
                }
            }
            else if (System.currentTimeMillis() - chatWindow
                .getLastIncomingMsgTimestamp(chatPanel) < 2 * 1000)
            {
                SIPCommMsgTextArea msgText = new SIPCommMsgTextArea(
                    GuiActivator.getResources()
                    .getI18NString("service.gui.CLOSE_CHAT_AFTER_NEW_MESSAGE"));

                int answer = JOptionPane.showConfirmDialog(
                    chatWindow,
                    msgText,
                    GuiActivator.getResources()
                        .getI18NString("service.gui.WARNING"),
                    JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);

                if (answer == JOptionPane.OK_OPTION)
                {
                    this.disposeChatWindow(chatWindow);
                }
            }
            else
            {
                this.disposeChatWindow(chatWindow);
            }
        }
    }

    /**
     * Returns the chat panel corresponding to the given meta contact
     *
     * @param metaContact the meta contact.
     * @return the chat panel corresponding to the given meta contact
     */
    public ChatPanel getContactChat(MetaContact metaContact)
    {
        synchronized (syncChat)
        {
            ChatSession chatSession = findChatSessionForDescriptor(metaContact);

            if(chatSession != null)
            {
                return getChat(chatSession);
            }
            else
            {
                return createChat(metaContact);
            }
        }
    }

    /**
     * Returns the chat panel corresponding to the given meta contact
     *
     * @param metaContact the meta contact.
     * @param protocolContact the protocol specific contact
     * @return the chat panel corresponding to the given meta contact
     */
    public ChatPanel getContactChat(MetaContact metaContact,
                                    Contact protocolContact)
    {
        synchronized (syncChat)
        {
            ChatSession chatSession = findChatSessionForDescriptor(metaContact);

            if(chatSession != null)
            {
                return getChat(chatSession);
            }
            else
                return createChat(metaContact, protocolContact);
        }
    }

    /**
     * Returns the chat panel corresponding to the given meta contact
     *
     * @param metaContact the meta contact.
     * @param protocolContact the protocol specific contact
     * @param escapedMessageID the message ID of the message that should be
     * excluded from the history when the last one is loaded in the chat
     * @return the chat panel corresponding to the given meta contact
     */
    public ChatPanel getContactChat(MetaContact metaContact,
                                    Contact protocolContact,
                                    String escapedMessageID)
    {
        synchronized (syncChat)
        {
            ChatSession chatSession = findChatSessionForDescriptor(metaContact);

            if(chatSession != null)
            {
                return getChat(chatSession);
            }
            else
                return createChat(  metaContact,
                                    protocolContact,
                                    escapedMessageID);
        }
    }

    /**
     * Returns the currently selected <tt>ChatPanel</tt>.
     * @return the currently selected <tt>ChatPanel</tt>
     */
    public ChatPanel getSelectedChat()
    {
        ChatPanel selectedChat = null;

        Iterator<ChatPanel> chatPanelsIter = chatPanels.iterator();

        synchronized (syncChat)
        {
            if (ConfigurationManager.isMultiChatWindowEnabled())
            {
                if (chatPanelsIter.hasNext())
                {
                    ChatPanel firstChatPanel = chatPanelsIter.next();

                    selectedChat
                        = firstChatPanel.getChatWindow().getCurrentChatPanel();
                }
            }
            else
            {
                while (chatPanelsIter.hasNext())
                {
                    ChatPanel chatPanel = chatPanelsIter.next();

                    if (chatPanel.getChatWindow().isFocusOwner())
                        selectedChat = chatPanel;
                }
            }

            return selectedChat;
        }
    }

    /**
     * Returns the chat panel corresponding to the given chat room wrapper.
     *
     * @param chatRoomWrapper the chat room wrapper, corresponding to the chat
     * room for which the chat panel is about
     * @return the chat panel corresponding to the given chat room
     */
    public ChatPanel getMultiChat(ChatRoomWrapper chatRoomWrapper)
    {
        synchronized (syncChat)
        {
            ChatSession chatSession
                = findChatSessionForDescriptor(chatRoomWrapper);

            if(chatSession != null)
            {
                return getChat(chatSession);
            }
            else
                return createChat(chatRoomWrapper);
            }
        }

    /**
     * Returns the chat panel corresponding to the given chat room.
     *
     * @param chatRoom the chat room, for which the chat panel is about
     * @return the chat panel corresponding to the given chat room
     */
    public ChatPanel getMultiChat(ChatRoom chatRoom)
    {
        return getMultiChat(chatRoom, null);
    }

    /**
     * Returns the chat panel corresponding to the given chat room.
     *
     * @param chatRoom the chat room, for which the chat panel is about
     * @param escapedMessageID the message ID of the message that should be
     * excluded from the history when the last one is loaded in the chat
     * @return the chat panel corresponding to the given chat room
     */
    public ChatPanel getMultiChat(  ChatRoom chatRoom,
                                    String escapedMessageID)
    {
        synchronized (syncChat)
        {
            ChatRoomList chatRoomList = GuiActivator.getUIService()
                .getConferenceChatManager().getChatRoomList();

            // Search in the chat room's list for a chat room that correspond
            // to the given one.
            ChatRoomWrapper chatRoomWrapper
                = chatRoomList.findChatRoomWrapperFromChatRoom(chatRoom);

            if (chatRoomWrapper == null)
            {
                ChatRoomProviderWrapper parentProvider
                    = chatRoomList.findServerWrapperFromProvider(
                        chatRoom.getParentProvider());

                chatRoomWrapper = new ChatRoomWrapper(parentProvider, chatRoom);

                chatRoomList.addChatRoom(chatRoomWrapper);
            }

            ChatSession chatSession
                = findChatSessionForDescriptor(chatRoomWrapper);

            if (chatSession != null)
            {
                return getChat(chatSession);
            }

            return createChat(chatRoomWrapper, escapedMessageID);
        }
    }

    /**
     * Closes the selected chat tab or the window if there are no tabs.
     *
     * @param chatPanel the chat panel to close.
     */
    private void closeChatPanel(ChatPanel chatPanel)
    {
        ChatWindow chatWindow = chatPanel.getChatWindow();

        chatWindow.removeChat(chatPanel);

        if (chatWindow.getChatCount() == 0)
            disposeChatWindow(chatWindow);

        boolean isChatPanelContained;
        synchronized (chatPanels)
        {
            isChatPanelContained = chatPanels.remove(chatPanel);
        }

        if (isChatPanelContained)
            chatPanel.dispose();
    }

    /**
     * Creates a chat for the given meta contact. If the most connected proto
     * contact of the meta contact is offline choose the proto contact that
     * supports offline messaging.
     *
     * @param metaContact the meta contact for the chat
     *
     * @return the newly created ChatPanel
     */
    private ChatPanel createChat(MetaContact metaContact)
    {
        Contact defaultContact = metaContact.getDefaultContact();

        ProtocolProviderService defaultProvider
            = defaultContact.getProtocolProvider();

        OperationSetBasicInstantMessaging
            defaultIM = (OperationSetBasicInstantMessaging)
                defaultProvider.getOperationSet(
                        OperationSetBasicInstantMessaging.class);

        ProtocolProviderService protoContactProvider;
        OperationSetBasicInstantMessaging protoContactIM;

        if (defaultContact.getPresenceStatus().getStatus() < 1
                && (!defaultIM.isOfflineMessagingSupported()
                        || !defaultProvider.isRegistered()))
        {
            Iterator<Contact> protoContacts = metaContact.getContacts();

            while(protoContacts.hasNext())
            {
                Contact contact = protoContacts.next();

                protoContactProvider = contact.getProtocolProvider();

                protoContactIM = (OperationSetBasicInstantMessaging)
                    protoContactProvider.getOperationSet(
                        OperationSetBasicInstantMessaging.class);

                if(protoContactIM.isOfflineMessagingSupported()
                        && protoContactProvider.isRegistered())
                {
                    defaultContact = contact;
                }
            }
        }

        return createChat(metaContact, defaultContact);
    }


    /**
     * Creates a <tt>ChatPanel</tt> for the given contact and saves it in the
     * list ot created <tt>ChatPanel</tt>s.
     *
     * @param contact The MetaContact for this chat.
     * @param protocolContact The protocol contact.
     * @return The <code>ChatPanel</code> newly created.
     */
    private ChatPanel createChat(MetaContact contact,
                                            Contact protocolContact)
    {
        return createChat(contact, protocolContact, null);
    }

    /**
     * Creates a <tt>ChatPanel</tt> for the given contact and saves it in the
     * list ot created <tt>ChatPanel</tt>s.
     *
     * @param metaContact The MetaContact for this chat.
     * @param protocolContact The protocol contact.
     * @param escapedMessageID the message ID of the message that should be
     * excluded from the history when the last one is loaded in the chat.
     * @return The <code>ChatPanel</code> newly created.
     */
    private ChatPanel createChat(   MetaContact metaContact,
                                    Contact protocolContact,
                                    String escapedMessageID)
    {
        ChatWindow chatWindow;

        if(ConfigurationManager.isMultiChatWindowEnabled())
        {
            Iterator<ChatPanel> chatPanelsIter = chatPanels.iterator();

            // If we're in a tabbed window we're looking for the chat window
            // through one of the already created chats.
            if(chatPanelsIter.hasNext())
            {
                chatWindow = chatPanelsIter.next().getChatWindow();
            }
            else
            {
                chatWindow = new ChatWindow();

                GuiActivator.getUIService()
                    .registerExportedWindow(chatWindow);
            }
        }
        else
        {
            chatWindow = new ChatWindow();
        }

        ChatPanel chatPanel = new ChatPanel(chatWindow);

        MetaContactChatSession chatSession
            = new MetaContactChatSession(   chatPanel,
                                            metaContact,
                                            protocolContact);

        chatPanel.setChatSession(chatSession);

        synchronized (chatPanels)
        {
            this.chatPanels.add(chatPanel);
        }

        if (ConfigurationManager.isHistoryShown())
        {
            if(escapedMessageID != null)
                chatPanel.loadHistory(escapedMessageID);
            else
                chatPanel.loadHistory();
        }

        return chatPanel;
    }

    /**
     * Creates a <tt>ChatPanel</tt> for the given <tt>ChatRoom</tt> and saves it
     * in the list of created <tt>ChatPanel</tt>s.
     *
     * @param chatRoomWrapper the <tt>ChatRoom</tt>, for which the chat will be created
     * @return The <code>ChatPanel</code> newly created.
     */
    private ChatPanel createChat( ChatRoomWrapper chatRoomWrapper)
    {
        return createChat(chatRoomWrapper, null);
    }

    /**
     * Creates a <tt>ChatPanel</tt> for the given <tt>ChatRoom</tt> and saves it
     * in the list of created <tt>ChatPanel</tt>s.
     *
     * @param chatRoomWrapper the <tt>ChatRoom</tt>, for which the chat will be created
     * @param escapedMessageID the message ID of the message that should be
     * excluded from the history when the last one is loaded in the chat.
     * @return The <code>ChatPanel</code> newly created.
     */
    private ChatPanel createChat( ChatRoomWrapper chatRoomWrapper,
                                            String escapedMessageID)
    {
        ChatWindow chatWindow;

        if(ConfigurationManager.isMultiChatWindowEnabled())
        {
            Iterator<ChatPanel> chatPanelsIter = chatPanels.iterator();

            // If we're in a tabbed window we're looking for the chat window
            // through one of the already created chats.
            if(chatPanelsIter.hasNext())
            {
                chatWindow = chatPanelsIter.next().getChatWindow();
            }
            else
            {
                chatWindow = new ChatWindow();

                GuiActivator.getUIService()
                    .registerExportedWindow(chatWindow);
            }
        }
        else
        {
            chatWindow = new ChatWindow();
        }

        ChatPanel chatPanel = new ChatPanel(chatWindow);

        ConferenceChatSession chatSession
            = new ConferenceChatSession(chatPanel,
                                        chatRoomWrapper);

        chatPanel.setChatSession(chatSession);

        synchronized (chatPanels)
        {
            this.chatPanels.add(chatPanel);
        }

        if (ConfigurationManager.isHistoryShown())
        {
            if(escapedMessageID != null)
                chatPanel.loadHistory(escapedMessageID);
            else
                chatPanel.loadHistory();
        }

        return chatPanel;
    }

    /**
     * Finds the chat session corresponding to the given chat descriptor.
     * 
     * @param descriptor The chat descriptor.
     * @return The chat session corresponding to the given chat descriptor.
     */
    private ChatSession findChatSessionForDescriptor(Object descriptor)
    {
        for (ChatPanel chatPanel : chatPanels)
        {
            ChatSession chatSession = chatPanel.getChatSession();

            if (chatSession.getDescriptor().equals(descriptor))
                return chatSession;
        }

        return null;
    }

    /**
     * Returns TRUE if this chat window contains the given chatPanel,
     * FALSE otherwise.
     *
     * @param chatPanel the chat panel that we're looking for.
     * @return TRUE if this chat window contains the given chatPanel,
     * FALSE otherwise
     */
    private boolean containsChat(ChatPanel chatPanel)
    {
        synchronized (chatPanels)
        {
            return chatPanels.contains(chatPanel);
        }
    }

    /**
     * Returns the <tt>ChatPanel</tt> corresponding to the given meta contact.
     * 
     * @param chatSession the key, which corresponds to the chat we are looking for. It
     * could be a <tt>MetaContact</tt> in the case of single user chat and
     * a <tt>ChatRoom</tt> in the case of a multi user chat
     * @return the <tt>ChatPanel</tt> corresponding to the given meta contact
     */
    private ChatPanel getChat(ChatSession chatSession)
    {
        for (ChatPanel chatPanel : chatPanels)
        {
            if (chatSession.equals(chatPanel.getChatSession()))
                return chatPanel;
        }

        return null;
    }

    /**
     * Disposes the chat window.
     */
    private void disposeChatWindow(ChatWindow chatWindow)
    {
        for (ChatPanel chatPanel : chatPanels)
            chatPanel.dispose();

        synchronized (chatPanels)
        {
            chatPanels.clear();
        }

        if (chatWindow.getChatCount() > 0)
            chatWindow.removeAllChats();

        /*
         * The ChatWindow should seize to exist so we don't want any strong
         * references to it i.e. it cannot be exported anymore.
         */
        GuiActivator.getUIService().unregisterExportedWindow(chatWindow);
        chatWindow.dispose();

        ContactList clist
            = GuiActivator.getUIService().getMainFrame()
                .getContactListPanel().getContactList();

        // Remove the envelope from the all active contacts in the contact list.
        clist.removeAllActiveContacts();
    }
}