jdk/src/solaris/classes/sun/awt/X11/XKeyboardFocusManagerPeer.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 441 f5da1014ed23
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.KeyboardFocusManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.FocusEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.peer.KeyboardFocusManagerPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.CausedFocusEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class XKeyboardFocusManagerPeer implements KeyboardFocusManagerPeer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final Logger focusLog = Logger.getLogger("sun.awt.X11.focus.XKeyboardFocusManagerPeer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    KeyboardFocusManager manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    XKeyboardFocusManagerPeer(KeyboardFocusManager manager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        this.manager = manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static Object lock = new Object() {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static Component currentFocusOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static Window currentFocusedWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static void setCurrentNativeFocusOwner(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Setting current native focus owner " + comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            currentFocusOwner = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static void setCurrentNativeFocusedWindow(Window win) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Setting current native focused window " + win);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            currentFocusedWindow = win;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static Component getCurrentNativeFocusOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            return currentFocusOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static Window getCurrentNativeFocusedWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return currentFocusedWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public Window getCurrentFocusedWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return getCurrentNativeFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public void setCurrentFocusOwner(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        setCurrentNativeFocusOwner(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public Component getCurrentFocusOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return getCurrentNativeFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void clearGlobalFocusOwner(Window activeWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         if (activeWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            Component focusOwner = activeWindow.getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (focusLog.isLoggable(Level.FINE)) focusLog.fine("Clearing global focus owner " + focusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (focusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                XComponentPeer nativePeer = XComponentPeer.getNativeContainer(focusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                if (nativePeer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                                         CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    XWindow.sendEvent(fl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static boolean simulateMotifRequestFocus(Component lightweightChild, Component target, boolean temporary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                      boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (lightweightChild == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            lightweightChild = (Component)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        Component currentOwner = XKeyboardFocusManagerPeer.getCurrentNativeFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (currentOwner != null && currentOwner.getPeer() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            currentOwner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Simulating transfer from " + currentOwner + " to " + lightweightChild);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        FocusEvent  fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED, false, currentOwner, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        FocusEvent fl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (currentOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST, false, lightweightChild, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (fl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            XWindow.sendEvent(fl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        XWindow.sendEvent(fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    static Method shouldNativelyFocusHeavyweightMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static int shouldNativelyFocusHeavyweight(Component heavyweight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         Component descendant, boolean temporary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (shouldNativelyFocusHeavyweightMethod == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            Class[] arg_types =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                new Class[] { Component.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                              Component.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                              Boolean.TYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                              Boolean.TYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                              Long.TYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                              CausedFocusEvent.Cause.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            shouldNativelyFocusHeavyweightMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                SunToolkit.getMethod(KeyboardFocusManager.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                   "shouldNativelyFocusHeavyweight",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                   arg_types);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Object[] args = new Object[] { heavyweight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                       descendant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                       Boolean.valueOf(temporary),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                       Boolean.valueOf(focusedWindowChangeAllowed),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                       Long.valueOf(time), cause};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int result = XComponentPeer.SNFH_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (shouldNativelyFocusHeavyweightMethod != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                result = ((Integer) shouldNativelyFocusHeavyweightMethod.invoke(null, args)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}