aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/muc/MUCCustomContactActionService.java
blob: 7c45b1d9c4cba80cd560e470d0e4948a0edcc4ef (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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.muc;

import java.util.*;

import net.java.sip.communicator.plugin.desktoputil.chat.*;
import net.java.sip.communicator.service.contactsource.*;
import net.java.sip.communicator.service.customcontactactions.*;
import net.java.sip.communicator.service.muc.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;

import org.jitsi.service.resources.*;

/**
 * Implements <tt>CustomContactActionsService</tt> for MUC contact source.
 *
 * @author Hristo Terezov
 */
public class MUCCustomContactActionService
    implements CustomContactActionsService<SourceContact>
{
    /**
     * List of custom menu items.
     */
    private final List<ContactActionMenuItem<SourceContact>> mucActionMenuItems
        = new LinkedList<ContactActionMenuItem<SourceContact>>();

    /**
     * List of custom actions.
     */
    private final List<ContactAction<SourceContact>> mucActions
        = new LinkedList<ContactAction<SourceContact>>();

    /**
     *
     */
    private static final String OWNER_CANT_REMOVE_CHATROOM_PROPERTY
        = "net.java.sip.communicator.impl.muc.OWNER_CANT_REMOVE_CHATROOM";

    /**
     * Array of names for the custom actions.
     */
    private String[] actionsNames = {
        "leave",
        "join",
        "autojoin",
        "autojoin_pressed",
        "destroy_chatroom"
    };

    /**
     * Array of labels for the custom actions.
     */
    private String[] actionsLabels = {
      "service.gui.LEAVE",
      "service.gui.JOIN",
      "service.gui.JOIN_AUTOMATICALLY",
      "service.gui.JOIN_AUTOMATICALLY",
      "service.gui.DESTROY_CHATROOM"
    };

    /**
     * Array of icons for the custom actions.
     */
    private String[] actionsIcons = {
        "service.gui.icons.LEAVE_ICON_BUTTON",
        "service.gui.icons.JOIN_ICON_BUTTON",
        "service.gui.icons.AUTOJOIN_ON_ICON_BUTTON",
        "service.gui.icons.AUTOJOIN_OFF_ICON_BUTTON",
        "service.gui.icons.DESTROY_ICON_BUTTON"
      };

    /**
     * Array of rollover icons for the custom actions.
     */
    private String[] actionsIconsRollover = {
        "service.gui.icons.LEAVE_ICON_ROLLOVER_BUTTON",
        "service.gui.icons.JOIN_ICON_ROLLOVER_BUTTON",
        "service.gui.icons.AUTOJOIN_ON_ICON_ROLLOVER_BUTTON",
        "service.gui.icons.AUTOJOIN_OFF_ICON_ROLLOVER_BUTTON",
        "service.gui.icons.DESTROY_ICON_ROLLOVER_BUTTON"
      };

    /**
     * Array of pressed icons for the custom actions.
     */
    private String[] actionsIconsPressed = {
        "service.gui.icons.LEAVE_ICON_PRESSED_BUTTON",
        "service.gui.icons.JOIN_ICON_PRESSED_BUTTON",
        "service.gui.icons.AUTOJOIN_ON_ICON_PRESSED_BUTTON",
        "service.gui.icons.AUTOJOIN_OFF_ICON_PRESSED_BUTTON",
        "service.gui.icons.DESTROY_ICON_PRESSED_BUTTON"
      };

    /**
     * Array of names for the custom menu items.
     */
    private String[] menuActionsNames = {
        "open",
        "join",
        "join_as",
        "leave",
        "remove",
        "change_nick",
        "autojoin",
        "autojoin_pressed",
        "open_automatically",
        "destroy_chatroom"
    };

    /**
     * Array of labels for the custom menu items.
     */
    private String[] menuActionsLabels = {
        "service.gui.OPEN",
        "service.gui.JOIN",
        "service.gui.JOIN_AS",
        "service.gui.LEAVE",
        "service.gui.REMOVE",
        "service.gui.CHANGE_NICK",
        "service.gui.JOIN_AUTOMATICALLY",
        "service.gui.DONT_JOIN_AUTOMATICALLY",
        "service.gui.OPEN_AUTOMATICALLY",
        "service.gui.DESTROY_CHATROOM"
    };

    /**
     * Array of icons for the custom menu items.
     */
    private String[] menuActionsIcons = {
        "service.gui.icons.CHAT_ROOM_16x16_ICON",
        "service.gui.icons.JOIN_ICON",
        "service.gui.icons.JOIN_AS_ICON",
        "service.gui.icons.LEAVE_ICON",
        "service.gui.icons.REMOVE_CHAT_ICON",
        "service.gui.icons.RENAME_16x16_ICON",
        "service.gui.icons.AUTOJOIN",
        "service.gui.icons.AUTOJOIN",
        "service.gui.icons.OPEN_AUTOMATICALLY",
        "service.gui.icons.DESTROY_CHATROOM"
      };

    /**
     * A runnable that leaves the chat room.
     */
    private MUCCustomActionRunnable leaveRunnable
        = new MUCCustomActionRunnable()
        {

            @Override
            public void run()
            {
                ChatRoomWrapper leavedRoomWrapped
                    = MUCActivator.getMUCService().leaveChatRoom(
                        chatRoomWrapper);
                if(leavedRoomWrapped != null)
                    MUCActivator.getUIService().closeChatRoomWindow(
                        leavedRoomWrapped);
            }
        };

    /**
     * A runnable that joins the chat room.
     */
    private MUCCustomActionRunnable joinRunnable
        = new MUCCustomActionRunnable()
        {

            @Override
            public void run()
            {
                String[] joinOptions;
                String subject = null;
                String nickName = null;

                nickName =
                    ConfigurationUtils.getChatRoomProperty(
                        chatRoomWrapper.getParentProvider()
                            .getProtocolProvider(), chatRoomWrapper
                        .getChatRoomID(), "userNickName");
                if(nickName == null)
                {
                    joinOptions = ChatRoomJoinOptionsDialog.getJoinOptions(
                        chatRoomWrapper.getParentProvider()
                            .getProtocolProvider(),
                        chatRoomWrapper.getChatRoomID(),
                        MUCActivator.getGlobalDisplayDetailsService()
                            .getDisplayName(chatRoomWrapper.getParentProvider()
                                .getProtocolProvider()));
                    nickName = joinOptions[0];
                    subject = joinOptions[1];
                }

                if (nickName != null)
                    MUCActivator.getMUCService().joinChatRoom(chatRoomWrapper,
                        nickName, null, subject);
            }
        };

    /**
     * A runnable that sets / unsets auto join setting of the chat room.
     */
    private MUCCustomActionRunnable autoJoinRunnable
        = new MUCCustomActionRunnable()
        {

            @Override
            public void run()
            {
                    chatRoomWrapper.setAutoJoin(!chatRoomWrapper.isAutojoin());

            }
        };

    /**
     * A runnable that destroys the chat room.
     */
    private MUCCustomActionRunnable destroyActionRunnable
        = new MUCCustomActionRunnable()
        {

            @Override
            public void run()
            {
                String destroyOptions[]
                    = ChatRoomDestroyReasonDialog.getDestroyOptions();
                if(destroyOptions == null)
                    return;

                MUCActivator.getMUCService().destroyChatRoom(chatRoomWrapper,
                    destroyOptions[0], destroyOptions[1]);

            }
        };

    /**
     * Array of <tt>MUCCustomActionRunnable</tt> objects for the custom menu
     * items. They will be executed when the item is pressed.
     */
    private MUCCustomActionRunnable[] actionsRunnable = {
        leaveRunnable,
        joinRunnable,
        autoJoinRunnable,
        autoJoinRunnable,
        destroyActionRunnable
    };

    /**
     * Array of <tt>MUCCustomActionRunnable</tt> objects for the custom menu
     * items. They will be executed when the item is pressed.
     */
    private MUCCustomActionRunnable[] menuActionsRunnable = {
        new MUCCustomActionRunnable()
        {
            @Override
            public void run()
            {
                MUCActivator.getMUCService().openChatRoom(chatRoomWrapper);
            }
        },
        joinRunnable,
        new MUCCustomActionRunnable()
        {

            @Override
            public void run()
            {
                String[] joinOptions;
                joinOptions = ChatRoomJoinOptionsDialog.getJoinOptions(
                    chatRoomWrapper.getParentProvider().getProtocolProvider(),
                    chatRoomWrapper.getChatRoomID(),
                    MUCActivator.getGlobalDisplayDetailsService()
                        .getDisplayName(chatRoomWrapper.getParentProvider()
                            .getProtocolProvider()));
                if(joinOptions[0] == null)
                    return;
                MUCActivator.getMUCService()
                    .joinChatRoom(chatRoomWrapper, joinOptions[0], null,
                        joinOptions[1]);
            }
        },
        leaveRunnable,
        new MUCCustomActionRunnable()
        {

            @Override
            public void run()
            {
                ChatRoom chatRoom = chatRoomWrapper.getChatRoom();

                if (chatRoom != null)
                {
                    ChatRoomWrapper leavedRoomWrapped
                        = MUCActivator.getMUCService().leaveChatRoom(
                            chatRoomWrapper);
                    if(leavedRoomWrapped != null)
                        MUCActivator.getUIService().closeChatRoomWindow(
                            leavedRoomWrapped);
                }

                MUCActivator.getUIService().closeChatRoomWindow(chatRoomWrapper);

                MUCActivator.getMUCService().removeChatRoom(chatRoomWrapper);
            }
        },
        new MUCCustomActionRunnable()
        {

            @Override
            public void run()
            {
                ChatRoomJoinOptionsDialog.getJoinOptions(true,
                    chatRoomWrapper.getParentProvider().getProtocolProvider(),
                    chatRoomWrapper.getChatRoomID(),
                    MUCActivator.getGlobalDisplayDetailsService()
                        .getDisplayName(chatRoomWrapper.getParentProvider()
                            .getProtocolProvider()));
            }
        },
        autoJoinRunnable,
        autoJoinRunnable,
        new MUCCustomActionRunnable()
        {

            @Override
            public void run()
            {
                MUCActivator.getUIService().showChatRoomAutoOpenConfigDialog(
                    chatRoomWrapper.getParentProvider().getProtocolProvider(),
                    chatRoomWrapper.getChatRoomID());
            }
        },
        destroyActionRunnable
    };

    /**
     * Array of <tt>EnableChecker</tt> objects for the custom menu items. They
     * are used to check if the item is enabled or disabled.
     */
    private EnableChecker[] actionsEnabledCheckers = {
      null,
      new JoinEnableChecker(),
      new JoinEnableChecker(),
      new LeaveEnableChecker(),
      null,
      null,
      null,
      null,
      null,
      null
    };

    /**
     * The resource management service instance.
     */
    ResourceManagementService resources = MUCActivator.getResources();

    /**
     * Constructs the custom actions.
     */
    public MUCCustomContactActionService()
    {
        for(int i = 0; i < menuActionsLabels.length; i++)
        {
            MUCActionMenuItems item
                = new MUCActionMenuItems(
                        menuActionsNames[i],
                        menuActionsLabels[i],
                        menuActionsIcons[i],
                        menuActionsRunnable[i]);
            mucActionMenuItems.add(item);
            if(actionsEnabledCheckers[i] != null)
                item.setEnabled(actionsEnabledCheckers[i]);
        }

        for(int i = 0; i < actionsLabels.length; i++)
        {
            MUCAction item = new MUCAction(
                                    actionsNames[i],
                                    actionsLabels[i],
                                    actionsIcons[i],
                                    actionsIconsRollover[i],
                                    actionsIconsPressed[i],
                                    actionsRunnable[i]);
            mucActions.add(item);
        }

    }

    /**
     * Returns the template class that this service has been initialized with
     *
     * @return the template class
     */
    public Class<SourceContact> getContactSourceClass()
    {
        return SourceContact.class;
    }

    @Override
    public Iterator<ContactActionMenuItem<SourceContact>>
        getCustomContactActionsMenuItems()
    {
        return mucActionMenuItems.iterator();
    }


    @Override
    public Iterator<ContactAction<SourceContact>> getCustomContactActions()
    {
        return mucActions.iterator();
    }

    /**
     * Implements the MUC custom action.
     */
    private class MUCAction
        implements ContactAction<SourceContact>
    {
        /**
         * The text of the action.
         */
        private String text;

        /**
         * The icon of the action
         */
        private byte[] icon;

        /**
         * The icon that is shown when the action is pressed.
         */
        private byte[] iconPressed;

        /**
         * The runnable that is executed when the action is pressed.
         */
        private MUCCustomActionRunnable actionPerformed;

        /**
         * The icon that is shown when the mouse is over the action.
         */
        private byte[] iconRollover;

        /**
         * The name of the action.
         */
        private String name;

        /**
         * Constructs <tt>MUCAction</tt> instance.
         *
         * @param textKey the key used to retrieve the label for the action.
         * @param iconKey the key used to retrieve the icon for the action.
         * @param actionPerformed the action executed when the action is
         * pressed.
         * @param iconRolloverKey the key used to retrieve the rollover icon for
         * the action.
         * @param iconPressedKey the key used to retrieve the pressed icon for
         * the action.
         */
        public MUCAction(String name, String textKey, String iconKey,
            String iconRolloverKey, String iconPressedKey,
            MUCCustomActionRunnable actionPerformed)
        {
            this.name = name;
            this.text = resources.getI18NString(textKey);
            this.icon = resources.getImageInBytes(iconKey);
            this.iconRollover = resources.getImageInBytes(iconRolloverKey);
            this.iconPressed = resources.getImageInBytes(iconPressedKey);
            this.actionPerformed = actionPerformed;
        }

        @Override
        public void actionPerformed(SourceContact actionSource, int x, int y)
            throws OperationFailedException
        {
            if(!(actionSource instanceof ChatRoomSourceContact))
                return;
            actionPerformed.setContact(actionSource);
            new Thread(actionPerformed).start();
        }

        @Override
        public byte[] getIcon()
        {
            return icon;
        }

        @Override
        public byte[] getRolloverIcon()
        {
            return iconRollover;
        }

        @Override
        public byte[] getPressedIcon()
        {
            return iconPressed;
        }

        @Override
        public String getToolTipText()
        {
            return text;
        }

        @Override
        public boolean isVisible(SourceContact actionSource)
        {
            if(actionSource instanceof ChatRoomSourceContact)
            {
                if(name.equals("leave"))
                {
                    return actionsEnabledCheckers[3].check(actionSource);
                }
                else if(name.equals("join"))
                {
                    return actionsEnabledCheckers[1].check(actionSource);
                }
                else if(name.equals("destroy_chatroom"))
                {
                    ChatRoomSourceContact contact
                        = (ChatRoomSourceContact) actionSource;
                    ChatRoomWrapper room = MUCActivator.getMUCService()
                        .findChatRoomWrapperFromSourceContact(contact);
                    if(room == null || room.getChatRoom() == null)
                        return false;
                    if(room.getChatRoom().getUserRole().equals(ChatRoomMemberRole.OWNER))
                        return true;
                    return false;
                }
                else
                {
                    ChatRoomSourceContact contact
                        = (ChatRoomSourceContact) actionSource;
                    ChatRoomWrapper room = MUCActivator.getMUCService()
                        .findChatRoomWrapperFromSourceContact(contact);
                    if(room == null)
                        return false;

                    if(name.equals("autojoin"))
                        return room.isAutojoin();
                    else if(name.equals("autojoin_pressed"))
                        return !room.isAutojoin();
                }
            }
            return false;
        }

    }

    /**
     * Implements the MUC custom menu items.
     */
    private class MUCActionMenuItems
        implements ContactActionMenuItem<SourceContact>
    {
        /**
         * The label for the menu item.
         */
        private String text;

        /**
         * The the icon for the menu item.
         */
        private byte[] image;

        /**
         * The action executed when the menu item is pressed.
         */
        private MUCCustomActionRunnable actionPerformed;

        /**
         * Object that is used to check if the item is enabled or disabled.
         */
        private EnableChecker enabled;

        /**
         * The name of the custom action menu item.
         */
        private String name;

        /**
         * The mnemonic for the action.
         */
        private char mnemonics;

        /**
         * Constructs <tt>MUCActionMenuItems</tt> instance.
         *
         * @param textKey the key used to retrieve the label for the menu item.
         * @param imageKey the key used to retrieve the icon for the menu item.
         * @param actionPerformed the action executed when the menu item is
         * pressed.
         */
        public MUCActionMenuItems(String name, String textKey, String imageKey,
            MUCCustomActionRunnable actionPerformed)
        {
            this.text = resources.getI18NString(textKey);
            this.image = (imageKey == null)? null :
                resources.getImageInBytes(imageKey);
            this.actionPerformed = actionPerformed;
            this.enabled = new EnableChecker();
            this.name = name;
            this.mnemonics = resources.getI18nMnemonic(textKey);
        }

        @Override
        public void actionPerformed(SourceContact actionSource)
            throws OperationFailedException
        {
            if(!(actionSource instanceof ChatRoomSourceContact))
                return;
            actionPerformed.setContact(actionSource);
            new Thread(actionPerformed).start();
        }

        @Override
        public byte[] getIcon()
        {
            return image;
        }


        @Override
        public String getText(SourceContact actionSource)
        {
            if(!(actionSource instanceof ChatRoomSourceContact))
                return "";

            if(!name.equals("open_automatically"))
                return text;

            String openAutomaticallyValue
                = MUCService.getChatRoomAutoOpenOption(
                    ((ChatRoomSourceContact)actionSource).getProvider(),
                    ((ChatRoomSourceContact)actionSource).getChatRoomID());
            if(openAutomaticallyValue == null)
                openAutomaticallyValue = MUCService.DEFAULT_AUTO_OPEN_BEHAVIOUR;
            String openAutomaticallyKey = MUCService.autoOpenConfigValuesTexts
                .get(openAutomaticallyValue);
            return "<html>" + text +  "...<br><font size=\"2\"><center> ("
                + resources.getI18NString(openAutomaticallyKey)
                + ")</center></font></html>";
        }

        @Override
        public boolean isVisible(SourceContact actionSource)
        {
            if(!(actionSource instanceof ChatRoomSourceContact))
                return false;

            ChatRoomSourceContact contact
                = (ChatRoomSourceContact) actionSource;
            ChatRoomWrapper room = MUCActivator.getMUCService()
                .findChatRoomWrapperFromSourceContact(contact);
            if(name.equals("autojoin") || name.equals("autojoin_pressed"))
            {
                if(room == null)
                    return true;

                if(name.equals("autojoin"))
                    return !room.isAutojoin();

                if(name.equals("autojoin_pressed"))
                    return room.isAutojoin();
            }
            else if(name.equals("remove"))
            {
                if(room == null || room.getChatRoom() == null)
                    return true;

                boolean ownerCannotRemoveRoom
                    = MUCActivator.getConfigurationService().getBoolean(
                            OWNER_CANT_REMOVE_CHATROOM_PROPERTY, false);

                // when joined role will be owner or member
                // when not joined and if we never has entered the room role
                // will be guest, if we joined and left the room the role
                // will be owner or member
                if(room.getChatRoom().getUserRole().equals(
                    ChatRoomMemberRole.MEMBER))
                {
                    return true;
                }
                else
                {
                    if(ownerCannotRemoveRoom)
                        return false;
                    else
                        return true;
                }
            }
            else if(name.equals("destroy_chatroom"))
            {
                if(room == null || room.getChatRoom() == null)
                    return false;
                if(room.getChatRoom().getUserRole().equals(
                    ChatRoomMemberRole.OWNER))
                    return true;
                return false;
            }
            return true;
        }

        @Override
        public char getMnemonics()
        {
            return mnemonics;
        }

        @Override
        public boolean isEnabled(SourceContact actionSource)
        {
            return enabled.check(actionSource);
        }

        /**
         * Sets <tt>EnabledChecker</tt> instance that will be used to check if
         * the item should be enabled or disabled.
         *
         * @param enabled the <tt>EnabledChecker</tt> instance.
         */
        public void setEnabled(EnableChecker enabled)
        {
            this.enabled = enabled;
        }

        @Override
        public boolean isCheckBox()
        {
            return false;
        }

        @Override
        public boolean isSelected(SourceContact contact)
        {
            ChatRoomWrapper chatRoomWrapper = MUCActivator.getMUCService()
                .findChatRoomWrapperFromSourceContact(contact);
            if(chatRoomWrapper == null)
                return false;
            return chatRoomWrapper.isAutojoin();
        }

    }

    /**
     * Checks if the menu item should be enabled or disabled. This is default
     * implementation. Always returns that the item should be enabled.
     */
    private static class EnableChecker
    {
        /**
         * Checks if the menu item should be enabled or disabled.
         *
         * @param contact the contact associated with the menu item.
         * @return always <tt>true</tt>
         */
        public boolean check(SourceContact contact)
        {
            return true;
        }
    }

    /**
     * Implements <tt>EnableChecker</tt> for the join menu items.
     */
    private static class JoinEnableChecker
        extends EnableChecker
    {
        /**
         * Checks if the menu item should be enabled or disabled.
         *
         * @param contact the contact associated with the menu item.
         * @return <tt>true</tt> if the item should be enabled and
         * <tt>false</tt> if not.
         */
        public boolean check(SourceContact contact)
        {
            ChatRoomWrapper chatRoomWrapper = MUCActivator.getMUCService()
                .findChatRoomWrapperFromSourceContact(contact);
            ChatRoom chatRoom = null;
            if(chatRoomWrapper != null)
            {
                chatRoom = chatRoomWrapper.getChatRoom();
            }

            if((chatRoom != null) && chatRoom.isJoined())
                return false;
            return true;
        }
    }

    /**
     * Implements <tt>EnableChecker</tt> for the leave menu item.
     */
    private static class LeaveEnableChecker
        extends JoinEnableChecker
    {
        /**
         * Checks if the menu item should be enabled or disabled.
         *
         * @param contact the contact associated with the menu item.
         * @return <tt>true</tt> if the item should be enabled and
         * <tt>false</tt> if not.
         */
        public boolean check(SourceContact contact)
        {
            return !super.check(contact);
        }
    }

    /**
     * Implements base properties for the MUC menu items.These properties are
     * used when the menu item is pressed.
     */
    private abstract class MUCCustomActionRunnable
        implements Runnable
    {
        /**
         * The contact associated with the menu item.
         */
        protected SourceContact contact;

        /**
         * The contact associated with the menu item.
         */
        protected ChatRoomWrapper chatRoomWrapper;

        /**
         * Sets the source contact.
         * @param contact the contact to set
         */
        public void setContact(SourceContact contact)
        {
            this.contact = contact;
            chatRoomWrapper = MUCActivator.getMUCService()
                .findChatRoomWrapperFromSourceContact(contact);
        }
    }
}