1 /*
2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package sun.awt.X11;
26 
27 import java.awt.Component;
28 import java.awt.Window;
29 
30 import sun.awt.AWTAccessor;
31 import java.awt.event.FocusEvent;
32 import sun.awt.KeyboardFocusManagerPeerImpl;
33 import sun.util.logging.PlatformLogger;
34 
35 public class XKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
36     private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.X11.focus.XKeyboardFocusManagerPeer");
37     private static final XKeyboardFocusManagerPeer inst = new XKeyboardFocusManagerPeer();
38 
39     private Component currentFocusOwner;
40     private Window currentFocusedWindow;
41 
getInstance()42     public static XKeyboardFocusManagerPeer getInstance() {
43         return inst;
44     }
45 
XKeyboardFocusManagerPeer()46     private XKeyboardFocusManagerPeer() {
47     }
48 
49     @Override
setCurrentFocusOwner(Component comp)50     public void setCurrentFocusOwner(Component comp) {
51         synchronized (this) {
52             currentFocusOwner = comp;
53         }
54     }
55 
56     @Override
getCurrentFocusOwner()57     public Component getCurrentFocusOwner() {
58         synchronized(this) {
59             return currentFocusOwner;
60         }
61     }
62 
63     @Override
setCurrentFocusedWindow(Window win)64     public void setCurrentFocusedWindow(Window win) {
65         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
66             focusLog.finer("Setting current focused window " + win);
67         }
68 
69         XWindowPeer from = null, to = null;
70 
71         synchronized(this) {
72             if (currentFocusedWindow != null) {
73                 from = AWTAccessor.getComponentAccessor().getPeer(currentFocusedWindow);
74             }
75 
76             currentFocusedWindow = win;
77 
78             if (currentFocusedWindow != null) {
79                 to = AWTAccessor.getComponentAccessor().getPeer(currentFocusedWindow);
80             }
81         }
82 
83         if (from != null) {
84             from.updateSecurityWarningVisibility();
85         }
86         if (to != null) {
87             to.updateSecurityWarningVisibility();
88         }
89     }
90 
91     @Override
getCurrentFocusedWindow()92     public Window getCurrentFocusedWindow() {
93         synchronized(this) {
94             return currentFocusedWindow;
95         }
96     }
97 
98     // TODO: do something to eliminate this forwarding
deliverFocus(Component lightweightChild, Component target, boolean temporary, boolean focusedWindowChangeAllowed, long time, FocusEvent.Cause cause)99     public static boolean deliverFocus(Component lightweightChild,
100                                        Component target,
101                                        boolean temporary,
102                                        boolean focusedWindowChangeAllowed,
103                                        long time,
104                                        FocusEvent.Cause cause)
105     {
106         return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild,
107                                                          target,
108                                                          temporary,
109                                                          focusedWindowChangeAllowed,
110                                                          time,
111                                                          cause,
112                                                          getInstance().getCurrentFocusOwner());
113     }
114 }
115