aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/systray/jdic/StatusSubMenu.java
blob: 1d5a96f6d3f5adc62920cae6e6aebd3f0703b289 (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
/*
 * 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.systray.jdic;

import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.util.*;

import javax.swing.*;

import net.java.sip.communicator.impl.systray.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;

import org.osgi.framework.*;

/**
 * The <tt>StatusSubMenu</tt> provides a menu which allow to select the status
 * for each of the protocol providers registered when the menu appears
 * 
 * @author Nicolas Chamouard
 * @author Lubomir Marinov
 */
public class StatusSubMenu
{
    /**
     * A reference of <tt>Systray</tt>
     */
    private final SystrayServiceJdicImpl parentSystray;

    /**
     * Contains all accounts and corresponding menus.
     */
    private final Map<AccountID, Object> accountSelectors =
        new Hashtable<AccountID, Object>();

    private final Logger logger = Logger.getLogger(StatusSubMenu.class);

    private final Object menu;

    /**
     * Creates an instance of <tt>StatusSubMenu</tt>.
     * 
     * @param tray a reference of the parent <tt>Systray</tt>
     * @param swing <tt>true</tt> to represent this instance with a Swing
     *            <code>JMenu</code>; <tt>false</tt> to use an AWT
     *            <code>Menu</code>
     */
    public StatusSubMenu(SystrayServiceJdicImpl tray, boolean swing)
    {
        parentSystray = tray;

        String text = Resources.getString("impl.systray.SET_STATUS");
        if (swing)
        {
            JMenu menu = new JMenu(text);
            menu
                .setIcon(Resources.getImage("service.systray.STATUS_MENU_ICON"));

            /* makes the menu look better */
            menu.setPreferredSize(new java.awt.Dimension(28, 24));

            this.menu = menu;
        }
        else
        {
            this.menu = new Menu(text);
        }

        this.init();
    }

    public Object getMenu()
    {
        return menu;
    }

    /**
     * Adds the account corresponding to the given protocol provider to this
     * menu.
     * 
     * @param protocolProvider the protocol provider corresponding to the
     *            account to add
     */
    private void addAccount(ProtocolProviderService protocolProvider)
    {
        OperationSetPresence presence =
            (OperationSetPresence) protocolProvider
                .getOperationSet(OperationSetPresence.class);
        boolean swing = (menu instanceof JComponent);

        if (presence == null)
        {
            StatusSimpleSelector simpleSelector =
                new StatusSimpleSelector(protocolProvider, swing);

            this.accountSelectors.put(protocolProvider.getAccountID(),
                simpleSelector);
            addMenuItem(menu, simpleSelector.getMenu());
        }
        else
        {
            StatusSelector statusSelector =
                new StatusSelector(parentSystray, protocolProvider, presence,
                    swing);

            this.accountSelectors.put(protocolProvider.getAccountID(),
                statusSelector);
            addMenuItem(menu, statusSelector.getMenu());

            presence
                .addProviderPresenceStatusListener(new SystrayProviderPresenceStatusListener());
        }
    }

    static void addMenuItem(Object menu, Object menuItem)
    {
        if (menu instanceof Container)
            ((Container) menu).add((Component) menuItem);
        else
            ((Menu) menu).add((MenuItem) menuItem);
    }

    /**
     * Removes the account corresponding to the given protocol provider from
     * this menu.
     * 
     * @param protocolProvider the protocol provider corresponding to the
     *            account to remove.
     */
    private void removeAccount(ProtocolProviderService protocolProvider)
    {
        Object selector =
            this.accountSelectors.get(protocolProvider.getAccountID());
        Object selectorMenu;
        if (selector instanceof StatusSimpleSelector)
            selectorMenu = ((StatusSimpleSelector) selector).getMenu();
        else
            selectorMenu = ((StatusSelector) selector).getMenu();

        if (menu instanceof Container)
            ((Container) menu).remove((Component) selectorMenu);
        else
            ((MenuContainer) menu).remove((MenuComponent) selectorMenu);
    }

    /**
     * We fill the protocolProviderTable with all
     * running protocol providers at the start of
     * the bundle.
     */
    private void init()
    {
        SystrayActivator.bundleContext
            .addServiceListener(new ProtocolProviderServiceListener());

        ServiceReference[] protocolProviderRefs = null;
        try
        {
            protocolProviderRefs
                = SystrayActivator.bundleContext.getServiceReferences(
                    ProtocolProviderService.class.getName(),null);
        }
        catch (InvalidSyntaxException ex)
        {
            // this shouldn't happen since we're providing no parameter string
            // but let's log just in case.
            logger .error("Error while retrieving service refs", ex);
            return;
        }

        // in case we found any
        if (protocolProviderRefs != null)
        {

            for (int i = 0; i < protocolProviderRefs.length; i++)
            {
                ProtocolProviderService provider
                    = (ProtocolProviderService) SystrayActivator.bundleContext
                        .getService(protocolProviderRefs[i]);

                boolean isHidden = 
                    provider.getAccountID().getAccountProperties().
                        get(ProtocolProviderFactory.IS_PROTOCOL_HIDDEN) != null;
                
                if(!isHidden)
                    this.addAccount(provider);
            }
        }
    }

    /**
     * Listens for <tt>ServiceEvent</tt>s indicating that a
     * <tt>ProtocolProviderService</tt> has been registered and completes the
     * account status menu.
     */
    private class ProtocolProviderServiceListener implements ServiceListener
    {
        /**
         * When a service is registered or unregistered, we update
         * the provider tables and add/remove listeners (if it supports
         * BasicInstantMessenging implementation)
         *
         * @param event ServiceEvent
         */
        public void serviceChanged(ServiceEvent event)
        {
            //if the event is caused by a bundle being stopped, we don't want to
            //know
            if(event.getServiceReference().getBundle().getState()
                == Bundle.STOPPING)
            {
                return;
            }

            Object service = SystrayActivator.bundleContext
                .getService( event.getServiceReference());

            if (! (service instanceof ProtocolProviderService))
                return;

            ProtocolProviderService provider = (ProtocolProviderService)service;

            switch (event.getType())
            {
            case ServiceEvent.REGISTERED:
                addAccount(provider);
                break;

            case ServiceEvent.UNREGISTERING:
                removeAccount(provider);
                break;
            }
        }
    }

    /**
     * Listens for all providerStatusChanged and providerStatusMessageChanged
     * events in order to refresh the account status panel, when a status is
     * changed.
     */
    private class SystrayProviderPresenceStatusListener
        implements ProviderPresenceStatusListener
    {
        /**
         * Fired when an account has changed its status. We update the icon
         * in the menu.
         */
        public void providerStatusChanged(ProviderPresenceStatusChangeEvent evt)
        {
            ProtocolProviderService pps = evt.getProvider();

            StatusSelector selectorBox 
                = (StatusSelector) accountSelectors.get(pps.getAccountID());
        
            if(selectorBox == null)
                return;

            selectorBox.updateStatus(evt.getNewStatus());
        }

        public void providerStatusMessageChanged(PropertyChangeEvent evt)
        {
        }
    }
}