1 /*
   2  * Copyright (c) 2003, 2008, 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 import sun.util.logging.PlatformLogger;
  30 import sun.awt.CausedFocusEvent;
  31 import sun.awt.KeyboardFocusManagerPeerImpl;
  32 
  33 public class XKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
  34     private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.X11.focus.XKeyboardFocusManagerPeer");
  35     private static final XKeyboardFocusManagerPeer inst = new XKeyboardFocusManagerPeer();
  36 
  37     private Component currentFocusOwner;
  38     private Window currentFocusedWindow;
  39 
  40     public static XKeyboardFocusManagerPeer getInstance() {
  41         return inst;
  42     }
  43 
  44     private XKeyboardFocusManagerPeer() {
  45     }
  46 
  47     @Override
  48     public void setCurrentFocusOwner(Component comp) {
  49         synchronized (this) {
  50             currentFocusOwner = comp;
  51         }
  52     }
  53 
  54     @Override
  55     public Component getCurrentFocusOwner() {
  56         synchronized(this) {
  57             return currentFocusOwner;
  58         }
  59     }
  60 
  61     @Override
  62     public void setCurrentFocusedWindow(Window win) {
  63         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
  64             focusLog.finer("Setting current focused window " + win);
  65         }
  66 
  67         XWindowPeer from = null, to = null;
  68 
  69         synchronized(this) {
  70             if (currentFocusedWindow != null) {
  71                 from = (XWindowPeer)currentFocusedWindow.getPeer();
  72             }
  73 
  74             currentFocusedWindow = win;
  75 
  76             if (currentFocusedWindow != null) {
  77                 to = (XWindowPeer)currentFocusedWindow.getPeer();
  78             }
  79         }
  80 
  81         if (from != null) {
  82             from.updateSecurityWarningVisibility();
  83         }
  84         if (to != null) {
  85             to.updateSecurityWarningVisibility();
  86         }
  87     }
  88 
  89     @Override
  90     public Window getCurrentFocusedWindow() {
  91         synchronized(this) {
  92             return currentFocusedWindow;
  93         }
  94     }
  95 
  96     // TODO: do something to eliminate this forwarding
  97     public static boolean deliverFocus(Component lightweightChild,
  98                                        Component target,
  99                                        boolean temporary,
 100                                        boolean focusedWindowChangeAllowed,
 101                                        long time,
 102                                        CausedFocusEvent.Cause cause)
 103     {
 104         return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild,
 105                                                          target,
 106                                                          temporary,
 107                                                          focusedWindowChangeAllowed,
 108                                                          time,
 109                                                          cause,
 110                                                          getInstance().getCurrentFocusOwner());
 111     }
 112 }