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

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

import javax.swing.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.plugin.desktoputil.*;

/**
 * The dialog created for a given call.
 *
 * @author Yana Stamcheva
 * @author Adam Netocny
 * @author Lyubomir Marinov
 */
public class CallDialog
    extends SIPCommFrame
    implements CallContainer,
               CallTitleListener
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    /**
     * Enabling force minimized mode will always open call dialog minimized.
     * The call dialog still can be shown, but by default it will be minimized.
     */
    private static final String FORCE_MINIMIZED_MODE
        = "net.java.sip.communicator.impl.gui.main.call.FORCE_MINIMIZED_MODE";

    /**
     * Finds a <tt>Container</tt> which is an ancestor of a specific
     * <tt>Component</tt>, has a set <tt>preferredSize</tt> and is closest to
     * the specified <tt>Component</tt> up the ancestor hierarchy.
     *
     * @param component the <tt>Component</tt> whose ancestor hierarchy is to be
     * searched upwards
     * @return a <tt>Container</tt>, if any, which is an ancestor of the
     * specified <tt>component</tt>, has a set <tt>preferredSize</tt> and is
     * closest to the specified <tt>component</tt> up the ancestor hierarchy
     */
    private static Container findClosestAncestorWithSetPreferredSize(
            Component component)
    {
        if ((component instanceof Container) && component.isPreferredSizeSet())
            return (Container) component;
        else
        {
            Container parent;

            while ((parent = component.getParent()) != null)
            {
                if (parent.isPreferredSizeSet())
                    return parent;
                else
                    component = parent;
            }
            return null;
        }
    }

    /**
     * The panel, where all call components are added.
     */
    private CallPanel callPanel;

    private final WindowStateListener windowStateListener
        = new WindowStateListener()
        {
            public void windowStateChanged(WindowEvent ev)
            {
                switch (ev.getID())
                {
                case WindowEvent.WINDOW_DEACTIVATED:
                case WindowEvent.WINDOW_ICONIFIED:
                case WindowEvent.WINDOW_LOST_FOCUS:
                    setFullScreen(false);
                    break;
                }
            }
        };

    /**
     * Creates a <tt>CallDialog</tt> by specifying the underlying call panel.
     */
    public CallDialog()
    {
        super(true, false);

        setMinimumSize(new Dimension(360, 300));
    }

    /**
     * Adds a call panel.
     *
     * @param callPanel the call panel to add to this dialog
     */
    public void addCallPanel(CallPanel callPanel)
    {
        this.callPanel = callPanel;

        getContentPane().add(callPanel);

        callPanel.addCallTitleListener(this);
        setTitle(callPanel.getCallTitle());

        if (!isVisible())
        {
            pack();

            // checks whether we need to open the call dialog in minimized mode
            if(GuiActivator.getConfigurationService()
                    .getBoolean(FORCE_MINIMIZED_MODE, false))
            {
                setState(ICONIFIED);
            }
            setVisible(true);
        }
    }

    /**
     * Called when the title of the given <tt>CallPanel</tt> changes.
     *
     * @param callPanel the <tt>CallPanel</tt>, which title has changed
     */
    public void callTitleChanged(CallPanel callPanel)
    {
        if (this.callPanel.equals(callPanel))
            setTitle(callPanel.getCallTitle());
    }

    /**
     * {@inheritDoc}
     *
     * Hang ups the call/telephony conference depicted by this
     * <tt>CallDialog</tt> on close.
     */
    @Override
    protected void close(boolean escape)
    {
        if (escape)
        {
            /*
             * In full-screen mode, ESC does not close this CallDialog but exits
             * from full-screen to windowed mode.
             */
            if (isFullScreen())
            {
                setFullScreen(false);
                return;
            }
        }
        else
        {
            /*
             * If the window has been closed by clicking the X button or
             * pressing the key combination corresponding to the same button we
             * close the window first and then perform all hang up operations.
             */

            callPanel.disposeCallInfoFrame();
            // We hide the window here. It will be disposed when the call has
            // been ended.
            setVisible(false);
        }

        // Then perform hang up operations.
        callPanel.actionPerformedOnHangupButton(escape);
    }

    /**
     * {@inheritDoc}
     *
     * The delay implemented by <tt>CallDialog</tt> is 5 seconds.
     */
    public void close(final CallPanel callPanel, boolean delay)
    {
        if (this.callPanel.equals(callPanel))
        {
            if (delay)
            {
                Timer timer
                    = new Timer(
                            5000,
                            new ActionListener()
                            {
                                public void actionPerformed(ActionEvent ev)
                                {
                                    dispose();
                                }
                            });

                timer.setRepeats(false);
                timer.start();
            }
            else
            {
                dispose();
            }
        }
    }

    /**
     * {@inheritDoc}
     *
     * <tt>CallDialog</tt> prepares the <tt>CallPanel</tt> it contains for
     * garbage collection.
     */
    @Override
    public void dispose()
    {
        super.dispose();

        /*
         * Technically, CallManager adds/removes the callPanel to/from this
         * instance. It may want to just move it to another CallContainer so it
         * does not sound right that we are disposing of it. But we do not have
         * such a case at this time so try to reduce the risk of memory leaks.
         */
        if (this.callPanel != null)
        {
            callPanel.disposeCallInfoFrame();
            callPanel.dispose();
        }
    }

    /**
     * {@inheritDoc}
     *
     * Attempts to adjust the size of this <tt>Frame</tt> as requested in the
     * AWT event dispatching thread.
     * <p>
     * The method may be executed on the AWT event dispatching thread only
     * because whoever is making the decision to request an adjustment of the
     * Frame size in relation to a AWT Component should be analyzing that same
     * Component in the AWT event dispatching thread only.
     * </p>
     *
     * @throws RuntimeException if the method is not called on the AWT event
     * dispatching thread
     */
    public void ensureSize(Component component, int width, int height)
    {
        CallManager.assertIsEventDispatchingThread();

        Frame frame = CallPeerRendererUtils.getFrame(component);

        if (frame == null)
            return;
        else if ((frame.getExtendedState() & Frame.MAXIMIZED_BOTH)
                == Frame.MAXIMIZED_BOTH)
        {
            /*
             * Forcing the size of a Component which is displayed in a maximized
             * window does not sound like anything we want to do.
             */
            return;
        }
        else if (frame.equals(
                frame.getGraphicsConfiguration().getDevice()
                        .getFullScreenWindow()))
        {
            /*
             * Forcing the size of a Component which is displayed in a
             * full-screen window does not sound like anything we want to do.
             */
            return;
        }
        else if (!frame.equals(this))
        {
            /* This Frame will try to adjust only its own size. */
            return;
        }
        else if ((component.getHeight() >= height)
                && (component.getWidth() >= width))
        {
            /*
             * We will only enlarge the frame size. If the component has already
             * been given at least what it is requesting, do not enlarge the
             * frame size because the whole calculation is prone to inaccuracy.
             */
            return;
        }
        else
        {
            /*
             * If there is no callPanel, it is unlikely that this CallDialog
             * will be asked to ensureSize. Anyway, support the scenario just in
             * case. In light of the absence of a callPanel to guide this
             * CallDialog about the preferred size, we do not have much of a
             * choice but to trust the method arguments.
             */
            if (callPanel != null)
            {
                /*
                 * If there is a callPanel, we are likely to get a much better
                 * estimation about the preferred size by asking the callPanel
                 * rather than by trusting the method arguments. For example,
                 * the visual Component displaying the video streaming from the
                 * local user/peer to the remote peer(s) will think that its
                 * preferred size is the one to base this Frame's size on but
                 * that may be misleading because the local video may not be
                 * displayed with its preferred size even if this Frame's size
                 * will accommodate it.
                 */
                /*
                 * Just asking the callPanel about its preferredSize would've
                 * been terrificly great. Unfortunately, that is presently
                 * futile because the callPanel may have a preferredSize while
                 * we are still required to display visual Components displaying
                 * video in their non-scaled size. The same goes for any
                 * Container which is an ancestor of the specified component.
                 */
                Container ancestor
                    = findClosestAncestorWithSetPreferredSize(component);

                if (ancestor == null)
                    ancestor = callPanel;
                /*
                 * If the ancestor has a forced preferredSize, its LayoutManager
                 * may be able to give a good enough estimation.
                 */
                if (ancestor.isPreferredSizeSet())
                {
                    LayoutManager ancestorLayout = ancestor.getLayout();

                    if (ancestorLayout != null)
                    {
                        Dimension preferredLayoutSize
                            = ancestorLayout.preferredLayoutSize(ancestor);

                        if (preferredLayoutSize != null)
                        {
                            component = ancestor;
                            width = preferredLayoutSize.width;
                            height = preferredLayoutSize.height;
                        }
                    }
                }
                else
                {
                    /*
                     * If the ancestor doesn't have a preferredSize forced, then
                     * we may think that it will calculate an appropriate
                     * preferredSize itself.
                     */
                    Dimension prefSize = ancestor.getPreferredSize();

                    if (prefSize != null)
                    {
                        component = ancestor;
                        width = prefSize.width;
                        height = prefSize.height;
                    }
                }
            }

            /*
             * If the component (which may be an ancestor of the Component
             * specified as an argument to the ensureSize method at this point)
             * has not been given a size, we will make a mistake if we try to
             * use it for the purposes of determining how much this Frame is to
             * be enlarged.
             */
            Dimension componentSize = component.getSize();

            if ((componentSize.width < 1) || (componentSize.height < 1))
                return;

            Dimension frameSize = frame.getSize();
            int newFrameWidth = frameSize.width + width - componentSize.width;
            int newFrameHeight
                = frameSize.height + height - componentSize.height;

            // Respect the minimum size.
            Dimension minSize = frame.getMinimumSize();

            if (newFrameWidth < minSize.width)
                newFrameWidth = minSize.width;
            if (newFrameHeight < minSize.height)
                newFrameHeight = minSize.height;

            // Don't get bigger than the screen.
            Rectangle screenBounds
                = frame.getGraphicsConfiguration().getBounds();

            if (newFrameWidth > screenBounds.width)
                newFrameWidth = screenBounds.width;
            if (newFrameHeight > screenBounds.height)
                newFrameHeight = screenBounds.height;

            /*
             * If we're going to make too small a change, don't even bother.
             * Besides, we don't want some weird recursive resizing.
             * Additionally, do not reduce the Frame size.
             */
            boolean changeWidth = ((newFrameWidth - frameSize.width) > 1);
            boolean changeHeight = ((newFrameHeight - frameSize.height) > 1);

            if (changeWidth || changeHeight)
            {
                if (!changeWidth)
                    newFrameWidth = frameSize.width;
                else if (!changeHeight)
                    newFrameHeight = frameSize.height;

                /*
                 * The latest requirement with respect to the behavior upon
                 * resizing is to center the Frame.
                 */
                int newFrameX
                    = screenBounds.x
                        + (screenBounds.width - newFrameWidth) / 2;
                int newFrameY
                    = screenBounds.y
                        + (screenBounds.height - newFrameHeight) / 2;

                // Do not let the top left go out of the screen.
                if (newFrameX < screenBounds.x)
                    newFrameX = screenBounds.x;
                if (newFrameY < screenBounds.y)
                    newFrameY = screenBounds.y;

                frame.setBounds(
                        newFrameX, newFrameY,
                        newFrameWidth, newFrameHeight);

                /*
                 * Make sure that the component which originally requested the
                 * update to the size of the frame realizes the change as soon
                 * as possible; otherwise, it may request yet another update.
                 */
                if (frame.isDisplayable())
                {
                    if (frame.isValid())
                        frame.doLayout();
                    else
                        frame.validate();
                    frame.repaint();
                }
                else
                    frame.doLayout();
            }
        }
    }

    /**
     * Returns the frame of the call window.
     *
     * @return the frame of the call window
     */
    public JFrame getFrame()
    {
        return this;
    }

    /**
     * Overrides getMinimumSize and checks the minimum size that
     * is needed to display buttons and use it for minimum size if
     * needed.
     * @return minimum size.
     */
    @Override
    public Dimension getMinimumSize()
    {
        Dimension minSize = super.getMinimumSize();

        if(callPanel != null)
        {
            int minButtonWidth = callPanel.getMinimumButtonWidth();

            if(minButtonWidth > minSize.getWidth())
                minSize = new Dimension(minButtonWidth, 300);
        }

        return minSize;
    }

    /**
     * Indicates if the given <tt>callPanel</tt> is currently visible.
     *
     * @param callPanel the <tt>CallPanel</tt>, for which we verify
     * @return <tt>true</tt> if the given call container is visible in this
     * call window, otherwise - <tt>false</tt>
     */
    public boolean isCallVisible(CallPanel callPanel)
    {
        return this.callPanel.equals(callPanel) ? isVisible() : false;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isFullScreen()
    {
        return isFullScreen(getFrame());
    }

    /**
     * Determines whether a specific <tt>Window</tt> is displayed in full-screen
     * mode.
     *
     * @param window the <tt>Window</tt> to be checked whether it is displayed
     * in full-screen mode
     * @return <tt>true</tt> if the specified <tt>window</tt> is displayed in
     * full-screen mode; otherwise, <tt>false</tt>
     */
    public static boolean isFullScreen(Window window)
    {
        GraphicsConfiguration graphicsConfiguration
            = window.getGraphicsConfiguration();

        if (graphicsConfiguration != null)
        {
            GraphicsDevice device = graphicsConfiguration.getDevice();

            if (device != null)
                return window.equals(device.getFullScreenWindow());
        }
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public void setFullScreen(boolean fullScreen)
    {
        GraphicsConfiguration graphicsConfiguration
            = getGraphicsConfiguration();

        if (graphicsConfiguration != null)
        {
            GraphicsDevice device = graphicsConfiguration.getDevice();

            if (device != null)
            {
                boolean thisIsFullScreen = equals(device.getFullScreenWindow());
                boolean firePropertyChange = false;
                boolean setVisible = isVisible();

                try
                {
                    if (fullScreen)
                    {
                        if (!thisIsFullScreen)
                        {
                            /*
                             * XXX The setUndecorated method will only work if
                             * this Window is not displayable.
                             */
                            windowDispose();
                            setUndecorated(true);

                            device.setFullScreenWindow(this);
                            firePropertyChange = true;
                        }
                    }
                    else if (thisIsFullScreen)
                    {
                        /*
                         * XXX The setUndecorated method will only work if this
                         * Window is not displayable.
                         */
                        windowDispose();
                        setUndecorated(false);

                        device.setFullScreenWindow(null);
                        firePropertyChange = true;
                    }

                    if (firePropertyChange)
                    {
                        if (fullScreen)
                        {
                            addWindowStateListener(windowStateListener);

                            /*
                             * If full-screen mode, a black background is the
                             * most common.
                             */
                            getContentPane().setBackground(Color.BLACK);
                        }
                        else
                        {
                            removeWindowStateListener(windowStateListener);

                            /*
                             * In windowed mode, a system-defined background is
                             * the most common.
                             */
                            getContentPane().setBackground(null);
                        }

                        firePropertyChange(
                            PROP_FULL_SCREEN,
                            thisIsFullScreen,
                            fullScreen);
                    }
                }
                finally
                {
                    /*
                     * Regardless of whether this Window successfully entered or
                     * exited full-screen mode, make sure that remains visible.
                     */
                    if (setVisible)
                        setVisible(true);
                }
            }
        }
    }
}