1 /*
   2  * Copyright (c) 2011, 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 
  26 package javafx.embed.swing;
  27 
  28 import java.awt.AWTEvent;
  29 import java.awt.Component;
  30 import java.awt.KeyboardFocusManager;
  31 import java.awt.Window;
  32 import java.awt.event.FocusEvent;
  33 import java.awt.peer.KeyboardFocusManagerPeer;
  34 import java.util.logging.Logger;
  35 
  36 import sun.awt.AppContext;
  37 import sun.awt.CausedFocusEvent;
  38 import sun.awt.CausedFocusEvent.Cause;
  39 import sun.awt.SunToolkit;
  40 
  41 class FXSwingKeyboardFocusManagerPeer implements KeyboardFocusManagerPeer {
  42 
  43     private static Logger log =
  44             Logger.getLogger(FXSwingKeyboardFocusManagerPeer.class.getName());
  45 
  46     private static FXSwingKeyboardFocusManagerPeer instance;
  47 
  48     static FXSwingKeyboardFocusManagerPeer getInstance() {
  49         if (instance == null) {
  50             instance = new FXSwingKeyboardFocusManagerPeer();
  51         }
  52         return instance;
  53     }
  54 
  55     private FXSwingKeyboardFocusManagerPeer() { /* nothing to do */ }
  56 
  57     private Window currentFocusedWindow;
  58     private Component currentFocusOwner;
  59 
  60     /**
  61      * Returns the currently focused window.
  62      *
  63      * @return the currently focused window
  64      *
  65      * @see KeyboardFocusManager#getNativeFocusedWindow()
  66      */
  67     @Override
  68     public Window getCurrentFocusedWindow() {
  69         return currentFocusedWindow;
  70     }
  71 
  72     /**
  73      * Sets the component that should become the focus owner.
  74      *
  75      * @param comp the component to become the focus owner
  76      *
  77      * @see KeyboardFocusManager#setNativeFocusOwner(Component)
  78      */
  79     @Override
  80     public void setCurrentFocusOwner(Component comp) {
  81         currentFocusOwner = comp;
  82         if (currentFocusOwner instanceof ProxyWindow) {
  83             ((ProxyWindow) currentFocusOwner).getProxyView().requestFocus();
  84         }
  85     }
  86 
  87     /**
  88      * Returns the component that currently owns the input focus.
  89      *
  90      * @return the component that currently owns the input focus
  91      *
  92      * @see KeyboardFocusManager#getNativeFocusOwner()
  93      */
  94     @Override
  95     public Component getCurrentFocusOwner() {
  96         return currentFocusOwner;
  97     }
  98 
  99     /**
 100      * Clears the current global focus owner.
 101      *
 102      * @param activeWindow
 103      *
 104      * @see KeyboardFocusManager#clearGlobalFocusOwner()
 105      */
 106     @Override
 107     public void clearGlobalFocusOwner(Window activeWindow) {
 108         // TODO: Implement.
 109     }
 110 
 111     public void setCurrentFocusedWindow(Window w) {
 112         currentFocusedWindow = w;
 113         if (currentFocusedWindow instanceof ProxyWindow) {
 114             ((ProxyWindow) currentFocusedWindow).getProxyView().requestFocus();
 115         }
 116     }
 117 
 118     boolean requestFocus(Component target, Component lightweightChild, boolean temporary,
 119                          boolean focusedWindowChangeAllowed, long time, Cause cause) {
 120 
 121         if (target instanceof ProxyWindow) {
 122             ((ProxyWindow) target).getProxyView().requestFocus();
 123         } else {
 124             log.warning("Cannot handle non-ProxyWindow component");
 125         }
 126         return false;
 127     }
 128 
 129     @SuppressWarnings("restriction")
 130     private void postEvent(AWTEvent ev) {
 131         SunToolkit.postEvent(AppContext.getAppContext(), ev);
 132     }
 133 
 134     void focusGained(ProxyWindow w) {
 135         log.fine("Focus gained: " + w);
 136         @SuppressWarnings("restriction")
 137         FocusEvent fg = new CausedFocusEvent(w, FocusEvent.FOCUS_GAINED, false,
 138                                              currentFocusOwner,
 139                                              Cause.NATIVE_SYSTEM);
 140         postEvent(fg);
 141     }
 142 
 143     void focusLost(ProxyWindow w) {
 144         log.fine("Focus lost: " + w);
 145         @SuppressWarnings("restriction")
 146         FocusEvent fl = new CausedFocusEvent(w, FocusEvent.FOCUS_LOST, false,
 147                                              null, Cause.NATIVE_SYSTEM);
 148         postEvent(fl);
 149     }
 150 
 151 }