aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/call/MainCallPanel.java
blob: f272b5949216f5c6b91ec90bc3d93d02ce96f28e (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
/*
 * 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.call;

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

import javax.swing.*;
import javax.swing.event.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.customcontrols.*;
import net.java.sip.communicator.impl.gui.event.*;
import net.java.sip.communicator.impl.gui.i18n.*;
import net.java.sip.communicator.impl.gui.main.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.service.contactlist.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.gui.Container;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.swing.*;
import net.java.sip.communicator.util.*;

import org.osgi.framework.*;

/**
 * The panel containing the call field and button, serving to make calls.
 * 
 * @author Yana Stamcheva
 */
public class MainCallPanel
    extends TransparentPanel
    implements  ActionListener,
                ListSelectionListener,
                RegistrationStateChangeListener,
                PluginComponentListener
{
    private Logger logger = Logger.getLogger(MainCallPanel.class);

    private static final String CALL_BUTTON = "CallButton";

    private static final String DIAL_BUTTON = "HangupButton";

    private MainFrame mainFrame;

    private ProtocolProviderService protocolProvider;

    private TransparentPanel buttonsPanel
        = new TransparentPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));

    private SIPCommButton callButton = new SIPCommButton(
        ImageLoader.getImage(ImageLoader.CALL_BUTTON_BG),
        ImageLoader.getImage(ImageLoader.CALL_BUTTON_PRESSED_BG),
        null);

    private CallComboBox phoneNumberCombo = new CallComboBox(this);

    private TransparentPanel comboPanel
        = new TransparentPanel(new BorderLayout());

    private JLabel callViaLabel
        = new JLabel(Messages.getI18NString("callVia").getText() + " ");

    private TransparentPanel callViaPanel
        = new TransparentPanel(new FlowLayout(FlowLayout.RIGHT, 0, 4));

    private AccountSelectorBox accountSelectorBox = new AccountSelectorBox(this);

    private SIPCommButton dialButton = new SIPCommButton(
        ImageLoader.getImage(ImageLoader.DIAL_BUTTON));

    private DialpadDialog dialpadDialog;

    private boolean isCallMetaContact = false;

    /**
     * Initializes and constructs this panel.
     */
    public MainCallPanel(MainFrame mainFrame)
    {
        super(new BorderLayout());

        this.mainFrame = mainFrame;

        this.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));

        this.dialpadDialog = new DialpadDialog(this);

        phoneNumberCombo.setEditable(true);

        callButton.setEnabled(false);

        this.add(comboPanel, BorderLayout.NORTH);

        callViaPanel.add(callViaLabel);
        callViaPanel.add(accountSelectorBox);

        buttonsPanel.add(callButton);

        phoneNumberCombo.setOpaque(false);

        comboPanel.add(dialButton, BorderLayout.WEST);
        comboPanel.add(phoneNumberCombo, BorderLayout.CENTER);
        comboPanel.add(buttonsPanel, BorderLayout.EAST);

        callButton.setName(CALL_BUTTON);
        dialButton.setName(DIAL_BUTTON);

        callButton.setToolTipText(
            Messages.getI18NString("call").getText());

        callButton.addActionListener(this);
        dialButton.addActionListener(this);

        this.initPluginComponents();
    }

    /**
     * Handles the <tt>ActionEvent</tt> generated when user presses one of the
     * buttons in this panel.
     */
    public void actionPerformed(ActionEvent evt)
    {
        JButton button = (JButton) evt.getSource();
        String buttonName = button.getName();

        if (buttonName.equals(CALL_BUTTON))
        {
            // call button is pressed when a meta contact is selected
            if (isCallMetaContact)
            {
                Object[] selectedContacts = mainFrame.getContactListPanel()
                    .getContactList().getSelectedValues();

                java.util.List<Contact> telephonyContacts =
                    new Vector<Contact>();

                for (int i = 0; i < selectedContacts.length; i++)
                {
                    Object o = selectedContacts[i];

                    if (o instanceof MetaContact)
                    {
                        Contact contact = ((MetaContact) o)
                                .getDefaultContact(
                                OperationSetBasicTelephony.class);

                        if (contact != null)
                            telephonyContacts.add(contact);
                        else
                        {
                            new ErrorDialog(
                                this.mainFrame,
                                Messages.getI18NString("warning").getText(),
                                Messages.getI18NString(
                                    "contactNotSupportingTelephony",
                                    new String[]
                                    { ((MetaContact) o).getDisplayName() })
                                        .getText())
                                .showDialog();
                        }
                    }
                }

                if (telephonyContacts.size() > 0)
                    CallManager.createCall(protocolProvider, telephonyContacts);
            }
            else if (!phoneNumberCombo.isComboFieldEmpty())
            {
                // if no contact is selected checks if the user has chosen
                // or has
                // writen something in the phone combo box

                String stringContact = phoneNumberCombo.getEditor()
                    .getItem().toString();

                CallManager.createCall(protocolProvider, stringContact);
            }
        }
        else if (buttonName.equals(DIAL_BUTTON))
        {

            if(!dialpadDialog.isVisible())
            {
                dialpadDialog.setSize(
                    mainFrame.getWidth() - 20,
                    dialpadDialog.getHeight());

                dialpadDialog.setLocation(
                    mainFrame.getX() + 10,
                    dialButton.getLocationOnScreen().y
                        - dialpadDialog.getHeight());

                dialpadDialog.setVisible(true);
            }
            else
            {
                dialpadDialog.setVisible(false);
            }
        }
    }

    /**
     * Returns the main application window, which is the parent of this panel.
     * 
     * @return the main application window, which is the parent of this panel.
     */
    public MainFrame getMainFrame()
    {
        return mainFrame;
    }

    /**
     * Sets the protocol provider to be used for making calls.
     * 
     * @param protocolProvider the protocol provider to be used for making calls.
     */
    public ProtocolProviderService getCallProvider()
    {
        return this.protocolProvider;
    }

    /**
     * Sets the protocol provider to be used for making calls.
     * 
     * @param protocolProvider the protocol provider to be used for making calls.
     */
    public void setCallProvider(ProtocolProviderService protocolProvider)
    {
        this.protocolProvider = protocolProvider;
    }

    /**
     * Sets the isCallMetaContact variable to TRUE or FALSE. This defines if
     * this call is a call to a given meta contact selected from the contact
     * list or a call to an external contact or phone number.
     * 
     * @param isCallMetaContact TRUE to define this call as a call to an
     *            internal meta contact and FALSE to define it as a call to an
     *            external contact or phone number.
     */
    public void setCallMetaContact(boolean isCallMetaContact)
    {
        this.isCallMetaContact = isCallMetaContact;
    }

    /**
     * Enables or disabled the call button.
     * 
     * @param isEnabled <code>true</code> to enable the call button and
     * <tt>false</tt> otherwise.
     */
    public void setCallButtonEnabled(boolean isEnabled)
    {
        this.callButton.setEnabled(isEnabled);
    }

    /**
     * Sets the given <tt>phoneNumber</tt> to the phone number combo box.
     * 
     * @param phoneNumber the phone number to set.
     */
    public void setPhoneNumberComboText(String phoneNumber)
    {
        this.phoneNumberCombo.getEditor().setItem(phoneNumber);
    }

    /**
     * Returns the content of the phone number combo box.
     * 
     * @return the content of the phone number combo box.
     */
    public String getPhoneNumberComboText()
    {
        return (String) this.phoneNumberCombo.getEditor().getItem();
    }
    
    /**
     * Requests the focus in the phone number combo box.
     */
    public void requestFocusInPhoneCombo()
    {
        this.phoneNumberCombo.requestFocus();
    }

    /**
     * Adds the given call account to the list of call via accounts.
     * 
     * @param pps the protocol provider service corresponding to the account
     */
    public void addCallAccount(ProtocolProviderService pps)
    {
        if (accountSelectorBox.getAccountsNumber() > 0)
        {
            this.comboPanel.add(callViaPanel, BorderLayout.SOUTH);
        }

        accountSelectorBox.addAccount(pps);

        pps.addRegistrationStateChangeListener(this);
    }

    /**
     * Removes the account corresponding to the given protocol provider from the
     * call via selector box.
     * 
     * @param pps the protocol provider service to remove
     */
    public void removeCallAccount(ProtocolProviderService pps)
    {
        this.accountSelectorBox.removeAccount(pps);

        pps.removeRegistrationStateChangeListener(this);

        if (accountSelectorBox.getAccountsNumber() < 2)
        {
            this.comboPanel.remove(callViaPanel);
        }
    }

    /**
     * Returns TRUE if the account corresponding to the given protocol provider
     * is already contained in the call via selector box, otherwise returns
     * FALSE.
     * 
     * @param pps the protocol provider service for the account
     * @return TRUE if the account corresponding to the given protocol provider
     *         is already contained in the call via selector box, otherwise
     *         returns FALSE
     */
    public boolean containsCallAccount(ProtocolProviderService pps)
    {
        return accountSelectorBox.containsAccount(pps);
    }

    /**
     * Updates the call via account status.
     * 
     * @param pps the protocol provider service for the account
     */
    public void updateCallAccountStatus(ProtocolProviderService pps)
    {
        accountSelectorBox.updateAccountStatus(pps);
    }

    /**
     * Returns the account selector box.
     * 
     * @return the account selector box.
     */
    public AccountSelectorBox getAccountSelectorBox()
    {
        return accountSelectorBox;
    }

    /**
     * Implements ListSelectionListener.valueChanged. Enables or disables call
     * and hangup buttons depending on the selection in the contactlist.
     */
    public void valueChanged(ListSelectionEvent e)
    {
        Object o = mainFrame.getContactListPanel().getContactList()
            .getSelectedValue();

        if ((e.getFirstIndex() != -1 || e.getLastIndex() != -1)
            && (o instanceof MetaContact))
        {
            setCallMetaContact(true);

            // Switch automatically to the appropriate pps in account selector
            // box and enable callButton if telephony is supported.
            Contact contact = ((MetaContact) o)
                    .getDefaultContact(OperationSetBasicTelephony.class);

            if (contact != null)
            {
                callButton.setEnabled(true);

                if(contact.getProtocolProvider().isRegistered())
                    getAccountSelectorBox().
                        setSelected(contact.getProtocolProvider());
            }
            else
            {
                callButton.setEnabled(false);
            }
        }
        else if (phoneNumberCombo.isComboFieldEmpty())
        {
            callButton.setEnabled(false);
        }
    }

    public void registrationStateChanged(RegistrationStateChangeEvent evt)
    {
        ProtocolProviderService protocolProvider = evt.getProvider();

        this.updateCallAccountStatus(protocolProvider);
    }

    private void initPluginComponents()
    {
     // Search for plugin components registered through the OSGI bundle
        // context.
        ServiceReference[] serRefs = null;

        String osgiFilter = "("
            + Container.CONTAINER_ID
            + "="+Container.CONTAINER_CALL_BUTTONS_PANEL.getID()+")";

        try
        {
            serRefs = GuiActivator.bundleContext.getServiceReferences(
                PluginComponent.class.getName(),
                osgiFilter);
        }
        catch (InvalidSyntaxException exc)
        {
            logger.error("Could not obtain plugin reference.", exc);
        }

        if (serRefs != null)
        {
            for (int i = 0; i < serRefs.length; i ++)
            {
                PluginComponent component = (PluginComponent) GuiActivator
                    .bundleContext.getService(serRefs[i]);;

                    Object selectedValue = mainFrame.getContactListPanel()
                    .getContactList().getSelectedValue();

                if(selectedValue instanceof MetaContact)
                {
                    component.setCurrentContact((MetaContact)selectedValue);
                }
                else if(selectedValue instanceof MetaContactGroup)
                {
                    component
                        .setCurrentContactGroup((MetaContactGroup)selectedValue);
                }

                Object constraints = null;

                if (component.getConstraints() != null)
                    constraints = UIServiceImpl
                        .getBorderLayoutConstraintsFromContainer(
                            component.getConstraints());
                else
                    constraints = BorderLayout.SOUTH;

                this.buttonsPanel.add(
                    (Component)component.getComponent(), constraints);

                this.repaint();
            }
        }

        GuiActivator.getUIService().addPluginComponentListener(this);
    }

    public void pluginComponentAdded(PluginComponentEvent event)
    {
        PluginComponent pluginComponent = event.getPluginComponent();

        // If the container id doesn't correspond to the id of the plugin
        // container we're not interested.
        if(!pluginComponent.getContainer()
                .equals(Container.CONTAINER_CALL_BUTTONS_PANEL))
            return;

        Object constraints = UIServiceImpl
            .getBorderLayoutConstraintsFromContainer(
                    pluginComponent.getConstraints());

        if (constraints == null)
            constraints = BorderLayout.SOUTH;

        this.buttonsPanel.add(
            (Component) pluginComponent.getComponent(), constraints);

        Object selectedValue = mainFrame.getContactListPanel()
                .getContactList().getSelectedValue();

        if(selectedValue instanceof MetaContact)
        {
            pluginComponent
                .setCurrentContact((MetaContact)selectedValue);
        }
        else if(selectedValue instanceof MetaContactGroup)
        {
            pluginComponent
                .setCurrentContactGroup((MetaContactGroup)selectedValue);
        }

        this.revalidate();
        this.repaint();
    }

    public void pluginComponentRemoved(PluginComponentEvent event)
    {
        PluginComponent c = event.getPluginComponent();

        // If the container id doesn't correspond to the id of the plugin
        // container we're not interested.
        if(!c.getContainer()
                .equals(Container.CONTAINER_CALL_BUTTONS_PANEL))
            return;

        this.buttonsPanel.remove((Component) c.getComponent());
    }
}