Blame src/libbluray/bdj/java-j2se/java/awt/peer/BDKeyboardFocusManagerPeer.java

Packit 5e46da
/*
Packit 5e46da
 * This file is part of libbluray
Packit 5e46da
 * Copyright (C) 2012  Petri Hintukainen <phintuka@users.sourceforge.net>
Packit 5e46da
 *
Packit 5e46da
 * This library is free software; you can redistribute it and/or
Packit 5e46da
 * modify it under the terms of the GNU Lesser General Public
Packit 5e46da
 * License as published by the Free Software Foundation; either
Packit 5e46da
 * version 2.1 of the License, or (at your option) any later version.
Packit 5e46da
 *
Packit 5e46da
 * This library is distributed in the hope that it will be useful,
Packit 5e46da
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5e46da
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 5e46da
 * Lesser General Public License for more details.
Packit 5e46da
 *
Packit 5e46da
 * You should have received a copy of the GNU Lesser General Public
Packit 5e46da
 * License along with this library. If not, see
Packit 5e46da
 * <http://www.gnu.org/licenses/>.
Packit 5e46da
 */
Packit 5e46da
Packit 5e46da
package java.awt.peer;
Packit 5e46da
Packit 5e46da
import java.awt.Component;
Packit 5e46da
import java.awt.KeyboardFocusManager;
Packit 5e46da
import java.awt.Window;
Packit 5e46da
import java.lang.reflect.Field;
Packit 5e46da
Packit 5e46da
public class BDKeyboardFocusManagerPeer implements KeyboardFocusManagerPeer {
Packit 5e46da
    private static boolean java7 = false;
Packit 5e46da
    static BDKeyboardFocusManagerPeer instance = null;
Packit 5e46da
Packit 5e46da
    /* used in java 7 only */
Packit 5e46da
    public static KeyboardFocusManagerPeer getInstance() {
Packit 5e46da
        java7 = true;
Packit 5e46da
Packit 5e46da
        if (instance == null) {
Packit 5e46da
            instance = new BDKeyboardFocusManagerPeer();
Packit 5e46da
        }
Packit 5e46da
        return instance;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public static void shutdown()
Packit 5e46da
    {
Packit 5e46da
        if (instance != null) {
Packit 5e46da
            instance.dispose();
Packit 5e46da
            instance = null;
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public static void init(Window window)
Packit 5e46da
    {
Packit 5e46da
        /* running in java 7 ? */
Packit 5e46da
        if (java7 == true)
Packit 5e46da
            return;
Packit 5e46da
Packit 5e46da
        if (instance == null)
Packit 5e46da
            instance = new BDKeyboardFocusManagerPeer();
Packit 5e46da
        instance.focusOwner = null;
Packit 5e46da
        instance.window = window;
Packit 5e46da
Packit 5e46da
        /* replace default keyboard focus manager peer */
Packit 5e46da
        Field kbPeer;
Packit 5e46da
        try {
Packit 5e46da
            Class c = Class.forName("java.awt.KeyboardFocusManager");
Packit 5e46da
            kbPeer = c.getDeclaredField("peer");
Packit 5e46da
            kbPeer.setAccessible(true);
Packit 5e46da
        } catch (ClassNotFoundException e) {
Packit 5e46da
            throw new Error("java.awt.KeyboardFocusManager not found");
Packit 5e46da
        } catch (SecurityException e) {
Packit 5e46da
            throw new Error("java.awt.KeyboardFocusManager not accessible");
Packit 5e46da
        } catch (NoSuchFieldException e) {
Packit 5e46da
            throw new Error("java.awt.KeyboardFocusManager.peer not found");
Packit 5e46da
        }
Packit 5e46da
        try {
Packit 5e46da
            kbPeer.set(KeyboardFocusManager.getCurrentKeyboardFocusManager(),
Packit 5e46da
                       instance);
Packit 5e46da
        } catch (java.lang.IllegalAccessException e) {
Packit 5e46da
            throw new Error("java.awt.KeyboardFocusManager.peer not accessible:" + e);
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    private Component focusOwner = null;
Packit 5e46da
    private Window window = null; /* used in java 6 only */
Packit 5e46da
    private boolean disposed = false;
Packit 5e46da
Packit 5e46da
    public void dispose()
Packit 5e46da
    {
Packit 5e46da
        focusOwner = null;
Packit 5e46da
        window = null;
Packit 5e46da
        disposed = true;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    private BDKeyboardFocusManagerPeer() {
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public void clearGlobalFocusOwner(Window w) {
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public Component getCurrentFocusOwner() {
Packit 5e46da
        return focusOwner;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public void setCurrentFocusOwner(Component c) {
Packit 5e46da
        if (!disposed) {
Packit 5e46da
            focusOwner = c;
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    /* java 6 only */
Packit 5e46da
    public void setCurrentFocusedWindow(Window w) {
Packit 5e46da
        if (!disposed) {
Packit 5e46da
            window = w;
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    /* java 6 only */
Packit 5e46da
    public Window getCurrentFocusedWindow() {
Packit 5e46da
        return window;
Packit 5e46da
    }
Packit 5e46da
};
Packit 5e46da
Packit 5e46da