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
|
/*
* 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.osdependent.jdic;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import net.java.sip.communicator.impl.osdependent.*;
import net.java.sip.communicator.plugin.desktoputil.presence.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.service.protocol.globalstatus.*;
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 Lyubomir Marinov
*/
public class StatusSubMenu
implements ProviderPresenceStatusListener,
RegistrationStateChangeListener,
ActionListener,
ItemListener
{
/**
* Contains all accounts and corresponding menus.
*/
private final Map<AccountID, Object> accountSelectors =
new Hashtable<AccountID, Object>();
private final Object menu;
/**
* Hide accounts from accounts status list.
*/
private static boolean hideAccountStatusSelectors = false;
/**
* Initializes a new <tt>StatusSubMenu</tt> instance.
*
* @param swing <tt>true</tt> to represent this instance with a Swing
* <tt>JMenu</tt>; <tt>false</tt> to use an AWT <tt>Menu</tt>
*/
public StatusSubMenu(boolean swing, boolean accountMenuSupported)
{
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);
}
if (accountMenuSupported)
{
String hideAccountStatusSelectorsProperty
= "impl.gui.HIDE_ACCOUNT_STATUS_SELECTORS";
String hideAccountsStatusDefaultValue =
OsDependentActivator.getResources()
.getSettingsString(hideAccountStatusSelectorsProperty);
if(hideAccountsStatusDefaultValue != null)
hideAccountStatusSelectors = Boolean.parseBoolean(
hideAccountsStatusDefaultValue);
hideAccountStatusSelectors =
OsDependentActivator.getConfigurationService()
.getBoolean(
hideAccountStatusSelectorsProperty,
hideAccountStatusSelectors);
}
else
{
hideAccountStatusSelectors = true;
}
PresenceStatus offlineStatus = null;
// creates menu item entry for every global status
for(GlobalStatusEnum status : GlobalStatusEnum.globalStatusSet)
{
createMenuItem(status, swing);
if(status.getStatus() < 1)
offlineStatus = status;
}
// initially it is offline
selectItemFromStatus(offlineStatus.getStatus());
if (accountMenuSupported)
{
this.addSeparator();
addMenuItem(menu, new GlobalStatusMessageMenu(swing).getMenu());
}
if(!hideAccountStatusSelectors)
this.addSeparator();
this.init();
}
public Object getMenu()
{
return menu;
}
/**
* Creates a menu item with the given <tt>textKey</tt>, <tt>iconID</tt> and
* <tt>name</tt>.
* @return the created <tt>JCheckBoxMenuItem</tt>
*/
private Object createMenuItem(
GlobalStatusEnum status,
boolean swing)
{
Object menuItem = null;
if(swing)
{
JCheckBoxMenuItem mItem = new JCheckBoxMenuItem(
GlobalStatusEnum.getI18NStatusName(status),
new ImageIcon(status.getStatusIcon()));
mItem.setName(status.getStatusName());
mItem.addActionListener(this);
menuItem = mItem;
}
else
{
CheckboxMenuItem mItem = new CheckboxMenuItem(
GlobalStatusEnum.getI18NStatusName(status));
mItem.setName(status.getStatusName());
mItem.addItemListener(this);
menuItem = mItem;
}
addMenuItem(getMenu(), menuItem);
return menuItem;
}
/**
* Adds separator to underlying menu implementation.
*/
private void addSeparator()
{
if (menu instanceof JMenu)
((JMenu) menu).addSeparator();
else
((Menu) menu).addSeparator();
}
/**
* 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)
{
if(protocolProvider.getAccountID().isStatusMenuHidden())
return;
OperationSetPresence presence
= protocolProvider.getOperationSet(OperationSetPresence.class);
boolean swing = (menu instanceof JComponent);
if (presence == null)
{
StatusSimpleSelector simpleSelector =
new StatusSimpleSelector(protocolProvider, swing);
this.accountSelectors.put(protocolProvider.getAccountID(),
simpleSelector);
if(!hideAccountStatusSelectors)
addMenuItem(menu, simpleSelector.getMenu());
protocolProvider.addRegistrationStateChangeListener(this);
}
else
{
StatusSelector statusSelector
= new StatusSelector(protocolProvider, presence, swing);
this.accountSelectors.put(protocolProvider.getAccountID(),
statusSelector);
if(!hideAccountStatusSelectors)
addMenuItem(menu, statusSelector.getMenu());
presence.addProviderPresenceStatusListener(this);
}
}
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());
// no such provider added
if(selector == null)
return;
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);
/*
* Remove the listeners installed in
* addAccount(ProtocolProviderService).
*/
OperationSetPresence presence
= protocolProvider.getOperationSet(OperationSetPresence.class);
if (presence != null)
presence.removeProviderPresenceStatusListener(this);
else
protocolProvider.removeRegistrationStateChangeListener(this);
}
/**
* We fill the protocolProviderTable with all
* running protocol providers at the start of
* the bundle.
*/
private void init()
{
OsDependentActivator.bundleContext
.addServiceListener(new ProtocolProviderServiceListener());
for(ProtocolProviderService provider : getProtocolProviders())
{
if(!provider.getAccountID().isHidden())
this.addAccount(provider);
}
}
/**
* Obtains all currently registered ProtocolProviderServices.
* @return all currently registered ProtocolProviderServices.
*/
private List<ProtocolProviderService> getProtocolProviders()
{
BundleContext bundleContext = OsDependentActivator.bundleContext;
Collection<ServiceReference<ProtocolProviderService>> ppsRefs
= ServiceUtils.getServiceReferences(
bundleContext,
ProtocolProviderService.class);
List<ProtocolProviderService> protocolProviders
= new ArrayList<ProtocolProviderService>();
// in case we found any
if ((ppsRefs != null) && !ppsRefs.isEmpty())
{
for (ServiceReference<ProtocolProviderService> ppsRef : ppsRefs)
protocolProviders.add(bundleContext.getService(ppsRef));
}
return protocolProviders;
}
/**
* Fired when an account has changed its status. We update the icon
* in the menu.
*
* @param evt
*/
public void providerStatusChanged(ProviderPresenceStatusChangeEvent evt)
{
ProtocolProviderService pps = evt.getProvider();
StatusSelector selectorBox
= (StatusSelector) accountSelectors.get(pps.getAccountID());
if (selectorBox != null)
selectorBox.updateStatus(evt.getNewStatus());
this.updateGlobalStatus();
}
/*
* ImplementsProviderPresenceStatusListener#providerStatusMessageChanged(
* PropertyChangeEvent).
*/
public void providerStatusMessageChanged(PropertyChangeEvent evt)
{
}
/*
* ImplementsRegistrationStateChangeListener#registrationStateChanged(
* RegistrationStateChangeEvent). Updates the status of accounts which do
* not support presence.
*/
public void registrationStateChanged(RegistrationStateChangeEvent evt)
{
ProtocolProviderService pps = evt.getProvider();
StatusSimpleSelector selectorBox
= (StatusSimpleSelector) accountSelectors.get(pps.getAccountID());
if (selectorBox != null)
selectorBox.updateStatus();
this.updateGlobalStatus();
}
/**
* Updates the global status by picking the most connected protocol provider
* status.
*/
private void updateGlobalStatus()
{
int status = 0;
boolean hasAvailableProvider = false;
for(ProtocolProviderService protocolProvider : getProtocolProviders())
{
// We do not show hidden protocols in our status bar, so we do not
// care about their status here.
if (protocolProvider.getAccountID().isHidden())
continue;
if (!protocolProvider.isRegistered())
continue;
OperationSetPresence presence
= protocolProvider.getOperationSet(OperationSetPresence.class);
if(presence == null)
{
hasAvailableProvider = true;
continue;
}
int presenceStatus
= (presence == null)
? PresenceStatus.AVAILABLE_THRESHOLD
: presence.getPresenceStatus().getStatus();
if (status < presenceStatus)
status = presenceStatus;
}
// if we have at least one online provider
if(status == 0 && hasAvailableProvider)
status = PresenceStatus.AVAILABLE_THRESHOLD;
selectItemFromStatus(status);
}
/**
* Selects the menu item corresponding to the given status.
* For status constants we use here the values defined in the
* <tt>PresenceStatus</tt>, but this is only for convenience.
*
* @param status the status to which the item should correspond
*/
private void selectItemFromStatus(int status)
{
String nameToSelect;
if(status < PresenceStatus.ONLINE_THRESHOLD)
{
nameToSelect = GlobalStatusEnum.OFFLINE_STATUS;
}
else if(status < PresenceStatus.EXTENDED_AWAY_THRESHOLD)
{
nameToSelect = GlobalStatusEnum.DO_NOT_DISTURB_STATUS;
}
else if(status < PresenceStatus.AWAY_THRESHOLD)
{
nameToSelect = GlobalStatusEnum.EXTENDED_AWAY_STATUS;
}
else if(status < PresenceStatus.AVAILABLE_THRESHOLD)
{
nameToSelect = GlobalStatusEnum.AWAY_STATUS;
}
else if(status < PresenceStatus.EAGER_TO_COMMUNICATE_THRESHOLD)
{
nameToSelect = GlobalStatusEnum.ONLINE_STATUS;
}
else if(status < PresenceStatus.MAX_STATUS_VALUE)
{
nameToSelect = GlobalStatusEnum.FREE_FOR_CHAT_STATUS;
}
else
{
nameToSelect = GlobalStatusEnum.OFFLINE_STATUS;
}
if(menu instanceof Menu)
{
Menu theMenu = (Menu) menu;
for(int i =0; i < theMenu.getItemCount(); i++)
{
MenuItem item = theMenu.getItem(i);
if(item instanceof CheckboxMenuItem)
{
if(item.getName().equals(nameToSelect))
{
((CheckboxMenuItem)item).setState(true);
}
else
{
((CheckboxMenuItem)item).setState(false);
}
}
}
}
else if(menu instanceof JMenu)
{
JMenu theMenu = (JMenu) menu;
for(int i =0; i < theMenu.getItemCount(); i++)
{
JMenuItem item = theMenu.getItem(i);
if(item instanceof JCheckBoxMenuItem)
{
if(item.getName().equals(nameToSelect))
item.setSelected(true);
else
item.setSelected(false);
}
}
}
}
/**
* Change the status of the protocol according to the menu item selected
*
* @param evt the event containing the menu item name
*/
public void actionPerformed(ActionEvent evt)
{
Object source = evt.getSource();
if(source instanceof JMenuItem)
changeStatusFromName(((JMenuItem)source).getName());
}
/**
* Listens for changes in item state (CheckboxMenuItem)s.
* @param e the event.
*/
public void itemStateChanged(ItemEvent e)
{
Object sourceItem = e.getSource();
if(e.getStateChange() == ItemEvent.SELECTED)
{
if(sourceItem instanceof CheckboxMenuItem)
{
changeStatusFromName(((CheckboxMenuItem)sourceItem).getName());
}
}
else if(e.getStateChange() == ItemEvent.DESELECTED)
{
if(sourceItem instanceof CheckboxMenuItem)
{
((CheckboxMenuItem)sourceItem).setState(true);
}
}
}
/**
* Changes global status from selected item name.
* @param itemName the item name that was selected.
*/
private void changeStatusFromName(String itemName)
{
OsDependentActivator.getGlobalStatusService().publishStatus(
GlobalStatusEnum.getStatusByName(itemName));
}
/**
* 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
* BasicInstantMessaging 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
ServiceReference<?> serviceRef = event.getServiceReference();
if(serviceRef.getBundle().getState() == Bundle.STOPPING)
return;
Object service
= OsDependentActivator.bundleContext.getService(serviceRef);
if (! (service instanceof ProtocolProviderService))
return;
ProtocolProviderService provider = (ProtocolProviderService)service;
switch (event.getType())
{
case ServiceEvent.REGISTERED:
addAccount(provider);
break;
case ServiceEvent.UNREGISTERING:
removeAccount(provider);
break;
}
}
}
}
|