src/java.desktop/share/classes/sun/awt/KeyboardFocusManagerPeerImpl.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2013, 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;
  26 
  27 import java.awt.Component;
  28 import java.awt.KeyboardFocusManager;
  29 import java.awt.Window;
  30 import java.awt.Canvas;
  31 import java.awt.Scrollbar;
  32 import java.awt.Panel;
  33 
  34 import java.awt.event.FocusEvent;
  35 
  36 import java.awt.peer.KeyboardFocusManagerPeer;
  37 import java.awt.peer.ComponentPeer;
  38 
  39 import java.lang.reflect.InvocationTargetException;
  40 import java.lang.reflect.Method;
  41 
  42 import sun.util.logging.PlatformLogger;
  43 
  44 public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManagerPeer {
  45 
  46     private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.focus.KeyboardFocusManagerPeerImpl");
  47 
  48     private static AWTAccessor.KeyboardFocusManagerAccessor kfmAccessor =
  49         AWTAccessor.getKeyboardFocusManagerAccessor();
  50 
  51     // The constants are copied from java.awt.KeyboardFocusManager
  52     public static final int SNFH_FAILURE         = 0;
  53     public static final int SNFH_SUCCESS_HANDLED = 1;
  54     public static final int SNFH_SUCCESS_PROCEED = 2;
  55 
  56     @Override
  57     public void clearGlobalFocusOwner(Window activeWindow) {
  58         if (activeWindow != null) {
  59             Component focusOwner = activeWindow.getFocusOwner();
  60             if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
  61                 focusLog.fine("Clearing global focus owner " + focusOwner);
  62             }
  63             if (focusOwner != null) {
  64                 FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null,
  65                                                      CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
  66                 SunToolkit.postPriorityEvent(fl);
  67             }
  68         }
  69     }
  70 
  71     /*
  72      * WARNING: Don't call it on the Toolkit thread.
  73      *
  74      * Checks if the component:
  75      * 1) accepts focus on click (in general)
  76      * 2) may be a focus owner (in particular)
  77      */
  78     @SuppressWarnings("deprecation")
  79     public static boolean shouldFocusOnClick(Component component) {
  80         boolean acceptFocusOnClick = false;
  81 
  82         // A component is generally allowed to accept focus on click
  83         // if its peer is focusable. There're some exceptions though.
  84 
  85 
  86         // CANVAS & SCROLLBAR accept focus on click

  87         if (component instanceof Canvas ||
  88             component instanceof Scrollbar)
  89         {
  90             acceptFocusOnClick = true;
  91 
  92         // PANEL, empty only, accepts focus on click
  93         } else if (component instanceof Panel) {
  94             acceptFocusOnClick = (((Panel)component).getComponentCount() == 0);
  95 
  96 
  97         // Other components
  98         } else {
  99             ComponentPeer peer = (component != null ? component.getPeer() : null);
 100             acceptFocusOnClick = (peer != null ? peer.isFocusable() : false);
 101         }
 102         return acceptFocusOnClick &&
 103                AWTAccessor.getComponentAccessor().canBeFocusOwner(component);
 104     }
 105 
 106     /*
 107      * Posts proper lost/gain focus events to the event queue.
 108      */
 109     @SuppressWarnings("deprecation")
 110     public static boolean deliverFocus(Component lightweightChild,
 111                                        Component target,
 112                                        boolean temporary,
 113                                        boolean focusedWindowChangeAllowed,
 114                                        long time,
 115                                        CausedFocusEvent.Cause cause,
 116                                        Component currentFocusOwner) // provided by the descendant peers
 117     {
 118         if (lightweightChild == null) {
 119             lightweightChild = target;
 120         }
 121 
 122         Component currentOwner = currentFocusOwner;
 123         if (currentOwner != null && currentOwner.getPeer() == null) {
 124             currentOwner = null;
 125         }
 126         if (currentOwner != null) {
 127             FocusEvent fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST,
 128                                                  false, lightweightChild, cause);
 129 
 130             if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
 131                 focusLog.finer("Posting focus event: " + fl);
 132             }
 133             SunToolkit.postEvent(SunToolkit.targetToAppContext(currentOwner), fl);
 134         }
 135 
 136         FocusEvent fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED,
 137                                              false, currentOwner, cause);
 138 
 139         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
 140             focusLog.finer("Posting focus event: " + fg);
 141         }
 142         SunToolkit.postEvent(SunToolkit.targetToAppContext(lightweightChild), fg);
 143         return true;


   1 /*
   2  * Copyright (c) 2003, 2015, 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;
  26 
  27 import java.awt.Component;

  28 import java.awt.Window;
  29 import java.awt.Canvas;
  30 import java.awt.Scrollbar;
  31 import java.awt.Panel;
  32 
  33 import java.awt.event.FocusEvent;
  34 
  35 import java.awt.peer.KeyboardFocusManagerPeer;
  36 import java.awt.peer.ComponentPeer;
  37 
  38 import sun.awt.AWTAccessor.ComponentAccessor;


  39 import sun.util.logging.PlatformLogger;
  40 
  41 public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManagerPeer {
  42 
  43     private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.focus.KeyboardFocusManagerPeerImpl");
  44 
  45     private static AWTAccessor.KeyboardFocusManagerAccessor kfmAccessor =
  46         AWTAccessor.getKeyboardFocusManagerAccessor();
  47 
  48     // The constants are copied from java.awt.KeyboardFocusManager
  49     public static final int SNFH_FAILURE         = 0;
  50     public static final int SNFH_SUCCESS_HANDLED = 1;
  51     public static final int SNFH_SUCCESS_PROCEED = 2;
  52 
  53     @Override
  54     public void clearGlobalFocusOwner(Window activeWindow) {
  55         if (activeWindow != null) {
  56             Component focusOwner = activeWindow.getFocusOwner();
  57             if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
  58                 focusLog.fine("Clearing global focus owner " + focusOwner);
  59             }
  60             if (focusOwner != null) {
  61                 FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null,
  62                                                      CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
  63                 SunToolkit.postPriorityEvent(fl);
  64             }
  65         }
  66     }
  67 
  68     /*
  69      * WARNING: Don't call it on the Toolkit thread.
  70      *
  71      * Checks if the component:
  72      * 1) accepts focus on click (in general)
  73      * 2) may be a focus owner (in particular)
  74      */

  75     public static boolean shouldFocusOnClick(Component component) {
  76         boolean acceptFocusOnClick = false;
  77 
  78         // A component is generally allowed to accept focus on click
  79         // if its peer is focusable. There're some exceptions though.
  80 
  81 
  82         // CANVAS & SCROLLBAR accept focus on click
  83         final ComponentAccessor acc = AWTAccessor.getComponentAccessor();
  84         if (component instanceof Canvas ||
  85             component instanceof Scrollbar)
  86         {
  87             acceptFocusOnClick = true;
  88 
  89         // PANEL, empty only, accepts focus on click
  90         } else if (component instanceof Panel) {
  91             acceptFocusOnClick = (((Panel)component).getComponentCount() == 0);
  92 
  93 
  94         // Other components
  95         } else {
  96             ComponentPeer peer = (component != null ? acc.getPeer(component) : null);
  97             acceptFocusOnClick = (peer != null ? peer.isFocusable() : false);
  98         }
  99         return acceptFocusOnClick && acc.canBeFocusOwner(component);

 100     }
 101 
 102     /*
 103      * Posts proper lost/gain focus events to the event queue.
 104      */
 105     @SuppressWarnings("deprecation")
 106     public static boolean deliverFocus(Component lightweightChild,
 107                                        Component target,
 108                                        boolean temporary,
 109                                        boolean focusedWindowChangeAllowed,
 110                                        long time,
 111                                        CausedFocusEvent.Cause cause,
 112                                        Component currentFocusOwner) // provided by the descendant peers
 113     {
 114         if (lightweightChild == null) {
 115             lightweightChild = target;
 116         }
 117 
 118         Component currentOwner = currentFocusOwner;
 119         if (currentOwner != null && !currentOwner.isDisplayable()) {
 120             currentOwner = null;
 121         }
 122         if (currentOwner != null) {
 123             FocusEvent fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST,
 124                                                  false, lightweightChild, cause);
 125 
 126             if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
 127                 focusLog.finer("Posting focus event: " + fl);
 128             }
 129             SunToolkit.postEvent(SunToolkit.targetToAppContext(currentOwner), fl);
 130         }
 131 
 132         FocusEvent fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED,
 133                                              false, currentOwner, cause);
 134 
 135         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
 136             focusLog.finer("Posting focus event: " + fg);
 137         }
 138         SunToolkit.postEvent(SunToolkit.targetToAppContext(lightweightChild), fg);
 139         return true;