aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/whiteboard/WhiteboardMenuItem.java
blob: d83f996bd75dfe88caa3a291b9e30e5991e49932 (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
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.plugin.whiteboard;

import java.awt.event.*;
import java.util.*;

import javax.swing.*;

import net.java.sip.communicator.service.contactlist.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.protocol.*;

/**
 * WhiteboardMenuItem
 *
 * @author Julien Waechter
 */
public class WhiteboardMenuItem
    extends AbstractPluginComponent
    implements ActionListener
{
    private final JMenu whiteboardMenu
        = new JMenu(Resources.getString("plugin.whiteboard.MENU_ITEM"));

    /**
     * The current meta contact
     */
    private MetaContact metaContact;

    /**
     * The Whiteboard session manager
     */
    private final WhiteboardSessionManager session;

    /**
     * WhiteboardMenuItem constructor.
     *
     * @param session the whiteboard session manager
     */
    public WhiteboardMenuItem (WhiteboardSessionManager session,
                               PluginComponentFactory parentFactory)
    {
        super(Container.CONTAINER_CONTACT_RIGHT_BUTTON_MENU, parentFactory);

        this.session = session;
        this.whiteboardMenu.setIcon (
            Resources.getImage ("plugin.whiteboard.MPEN_ICON"));
    }

    @Override
    public void setCurrentContact (Contact contact)
    {
    }

    /**
     * Sets the current meta contact.
     *
     * @param metaContact the current meta contact
     */
    @Override
    public void setCurrentContact (MetaContact metaContact)
    {
        this.metaContact = metaContact;

        this.whiteboardMenu.removeAll();

        Iterator<Contact> iter = metaContact.getContacts();
        while (iter.hasNext())
        {
            Contact contact = iter.next();
            ProtocolProviderService pps = contact.getProtocolProvider();

            OperationSetWhiteboarding opSetWb
                = pps.getOperationSet(OperationSetWhiteboarding.class);

            String contactDisplayName = contact.getDisplayName();

            JMenuItem contactItem = new JMenuItem(contactDisplayName);
            contactItem.setName(contact.getDisplayName() + pps.getProtocolName());

            if (opSetWb != null)
            {
                contactItem.addActionListener(this);
            }
            else
            {
                contactItem.setEnabled(false);
                contactItem.setToolTipText(
                        Resources.getString("plugin.whiteboard.NOT_SUPPORTED"));
            }

            this.whiteboardMenu.add(contactItem);
        }
    }

    /**
     * Sets the current meta group.
     *
     * @param metaGroup the current meta contact group
     */
    @Override
    public void setCurrentContactGroup (MetaContactGroup metaGroup)
    {
    }

    /**
     * Invoked when an action occurs: user start a whiteboard session.
     *
     * @param e event
     */
    public void actionPerformed (ActionEvent e)
    {
        String itemID = ((JMenuItem)e.getSource()).getName();
        Iterator<Contact> i = this.metaContact.getContacts();

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

            String id = contact.getAddress()
                + contact.getProtocolProvider().getProtocolName();

            if(itemID.equals(id))
                session.initWhiteboard (contact);
        }
    }

    public Object getComponent()
    {
        if(metaContact == null)
        {
            whiteboardMenu.setEnabled(false);
            return whiteboardMenu;
        }

        Iterator<Contact> iter = metaContact.getContacts();
        while (iter.hasNext())
        {
            Contact contact = iter.next();
            ProtocolProviderService pps = contact.getProtocolProvider();

            OperationSetWhiteboarding opSetWb
                = pps.getOperationSet(OperationSetWhiteboarding.class);

            if (opSetWb != null)
            {
                whiteboardMenu.setEnabled(true);
                return whiteboardMenu;
            }
        }

        whiteboardMenu.setEnabled(false);
        return whiteboardMenu;
    }

    public String getName()
    {
        return whiteboardMenu.getText();
    }
}