001: /*
002: * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.awt.X11;
026:
027: import java.awt.Component;
028: import java.awt.KeyboardFocusManager;
029: import java.awt.Window;
030:
031: import java.awt.event.FocusEvent;
032:
033: import java.awt.peer.KeyboardFocusManagerPeer;
034:
035: import java.lang.reflect.InvocationTargetException;
036: import java.lang.reflect.Method;
037:
038: import java.util.logging.Level;
039: import java.util.logging.Logger;
040:
041: import sun.awt.CausedFocusEvent;
042: import sun.awt.SunToolkit;
043:
044: public class XKeyboardFocusManagerPeer implements
045: KeyboardFocusManagerPeer {
046: private static final Logger focusLog = Logger
047: .getLogger("sun.awt.X11.focus.XKeyboardFocusManagerPeer");
048: KeyboardFocusManager manager;
049:
050: XKeyboardFocusManagerPeer(KeyboardFocusManager manager) {
051: this .manager = manager;
052: }
053:
054: private static Object lock = new Object() {
055: };
056: private static Component currentFocusOwner;
057: private static Window currentFocusedWindow;
058:
059: static void setCurrentNativeFocusOwner(Component comp) {
060: if (focusLog.isLoggable(Level.FINER))
061: focusLog
062: .finer("Setting current native focus owner " + comp);
063: synchronized (lock) {
064: currentFocusOwner = comp;
065: }
066: }
067:
068: static void setCurrentNativeFocusedWindow(Window win) {
069: if (focusLog.isLoggable(Level.FINER))
070: focusLog.finer("Setting current native focused window "
071: + win);
072: synchronized (lock) {
073: currentFocusedWindow = win;
074: }
075: }
076:
077: static Component getCurrentNativeFocusOwner() {
078: synchronized (lock) {
079: return currentFocusOwner;
080: }
081: }
082:
083: static Window getCurrentNativeFocusedWindow() {
084: synchronized (lock) {
085: return currentFocusedWindow;
086: }
087: }
088:
089: public Window getCurrentFocusedWindow() {
090: return getCurrentNativeFocusedWindow();
091: }
092:
093: public void setCurrentFocusOwner(Component comp) {
094: setCurrentNativeFocusOwner(comp);
095: }
096:
097: public Component getCurrentFocusOwner() {
098: return getCurrentNativeFocusOwner();
099: }
100:
101: public void clearGlobalFocusOwner(Window activeWindow) {
102: if (activeWindow != null) {
103: Component focusOwner = activeWindow.getFocusOwner();
104: if (focusLog.isLoggable(Level.FINE))
105: focusLog.fine("Clearing global focus owner "
106: + focusOwner);
107: if (focusOwner != null) {
108: XComponentPeer nativePeer = XComponentPeer
109: .getNativeContainer(focusOwner);
110: if (nativePeer != null) {
111: FocusEvent fl = new CausedFocusEvent(
112: focusOwner,
113: FocusEvent.FOCUS_LOST,
114: false,
115: null,
116: CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
117: XWindow.sendEvent(fl);
118: }
119: }
120: }
121: }
122:
123: static boolean simulateMotifRequestFocus(
124: Component lightweightChild, Component target,
125: boolean temporary, boolean focusedWindowChangeAllowed,
126: long time, CausedFocusEvent.Cause cause) {
127: if (lightweightChild == null) {
128: lightweightChild = (Component) target;
129: }
130: Component currentOwner = XKeyboardFocusManagerPeer
131: .getCurrentNativeFocusOwner();
132: if (currentOwner != null && currentOwner.getPeer() == null) {
133: currentOwner = null;
134: }
135: if (focusLog.isLoggable(Level.FINER))
136: focusLog.finer("Simulating transfer from " + currentOwner
137: + " to " + lightweightChild);
138: FocusEvent fg = new CausedFocusEvent(lightweightChild,
139: FocusEvent.FOCUS_GAINED, false, currentOwner, cause);
140: FocusEvent fl = null;
141: if (currentOwner != null) {
142: fl = new CausedFocusEvent(currentOwner,
143: FocusEvent.FOCUS_LOST, false, lightweightChild,
144: cause);
145: }
146:
147: if (fl != null) {
148: XWindow.sendEvent(fl);
149: }
150: XWindow.sendEvent(fg);
151: return true;
152: }
153:
154: static Method shouldNativelyFocusHeavyweightMethod;
155:
156: static int shouldNativelyFocusHeavyweight(Component heavyweight,
157: Component descendant, boolean temporary,
158: boolean focusedWindowChangeAllowed, long time,
159: CausedFocusEvent.Cause cause) {
160: if (shouldNativelyFocusHeavyweightMethod == null) {
161: Class[] arg_types = new Class[] { Component.class,
162: Component.class, Boolean.TYPE, Boolean.TYPE,
163: Long.TYPE, CausedFocusEvent.Cause.class };
164:
165: shouldNativelyFocusHeavyweightMethod = SunToolkit
166: .getMethod(KeyboardFocusManager.class,
167: "shouldNativelyFocusHeavyweight", arg_types);
168: }
169: Object[] args = new Object[] { heavyweight, descendant,
170: Boolean.valueOf(temporary),
171: Boolean.valueOf(focusedWindowChangeAllowed),
172: Long.valueOf(time), cause };
173:
174: int result = XComponentPeer.SNFH_FAILURE;
175: if (shouldNativelyFocusHeavyweightMethod != null) {
176: try {
177: result = ((Integer) shouldNativelyFocusHeavyweightMethod
178: .invoke(null, args)).intValue();
179: } catch (IllegalAccessException e) {
180: assert false;
181: } catch (InvocationTargetException e) {
182: assert false;
183: }
184: }
185:
186: return result;
187: }
188: }
|