J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package sun.awt.X11; |
| 26 | |
| 27 | import java.awt.Component; |
| 28 | import java.awt.KeyboardFocusManager; |
| 29 | import java.awt.Window; |
| 30 | |
| 31 | import java.awt.event.FocusEvent; |
| 32 | |
| 33 | import java.awt.peer.KeyboardFocusManagerPeer; |
| 34 | |
| 35 | import java.lang.reflect.InvocationTargetException; |
| 36 | import java.lang.reflect.Method; |
| 37 | |
| 38 | import java.util.logging.Level; |
| 39 | import java.util.logging.Logger; |
| 40 | |
| 41 | import sun.awt.CausedFocusEvent; |
| 42 | import sun.awt.SunToolkit; |
| 43 | |
| 44 | public class XKeyboardFocusManagerPeer implements KeyboardFocusManagerPeer { |
| 45 | private static final Logger focusLog = Logger.getLogger("sun.awt.X11.focus.XKeyboardFocusManagerPeer"); |
| 46 | KeyboardFocusManager manager; |
| 47 | |
| 48 | XKeyboardFocusManagerPeer(KeyboardFocusManager manager) { |
| 49 | this.manager = manager; |
| 50 | } |
| 51 | |
| 52 | private static Object lock = new Object() {}; |
| 53 | private static Component currentFocusOwner; |
| 54 | private static Window currentFocusedWindow; |
| 55 | |
| 56 | static void setCurrentNativeFocusOwner(Component comp) { |
| 57 | if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Setting current native focus owner " + comp); |
| 58 | synchronized(lock) { |
| 59 | currentFocusOwner = comp; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static void setCurrentNativeFocusedWindow(Window win) { |
| 64 | if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Setting current native focused window " + win); |
| 65 | synchronized(lock) { |
| 66 | currentFocusedWindow = win; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static Component getCurrentNativeFocusOwner() { |
| 71 | synchronized(lock) { |
| 72 | return currentFocusOwner; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static Window getCurrentNativeFocusedWindow() { |
| 77 | synchronized(lock) { |
| 78 | return currentFocusedWindow; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public Window getCurrentFocusedWindow() { |
| 83 | return getCurrentNativeFocusedWindow(); |
| 84 | } |
| 85 | |
| 86 | public void setCurrentFocusOwner(Component comp) { |
| 87 | setCurrentNativeFocusOwner(comp); |
| 88 | } |
| 89 | |
| 90 | public Component getCurrentFocusOwner() { |
| 91 | return getCurrentNativeFocusOwner(); |
| 92 | } |
| 93 | |
| 94 | public void clearGlobalFocusOwner(Window activeWindow) { |
| 95 | if (activeWindow != null) { |
| 96 | Component focusOwner = activeWindow.getFocusOwner(); |
| 97 | if (focusLog.isLoggable(Level.FINE)) focusLog.fine("Clearing global focus owner " + focusOwner); |
| 98 | if (focusOwner != null) { |
| 99 | XComponentPeer nativePeer = XComponentPeer.getNativeContainer(focusOwner); |
| 100 | if (nativePeer != null) { |
| 101 | FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null, |
| 102 | CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER); |
| 103 | XWindow.sendEvent(fl); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static boolean simulateMotifRequestFocus(Component lightweightChild, Component target, boolean temporary, |
| 110 | boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause) |
| 111 | { |
| 112 | if (lightweightChild == null) { |
| 113 | lightweightChild = (Component)target; |
| 114 | } |
| 115 | Component currentOwner = XKeyboardFocusManagerPeer.getCurrentNativeFocusOwner(); |
| 116 | if (currentOwner != null && currentOwner.getPeer() == null) { |
| 117 | currentOwner = null; |
| 118 | } |
| 119 | if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Simulating transfer from " + currentOwner + " to " + lightweightChild); |
| 120 | FocusEvent fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED, false, currentOwner, cause); |
| 121 | FocusEvent fl = null; |
| 122 | if (currentOwner != null) { |
| 123 | fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST, false, lightweightChild, cause); |
| 124 | } |
| 125 | |
| 126 | if (fl != null) { |
| 127 | XWindow.sendEvent(fl); |
| 128 | } |
| 129 | XWindow.sendEvent(fg); |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | static Method shouldNativelyFocusHeavyweightMethod; |
| 134 | |
| 135 | static int shouldNativelyFocusHeavyweight(Component heavyweight, |
| 136 | Component descendant, boolean temporary, |
| 137 | boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause) |
| 138 | { |
| 139 | if (shouldNativelyFocusHeavyweightMethod == null) { |
| 140 | Class[] arg_types = |
| 141 | new Class[] { Component.class, |
| 142 | Component.class, |
| 143 | Boolean.TYPE, |
| 144 | Boolean.TYPE, |
| 145 | Long.TYPE, |
| 146 | CausedFocusEvent.Cause.class |
| 147 | }; |
| 148 | |
| 149 | shouldNativelyFocusHeavyweightMethod = |
| 150 | SunToolkit.getMethod(KeyboardFocusManager.class, |
| 151 | "shouldNativelyFocusHeavyweight", |
| 152 | arg_types); |
| 153 | } |
| 154 | Object[] args = new Object[] { heavyweight, |
| 155 | descendant, |
| 156 | Boolean.valueOf(temporary), |
| 157 | Boolean.valueOf(focusedWindowChangeAllowed), |
| 158 | Long.valueOf(time), cause}; |
| 159 | |
| 160 | int result = XComponentPeer.SNFH_FAILURE; |
| 161 | if (shouldNativelyFocusHeavyweightMethod != null) { |
| 162 | try { |
| 163 | result = ((Integer) shouldNativelyFocusHeavyweightMethod.invoke(null, args)).intValue(); |
| 164 | } |
| 165 | catch (IllegalAccessException e) { |
| 166 | assert false; |
| 167 | } |
| 168 | catch (InvocationTargetException e) { |
| 169 | assert false; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return result; |
| 174 | } |
| 175 | } |