aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/desktoputil/SIPCommPopupMenu.java
blob: 7e6713f08828f3551bd0acc2996cd8d3631ce37f (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
/*
 * 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.desktoputil;

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

import javax.swing.*;

/**
 * A custom popup menu that detects parent focus lost.
 *
 * @author Yana Stamcheva
 */
public class SIPCommPopupMenu
    extends JPopupMenu
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    /**
     * Constructor.
     */
    public SIPCommPopupMenu()
    {
        // Hides the popup menu when the parent window loses focus.
        addComponentListener(new ComponentAdapter()
        {
            @Override
            public void componentResized(ComponentEvent evt)
            {
                final Window parentWindow;

                Component parent = getParent();

                // If this is a submenu get the invoker first.
                if (parent instanceof JPopupMenu)
                    parentWindow = SwingUtilities.getWindowAncestor(
                        ((JPopupMenu) parent).getInvoker());
                else
                    parentWindow
                        = SwingUtilities.getWindowAncestor(getInvoker());

                if (parentWindow != null)
                {
                    if (!parentWindow.isActive())
                        setVisible(false);

                    parentWindow.addWindowListener(new WindowAdapter()
                    {
                        @Override
                        public void windowDeactivated(WindowEvent e)
                        {
                            if (SIPCommPopupMenu.this != null
                                    && SIPCommPopupMenu.this.isVisible())
                                SIPCommPopupMenu.this.setVisible(false);
                        }

                        /**
                         * Invoked when a window has been closed.
                         * Remove the listener as we do not need it any more.
                         */
                        @Override
                        public void windowClosed(WindowEvent e)
                        {
                            parentWindow.removeWindowListener(this);
                        }
                    });
                }
            }
        });
    }
}