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.Component;
  29 import java.awt.KeyboardFocusManager;
  30 import java.lang.reflect.InvocationTargetException;
  31 import java.lang.reflect.Method;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedActionException;
  34 import java.security.PrivilegedExceptionAction;
  35 
  36 import sun.awt.CausedFocusEvent;
  37 
  38 class KFMHelper {
  39 
  40     static final int SNFH_FAILURE = 0;
  41     static final int SNFH_SUCCESS_HANDLED = 1;
  42     static final int SNFH_SUCCESS_PROCEED = 2;
  43 
  44     private static Method processSynchronousLightweightTransferMethod;
  45 
  46     private static Method shouldNativelyFocusHeavyweightMethod;
  47 
  48     static boolean processSynchronousLightweightTransfer(Component target,
  49                                                          Component lightweightChild,
  50                                                          boolean temporary,
  51                                                          boolean focusedWindowChangeAllowed,
  52                                                          long time)
  53     {
  54         try {
  55             if (processSynchronousLightweightTransferMethod == null) {
  56                 processSynchronousLightweightTransferMethod = AccessController
  57                         .doPrivileged(new PrivilegedExceptionAction<Method>() {
  58                             public Method run() throws IllegalAccessException,
  59                                     NoSuchMethodException {
  60                                 Method m = KeyboardFocusManager.class
  61                                         .getDeclaredMethod(
  62                                                 "processSynchronousLightweightTransfer",
  63                                                 new Class[] { Component.class,
  64                                                         Component.class,
  65                                                         Boolean.TYPE,
  66                                                         Boolean.TYPE, Long.TYPE });
  67                                 m.setAccessible(true);
  68                                 return m;
  69                             }
  70                         });
  71             }
  72             Object[] params = new Object[] { target, lightweightChild,
  73                     Boolean.valueOf(temporary),
  74                     Boolean.valueOf(focusedWindowChangeAllowed),
  75                     Long.valueOf(time) };
  76             return ((Boolean) processSynchronousLightweightTransferMethod
  77                     .invoke(null, params)).booleanValue();
  78         } catch (PrivilegedActionException pae) {
  79             pae.printStackTrace();
  80             return false;
  81         } catch (IllegalAccessException iae) {
  82             iae.printStackTrace();
  83             return false;
  84         } catch (IllegalArgumentException iaee) {
  85             iaee.printStackTrace();
  86             return false;
  87         } catch (InvocationTargetException ite) {
  88             ite.printStackTrace();
  89             return false;
  90         }
  91     }
  92 
  93     static int shouldNativelyFocusHeavyweight(Component heavyweight,
  94                                               Component descendant,
  95                                               boolean temporary,
  96                                               boolean focusedWindowChangeAllowed,
  97                                               long time,
  98                                               CausedFocusEvent.Cause cause)
  99     {
 100         if (shouldNativelyFocusHeavyweightMethod == null) {
 101             Class<?>[] arg_types = new Class<?>[] {
 102                     Component.class, Component.class,
 103                     Boolean.TYPE, Boolean.TYPE, Long.TYPE,
 104                     CausedFocusEvent.Cause.class
 105             };
 106 
 107             try {
 108                 shouldNativelyFocusHeavyweightMethod = KeyboardFocusManager.class
 109                         .getDeclaredMethod("shouldNativelyFocusHeavyweight",
 110                                 arg_types);
 111                 shouldNativelyFocusHeavyweightMethod.setAccessible(true);
 112                 
 113             } catch (NoSuchMethodException e) {
 114                 e.printStackTrace();
 115             } catch (SecurityException e) {
 116                 e.printStackTrace();
 117             }
 118         }
 119         Object[] args = new Object[] {
 120                 heavyweight, descendant,
 121                 Boolean.valueOf(temporary),
 122                 Boolean.valueOf(focusedWindowChangeAllowed),
 123                 Long.valueOf(time), cause
 124         };
 125 
 126         int result = SNFH_FAILURE;
 127         if (shouldNativelyFocusHeavyweightMethod != null) {
 128             try {
 129                 result = ((Integer)
 130                         shouldNativelyFocusHeavyweightMethod.invoke(null, args)).intValue();
 131             } catch (IllegalAccessException e) {
 132                 assert false;
 133                 
 134             } catch (InvocationTargetException e) {
 135                 assert false;
 136             }
 137         }
 138 
 139         return result;
 140     }
 141 
 142 }